{"record":{"id":"67ecd5bc21e8c776","repo":"chinabugotech/hutool","slug":"alphabet-must-not-contain-spaces-index-d","errorCode":null,"errorMessage":"alphabet must not contain spaces: index %d","messagePattern":"alphabet must not contain spaces: index (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/codec/Hashids.java","lineNumber":462,"sourceCode":"\t}\n\n\tprivate char[] validateAndFilterAlphabet(final char[] alphabet, final char[] separators) {\n\t\t// validate size\n\t\tif (alphabet.length < MIN_ALPHABET_LENGTH) {\n\t\t\tthrow new IllegalArgumentException(String.format(\"alphabet must contain at least %d unique \" +\n\t\t\t\t\t\"characters: %d\", MIN_ALPHABET_LENGTH, alphabet.length));\n\t\t}\n\n\t\tfinal Set<Character> seen = new LinkedHashSet<>(alphabet.length);\n\t\tfinal Set<Character> invalid = IntStream.range(0, separators.length)\n\t\t\t\t.mapToObj(idx -> separators[idx])\n\t\t\t\t.collect(Collectors.toSet());\n\n\t\t// add to seen set (without duplicates)\n\t\tIntStream.range(0, alphabet.length)\n\t\t\t\t.forEach(i -> {\n\t\t\t\t\tif (alphabet[i] == ' ') {\n\t\t\t\t\t\tthrow new IllegalArgumentException(String.format(\"alphabet must not contain spaces: \" +\n\t\t\t\t\t\t\t\t\"index %d\", i));\n\t\t\t\t\t}\n\t\t\t\t\tfinal Character c = alphabet[i];\n\t\t\t\t\tif (!invalid.contains(c)) {\n\t\t\t\t\t\tseen.add(c);\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t// create a new alphabet without the duplicates\n\t\tfinal char[] uniqueAlphabet = new char[seen.size()];\n\t\tint idx = 0;\n\t\tfor (char c : seen) {\n\t\t\tuniqueAlphabet[idx++] = c;\n\t\t}\n\t\treturn uniqueAlphabet;\n\t}\n\n\t@SuppressWarnings(\"SameParameterValue\")","sourceCodeStart":444,"sourceCodeEnd":480,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/codec/Hashids.java#L444-L480","documentation":"Thrown during Hashids construction in validateAndFilterAlphabet() when any character in the custom alphabet is a space character (' '). The space character is reserved and must not appear in the alphabet because it would conflict with the algorithm's internal encoding logic. The error message includes the index of the offending space.","triggerScenarios":"Calling new Hashids(salt, customAlphabet, minLength) where customAlphabet contains at least one space character at any index. The check iterates every character and throws on the first space found.","commonSituations":"Building an alphabet from user input or a configuration string that accidentally contains whitespace. Including delimiters or padding characters in the alphabet. Copy-pasting an alphabet string with trailing or embedded spaces.","solutions":["Remove all space characters from the custom alphabet before passing it to the Hashids constructor.","Trim and sanitize configuration-sourced alphabet strings: alphabetStr.replace(\" \", \"\").toCharArray().","Use the default DEFAULT_ALPHABET which is guaranteed space-free."],"exampleFix":"// before\nchar[] alphabet = \"abc def ghi jklmnop\".toCharArray(); // contains spaces\nHashids h = new Hashids(salt, alphabet, -1); // throws\n\n// after\nchar[] alphabet = \"abcdefghijklmnopqrstuvwxyz0123456789\".toCharArray();\nHashids h = new Hashids(salt, alphabet, -1);","handlingStrategy":"validation","validationCode":"String cleaned = new String(alphabet).replace(\" \", \"\");\nif (cleaned.length() != alphabet.length) {\n    throw new IllegalStateException(\"Alphabet must not contain spaces\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Trim and sanitize alphabet strings from configuration files before use.","Use a unit test to verify no spaces in the alphabet constant.","Log the alphabet at startup to catch configuration issues early."],"tags":["hashids","alphabet","construction","configuration","codec"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}