Class: WTF8::String
- Inherits:
-
Object
- Object
- WTF8::String
- Includes:
- Comparable
- Defined in:
- lib/wtf8/string.rb,
sig/wtf8/string.rbs
Instance Attribute Summary collapse
- #code_points ⇒ Array[Integer] readonly
- #raw ⇒ ::String readonly
Class Method Summary collapse
-
.from_cesu8(bytes, strict: true) ⇒ WTF8::String
: (::String, ?strict: bool) -> WTF8::String.
-
.from_code_points(code_points) ⇒ WTF8::String
: (Array) -> WTF8::String.
-
.from_modified_utf8(bytes, strict: true) ⇒ WTF8::String
: (::String, ?strict: bool) -> WTF8::String.
-
.from_utf16(units) ⇒ WTF8::String
: (Array) -> WTF8::String.
-
.from_utf16be(bytes) ⇒ WTF8::String
: (::String) -> WTF8::String.
-
.from_utf16le(bytes) ⇒ WTF8::String
: (::String) -> WTF8::String.
-
.from_utf8(string) ⇒ WTF8::String
: (::String) -> WTF8::String.
Instance Method Summary collapse
-
#+(other) ⇒ WTF8::String
: (WTF8::String | ::String) -> WTF8::String.
-
#<=>(other) ⇒ Integer?
: (untyped) -> Integer?.
-
#==(other) ⇒ Boolean
(also: #eql?)
: (untyped) -> bool.
-
#[](index) ⇒ Object
(also: #slice)
: (Integer) -> WTF8::String? : (Range) -> WTF8::String?.
-
#bytesize ⇒ Integer
: () -> Integer.
-
#each_code_point {|arg0| ... } ⇒ Object
: () { (Integer) -> void } -> WTF8::String : () -> Enumerator[Integer, WTF8::String].
-
#empty? ⇒ Boolean
: () -> bool.
-
#end_with?(suffix) ⇒ Boolean
: (WTF8::String | ::String) -> bool.
-
#hash ⇒ Integer
: () -> Integer.
-
#initialize(bytes) ⇒ String
constructor
: (::String) -> void.
-
#inspect ⇒ ::String
: () -> ::String.
-
#length ⇒ Integer
(also: #size)
: () -> Integer.
-
#slice_utf16(range) ⇒ WTF8::String
: (Range) -> WTF8::String.
-
#start_with?(prefix) ⇒ Boolean
: (WTF8::String | ::String) -> bool.
-
#to_cesu8 ⇒ ::String
: () -> ::String.
-
#to_modified_utf8 ⇒ ::String
: () -> ::String.
-
#to_utf16 ⇒ Array[Integer]
: () -> Array.
-
#to_utf16be ⇒ ::String
: () -> ::String.
-
#to_utf16le ⇒ ::String
: () -> ::String.
-
#to_utf8(replacement: Codec::REPLACEMENT_CHARACTER) ⇒ ::String
(also: #to_s)
: (?replacement: ::String) -> ::String.
-
#well_formed? ⇒ Boolean
: () -> bool.
Constructor Details
Instance Attribute Details
#code_points ⇒ Array[Integer] (readonly)
29 30 31 |
# File 'lib/wtf8/string.rb', line 29 def code_points @code_points end |
#raw ⇒ ::String (readonly)
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
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
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
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
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
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
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
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
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?
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
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?
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 |
#bytesize ⇒ Integer
: () -> Integer
45 |
# File 'lib/wtf8/string.rb', line 45 def bytesize = @raw.bytesize |
#each_code_point ⇒ WTF8::String #each_code_point ⇒ Enumerator[Integer, WTF8::String]
: () { (Integer) -> void } -> WTF8::String : () -> Enumerator[Integer, WTF8::String]
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
48 |
# File 'lib/wtf8/string.rb', line 48 def empty? = @raw.empty? |
#end_with?(suffix) ⇒ Boolean
: (WTF8::String | ::String) -> bool
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 |
#hash ⇒ Integer
: () -> Integer
128 |
# File 'lib/wtf8/string.rb', line 128 def hash = [self.class, @raw].hash |
#inspect ⇒ ::String
: () -> ::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 |
#length ⇒ Integer Also known as: size
: () -> Integer
40 |
# File 'lib/wtf8/string.rb', line 40 def length = code_points.length |
#slice_utf16(range) ⇒ WTF8::String
: (Range) -> WTF8::String
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
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
115 |
# File 'lib/wtf8/string.rb', line 115 def to_cesu8 = CESU8.encode(@raw) |
#to_modified_utf8 ⇒ ::String
: () -> ::String
118 |
# File 'lib/wtf8/string.rb', line 118 def to_modified_utf8 = ModifiedUTF8.encode(@raw) |
#to_utf16 ⇒ Array[Integer]
: () -> Array
106 |
# File 'lib/wtf8/string.rb', line 106 def to_utf16 = Codec.to_utf16(@raw) |
#to_utf16be ⇒ ::String
: () -> ::String
112 |
# File 'lib/wtf8/string.rb', line 112 def to_utf16be = Codec.to_utf16be(@raw) |
#to_utf16le ⇒ ::String
: () -> ::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
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
51 |
# File 'lib/wtf8/string.rb', line 51 def well_formed? = Codec.well_formed?(@raw) |