@@ -93,6 +93,11 @@ public class RandomStringUtils {
9393 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' , '0' , '1' ,
9494 '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' };
9595
96+ private static final int ASCII_0 = '0' ;
97+ private static final int ASCII_9 = '9' ;
98+ private static final int ASCII_A = 'A' ;
99+ private static final int ASCII_z = 'z' ;
100+
96101 /**
97102 * Gets the singleton instance based on {@link ThreadLocalRandom#current()}; <b>which is not cryptographically
98103 * secure</b>; use {@link #secure()} to use an algorithms/providers specified in the
@@ -279,24 +284,19 @@ public static String random(int count, int start, int end, final boolean letters
279284
280285 // Optimizations and tests when chars == null and using ASCII characters (end <= 0x7f)
281286 if (chars == null && end <= 0x7f ) {
282- final int zeroDigitAscii = 48 ; // '0'
283- final int lastDigitAscii = 57 ; // '9'
284- final int firstLetterAscii = 65 ; // 'A'
285- final int lastLetterAscii = 122 ; // 'z'
286-
287287 // Optimize generation of full alphanumerical characters
288288 // Normally, we would need to pick a 7-bit integer, since gap = 'z' - '0' + 1 = 75 > 64
289289 // In turn, this would make us reject the sampling with probability 1 - 62 / 2^7 > 1 / 2
290290 // Instead we can pick directly from the right set of 62 characters, which requires
291291 // picking a 6-bit integer and only rejecting with probability 2 / 64 = 1 / 32
292- if (letters && numbers && start <= zeroDigitAscii && end >= lastLetterAscii + 1 ) {
292+ if (letters && numbers && start <= ASCII_0 && end >= ASCII_z + 1 ) {
293293 return random (count , 0 , 0 , false , false , ALPHANUMERICAL_CHARS , random );
294294 }
295295
296- if (numbers && end <= zeroDigitAscii || letters && end <= firstLetterAscii ) {
296+ if (numbers && end <= ASCII_0 || letters && end <= ASCII_A ) {
297297 throw new IllegalArgumentException (
298- "Parameter end (" + end + ") must be greater then (" + zeroDigitAscii + ") for generating digits "
299- + "or greater then (" + firstLetterAscii + ") for generating letters." );
298+ "Parameter end (" + end + ") must be greater then (" + ASCII_0 + ") for generating digits "
299+ + "or greater then (" + ASCII_A + ") for generating letters." );
300300 }
301301
302302 // Optimize start and end when filtering by letters and/or numbers:
@@ -308,16 +308,16 @@ public static String random(int count, int start, int end, final boolean letters
308308 // Note that because of the above test, we will always have start < end
309309 // even after this optimization.
310310 if (letters && numbers ) {
311- start = Math .max (zeroDigitAscii , start );
312- end = Math .min (lastLetterAscii + 1 , end );
311+ start = Math .max (ASCII_0 , start );
312+ end = Math .min (ASCII_z + 1 , end );
313313 } else if (numbers ) {
314314 // just numbers, no letters
315- start = Math .max (zeroDigitAscii , start );
316- end = Math .min (lastDigitAscii + 1 , end );
315+ start = Math .max (ASCII_0 , start );
316+ end = Math .min (ASCII_9 + 1 , end );
317317 } else if (letters ) {
318318 // just letters, no numbers
319- start = Math .max (firstLetterAscii , start );
320- end = Math .min (lastLetterAscii + 1 , end );
319+ start = Math .max (ASCII_A , start );
320+ end = Math .min (ASCII_z + 1 , end );
321321 }
322322 }
323323
0 commit comments