Class: WTF8::String

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/wtf8/string.rb,
sig/wtf8/string.rbs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ String

: (::String) -> void

Parameters:

  • (::String)


32
33
34
35
36
37
# File 'lib/wtf8/string.rb', line 32

def initialize(bytes)
  @raw = bytes.b.freeze
  @code_points = Codec.decode(@raw).freeze

  freeze
end

Instance Attribute Details

#code_pointsArray[Integer] (readonly)

Signature:

  • Array[Integer]

Returns:

  • (Array[Integer])


29
30
31
# File 'lib/wtf8/string.rb', line 29

def code_points
  @code_points
end

#raw::String (readonly)

Signature:

  • ::String

Returns:

  • (::String)


28
29
30
# File 'lib/wtf8/string.rb', line 28

def raw
  @raw
end

Class Method Details

.from_cesu8(bytes, strict: true) ⇒ WTF8::String

: (::String, ?strict: bool) -> WTF8::String

Parameters:

  • (::String)
  • strict: (Boolean) (defaults to: true)

Returns:



23
# File 'lib/wtf8/string.rb', line 23

def self.from_cesu8(bytes, strict: true) = new(CESU8.decode(bytes, strict: strict))

.from_code_points(code_points) ⇒ WTF8::String

: (Array) -> WTF8::String

Parameters:

  • (Array[Integer])

Returns:



20
# File 'lib/wtf8/string.rb', line 20

def self.from_code_points(code_points) = new(Codec.encode(code_points))

.from_modified_utf8(bytes, strict: true) ⇒ WTF8::String

: (::String, ?strict: bool) -> WTF8::String

Parameters:

  • (::String)
  • strict: (Boolean) (defaults to: true)

Returns:



26
# File 'lib/wtf8/string.rb', line 26

def self.from_modified_utf8(bytes, strict: true) = new(ModifiedUTF8.decode(bytes, strict: strict))

.from_utf16(units) ⇒ WTF8::String

: (Array) -> WTF8::String

Parameters:

  • (Array[Integer])

Returns:



11
# File 'lib/wtf8/string.rb', line 11

def self.from_utf16(units) = new(Codec.from_utf16(units))

.from_utf16be(bytes) ⇒ WTF8::String

: (::String) -> WTF8::String

Parameters:

  • (::String)

Returns:



17
# File 'lib/wtf8/string.rb', line 17

def self.from_utf16be(bytes) = new(Codec.from_utf16be(bytes))

.from_utf16le(bytes) ⇒ WTF8::String

: (::String) -> WTF8::String

Parameters:

  • (::String)

Returns:



14
# File 'lib/wtf8/string.rb', line 14

def self.from_utf16le(bytes) = new(Codec.from_utf16le(bytes))

.from_utf8(string) ⇒ WTF8::String

: (::String) -> WTF8::String

Parameters:

  • (::String)

Returns:



8
# File 'lib/wtf8/string.rb', line 8

def self.from_utf8(string) = new(Codec.from_utf8(string))

Instance Method Details

#+(other) ⇒ WTF8::String

: (WTF8::String | ::String) -> WTF8::String

Parameters:

Returns:



64
65
66
# File 'lib/wtf8/string.rb', line 64

def +(other)
  self.class.new(Codec.concat(@raw, other.is_a?(WTF8::String) ? other.raw : Codec.from_utf8(other)))
end

#<=>(other) ⇒ Integer?

: (untyped) -> Integer?

Parameters:

  • (Object)

Returns:

  • (Integer, nil)


131
132
133
# File 'lib/wtf8/string.rb', line 131

def <=>(other)
  other.is_a?(WTF8::String) ? (@raw <=> other.raw) : nil
end

#==(other) ⇒ Boolean Also known as: eql?

: (untyped) -> bool

Parameters:

  • (Object)

Returns:

  • (Boolean)


121
122
123
# File 'lib/wtf8/string.rb', line 121

def ==(other)
  other.is_a?(WTF8::String) && other.raw == @raw
end

#[](arg0) ⇒ WTF8::String? #[](arg0) ⇒ WTF8::String? Also known as: slice

: (Integer) -> WTF8::String? : (Range) -> WTF8::String?

Overloads:



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wtf8/string.rb', line 70

def [](index)
  case index
  when Range
    sliced = code_points[index]

    sliced && self.class.from_code_points(sliced)
  else
    code_point = code_points[index]

    code_point && self.class.from_code_points([code_point])
  end
