Method: String#truncate_bytes

Defined in:
lib/active_support/core_ext/string/filters.rb

#truncate_bytes(truncate_to, omission: "…") ⇒ Object

Truncates text to at most truncate_to bytes in length without breaking string encoding by splitting multibyte characters or breaking grapheme clusters (“perceptual characters”) by truncating at combining characters.

>> "🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪".size
=> 20
>> "🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪".bytesize
=> 80
>> "🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪🔪".truncate_bytes(20)
=> "🔪🔪🔪🔪…"

The truncated text ends with the :omission string, defaulting to “…”, for a total length not exceeding truncate_to.

Raises ArgumentError when the bytesize of :omission exceeds truncate_to.

[View source]

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/active_support/core_ext/string/filters.rb', line 101

def truncate_bytes(truncate_to, omission: "")
  omission ||= ""

  case
  when bytesize <= truncate_to
    dup
  when omission.bytesize > truncate_to
    raise ArgumentError, "Omission #{omission.inspect} is #{omission.bytesize}, larger than the truncation length of #{truncate_to} bytes"
  when omission.bytesize == truncate_to
    omission.dup
  else
    self.class.new.force_encoding(encoding).tap do |cut|
      cut_at = truncate_to - omission.bytesize

      each_grapheme_cluster do |grapheme|
        if cut.bytesize + grapheme.bytesize <= cut_at
          cut << grapheme
        else
          break
        end
      end

      cut << omission
    end
  end
end