8
8
* Helper class that defines API used by
9
9
* {@link com.fasterxml.jackson.dataformat.yaml.YAMLGenerator}
10
10
* to check whether property names and String values need to be quoted or not.
11
- * Also contains default logic implementation; may be sub-classes to provide
11
+ * Also contains default logic implementation; may be sub-classed to provide
12
12
* alternate implementation.
13
13
*
14
14
* @since 2.12
@@ -58,10 +58,10 @@ public abstract class StringQuotingChecker
58
58
* Helper method that sub-classes may use to see if given String value is
59
59
* one of:
60
60
*<ul>
61
- * <li>YAML 1.1 keyword representing
61
+ * <li>YAML 1.1 keyword representing
62
62
* <a href="https://yaml.org/type/bool.html">boolean</a>
63
63
* </li>
64
- * <li>YAML 1.1 keyword representing
64
+ * <li>YAML 1.1 keyword representing
65
65
* <a href="https://yaml.org/type/null.html">null</a> value
66
66
* </li>
67
67
* <li>empty String (length 0)
@@ -142,20 +142,16 @@ protected boolean valueHasQuotableChar(String inputStr)
142
142
return true ;
143
143
case '#' :
144
144
// [dataformats-text#201]: limit quoting with MINIMIZE_QUOTES
145
- if (i > 0 ) {
146
- char d = inputStr .charAt (i -1 );
147
- if (' ' == d || '\t' == d ) {
148
- return true ;
149
- }
145
+ // (but not recognized as comment unless starts line or preceded by whitespace)
146
+ if (precededOnlyByBlank (inputStr , i )) {
147
+ return true ;
150
148
}
151
149
break ;
152
150
case ':' :
153
151
// [dataformats-text#201]: limit quoting with MINIMIZE_QUOTES
154
- if (i < (end -1 )) {
155
- char d = inputStr .charAt (i + 1 );
156
- if (' ' == d || '\t' == d ) {
157
- return true ;
158
- }
152
+ // (but recognized as separator only if end-of-line or followed by whitespace)
153
+ if (followedOnlyByBlank (inputStr , i )) {
154
+ return true ;
159
155
}
160
156
break ;
161
157
default :
@@ -164,6 +160,27 @@ protected boolean valueHasQuotableChar(String inputStr)
164
160
return false ;
165
161
}
166
162
163
+ // @since 2.17
164
+ protected boolean precededOnlyByBlank (String inputStr , int offset ) {
165
+ if (offset == 0 ) {
166
+ return true ;
167
+ }
168
+ return isBlank (inputStr .charAt (offset - 1 ));
169
+ }
170
+
171
+ // @since 2.17
172
+ protected boolean followedOnlyByBlank (String inputStr , int offset ) {
173
+ if (offset == inputStr .length () - 1 ) {
174
+ return true ;
175
+ }
176
+ return isBlank (inputStr .charAt (offset + 1 ));
177
+ }
178
+
179
+ // @since 2.17
180
+ protected boolean isBlank (char value ) {
181
+ return (' ' == value || '\t' == value );
182
+ }
183
+
167
184
/**
168
185
* Looks like we may get names with "funny characters" so.
169
186
*
@@ -199,7 +216,7 @@ public static class Default
199
216
public Default () { }
200
217
201
218
public static Default instance () { return INSTANCE ; }
202
-
219
+
203
220
/**
204
221
* Default implementation will call
205
222
* {@link #isReservedKeyword(String)} and
0 commit comments