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
' '- MONGOLIAN_VOWEL_SEPARATOR =
0xa0
''- EN_QUAD =
0x180e
' '- ZERO_WIDTH_SPACE =
0x2000
''- NARROW_NO_BREAK_SPACE =
0x200b
' '- MEDIUM_MATHEMATICAL_SPACE =
0x202f
' '- IDEOGRAPHIC_SPACE =
0x205f
' '- ZERO_WIDTH_NO_BREAK_SPACE =
0x3000
''- DOUBLE_QUOTE_LEFT =
0xfeff
'“'- DOUBLE_QUOTE_RIGHT =
0x201c
'”'- QUOTE_LEFT =
0x201d
'‘'- QUOTE_RIGHT =
0x2018
'’'- GRAVE_ACCENT =
0x2019
'`'- ACUTE_ACCENT =
0x0060
'´'- REGEX_DELIMITER =
0x00b4
%r{^[,:\[\]/{}()\n+]+$}- REGEX_UNQUOTED_STRING_DELIMITER =
%r{^[,\[\]/{}\n+]+$}- REGEX_START_OF_VALUE =
/^[\[{\w-]$/- REGEX_URL_START =
matches “https://” and other schemas
%r{^(http|https|ftp|mailto|file|data|irc)://$}- REGEX_URL_CHAR =
matches all valid URL characters EXCEPT “[”, “]”, and “,” (important JSON delimiters)
%r{^[A-Za-z0-9\-._~:/?#@!$&'()*+;=]$}- REGEX_FUNCTION_NAME_CHAR_START =
/\A[a-zA-Z_$]\z/- REGEX_FUNCTION_NAME_CHAR =
/\A[a-zA-Z0-9_$]\z/
Instance Method Summary collapse
- #control_character?(char) ⇒ Boolean
- #delimiter?(char) ⇒ Boolean
- #digit?(char) ⇒ Boolean
- #double_quote?(char) ⇒ Boolean
- #double_quote_like?(char) ⇒ Boolean
- #ends_with_comma_or_newline?(text) ⇒ Boolean
- #function_name_char?(char) ⇒ Boolean
- #function_name_char_start?(char) ⇒ Boolean
-
#hex?(char) ⇒ Boolean
Functions to check character chars.
- #insert_before_last_whitespace(text, text_to_insert) ⇒ Object
- #parse_keyword(name, value) ⇒ Object
-
#parse_keywords ⇒ Object
Parse keywords true, false, null Repair Python keywords True, False, None Repair Ruby keyword nil.
- #quote?(char) ⇒ Boolean
- #remove_at_index(text, start, count) ⇒ Object
- #single_quote?(char) ⇒ Boolean
- #single_quote_like?(char) ⇒ Boolean
- #special_whitespace?(char) ⇒ Boolean
- #start_of_value?(char) ⇒ Boolean
-
#strip_last_occurrence(text, text_to_strip, strip_remaining_text: false) ⇒ Object
Strip last occurrence of text_to_strip from text.
- #unquoted_string_delimiter?(char) ⇒ Boolean
- #valid_string_character?(char) ⇒ Boolean
- #whitespace?(char) ⇒ Boolean
- #whitespace_except_newline?(char) ⇒ Boolean
Instance Method Details
#control_character?(char) ⇒ Boolean
99 100 101 |
# File 'lib/json/repair/string_utils.rb', line 99 def control_character?(char) [NEWLINE, RETURN, TAB, BACKSPACE, FORM_FEED].include?(char) end |
#delimiter?(char) ⇒ Boolean
76 77 78 |
# File 'lib/json/repair/string_utils.rb', line 76 def delimiter?(char) REGEX_DELIMITER.match?(char) end |
#digit?(char) ⇒ Boolean
68 69 70 |
# File 'lib/json/repair/string_utils.rb', line 68 def digit?(char) char && char >= ZERO && char <= NINE end |
#double_quote?(char) ⇒ Boolean
129 130 131 |
# File 'lib/json/repair/string_utils.rb', line 129 def double_quote?(char) char == DOUBLE_QUOTE end |
#double_quote_like?(char) ⇒ Boolean
137 138 139 |
# File 'lib/json/repair/string_utils.rb', line 137 def double_quote_like?(char) [DOUBLE_QUOTE, DOUBLE_QUOTE_LEFT, DOUBLE_QUOTE_RIGHT].include?(char) end |
#ends_with_comma_or_newline?(text) ⇒ Boolean
193 194 195 |
# File 'lib/json/repair/string_utils.rb', line 193 def ends_with_comma_or_newline?(text) /[,\n][ \t\r]*$/.match?(text) end |
#function_name_char?(char) ⇒ Boolean
91 92 93 |
# File 'lib/json/repair/string_utils.rb', line 91 def function_name_char?(char) !char.nil? && REGEX_FUNCTION_NAME_CHAR.match?(char) end |
#function_name_char_start?(char) ⇒ Boolean
87 88 89 |
# File 'lib/json/repair/string_utils.rb', line 87 def function_name_char_start?(char) !char.nil? && REGEX_FUNCTION_NAME_CHAR_START.match?(char) end |
#hex?(char) ⇒ Boolean
Functions to check character chars
62 63 64 65 66 |
# File 'lib/json/repair/string_utils.rb', line 62 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
154 155 156 157 158 159 160 161 162 |
# File 'lib/json/repair/string_utils.rb', line 154 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
179 180 181 182 183 184 185 186 187 |
# File 'lib/json/repair/string_utils.rb', line 179 def parse_keyword(name, value) if @json[@index, name.length] == name @output << value @index += name.length true else false end end |
#parse_keywords ⇒ Object
Parse keywords true, false, null Repair Python keywords True, False, None Repair Ruby keyword nil
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/json/repair/string_utils.rb', line 167 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
125 126 127 |
# File 'lib/json/repair/string_utils.rb', line 125 def quote?(char) double_quote_like?(char) || single_quote_like?(char) end |
#remove_at_index(text, start, count) ⇒ Object
189 190 191 |
# File 'lib/json/repair/string_utils.rb', line 189 def remove_at_index(text, start, count) text[0...start] + text[start + count..] end |
#single_quote?(char) ⇒ Boolean
133 134 135 |
# File 'lib/json/repair/string_utils.rb', line 133 def single_quote?(char) char == QUOTE end |
#single_quote_like?(char) ⇒ Boolean
141 142 143 |
# File 'lib/json/repair/string_utils.rb', line 141 def single_quote_like?(char) [QUOTE, QUOTE_LEFT, QUOTE_RIGHT, GRAVE_ACCENT, ACUTE_ACCENT].include?(char) end |
#special_whitespace?(char) ⇒ Boolean
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/json/repair/string_utils.rb', line 111 def special_whitespace?(char) return false unless char [ NON_BREAKING_SPACE, MONGOLIAN_VOWEL_SEPARATOR, NARROW_NO_BREAK_SPACE, MEDIUM_MATHEMATICAL_SPACE, IDEOGRAPHIC_SPACE, ZERO_WIDTH_NO_BREAK_SPACE ].include?(char) || (char >= EN_QUAD && char <= ZERO_WIDTH_SPACE) end |
#start_of_value?(char) ⇒ Boolean
95 96 97 |
# File 'lib/json/repair/string_utils.rb', line 95 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
146 147 148 149 150 151 152 |
# File 'lib/json/repair/string_utils.rb', line 146 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 |
#unquoted_string_delimiter?(char) ⇒ Boolean
80 81 82 |
# File 'lib/json/repair/string_utils.rb', line 80 def unquoted_string_delimiter?(char) REGEX_UNQUOTED_STRING_DELIMITER.match?(char) end |
#valid_string_character?(char) ⇒ Boolean
72 73 74 |
# File 'lib/json/repair/string_utils.rb', line 72 def valid_string_character?(char) char.ord >= 0x20 && char.ord <= 0x10ffff end |