end

#bytesizeInteger

: () -> Integer

Returns:

  • (Integer)


45
# File 'lib/wtf8/string.rb', line 45

def bytesize = @raw.bytesize

#each_code_pointWTF8::String #each_code_pointEnumerator[Integer, WTF8::String]

: () { (Integer) -> void } -> WTF8::String : () -> Enumerator[Integer, WTF8::String]

Overloads:

Yields:

Yield Parameters:

  • arg0 (Integer)

Yield Returns:

  • (void)


55
56
57
58
59
60
61
# File 'lib/wtf8/string.rb', line 55

def each_code_point(&block)
  return enum_for(:each_code_point) unless block

  code_points.each(&block)

  self
end

#empty?Boolean

: () -> bool

Returns:

  • (Boolean)


48
# File 'lib/wtf8/string.rb', line 48

def empty? = @raw.empty?

#end_with?(suffix) ⇒ Boolean

: (WTF8::String | ::String) -> bool

Parameters:

Returns:

  • (Boolean)


96
97
98
# File 'lib/wtf8/string.rb', line 96

def end_with?(suffix)
  @raw.end_with?(suffix.is_a?(WTF8::String) ? suffix.raw : Codec.from_utf8(suffix))
end

#hashInteger

: () -> Integer

Returns:

  • (Integer)


128
# File 'lib/wtf8/string.rb', line 128

def hash = [self.class, @raw].hash

#inspect::String

: () -> ::String

Returns:

  • (::String)


136
137
138
139
140
141
142
143
144
145
146
# File 'lib/wtf8/string.rb', line 136

def inspect
  shown = code_points.map { |code_point|
    if Surrogates.surrogate?(code_point)
      format("\\u{%04X}", code_point)
    else
      [code_point].pack("U").inspect[1..-2]
    end
  }.join

  %(#<#{self.class.name} "#{shown}">)
end

#lengthInteger Also known as: size

: () -> Integer

Returns:

  • (Integer)


40
# File 'lib/wtf8/string.rb', line 40

def length = code_points.length

#slice_utf16(range) ⇒ WTF8::String

: (Range) -> WTF8::String

Parameters:

  • (Range[Integer])

Returns:



86
87
88
# File 'lib/wtf8/string.rb', line 86

def slice_utf16(range)
  self.class.from_utf16(to_utf16[range] || [])
end

#start_with?(prefix) ⇒ Boolean

: (WTF8::String | ::String) -> bool

Parameters:

Returns:

  • (Boolean)


91
92
93
# File 'lib/wtf8/string.rb', line 91

def start_with?(prefix)
  @raw.start_with?(prefix.is_a?(WTF8::String) ? prefix.raw : Codec.from_utf8(prefix))
end

#to_cesu8::String

: () -> ::String

Returns:

  • (::String)


115
# File 'lib/wtf8/string.rb', line 115

def to_cesu8 = CESU8.encode(@raw)

#to_modified_utf8::String

: () -> ::String

Returns:

  • (::String)


118
# File 'lib/wtf8/string.rb', line 118

def to_modified_utf8 = ModifiedUTF8.encode(@raw)

#to_utf16Array[Integer]

: () -> Array

Returns:

  • (Array[Integer])


106
# File 'lib/wtf8/string.rb', line 106

def to_utf16 = Codec.to_utf16(@raw)

#to_utf16be::String

: () -> ::String

Returns:

  • (::String)


112
# File 'lib/wtf8/string.rb', line 112

def to_utf16be = Codec.to_utf16be(@raw)

#to_utf16le::String

: () -> ::String

Returns:

  • (::String)


109
# File 'lib/wtf8/string.rb', line 109

def to_utf16le = Codec.to_utf16le(@raw)

#to_utf8(replacement: Codec::REPLACEMENT_CHARACTER) ⇒ ::String Also known as: to_s

: (?replacement: ::String) -> ::String

Parameters:

  • replacement: (::String) (defaults to: Codec::REPLACEMENT_CHARACTER)

Returns:

  • (::String)


101
# File 'lib/wtf8/string.rb', line 101

def to_utf8(replacement: Codec::REPLACEMENT_CHARACTER) = Codec.to_utf8(@raw, replacement: replacement)

#well_formed?Boolean

: () -> bool

Returns:

  • (Boolean)


51
# File 'lib/wtf8/string.rb', line 51

def well_formed? = Codec.well_formed?(@raw)