Module: DumpCleaner::Cleanup::BytesizeHelpers

Included in:
CleaningSteps::AddRepetitionSuffix, CleaningSteps::FillUpWithString
Defined in:
lib/dump_cleaner/cleanup/bytesize_helpers.rb

Instance Method Summary collapse

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



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