Skip to content

Commit

Permalink
Add escape quote utility method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpe42 committed Sep 26, 2023
1 parent ab93910 commit e70139d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
39 changes: 0 additions & 39 deletions .github/workflows/gradle-nightly.yml

This file was deleted.

2 changes: 0 additions & 2 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
branches:
- master
pull_request:
schedule:
- cron: '0 0 * * 1,4'

jobs:
check:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,36 @@ public static long compileReplacePattern(final byte byteToFind) {
| (pattern << 48)
| (pattern << 56);
}

public static String escapeQuotes(final String str) {
final char[] chars = str.toCharArray();
final char[] escaped = new char[chars.length << 1];
char c;
for (int escapes = 0, from = 0, dest = 0, to = 0; ; to++) {
if (to == chars.length) {
if (from == 0) {
return str;
} else {
final int len = to - from;
System.arraycopy(chars, from, escaped, dest, len);
dest += len;
return new String(escaped, 0, dest);
}
} else {
c = chars[to];
if (c == '\\') {
escapes++;
} else if (c == '"' && (escapes & 1) == 0) {
final int len = to - from;
System.arraycopy(chars, from, escaped, dest, len);
dest += len;
escaped[dest++] = '\\';
from = to;
escapes = 0;
} else {
escapes = 0;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ void test_ascii_string(final JsonIteratorFactory factory) {
assertArrayEquals(world, ji.decodeBase64String());
}

@Test
void testEscapeQuotes() {
var nestedJson = """
{"hello": "world"}""";
assertEquals("""
{\\"hello\\": \\"world\\"}""", JIUtil.escapeQuotes(nestedJson));

nestedJson = """
{"hello": "\\"world\\""}""";
assertEquals("""
{\\"hello\\": \\"\\"world\\"\\"}""", JIUtil.escapeQuotes(nestedJson));
}

@ParameterizedTest
@MethodSource("systems.comodal.jsoniter.TestFactories#factories")
void testRandomBase64Data(final JsonIteratorFactory factory) {
Expand Down

0 comments on commit e70139d

Please sign in to comment.