Class: JSON5::ECMAString

Inherits:
Object
  • Object
show all
Defined in:
lib/json5/ecma_string.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code_units) ⇒ ECMAString

Returns a new instance of ECMAString.



7
8
9
10
11
12
13
# File 'lib/json5/ecma_string.rb', line 7

def initialize(code_units)
  unless code_units.all? { |unit| unit.is_a?(Integer) && unit.between?(0, 0xffff) }
    raise ArgumentError, "code units must be UTF-16 integers"
  end
  @code_units = code_units.dup.freeze
  freeze
end

Instance Attribute Details

#code_unitsObject (readonly)

Returns the value of attribute code_units.



5
6
7
# File 'lib/json5/ecma_string.rb', line 5

def code_units
  @code_units
end

Instance Method Details

#each_code_unit(&block) ⇒ Object



15
16
17
18
# File 'lib/json5/ecma_string.rb', line 15

def each_code_unit(&block)
  return enum_for(__method__) unless block
  code_units.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/json5/ecma_string.rb', line 24

def empty?
  code_units.empty?
end

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

Returns:

  • (Boolean)


28
29
30
# File 'lib/json5/ecma_string.rb', line 28

def eql?(other)
  other.is_a?(ECMAString) && code_units == other.code_units
end

#hashObject



33
34
35
# File 'lib/json5/ecma_string.rb', line 33

def hash
  code_units.hash
end

#lengthObject



20
21
22
# File 'lib/json5/ecma_string.rb', line 20

def length
  code_units.length
end

#lone_surrogate?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/json5/ecma_string.rb', line 37

def lone_surrogate?
  index = 0
  while index < code_units.length
    unit = code_units[index]
    if surrogate_pair_at?(index)
      index += 2
      next
    end
    return true if high_surrogate?(unit) || low_surrogate?(unit)

    index += 1
  end
  false
end

#to_sObject



84
85
86
# File 'lib/json5/ecma_string.rb', line 84

def to_s
  to_utf8
end

#to_utf8(lone_surrogate: :error) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/json5/ecma_string.rb', line 52

def to_utf8(lone_surrogate: :error)
  result = String.new(encoding: Encoding::UTF_8)
  index = 0
  while index < code_units.length
    unit = code_units[index]
    if surrogate_pair_at?(index)
      codepoint = 0x10000 + ((unit - 0xd800) << 10) + code_units[index + 1] - 0xdc00
      append_codepoint(result, codepoint)
      index += 2
      next
    end

    if high_surrogate?(unit) || low_surrogate?(unit)
      case lone_surrogate
      when :error
        raise ConversionError.new(
          "lone surrogate cannot be represented as UTF-8",
          code: "lone_surrogate"
        )
      when :replace
        append_codepoint(result, 0xfffd)
      else
        raise ArgumentError, "unknown lone_surrogate policy: #{lone_surrogate.inspect}"
      end
    else
      append_codepoint(result, unit)
    end
    index += 1
  end
  result
end