Class ToonEscapes
java.lang.Object
io.micronaut.serde.toon.util.ToonEscapes
Quote-aware lexical utilities shared by
ToonEncoder (encode) and
ToonDecoder (decode).- Since:
- 3.3.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic intcountLeadingSpaces(String line) Counts the number of leading space characters in the given line, ensuring no tabs are used for indentation.static intfindClosingQuote(String s, int startQuoteIndex) Finds the index of the unescaped closing quote matching the opening quote atstartQuoteIndex.static booleanisAsciiDigit(char c) Returns whether the given character is an ASCII digit.static booleanisCommentLine(String line, int leadingSpaces) Returns whether the given line is a full-line comment, i.e.static booleanisKeyPart(char c) Returns whether the given character is valid as a subsequent character of an unquoted key.static booleanisKeyStart(char c) Returns whether the given character is valid as the first character of an unquoted key.static booleanisNumberToken(String token) Returns whether the given unquoted token matches the strict decode number grammar, and should therefore decode as a number rather than a string.static booleanisValidUnquotedKey(String key) Returns whether the given key is a valid unquoted TOON key (^[A-Za-z_][A-Za-z0-9_.]*$).static booleanlooksLikeScalar(String content) Returns whether the given unindented content string represents a scalar value rather than an object field or an array/tabular header (i.e.static StringquoteKeyIfNeeded(String key) Quotes and escapes the given key if it does not match the unquoted key grammar (^[A-Za-z_][A-Za-z0-9_.]*$), otherwise returns it unchanged.static StringquoteValueIfNeeded(String value, char delimiter) Quotes and escapes the given string value if the TOON specification requires it in the context of the given active delimiter, otherwise returns it unchanged.static intskipSpaces(String value, int from) Returns the index of the first character at or afterfromthat is not a space.splitByDelimiter(String s, char delimiter) Splitsson the given delimiter, treating any quoted substring as opaque (a delimiter character inside a quoted token is not a split point), and trims surrounding spaces from each resulting token.static StringStrips trailing spaces from the given line.static StringtrimSpaces(String value) Trims leading and trailing spaces, and no other character.static StringUnescapes a fully-quoted token, including its surrounding quote characters, decoding\\ \" \n \r \tand\\ufollowed by four hex digits.
-
Method Details
-
quoteValueIfNeeded
Quotes and escapes the given string value if the TOON specification requires it in the context of the given active delimiter, otherwise returns it unchanged.- Parameters:
value- The unquoted string valuedelimiter- The active delimiter for the current context- Returns:
- The value, quoted and escaped if required
-
isAsciiDigit
public static boolean isAsciiDigit(char c) Returns whether the given character is an ASCII digit.- Parameters:
c- The character to check- Returns:
trueif the character is in'0'..'9'
-
isKeyStart
public static boolean isKeyStart(char c) Returns whether the given character is valid as the first character of an unquoted key.- Parameters:
c- The character to check- Returns:
trueif the character is an ASCII letter or underscore
-
isKeyPart
public static boolean isKeyPart(char c) Returns whether the given character is valid as a subsequent character of an unquoted key.- Parameters:
c- The character to check- Returns:
trueif the character is an ASCII letter, digit, underscore, or dot
-
isValidUnquotedKey
Returns whether the given key is a valid unquoted TOON key (^[A-Za-z_][A-Za-z0-9_.]*$).- Parameters:
key- The key to check- Returns:
trueif the key is valid without quotes
-
quoteKeyIfNeeded
-
isNumberToken
Returns whether the given unquoted token matches the strict decode number grammar, and should therefore decode as a number rather than a string.- Parameters:
token- The unquoted token- Returns:
trueif the token should decode as a number
-
unquote
Unescapes a fully-quoted token, including its surrounding quote characters, decoding\\ \" \n \r \tand\\ufollowed by four hex digits.- Parameters:
quoted- The token, including its leading and trailing quote characters- Returns:
- The unescaped string value
- Throws:
SerdeException- If the token contains an invalid or unterminated escape sequence
-
findClosingQuote
Finds the index of the unescaped closing quote matching the opening quote atstartQuoteIndex.- Parameters:
s- The string to scanstartQuoteIndex- The index of the opening"character- Returns:
- The index of the matching closing quote
- Throws:
SerdeException- If the quoted string is unterminated
-
splitByDelimiter
Splitsson the given delimiter, treating any quoted substring as opaque (a delimiter character inside a quoted token is not a split point), and trims surrounding spaces from each resulting token.- Parameters:
s- The string to splitdelimiter- The delimiter to split on- Returns:
- The tokens, in order
- Throws:
SerdeException- If a quoted substring is unterminated
-
stripTrailingWhitespace
Strips trailing spaces from the given line. Tabs are deliberately left alone: a tab is a legal delimiter, so a trailing tab on a row/entry line can be significant (an intentionally-empty last cell), not incidental whitespace. This runs once, before any line is known to be a row/entry line rather than a bare scalar or list-item marker; callers that parse a line in a context with no delimiter at all strip a leftover trailing tab themselves once that context is known.- Parameters:
line- The line to strip trailing spaces from- Returns:
- The line without trailing spaces
-
countLeadingSpaces
Counts the number of leading space characters in the given line, ensuring no tabs are used for indentation.- Parameters:
line- The line to inspect- Returns:
- The number of leading space characters
- Throws:
SerdeException- If a tab character is used for indentation
-
isCommentLine
Returns whether the given line is a full-line comment, i.e. its first non-space character is#. Comment lines are stripped before structural parsing and are exempt from indentation consistency checks applied to every other line.- Parameters:
line- The line, with trailing whitespace already strippedleadingSpaces- The number of leading space characters inline- Returns:
trueif the line is a full-line comment
-
looksLikeScalar
Returns whether the given unindented content string represents a scalar value rather than an object field or an array/tabular header (i.e. not beginning with[or containing an unquoted:or[).- Parameters:
content- The line content to check- Returns:
trueif the content looks like a scalar value- Throws:
SerdeException- If the content contains an unterminated quoted string
-
skipSpaces
Returns the index of the first character at or afterfromthat is not a space. Tabs are not skipped: a tab can be the active delimiter.- Parameters:
value- The text to scanfrom- The index to start at- Returns:
- The index of the first non-space character, or
value.length()if there is none
-
trimSpaces
-