@@ -159,4 +159,72 @@ public static boolean isIdentifierChar(int c) {
159
159
public static boolean isBlank (int c ) {
160
160
return c == ' ' || c == '\t' || c == '\n' ;
161
161
}
162
+
163
+ /**
164
+ * Return {@code true} if the given string is surrounded
165
+ * by single quotes, and {@code false} otherwise.
166
+ *
167
+ * @param value The string to inspect.
168
+ * @return {@code true} if the given string is surrounded
169
+ * by single quotes, and {@code false} otherwise.
170
+ */
171
+ public static boolean isQuoted (String value ) {
172
+ return value != null && value .length () > 1 && value .charAt (0 ) == '\'' && value .charAt (value .length () - 1 ) == '\'' ;
173
+ }
174
+
175
+ /**
176
+ * Quote the given string; single quotes are escaped.
177
+ * If the given string is null, this method returns a quoted empty string ({@code ''}).
178
+ *
179
+ * @param value The value to quote.
180
+ * @return The quoted string.
181
+ */
182
+ public static String quote (String value ) {
183
+ return '\'' + replaceChar (value , '\'' , "''" ) + '\'' ;
184
+ }
185
+
186
+ /**
187
+ * Unquote the given string if it is quoted; single quotes are unescaped.
188
+ * If the given string is not quoted, it is returned without any modification.
189
+ *
190
+ * @param value The string to unquote.
191
+ * @return The unquoted string.
192
+ */
193
+ public static String unquote (String value ) {
194
+ if (!isQuoted (value ))
195
+ return value ;
196
+ return value .substring (1 , value .length () - 1 ).replace ("''" , "'" );
197
+ }
198
+
199
+ // Simple method to replace a single character. String.replace is a bit too
200
+ // inefficient (see JAVA-67)
201
+ static String replaceChar (String text , char search , String replacement ) {
202
+ if (text == null || text .isEmpty ())
203
+ return text ;
204
+
205
+ int nbMatch = 0 ;
206
+ int start = -1 ;
207
+ do {
208
+ start = text .indexOf (search , start + 1 );
209
+ if (start != -1 )
210
+ ++nbMatch ;
211
+ } while (start != -1 );
212
+
213
+ if (nbMatch == 0 )
214
+ return text ;
215
+
216
+ int newLength = text .length () + nbMatch * (replacement .length () - 1 );
217
+ char [] result = new char [newLength ];
218
+ int newIdx = 0 ;
219
+ for (int i = 0 ; i < text .length (); i ++) {
220
+ char c = text .charAt (i );
221
+ if (c == search ) {
222
+ for (int r = 0 ; r < replacement .length (); r ++)
223
+ result [newIdx ++] = replacement .charAt (r );
224
+ } else {
225
+ result [newIdx ++] = c ;
226
+ }
227
+ }
228
+ return new String (result );
229
+ }
162
230
}
0 commit comments