2 ms·
If you're going to allow unicode usernames then you should casefold them rather than lowercasing them before normalizing as NFKC. You should ideally also store
by Alex3917 9y ago
If you're going to allow unicode usernames then you should casefold them rather than lowercasing them before normalizing as NFKC.
You should ideally also store a second copy of the username in the original casing and normalized as NFC for display purposes, as some users care a lot about seeing their username exactly as they entered it. (And in fact not allowing this may be seen as culturally insensitive in some cases, much like not supporting unicode.) The same applies to the user's first and last name, which you can store in NFC for display purposes and casefolded into NFKC for string comparison (e.g. search) purposes.
That said, most sites limit usernames to ASCII characters so that they can be (easily) used in URLs. In this case you don't need to casefold or normalize, just converting to lowercase is enough.
- ubernostrum 9y agoIf you're going to allow unicode usernames then you should casefold them rather than lowercasing them before normalizing as NFKC. I wanted to stay out of the Python 2 vs. 3 quagmire in this article, but it's worth knowing that in Python 3.3+, strings have a 'casefold()' method: https://docs.python.org/3/library/stdtypes.html#str.casefold https://docs.python.org/3/library/stdtypes.html#str.casefold Unfortunately, since Python 2 still has around two years of upstream support before EOL, I can't universally recommend people just use 'casefold()', no matter how much I'd like to.