Module: WTF8::CESU8

Defined in:
lib/wtf8/cesu8.rb,
sig/wtf8/cesu8.rbs

Class Method Summary collapse

Class Method Details

.decode(bytes, strict: true) ⇒ ::String

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

Parameters:

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

Returns:

  • (::String)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wtf8/cesu8.rb', line 21

def self.decode(bytes, strict: true)
  code_points = Codec.unpack(bytes)

  wtf8 = +"".b
  index = 0

  while index < code_points.length
    code_point = code_points[index]
    following = code_points[index + 1]

    if code_point > Codec::MAX_CODE_POINT
      raise InvalidCodePointError, format("U+%04X is above U+10FFFF", code_point)
    elsif code_point > 0xFFFF
      raise InvalidByteSequenceError, "a four-byte sequence is not CESU-8" if strict

      wtf8 << [code_point].pack("U").b
      index += 1
    elsif Surrogates.lead?(code_point) && following && Surrogates.trail?(following)
      wtf8 << [Surrogates.combine(code_point, following)].pack("U").b
      index += 2
    else
      wtf8 << [code_point].pack("U").b
      index += 1
    end
  end

  wtf8
end

.encode(bytes) ⇒ ::String

: (::String) -> ::String

Parameters:

  • (::String)

Returns:

  • (::String)


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/wtf8/cesu8.rb', line 6

def self.encode(bytes)
  cesu8 = +"".b

  Codec.decode(bytes).each do |code_point|
    if code_point > 0xFFFF
      Surrogates.split(code_point).each { |unit| cesu8 << [unit].pack("U").b }
    else
      cesu8 << [code_point].pack("U").b
    end
  end

  cesu8
end