Branimir Karadžić 8 years ago
parent
commit
3e132964d2
3 changed files with 87 additions and 5 deletions
  1. 5 0
      include/bx/inline/string.inl
  2. 24 0
      include/bx/string.h
  3. 58 5
      src/string.cpp

+ 5 - 0
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 = "";

+ 24 - 0
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);
 

+ 58 - 5
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<CharTestFn fn>
+	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<isSpace>(_str);
+	}
+
+	bool isUpper(const StringView& _str)
+	{
+		return isCharTest<isUpper>(_str);
+	}
+
+	bool isLower(const StringView& _str)
+	{
+		return isCharTest<isLower>(_str);
+	}
+
+	bool isAlpha(const StringView& _str)
+	{
+		return isCharTest<isAlpha>(_str);
+	}
+
+	bool isNumeric(const StringView& _str)
+	{
+		return isCharTest<isNumeric>(_str);
+	}
+
+	bool isAlphaNum(const StringView& _str)
+	{
+		return isCharTest<isAlphaNum>(_str);
+	}
+
+	bool isPrint(const StringView& _str)
+	{
+		return isCharTest<isPrint>(_str);
+	}
+
 	char toLower(char _ch)
 	{
 		return _ch + (isUpper(_ch) ? 0x20 : 0);