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


The following commit(s) were added to refs/heads/master by this push:
     new ec4f545b6 Refactor into constants
ec4f545b6 is described below

commit ec4f545b69571813e1792e4ff9a42ee219b57928
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Wed Sep 11 09:36:17 2024 -0400

    Refactor into constants
---
 .../apache/commons/lang3/RandomStringUtils.java    | 30 +++++++++++-----------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/RandomStringUtils.java 
b/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
index 573f9ae5e..07f4706e5 100644
--- a/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
@@ -93,6 +93,11 @@ public class RandomStringUtils {
             'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 
'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1',
             '2', '3', '4', '5', '6', '7', '8', '9' };
 
+    private static final int ASCII_0 = '0';
+    private static final int ASCII_9 = '9';
+    private static final int ASCII_A = 'A';
+    private static final int ASCII_z = 'z';
+
     /**
      * Gets the singleton instance based on {@link 
ThreadLocalRandom#current()}; <b>which is not cryptographically
      * secure</b>; use {@link #secure()} to use an algorithms/providers 
specified in the
@@ -279,24 +284,19 @@ public class RandomStringUtils {
 
         // Optimizations and tests when chars == null and using ASCII 
characters (end <= 0x7f)
         if (chars == null && end <= 0x7f) {
-            final int zeroDigitAscii = 48; // '0'
-            final int lastDigitAscii = 57; // '9'
-            final int firstLetterAscii = 65; // 'A'
-            final int lastLetterAscii = 122; // 'z'
-
             // Optimize generation of full alphanumerical characters
             // Normally, we would need to pick a 7-bit integer, since gap = 
'z' - '0' + 1 = 75 > 64
             // In turn, this would make us reject the sampling with 
probability 1 - 62 / 2^7 > 1 / 2
             // Instead we can pick directly from the right set of 62 
characters, which requires
             // picking a 6-bit integer and only rejecting with probability 2 / 
64 = 1 / 32
-            if (letters && numbers && start <= zeroDigitAscii && end >= 
lastLetterAscii + 1) {
+            if (letters && numbers && start <= ASCII_0 && end >= ASCII_z + 1) {
                 return random(count, 0, 0, false, false, ALPHANUMERICAL_CHARS, 
random);
             }
 
-            if (numbers && end <= zeroDigitAscii || letters && end <= 
firstLetterAscii) {
+            if (numbers && end <= ASCII_0 || letters && end <= ASCII_A) {
                 throw new IllegalArgumentException(
-                        "Parameter end (" + end + ") must be greater then (" + 
zeroDigitAscii + ") for generating digits "
-                                + "or greater then (" + firstLetterAscii + ") 
for generating letters.");
+                        "Parameter end (" + end + ") must be greater then (" + 
ASCII_0 + ") for generating digits "
+                                + "or greater then (" + ASCII_A + ") for 
generating letters.");
             }
 
             // Optimize start and end when filtering by letters and/or numbers:
@@ -308,16 +308,16 @@ public class RandomStringUtils {
             // Note that because of the above test, we will always have start 
< end
             // even after this optimization.
             if (letters && numbers) {
-                start = Math.max(zeroDigitAscii, start);
-                end = Math.min(lastLetterAscii + 1, end);
+                start = Math.max(ASCII_0, start);
+                end = Math.min(ASCII_z + 1, end);
             } else if (numbers) {
                 // just numbers, no letters
-                start = Math.max(zeroDigitAscii, start);
-                end = Math.min(lastDigitAscii + 1, end);
+                start = Math.max(ASCII_0, start);
+                end = Math.min(ASCII_9 + 1, end);
             } else if (letters) {
                 // just letters, no numbers
-                start = Math.max(firstLetterAscii, start);
-                end = Math.min(lastLetterAscii + 1, end);
+                start = Math.max(ASCII_A, start);
+                end = Math.min(ASCII_z + 1, end);
             }
         }
 

Reply via email to