Skip to content

Commit

Permalink
YAML: consider starting # and ending : as quotable characters
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Feb 27, 2024
1 parent c603bd9 commit b93c363
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public abstract class StringQuotingChecker
* Helper method that sub-classes may use to see if given String value is
* one of:
*<ul>
* <li>YAML 1.1 keyword representing
* <li>YAML 1.1 keyword representing
* <a href="https://yaml.org/type/bool.html">boolean</a>
* </li>
* <li>YAML 1.1 keyword representing
* <li>YAML 1.1 keyword representing
* <a href="https://yaml.org/type/null.html">null</a> value
* </li>
* <li>empty String (length 0)
Expand Down Expand Up @@ -142,20 +142,14 @@ protected boolean valueHasQuotableChar(String inputStr)
return true;
case '#':
// [dataformats-text#201]: limit quoting with MINIMIZE_QUOTES
if (i > 0) {
char d = inputStr.charAt(i-1);
if (' ' == d || '\t' == d) {
return true;
}
if (precededByBlank(inputStr, i)) {
return true;
}
break;
case ':':
// [dataformats-text#201]: limit quoting with MINIMIZE_QUOTES
if (i < (end-1)) {
char d = inputStr.charAt(i + 1);
if (' ' == d || '\t' == d) {
return true;
}
if (followedByBlank(inputStr, i)) {
return true;
}
break;
default:
Expand All @@ -164,6 +158,24 @@ protected boolean valueHasQuotableChar(String inputStr)
return false;
}

private boolean precededByBlank(String inputStr, int offset) {
if (offset == 0) {
return true;
}
return isBlank(inputStr.charAt(offset - 1));
}

private boolean followedByBlank(String inputStr, int offset) {
if (offset == inputStr.length() - 1) {
return true;
}
return isBlank(inputStr.charAt(offset + 1));
}

private boolean isBlank(char value) {
return (' ' == value || '\t' == value);
}

/**
* Looks like we may get names with "funny characters" so.
*
Expand Down Expand Up @@ -199,7 +211,7 @@ public static class Default
public Default() { }

public static Default instance() { return INSTANCE; }

/**
* Default implementation will call
* {@link #isReservedKeyword(String)} and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,22 @@ public void testMinimizeQuotesWithStringsContainingSpecialChars() throws Excepti
"key: f:off", yaml);


/* scenarios with single quoted scalars */
/* scenarios with double quoted scalars */

content = Collections.singletonMap("key", "::");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: '::'", yaml);
"key: \"::\"", yaml);

content = Collections.singletonMap("key", "#");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: '#'", yaml);
"key: \"#\"", yaml);

content = Collections.singletonMap("key", "#a");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: '#a'", yaml);


/* scenarios with double quoted scalars */
"key: \"#a\"", yaml);

content = Collections.singletonMap("key", "a[b");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
Expand Down

0 comments on commit b93c363

Please sign in to comment.