Fast character-by-character validation without regex. Checks if name matches [_A-Za-z][_0-9A-Za-z]
(String name)
| 243 | * Checks if name matches [_A-Za-z][_0-9A-Za-z]* |
| 244 | */ |
| 245 | private static boolean isValidName(String name) { |
| 246 | if (name.isEmpty()) { |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | // First character must be [_A-Za-z] |
| 251 | char first = name.charAt(0); |
| 252 | if (!(first == '_' || (first >= 'A' && first <= 'Z') || (first >= 'a' && first <= 'z'))) { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | // Remaining characters must be [_0-9A-Za-z] |
| 257 | for (int i = 1; i < name.length(); i++) { |
| 258 | char c = name.charAt(i); |
| 259 | if (!(c == '_' || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))) { |
| 260 | return false; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | private static <T> T throwAssert(String format, Object... args) { |
| 268 | throw new AssertException(format(format, args)); |
no test coverage detected