Sha256: 5d1d8ea46f551dee4c763a3849fbbb5593e01e19866720830f6fd2a32752d09f

Contents?: true

Size: 909 Bytes

Versions: 6

Compression:

Stored size: 909 Bytes

Contents

if RUBY_ENGINE_JRUBY
  class String
    # Safely truncate the string to the specified number of bytes.
    # If a multibyte char gets split, the dangling fragment is removed.
    def limit_bytesize size
      return self unless size < bytesize
      result = (unpack %(a#{size}))[0]
      begin
        result.unpack 'U*'
      rescue ::ArgumentError
        result.chop!
        retry
      end
      result
    end unless method_defined? :limit_bytesize
  end
else
  class String
    ValidTrailingCharRx = /.$/u
    # Safely truncate the string to the specified number of bytes.
    # If a multibyte char gets split, the dangling fragment is removed.
    def limit_bytesize size
      return self unless size < bytesize
      result = (unpack %(a#{size}))[0]
      result.chop! until result.empty? || (ValidTrailingCharRx.match? result)
      result
    end unless method_defined? :limit_bytesize
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
asciidoctor-1.5.8 lib/asciidoctor/core_ext/1.8.7/string/limit_bytesize.rb
asciidoctor-1.5.7.1 lib/asciidoctor/core_ext/1.8.7/string/limit_bytesize.rb
asciidoctor-1.5.7 lib/asciidoctor/core_ext/1.8.7/string/limit_bytesize.rb
asciidoctor-1.5.6.2 lib/asciidoctor/core_ext/1.8.7/string/limit_bytesize.rb
asciidoctor-1.5.6.1 lib/asciidoctor/core_ext/1.8.7/string/limit_bytesize.rb
asciidoctor-1.5.6 lib/asciidoctor/core_ext/1.8.7/string/limit_bytesize.rb