Module: JSON::Repair::StringUtils

Included in:
JSON::Repairer
Defined in:
lib/json/repair/string_utils.rb

Constant Summary collapse

BACKSLASH =

Constants for character chars

'\\'
SLASH =

0x5c

'/'
ASTERISK =

0x2f

'*'
OPENING_BRACE =

0x2a

'{'
CLOSING_BRACE =

0x7b

'}'
OPENING_BRACKET =

0x7d

'['
CLOSING_BRACKET =

0x5b

']'
OPEN_PARENTHESIS =

0x5d

'('
CLOSE_PARENTHESIS =

0x28

')'
SPACE =

0x29

' '
NEWLINE =

0x20

"\n"
TAB =

0xa

"\t"
RETURN =

0x9

"\r"
BACKSPACE =

0xd

"\b"
FORM_FEED =

0x08

"\f"
DOUBLE_QUOTE =

0x0c

'"'
PLUS =

0x0022

'+'
MINUS =

0x2b

'-'
QUOTE =

0x2d

"'"
ZERO =

0x27

'0'
NINE =

0x30

'9'
COMMA =

0x39

','
DOT =

0x2c

'.'
COLON =

0x2e

':'
SEMICOLON =

0x3a

';'
UPPERCASE_A =

0x3b

'A'
LOWERCASE_A =

0x41

'a'
UPPERCASE_E =

0x61

'E'
LOWERCASE_E =

0x45

'e'
UPPERCASE_F =

0x65

'F'
LOWERCASE_F =

0x46

'f'
NON_BREAKING_SPACE =

0x66

"\u00a0"
EN_QUAD =

0xa0

"\u2000"
HAIR_SPACE =

0x2000

"\u200a"
NARROW_NO_BREAK_SPACE =

0x200a

"\u202f"
MEDIUM_MATHEMATICAL_SPACE =

0x202f

"\u205f"
IDEOGRAPHIC_SPACE =

0x205f

"\u3000"
DOUBLE_QUOTE_LEFT =

0x3000

"\u201c"
DOUBLE_QUOTE_RIGHT =

0x201c

"\u201d"
QUOTE_LEFT =

0x201d

"\u2018"
QUOTE_RIGHT =

0x2018

"\u2019"
GRAVE_ACCENT =

0x2019

'`'
ACUTE_ACCENT =

0x0060

"\u00b4"
REGEX_DELIMITER =

0x00b4

%r{^[,:\[\]/{}()\n+]+$}
REGEX_START_OF_VALUE =
/^[\[{\w-]$/

Instance Method Summary collapse

Instance Method Details

#control_character?(char) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/json/repair/string_utils.rb', line 81

def control_character?(char)
  [NEWLINE, RETURN, TAB, BACKSPACE, FORM_FEED].include?(char)
end

#delimiter?(char) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/json/repair/string_utils.rb', line 69

def delimiter?(char)
  REGEX_DELIMITER.match?(char)
end

#delimiter_except_slash?(char) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/json/repair/string_utils.rb', line 73

def delimiter_except_slash?(char)
  delimiter?(char) && char != SLASH
end

#digit?(char) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/json/repair/string_utils.rb', line 61

def digit?(char)
  char && char >= ZERO && char <= NINE
end

#double_quote?(char) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/json/repair/string_utils.rb', line 100

def double_quote?(char)
  char == DOUBLE_QUOTE
end

#double_quote_like?(char) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/json/repair/string_utils.rb', line 108

def double_quote_like?(char)
  [DOUBLE_QUOTE, DOUBLE_QUOTE_LEFT, DOUBLE_QUOTE_RIGHT].include?(char)
end

#ends_with_comma_or_newline?(text) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/json/repair/string_utils.rb', line 168

def ends_with_comma_or_newline?(text)
  /[,\n][ \t\r]*$/.match?(text)
end

#function_name?(text) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/json/repair/string_utils.rb', line 164

def function_name?(text)
  /^\w+$/.match?(text)
end

#hex?(char) ⇒ Boolean

Functions to check character chars

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/json/repair/string_utils.rb', line 55

def hex?(char)
  (char >= ZERO && char <= NINE) ||
    (char >= UPPERCASE_A && char <= UPPERCASE_F) ||
    (char >= LOWERCASE_A && char <= LOWERCASE_F)
end

#insert_before_last_whitespace(text, text_to_insert) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/json/repair/string_utils.rb', line 125

def insert_before_last_whitespace(text, text_to_insert)
  index = text.length

  return text + text_to_insert unless whitespace?(text[index - 1])

  index -= 1 while whitespace?(text[index - 1])

  text[0...index] + text_to_insert + text[index..]
end

#parse_keyword(name, value) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/json/repair/string_utils.rb', line 150

def parse_keyword(name, value)
  if @json[@index, name.length] == name
    @output += value
    @index += name.length
    true
  else
    false
  end
end

#parse_keywordsObject

Parse keywords true, false, null Repair Python keywords True, False, None Repair Ruby keyword nil



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/json/repair/string_utils.rb', line 138

def parse_keywords
  parse_keyword('true', 'true') ||
    parse_keyword('false', 'false') ||
    parse_keyword('null', 'null') ||
    # Repair Python keywords True, False, None
    parse_keyword('True', 'true') ||
    parse_keyword('False', 'false') ||
    parse_keyword('None', 'null') ||
    # Repair Ruby keyword nil
    parse_keyword('nil', 'null')
end

#quote?(char) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/json/repair/string_utils.rb', line 96

def quote?(char)
  double_quote_like?(char) || single_quote_like?(char)
end

#remove_at_index(text, start, count) ⇒ Object



160
161
162
# File 'lib/json/repair/string_utils.rb', line 160

def remove_at_index(text, start, count)
  text[0...start] + text[start + count..]
end

#single_quote?(char) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/json/repair/string_utils.rb', line 104

def single_quote?(char)
  char == QUOTE
end

#single_quote_like?(char) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/json/repair/string_utils.rb', line 112

def single_quote_like?(char)
  [QUOTE, QUOTE_LEFT, QUOTE_RIGHT, GRAVE_ACCENT, ACUTE_ACCENT].include?(char)
end

#special_whitespace?(char) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
# File 'lib/json/repair/string_utils.rb', line 89

def special_whitespace?(char)
  [
    NON_BREAKING_SPACE, NARROW_NO_BREAK_SPACE, MEDIUM_MATHEMATICAL_SPACE, IDEOGRAPHIC_SPACE
  ].include?(char) ||
    (char >= EN_QUAD && char <= HAIR_SPACE)
end

#start_of_value?(char) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/json/repair/string_utils.rb', line 77

def start_of_value?(char)
  REGEX_START_OF_VALUE.match?(char) || (char && quote?(char))
end

#strip_last_occurrence(text, text_to_strip, strip_remaining_text: false) ⇒ Object

Strip last occurrence of text_to_strip from text



117
118
119
120
121
122
123
# File 'lib/json/repair/string_utils.rb', line 117

def strip_last_occurrence(text, text_to_strip, strip_remaining_text: false)
  index = text.rindex(text_to_strip)
  return text unless index

  remaining_text = strip_remaining_text ? '' : text[index + 1..]
  text[0...index] + remaining_text
end

#valid_string_character?(char) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/json/repair/string_utils.rb', line 65

def valid_string_character?(char)
  char.ord >= 0x20 && char.ord <= 0x10ffff
end

#whitespace?(char) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/json/repair/string_utils.rb', line 85

def whitespace?(char)
  [SPACE, NEWLINE, TAB, RETURN].include?(char)
end