You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ki...@apache.org on 2023/02/20 21:31:27 UTC

[commons-imaging] branch master updated (fbb7549a -> ff13fa4f)

This is an automated email from the ASF dual-hosted git repository.

kinow pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-imaging.git


    from fbb7549a Bump actions/setup-java from 3.9.0 to 3.10.0
     new 3422d7b5 [IMAGING-347] Refactor BasicCParser::unescapeString()
     new 2608e567 [IMAGING-347] changelog
     new ff13fa4f Merge branch 'pr-280'

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |   3 +
 .../commons/imaging/common/BasicCParser.java       | 211 +++++++++++++--------
 2 files changed, 137 insertions(+), 77 deletions(-)


[commons-imaging] 02/03: [IMAGING-347] changelog

Posted by ki...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kinow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-imaging.git

commit 2608e567aaf8773c20b4172726f80fcc5939c041
Author: Bruno P. Kinoshita <ki...@users.noreply.github.com>
AuthorDate: Mon Feb 20 22:21:20 2023 +0100

    [IMAGING-347] changelog
---
 src/changes/changes.xml | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index fe5dea18..fe892873 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -83,6 +83,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="kinow" type="update" due-to="Dependabot">
         Bump maven-checkstyle plugin from 3.1.2 to 3.2.1 #233, #266.
       </action>
+      <action issue="IMAGING-347" dev="kinow" type="update" due-to="snumlautoken">
+        Refactor BasicCParser::unescapeString().
+      </action>
     </release>
     <release version="1.0-alpha3" date="2022-05-13" description="Third 1.0 alpha release">
       <action issue="IMAGING-330" dev="kinow" type="fix" due-to="Gary Lucas">


[commons-imaging] 03/03: Merge branch 'pr-280'

Posted by ki...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kinow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-imaging.git

commit ff13fa4f70d162f0be685e8a94e3706cfa71f989
Merge: fbb7549a 2608e567
Author: Bruno P. Kinoshita <ki...@users.noreply.github.com>
AuthorDate: Mon Feb 20 22:31:11 2023 +0100

    Merge branch 'pr-280'
    
    This closes #280

 src/changes/changes.xml                            |   3 +
 .../commons/imaging/common/BasicCParser.java       | 211 +++++++++++++--------
 2 files changed, 137 insertions(+), 77 deletions(-)


[commons-imaging] 01/03: [IMAGING-347] Refactor BasicCParser::unescapeString()

Posted by ki...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kinow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-imaging.git

commit 3422d7b5435f52d5e27cbed378138218bfe6ec79
Author: snumlautoken <ar...@live.se>
AuthorDate: Mon Feb 20 15:21:22 2023 +0100

    [IMAGING-347] Refactor BasicCParser::unescapeString()
---
 .../commons/imaging/common/BasicCParser.java       | 211 +++++++++++++--------
 1 file changed, 134 insertions(+), 77 deletions(-)

