+ private static StringBuilder appendValue(final StringBuilder sb, final QNameModule currentModule,
+ final Object value) {
+ final var str = String.valueOf(value);
+
+ // We have two specifications here: Section 6.1.3 of both RFC6020 and RFC7950:
+ //
+ // RFC6020 Section 6.1.3:
+ // If a string contains any space or tab characters, a semicolon (";"),
+ // braces ("{" or "}"), or comment sequences ("//", "/*", or "*/"), then
+ // it MUST be enclosed within double or single quotes.
+ //
+ // RFC7950 Section 6.1.3:
+ // An unquoted string is any sequence of characters that does not
+ // contain any space, tab, carriage return, or line feed characters, a
+ // single or double quote character, a semicolon (";"), braces ("{" or
+ // "}"), or comment sequences ("//", "/*", or "*/").
+ //
+ // Plus the common part:
+ // A single-quoted string (enclosed within ' ') preserves each character
+ // within the quotes. A single quote character cannot occur in a
+ // single-quoted string, even when preceded by a backslash.
+ //
+ // Unquoted strings are not interesting, as we are embedding the value in a string, not a YANG document, hence
+ // we have to use quotes. Single-quoted case is simpler, as it does not involve any escaping. The only case
+ // where we cannot use it is when the value itself has a single-quote in itself -- then we call back to
+ // double-quoting.
+
+ return str.indexOf('\'') == -1
+ // No escaping needed, use single quotes
+ ? sb.append('\'').append(str).append('\'')
+ // Escaping needed: use double quotes
+ : sb.append('"').append(DQUOT_ESCAPER.escape(str)).append('"');
+ }
+