diff --git a/include/bx/inline/string.inl b/include/bx/inline/string.inl index 21f9a88..82d428b 100644 --- a/include/bx/inline/string.inl +++ b/include/bx/inline/string.inl @@ -106,6 +106,11 @@ namespace bx set(_ptr, int32_t(_term-_ptr) ); } + inline void StringView::set(const StringView& _str) + { + set(_str.m_ptr, _str.m_len); + } + inline void StringView::clear() { m_ptr = ""; diff --git a/include/bx/string.h b/include/bx/string.h index f06c4b6..a968dbb 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -49,6 +49,9 @@ namespace bx /// void set(const char* _ptr, const char* _term); + /// + void set(const StringView& _str); + /// void clear(); @@ -105,24 +108,45 @@ namespace bx /// bool isSpace(char _ch); + /// + bool isSpace(const StringView& _str); + /// bool isUpper(char _ch); + /// + bool isUpper(const StringView& _str); + /// bool isLower(char _ch); + /// + bool isLower(const StringView& _str); + /// bool isAlpha(char _ch); + /// + bool isAlpha(const StringView& _str); + /// bool isNumeric(char _ch); + /// + bool isNumeric(const StringView& _str); + /// bool isAlphaNum(char _ch); + /// + bool isAlphaNum(const StringView& _str); + /// bool isPrint(char _ch); + /// + bool isPrint(const StringView& _str); + /// char toLower(char _ch); diff --git a/src/string.cpp b/src/string.cpp index c0f0328..58829bc 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -14,6 +14,11 @@ namespace bx { + inline bool isInRange(char _ch, char _from, char _to) + { + return unsigned(_ch - _from) <= unsigned(_to-_from); + } + bool isSpace(char _ch) { return ' ' == _ch @@ -25,11 +30,6 @@ namespace bx ; } - inline bool isInRange(char _ch, char _from, char _to) - { - return unsigned(_ch - _from) <= unsigned(_to-_from); - } - bool isUpper(char _ch) { return isInRange(_ch, 'A', 'Z'); @@ -60,6 +60,59 @@ namespace bx return isInRange(_ch, ' ', '~'); } + typedef bool (*CharTestFn)(char _ch); + + template + static bool isCharTest(const StringView& _str) + { + bool result = true; + + for (const char* ptr = _str.getPtr(), *term = _str.getTerm() + ; ptr != term && result + ; ++ptr + ) + { + result &= fn(*ptr); + } + + return result; + } + + bool isSpace(const StringView& _str) + { + return isCharTest(_str); + } + + bool isUpper(const StringView& _str) + { + return isCharTest(_str); + } + + bool isLower(const StringView& _str) + { + return isCharTest(_str); + } + + bool isAlpha(const StringView& _str) + { + return isCharTest(_str); + } + + bool isNumeric(const StringView& _str) + { + return isCharTest(_str); + } + + bool isAlphaNum(const StringView& _str) + { + return isCharTest(_str); + } + + bool isPrint(const StringView& _str) + { + return isCharTest(_str); + } + char toLower(char _ch) { return _ch + (isUpper(_ch) ? 0x20 : 0);