diff --git a/src/main/java/org/apache/commons/imaging/common/BasicCParser.java b/src/main/java/org/apache/commons/imaging/common/BasicCParser.java
index 572f019e..fb6e9b87 100644
--- a/src/main/java/org/apache/commons/imaging/common/BasicCParser.java
+++ b/src/main/java/org/apache/commons/imaging/common/BasicCParser.java
@@ -323,83 +323,7 @@ public class BasicCParser {
         for (int i = 1; i < (string.length() - 1); i++) {
             final char c = string.charAt(i);
             if (hadBackSlash) {
-                if (c == '\\') {
-                    stringBuilder.append('\\');
-                } else if (c == '"') {
-                    stringBuilder.append('"');
-                } else if (c == '\'') {
-                    stringBuilder.append('\'');
-                } else if (c == 'x') {
-                    if (i + 2 >= string.length()) {
-                        throw new ImageReadException(
-                                "Parsing XPM file failed, "
-                                        + "hex constant in string too short");
-                    }
-                    final char hex1 = string.charAt(i + 1);
-                    final char hex2 = string.charAt(i + 2);
-                    i += 2;
-                    int constant;
-                    try {
-                        constant = Integer.parseInt(hex1 + Character.toString(hex2), 16);
-                    } catch (final NumberFormatException nfe) {
-                        throw new ImageReadException(
-                                "Parsing XPM file failed, "
-                                        + "hex constant invalid", nfe);
-                    }
-                    stringBuilder.append((char) constant);
-                } else {
-                    switch (c) {
-                    case '0':
-                    case '1':
-                    case '2':
-                    case '3':
-                    case '4':
-                    case '5':
-                    case '6':
-                    case '7':
-                        int length = 1;
-                        if (i + 1 < string.length() && '0' <= string.charAt(i + 1)
-                                && string.charAt(i + 1) <= '7') {
-                            ++length;
-                        }
-                        if (i + 2 < string.length() && '0' <= string.charAt(i + 2)
-                                && string.charAt(i + 2) <= '7') {
-                            ++length;
-                        }
-                        int constant = 0;
-                        for (int j = 0; j < length; j++) {
-                            constant *= 8;
-                            constant += (string.charAt(i + j) - '0');
-                        }
-                        i += length - 1;
-                        stringBuilder.append((char) constant);
-                        break;
-                    case 'a':
-                        stringBuilder.append((char) 0x07);
-                        break;
-                    case 'b':
-                        stringBuilder.append((char) 0x08);
-                        break;
-                    case 'f':
-                        stringBuilder.append((char) 0x0c);
-                        break;
-                    case 'n':
-                        stringBuilder.append((char) 0x0a);
-                        break;
-                    case 'r':
-                        stringBuilder.append((char) 0x0d);
-                        break;
-                    case 't':
-                        stringBuilder.append((char) 0x09);
-                        break;
-                    case 'v':
-                        stringBuilder.append((char) 0x0b);
-                        break;
-                    default:
-                        throw new ImageReadException("Parsing XPM file failed, "
-                                + "invalid escape sequence");
-                    }
-                }
+                i = parseEscape(i, stringBuilder, string);
                 hadBackSlash = false;
             } else {
                 if (c == '\\') {
@@ -417,4 +341,137 @@ public class BasicCParser {
                     + "unterminated escape sequence found in string");
         }
     }
+
+    /**
+     * Parses the hexadecimal-base escape-sequence found at index {@code i} of {@code string}.
+     *
+     * <p>Helper-function for {@code unescapeString()}.</p>
+     *
+     * @param i  the index of the escape-sequence in the string
+     * @param stringBuilder the stringBuilder to append the escape-char to
+     * @param string the string whose chars are parsed
+     * @return the new index i
+     * @since 1.0-alpha3
+     */
+    private static int appendHex(int i, final StringBuilder stringBuilder, final String string)
+        throws ImageReadException {
+        if (i + 2 >= string.length()) {
+            throw new ImageReadException(
+                    "Parsing XPM file failed, "
+                            + "hex constant in string too short");
+        }
+        final char hex1 = string.charAt(i + 1);
+        final char hex2 = string.charAt(i + 2);
+        i += 2;
+        int constant;
+        try {
+            constant = Integer.parseInt(hex1 + Character.toString(hex2), 16);
+        } catch (final NumberFormatException nfe) {
+            throw new ImageReadException(
+                    "Parsing XPM file failed, "
+                            + "hex constant invalid", nfe);
+        }
+        stringBuilder.append((char) constant);
+        return i;
+    }
+
+    /**
+     * Parses the octal-base escape-sequence found at index {@code i} of {@code string}.
+     *
+     * <p>Helper-function for {@code unescapeString()}.</p>
+     *
+     * @param i  the index of the escape-sequence in the string
+     * @param stringBuilder the stringBuilder to append the escape-char to
+     * @param string the string whose chars are parsed
+     * @return the new index i
+     * @since 1.0-alpha3
+     */
+    private static int appendOct(int i, final StringBuilder stringBuilder, final String string)
+        throws ImageReadException {
+        int length = 1;
+        if (i + 1 < string.length() && '0' <= string.charAt(i + 1)
+                && string.charAt(i + 1) <= '7') {
+            ++length;
+        }
+        if (i + 2 < string.length() && '0' <= string.charAt(i + 2)
+                && string.charAt(i + 2) <= '7') {
+            ++length;
+        }
+        int constant = 0;
+        for (int j = 0; j < length; j++) {
+            constant *= 8;
+            constant += (string.charAt(i + j) - '0');
+        }
+        i += length - 1;
+        stringBuilder.append((char) constant);
+        return i;
+    }
+
+
+    /**
+     * Parses the {@code i:th} escape-char in the input {@code string} and appends it to {@code stringBuilder}.
+     *
+     * <p>Helper-function for {@code unescapeString()}.</p>
+     *
+     * @param  i  the index of the escape-char in the string
+     * @param  stringBuilder the stringBuilder to append the escape-char to
+     * @param  string the string whose chars are parsed
+     * @return the new index i
+     * @since 1.0-alpha3
+     */
+    private static int parseEscape(int i, final StringBuilder stringBuilder, final String string)
+        throws ImageReadException {
+        final char c = string.charAt(i);
+        switch (c) {
+        case '\\':
+            stringBuilder.append('\\');
+            break;
+        case '"':
+            stringBuilder.append('"');
+            break;
+        case '\'':
+            stringBuilder.append('\'');
+            break;
+        case 'x':
+            i = appendHex(i, stringBuilder, string);
+            break;
+        case '0':
+        case '1':
+        case '2':
+        case '3':
+        case '4':
+        case '5':
+        case '6':
+        case '7':
+            i = appendOct(i, stringBuilder, string);
+            break;
+        case 'a':
+            stringBuilder.append((char) 0x07);
+            break;
+        case 'b':
+            stringBuilder.append((char) 0x08);
+            break;
+        case 'f':
+            stringBuilder.append((char) 0x0c);
+            break;
+        case 'n':
+            stringBuilder.append((char) 0x0a);
+            break;
+        case 'r':
+            stringBuilder.append((char) 0x0d);
+            break;
+        case 't':
+            stringBuilder.append((char) 0x09);
+            break;
+        case 'v':
+            stringBuilder.append((char) 0x0b);
+            break;
+        default:
+            throw new ImageReadException("Parsing XPM file failed, "
+                    + "invalid escape sequence");
+        }
+        return i;
+
+    }
+
 }