diff options
author | Chris Robinson <[email protected]> | 2019-09-16 13:45:14 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2019-09-16 13:45:14 -0700 |
commit | 2c5c5a5397883602ca2aec4f34f9bd2c085779b3 (patch) | |
tree | 3909a6774d375c128e17c7667132adfb91cd160f /common/alstring.cpp | |
parent | 650764775febeedd2834ce3a0838d6468f4b6b31 (diff) |
Add and use custom string types and functions
Diffstat (limited to 'common/alstring.cpp')
-rw-r--r-- | common/alstring.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/common/alstring.cpp b/common/alstring.cpp new file mode 100644 index 00000000..4a84be1d --- /dev/null +++ b/common/alstring.cpp @@ -0,0 +1,45 @@ + +#include "config.h" + +#include "alstring.h" + +#include <cctype> +#include <string> + + +namespace { + +int to_upper(const char ch) +{ + using char8_traits = std::char_traits<char>; + return std::toupper(char8_traits::to_int_type(ch)); +} + +} // namespace + +namespace al { + +int strcasecmp(const char *str0, const char *str1) noexcept +{ + do { + const int diff{to_upper(*str0) - to_upper(*str1)}; + if(diff < 0) return -1; + if(diff > 0) return 1; + } while(*(str0++) && *(str1++)); + return 0; +} + +int strncasecmp(const char *str0, const char *str1, std::size_t len) noexcept +{ + if(len > 0) + { + do { + const int diff{to_upper(*str0) - to_upper(*str1)}; + if(diff < 0) return -1; + if(diff > 0) return 1; + } while(--len && *(str0++) && *(str1++)); + } + return 0; +} + +} // namespace al |