You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/06/15 12:59:37 UTC
(commons-lang) 08/18: Add org.apache.commons.lang3.CharUtils.isOctal(char)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit ec5319a7e354ef09b41a39ab73fa310efd56be30
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sun Jun 15 08:10:30 2025 -0400
Add org.apache.commons.lang3.CharUtils.isOctal(char)
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/lang3/CharUtils.java | 11 +++++++++++
.../org/apache/commons/lang3/math/NumberUtils.java | 6 +++---
.../commons/lang3/text/translate/OctalUnescaper.java | 20 +++++---------------
4 files changed, 20 insertions(+), 18 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index db1b12a31..119871bc7 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -129,6 +129,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Pankraz76, Gary Gregory">Add ObjectUtils.getIfNull(Object, Object) and deprecate defaultIfNull(Object, Object).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">org.apache.commons.lang3.mutable.Mutable now extends Supplier.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.CharUtils.isHex(char).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.lang3.CharUtils.isOctal(char).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 84 #1267, #1277, #1283, #1288, #1302, #1377.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">[site] Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to 3.2.1 #1300.</action>
diff --git a/src/main/java/org/apache/commons/lang3/CharUtils.java b/src/main/java/org/apache/commons/lang3/CharUtils.java
index ed90518cd..b82519f5c 100644
--- a/src/main/java/org/apache/commons/lang3/CharUtils.java
+++ b/src/main/java/org/apache/commons/lang3/CharUtils.java
@@ -255,6 +255,17 @@ public static boolean isHex(final char ch) {
return isAsciiNumeric(ch) || ch >= 'a' && ch <= 'f' || ch >= 'A' && ch <= 'F';
}
+ /**
+ * Tests if the given char is an octal digit. Octal digits are the character representations of the digits 0 to 7.
+ *
+ * @param ch the char to check
+ * @return true if the given char is the character representation of one of the digits from 0 to 7
+ * @since 3.18.0
+ */
+ public static boolean isOctal(final char ch) {
+ return ch >= '0' && ch <= '7';
+ }
+
/**
* Converts the Character to a char throwing an exception for {@code null}.
*
diff --git a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
index e35f48047..ada805324 100644
--- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
+++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
@@ -582,7 +582,7 @@ public static boolean isCreatable(final String str) {
// leading 0, but not hex, must be octal
int i = start + 1;
for (; i < chars.length; i++) {
- if (chars[i] < '0' || chars[i] > '7') {
+ if (!CharUtils.isOctal(chars[i])) {
return false;
}
}
@@ -595,7 +595,7 @@ public static boolean isCreatable(final String str) {
// loop to the next to last char or to the last char if we need another digit to
// make a valid number (e.g. chars[0..5] = "1234E")
while (i < sz || i < sz + 1 && allowSigns && !foundDigit) {
- if (chars[i] >= '0' && chars[i] <= '9') {
+ if (CharUtils.isAsciiNumeric(chars[i])) {
foundDigit = true;
allowSigns = false;
@@ -628,7 +628,7 @@ public static boolean isCreatable(final String str) {
i++;
}
if (i < chars.length) {
- if (chars[i] >= '0' && chars[i] <= '9') {
+ if (CharUtils.isAsciiNumeric(chars[i])) {
// no type qualifier, OK
return true;
}
diff --git a/src/main/java/org/apache/commons/lang3/text/translate/OctalUnescaper.java b/src/main/java/org/apache/commons/lang3/text/translate/OctalUnescaper.java
index 3bc7c2415..17a905990 100644
--- a/src/main/java/org/apache/commons/lang3/text/translate/OctalUnescaper.java
+++ b/src/main/java/org/apache/commons/lang3/text/translate/OctalUnescaper.java
@@ -19,6 +19,8 @@
import java.io.IOException;
import java.io.Writer;
+import org.apache.commons.lang3.CharUtils;
+
/**
* Translate escaped octal Strings back to their octal values.
*
@@ -42,15 +44,6 @@ public OctalUnescaper() {
// empty
}
- /**
- * Checks if the given char is an octal digit. Octal digits are the character representations of the digits 0 to 7.
- * @param ch the char to check
- * @return true if the given char is the character representation of one of the digits from 0 to 7
- */
- private boolean isOctalDigit(final char ch) {
- return ch >= '0' && ch <= '7';
- }
-
/**
* Checks if the given char is the character representation of one of the digit from 0 to 3.
* @param ch the char to check
@@ -67,21 +60,18 @@ private boolean isZeroToThree(final char ch) {
public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
final int remaining = input.length() - index - 1; // how many characters left, ignoring the first \
final StringBuilder builder = new StringBuilder();
- if (input.charAt(index) == '\\' && remaining > 0 && isOctalDigit(input.charAt(index + 1))) {
+ if (input.charAt(index) == '\\' && remaining > 0 && CharUtils.isOctal(input.charAt(index + 1))) {
final int next = index + 1;
final int next2 = index + 2;
final int next3 = index + 3;
-
// we know this is good as we checked it in the if block above
builder.append(input.charAt(next));
-
- if (remaining > 1 && isOctalDigit(input.charAt(next2))) {
+ if (remaining > 1 && CharUtils.isOctal(input.charAt(next2))) {
builder.append(input.charAt(next2));
- if (remaining > 2 && isZeroToThree(input.charAt(next)) && isOctalDigit(input.charAt(next3))) {
+ if (remaining > 2 && isZeroToThree(input.charAt(next)) && CharUtils.isOctal(input.charAt(next3))) {
builder.append(input.charAt(next3));
}
}
-
out.write(Integer.parseInt(builder.toString(), 8));
return 1 + builder.length();
}