Class ToonEscapes

java.lang.Object
io.micronaut.serde.toon.util.ToonEscapes

@Internal public final class ToonEscapes extends Object
Quote-aware lexical utilities shared by ToonEncoder (encode) and ToonDecoder (decode).
Since:
3.3.0
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    Counts the number of leading space characters in the given line, ensuring no tabs are used for indentation.
    static int
    findClosingQuote(String s, int startQuoteIndex)
    Finds the index of the unescaped closing quote matching the opening quote at startQuoteIndex.
    static boolean
    isAsciiDigit(char c)
    Returns whether the given character is an ASCII digit.
    static boolean
    isCommentLine(String line, int leadingSpaces)
    Returns whether the given line is a full-line comment, i.e.
    static boolean
    isKeyPart(char c)
    Returns whether the given character is valid as a subsequent character of an unquoted key.
    static boolean
    isKeyStart(char c)
    Returns whether the given character is valid as the first character of an unquoted key.
    static boolean
    Returns whether the given unquoted token matches the strict decode number grammar, and should therefore decode as a number rather than a string.
    static boolean
    Returns whether the given key is a valid unquoted TOON key (^[A-Za-z_][A-Za-z0-9_.]*$).
    static boolean
    Returns whether the given unindented content string represents a scalar value rather than an object field or an array/tabular header (i.e.
    static String
    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 String
    quoteValueIfNeeded(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 int
    skipSpaces(String value, int from)
    Returns the index of the first character at or after from that is not a space.
    static List<String>
    splitByDelimiter(String s, char delimiter)
    Splits s on 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 String
    Strips trailing spaces from the given line.
    static String
    Trims leading and trailing spaces, and no other character.
    static String
    unquote(String quoted)
    Unescapes a fully-quoted token, including its surrounding quote characters, decoding \\ \" \n \r \t and \\u followed by four hex digits.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • quoteValueIfNeeded

      public static String quoteValueIfNeeded(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.
      Parameters:
      value - The unquoted string value
      delimiter - 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:
      true if 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:
      true if 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:
      true if the character is an ASCII letter, digit, underscore, or dot
    • isValidUnquotedKey

      public static boolean isValidUnquotedKey(String key)
      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:
      true if the key is valid without quotes
    • quoteKeyIfNeeded

      public static String quoteKeyIfNeeded(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.
      Parameters:
      key - The unquoted key
      Returns:
      The key, quoted and escaped if required
    • isNumberToken

      public static boolean isNumberToken(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.
      Parameters:
      token - The unquoted token
      Returns:
      true if the token should decode as a number
    • unquote

      public static String unquote(String quoted) throws SerdeException
      Unescapes a fully-quoted token, including its surrounding quote characters, decoding \\ \" \n \r \t and \\u followed 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

      public static int findClosingQuote(String s, int startQuoteIndex) throws SerdeException
      Finds the index of the unescaped closing quote matching the opening quote at startQuoteIndex.
      Parameters:
      s - The string to scan
      startQuoteIndex - The index of the opening " character
      Returns:
      The index of the matching closing quote
      Throws:
      SerdeException - If the quoted string is unterminated
    • splitByDelimiter

      public static List<String> splitByDelimiter(String s, char delimiter) throws SerdeException
      Splits s on 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 split
      delimiter - The delimiter to split on
      Returns:
      The tokens, in order
      Throws:
      SerdeException - If a quoted substring is unterminated
    • stripTrailingWhitespace

      public static String stripTrailingWhitespace(String line)
      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

      public static int countLeadingSpaces(String line) throws SerdeException
      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

      public static boolean isCommentLine(String line, int leadingSpaces)
      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 stripped
      leadingSpaces - The number of leading space characters in line
      Returns:
      true if the line is a full-line comment
    • looksLikeScalar

      public static boolean looksLikeScalar(String content) throws SerdeException
      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:
      true if the content looks like a scalar value
      Throws:
      SerdeException - If the content contains an unterminated quoted string
    • skipSpaces

      public static int skipSpaces(String value, int from)
      Returns the index of the first character at or after from that is not a space. Tabs are not skipped: a tab can be the active delimiter.
      Parameters:
      value - The text to scan
      from - The index to start at
      Returns:
      The index of the first non-space character, or value.length() if there is none
    • trimSpaces

      public static String trimSpaces(String value)
      Trims leading and trailing spaces, and no other character.
      Parameters:
      value - The text to trim
      Returns:
      The text without leading or trailing spaces