@@ -93,6 +93,11 @@ public class RandomStringUtils {
93
93
'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' , '0' , '1' ,
94
94
'2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' };
95
95
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
+
96
101
/**
97
102
* Gets the singleton instance based on {@link ThreadLocalRandom#current()}; <b>which is not cryptographically
98
103
* 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
279
284
280
285
// Optimizations and tests when chars == null and using ASCII characters (end <= 0x7f)
281
286
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
-
287
287
// Optimize generation of full alphanumerical characters
288
288
// Normally, we would need to pick a 7-bit integer, since gap = 'z' - '0' + 1 = 75 > 64
289
289
// In turn, this would make us reject the sampling with probability 1 - 62 / 2^7 > 1 / 2
290
290
// Instead we can pick directly from the right set of 62 characters, which requires
291
291
// 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 ) {
293
293
return random (count , 0 , 0 , false , false , ALPHANUMERICAL_CHARS , random );
294
294
}
295
295
296
- if (numbers && end <= zeroDigitAscii || letters && end <= firstLetterAscii ) {
296
+ if (numbers && end <= ASCII_0 || letters && end <= ASCII_A ) {
297
297
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." );
300
300
}
301
301
302
302
// 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
308
308
// Note that because of the above test, we will always have start < end
309
309
// even after this optimization.
310
310
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 );
313
313
} else if (numbers ) {
314
314
// 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 );
317
317
} else if (letters ) {
318
318
// 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 );
321
321
}
322
322
}
323
323
0 commit comments