8000 Merge pull request #618 from datastax/java1094 · kecmu/java-driver@6afbc43 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6afbc43

Browse files
committed
Merge pull request apache#618 from datastax/java1094
JAVA-1094: Backport TypeCodec parse and format fixes from 3.0.
2 parents f717a81 + f89857f commit 6afbc43

File tree

4 files changed

+163
-46
lines changed

4 files changed

+163
-46
lines changed

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- [bug] JAVA-727: Allow monotonic timestamp generators to drift in the future + use microsecond precision when possible.
3838
- [improvement] JAVA-444: Add Java process information to UUIDs.makeNode() hash.
3939
- [improvement] JAVA-977: Preserve original cause when BuiltStatement value can't be serialized.
40+
- [bug] JAVA-1094: Backport TypeCodec parse and format fixes from 3.0.
4041

4142
Merged from 2.0 branch:
4243

driver-core/src/main/java/com/datastax/driver/core/ParseUtils.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,72 @@ public static boolean isIdentifierChar(int c) {
159159
public static boolean isBlank(int c) {
160160
return c == ' ' || c == '\t' || c == '\n';
161161
}
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+
}
162230
}

0 commit comments

Comments
 (0)
0