修复代码中的 Unicode 字符串处理
检查文本长度、截断、大小写和校验逻辑,针对多语言输入修正实现并补充边界测试。
完整提示词
<context>
What a user sees as one character can be several code points ("é" as e plus a combining accent, a flag, a family emoji joined by zero-width joiners, a Devanagari syllable) and each code point can be one or two UTF-16 code units or up to four UTF-8 bytes. So `str.length`, `slice`, `substring`, `[::-1]` and byte-based column limits split characters, leave half a surrogate pair, or drop an accent. `toUpperCase` without a locale turns Turkish "i" into "I" instead of "İ", German "ß" uppercases to "SS" (changing length), and case-insensitive comparison needs case folding, not lowercasing. "First letter as initial", slug generation and username validation written with `[A-Za-z]` lock out most of the world's names. Each language has the right tool: `Intl.Segmenter` in JavaScript, the `regex` package's `\X` or a grapheme library in Python, `unicode-segmentation` in Rust, Swift's `Character`, ICU's `BreakIterator` in Java and Kotlin. Encoding bugs (mojibake) are a different problem and are not in scope here.
</context>
<task>
Fix Unicode handling in this {{language}} code:
{{code}}
1. Find every operation that measures, cuts, reverses, iterates, changes case, compares, validates or generates identifiers from user text.
2. For each, give an input that breaks it, chosen from: a combining sequence (e plus U+0301), an emoji with skin tone, a ZWJ family emoji, a flag, Devanagari or Thai text, Turkish "i", German "ß", Greek final sigma, and a right-to-left name.
3. Fix with the right unit:
- Display length and truncation (counters, "Read more", initials): grapheme clusters, with an ellipsis that never splits a cluster.
- Storage limits: decide whether the limit is bytes (database column, protocol) or characters (product rule) and enforce the right one, truncating on a cluster boundary.
- Case: locale-aware case mapping when the locale is known; case folding for case-insensitive comparison.
- Equality and uniqueness: Unicode normalization (NFC for storage, NFKC with confusable checks for usernames when needed).
- Validation: Unicode property classes (`\p{L}`, `\p{M}`) instead of ASCII ranges for names; never require a first and last name.
- Slugs: transliterate only where a library supports the script; otherwise keep Unicode in the slug with percent-encoding.
4. Write tests with the breaking inputs as fixtures, asserting counts, truncation results and round trips.
5. Note where client and server count differently (for example a JavaScript `maxlength` counts UTF-16 units while the database counts characters) and align them.
</task>
<constraints>
- Change behaviour only for the identified bugs; keep ASCII results identical.
- Name the library or built-in you rely on and whether it is already a dependency; do not add a dependency when the standard library covers the case.
- Do not handle encoding or mojibake here; mention it if seen and leave it.
{{> output/uncertainty}}
</constraints>
<output_format>
## Findings
| Location | Operation | Breaking input | Wrong result | Correct result |
## Fixes
Diff per file.
## Tests
Test code with fixtures.
## Storage and limits
Byte versus character decisions and client-server alignment.
</output_format>可替换内容
- {{language}}
- {{code}}
- {{> output/uncertainty}}
- [::-1]
- [A-Za-z]
分类与标签
编程开发、国际化开发、字符串处理、Unicode 测试