diff --git a/nel/include/nel/misc/common.h b/nel/include/nel/misc/common.h index bca3372bd..1e99b5e70 100644 --- a/nel/include/nel/misc/common.h +++ b/nel/include/nel/misc/common.h @@ -255,6 +255,17 @@ int compareCaseInsensitive(const char *a, const char *b); int compareCaseInsensitive(const char *a, size_t lenA, const char *b, size_t lenB); inline int compareCaseInsensitive(const std::string &a, const std::string &b) { return compareCaseInsensitive(&a[0], a.size(), &b[0], b.size()); } +/** ASCII to lowercase. Useful for internal identifiers. +* Characters outside of the 7-bit ASCII space, and control characters, are replaced. +*/ +std::string toLowerAscii(const std::string &str, char replacement); +void toLowerAscii(char *str, char replacement); + +/** ASCII to uppercase. Useful for internal identifiers. +* Characters outside of the 7-bit ASCII space, and control characters, are replaced. +*/ +std::string toUpperAscii(const std::string &str, char replacement); +void toUpperAscii(char *str, char replacement); /** * Convert to an hexadecimal std::string diff --git a/nel/src/misc/common.cpp b/nel/src/misc/common.cpp index 8d046c97c..fce6af69c 100644 --- a/nel/src/misc/common.cpp +++ b/nel/src/misc/common.cpp @@ -689,6 +689,56 @@ void toUpper(char *str) } } +std::string toLowerAscii(const std::string &str, char replacement) +{ + std::string res; + res.reserve(str.size()); + for (std::string::const_iterator it(str.begin()), end(str.end()); it != end; ++it) + { + char c = *it; + if ((sint8)(c + '\x01') < (sint8)(' ' + '\x01')) res += replacement; + else if (c >= 'A' && c <= 'Z') res += c + ('a' - 'A'); + else res += c; + } + return res; +} + +void toLowerAscii(char *str, char replacement) +{ + for (ptrdiff_t i = 0; str[i]; ++i) + { + char c = str[i]; + if ((sint8)(c + '\x01') < (sint8)(' ' + '\x01')) str[i] = replacement; + else if (c >= 'A' && c <= 'Z') str[i] = c + ('a' - 'A'); + else str[i] = c; + } +} + +std::string toUpperAscii(const std::string &str, char replacement) +{ + std::string res; + res.reserve(str.size()); + for (std::string::const_iterator it(str.begin()), end(str.end()); it != end; ++it) + { + char c = *it; + if ((sint8)(c + '\x01') < (sint8)(' ' + '\x01')) res += replacement; + else if (c >= 'a' && c <= 'z') res += c - ('a' - 'A'); + else res += c; + } + return res; +} + +void toUpperAscii(char *str, char replacement) +{ + for (ptrdiff_t i = 0; str[i]; ++i) + { + char c = str[i]; + if ((sint8)(c + '\x01') < (sint8)(' ' + '\x01')) str[i] = replacement; + else if (c >= 'a' && c <= 'z') str[i] = c - ('a' - 'A'); + else str[i] = c; + } +} + std::string toHexa(const uint8 &b) { return toString("%02hhx", b);