Module: DumpCleaner::Cleanup::BytesizeHelpers
- Defined in:
- lib/dump_cleaner/cleanup/bytesize_helpers.rb
Instance Method Summary collapse
- #replace_suffix(string, suffix:, padding: " ") ⇒ Object
- #set_to_bytesize(string, bytesize:, padding: " ") ⇒ Object
-
#truncate_to_bytesize(string, max_bytesize:, padding: " ") ⇒ Object
inspired by stackoverflow.com/a/67825008/1544012.
Instance Method Details
#replace_suffix(string, suffix:, padding: " ") ⇒ Object
22 23 24 25 26 27 |
# File 'lib/dump_cleaner/cleanup/bytesize_helpers.rb', line 22 def replace_suffix(string, suffix:, padding: " ") front_max_bytes = string.bytesize - suffix.bytesize front = truncate_to_bytesize(string, max_bytesize: front_max_bytes, padding:) "#{front}#{suffix}" end |
#set_to_bytesize(string, bytesize:, padding: " ") ⇒ Object
17 18 19 20 |
# File 'lib/dump_cleaner/cleanup/bytesize_helpers.rb', line 17 def set_to_bytesize(string, bytesize:, padding: " ") string = string.ljust(bytesize, "#{padding}#{string}") if string.bytesize < bytesize truncate_to_bytesize(string, max_bytesize: bytesize, padding:) end |
#truncate_to_bytesize(string, max_bytesize:, padding: " ") ⇒ Object
inspired by stackoverflow.com/a/67825008/1544012
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/dump_cleaner/cleanup/bytesize_helpers.rb', line 5 def truncate_to_bytesize(string, max_bytesize:, padding: " ") return string unless string.bytesize > max_bytesize check_padding_bytesize(padding) just_over = (0...string.size).bsearch { string[0.._1].bytesize > max_bytesize } string = string[0...just_over] string << padding while string.bytesize < max_bytesize string end |