From e70139ddb39ac6c6b41387548dde9d6cd0937961 Mon Sep 17 00:00:00 2001 From: jim Date: Tue, 26 Sep 2023 08:05:24 -0500 Subject: [PATCH] Add escape quote utility method. --- .github/workflows/gradle-nightly.yml | 39 ------------------- .github/workflows/gradle.yml | 2 - .../java/systems/comodal/jsoniter/JIUtil.java | 32 +++++++++++++++ .../systems/comodal/jsoniter/TestString.java | 13 +++++++ 4 files changed, 45 insertions(+), 41 deletions(-) delete mode 100644 .github/workflows/gradle-nightly.yml diff --git a/.github/workflows/gradle-nightly.yml b/.github/workflows/gradle-nightly.yml deleted file mode 100644 index 3662944..0000000 --- a/.github/workflows/gradle-nightly.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: Gradle Nightly Check - -on: - push: - branches: - - master - schedule: - - cron: '0 0 * * 1,4' - -jobs: - check: - strategy: - matrix: - os: [ ubuntu-latest, macOS-latest ] - runs-on: ${{ matrix.os }} - - steps: - - uses: actions/checkout@v4 - - uses: gradle/wrapper-validation-action@v1 - - - uses: actions/setup-java@v3 - with: - distribution: 'zulu' - java-version: 20 - java-package: jre - cache: 'gradle' - - run: printf "org.gradle.java.home=%s" "$JAVA_HOME" > gradle.properties - - - uses: actions/setup-java@v3 - with: - distribution: 'zulu' - java-version: 22-ea - check-latest: true - - run: printf "\norg.gradle.java.installations.paths=%s" "$JAVA_HOME" >> gradle.properties - - run: java --version - - - name: Gradle Check - run: ./gradlew -PtargetJava=22 check --warning-mode=all --stacktrace --no-daemon - diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 1616dff..a63b613 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -5,8 +5,6 @@ on: branches: - master pull_request: - schedule: - - cron: '0 0 * * 1,4' jobs: check: diff --git a/systems.comodal.json_iterator/src/main/java/systems/comodal/jsoniter/JIUtil.java b/systems.comodal.json_iterator/src/main/java/systems/comodal/jsoniter/JIUtil.java index 8421bc6..31b69c5 100644 --- a/systems.comodal.json_iterator/src/main/java/systems/comodal/jsoniter/JIUtil.java +++ b/systems.comodal.json_iterator/src/main/java/systems/comodal/jsoniter/JIUtil.java @@ -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; + } + } + } + } } diff --git a/systems.comodal.json_iterator/src/test/java/systems/comodal/jsoniter/TestString.java b/systems.comodal.json_iterator/src/test/java/systems/comodal/jsoniter/TestString.java index 2a155d4..c0c4696 100644 --- a/systems.comodal.json_iterator/src/test/java/systems/comodal/jsoniter/TestString.java +++ b/systems.comodal.json_iterator/src/test/java/systems/comodal/jsoniter/TestString.java @@ -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) {