From 2c5c5a5397883602ca2aec4f34f9bd2c085779b3 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 16 Sep 2019 13:45:14 -0700 Subject: Add and use custom string types and functions --- common/alstring.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ common/alstring.h | 29 +++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 common/alstring.cpp create mode 100644 common/alstring.h (limited to 'common') 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 +#include + + +namespace { + +int to_upper(const char ch) +{ + using char8_traits = std::char_traits; + 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 diff --git a/common/alstring.h b/common/alstring.h new file mode 100644 index 00000000..194e54a1 --- /dev/null +++ b/common/alstring.h @@ -0,0 +1,29 @@ +#ifndef AL_STRING_H +#define AL_STRING_H + +#include +#include + +#include "almalloc.h" + + +namespace al { + +template> +using basic_string = std::basic_string>; + +using string = basic_string; +using wstring = basic_string; +using u16string = basic_string; +using u32string = basic_string; + + +/* These would be better served by using a string_view-like span/view with + * case-insensitive char traits. + */ +int strcasecmp(const char *str0, const char *str1) noexcept; +int strncasecmp(const char *str0, const char *str1, std::size_t len) noexcept; + +} // namespace al + +#endif /* AL_STRING_H */ -- cgit v1.2.3