Branimir Karadžić 8 лет назад
Родитель
Сommit
d3200d1d26
10 измененных файлов с 57 добавлено и 78 удалено
  1. 5 5
      include/bx/inline/string.inl
  2. 3 15
      include/bx/string.h
  3. 1 0
      scripts/toolchain.lua
  4. 1 1
      src/commandline.cpp
  5. 6 6
      src/crtnone.cpp
  6. 3 3
      src/dtoa.cpp
  7. 4 4
      src/os.cpp
  8. 17 27
      src/string.cpp
  9. 16 16
      tests/string_test.cpp
  10. 1 1
      tests/vsnprintf_test.cpp

+ 5 - 5
include/bx/inline/string.inl

@@ -43,8 +43,8 @@ namespace bx
 	{
 	{
 		Ty str = _str;
 		Ty str = _str;
 		typename Ty::size_type startPos = 0;
 		typename Ty::size_type startPos = 0;
-		const typename Ty::size_type fromLen = strnlen(_from);
-		const typename Ty::size_type toLen   = strnlen(_to);
+		const typename Ty::size_type fromLen = strLen(_from);
+		const typename Ty::size_type toLen   = strLen(_to);
 		while ( (startPos = str.find(_from, startPos) ) != Ty::npos)
 		while ( (startPos = str.find(_from, startPos) ) != Ty::npos)
 		{
 		{
 			str.replace(startPos, fromLen, _to);
 			str.replace(startPos, fromLen, _to);
@@ -81,7 +81,7 @@ namespace bx
 
 
 		if (NULL != _ptr)
 		if (NULL != _ptr)
 		{
 		{
-			int32_t len = strnlen(_ptr, _len);
+			int32_t len = strLen(_ptr, _len);
 			if (0 != len)
 			if (0 != len)
 			{
 			{
 				m_len = len;
 				m_len = len;
@@ -167,10 +167,10 @@ namespace bx
 		if (0 != _len)
 		if (0 != _len)
 		{
 		{
 			int32_t old = m_len;
 			int32_t old = m_len;
-			int32_t len = m_len + strnlen(_ptr, _len);
+			int32_t len = m_len + strLen(_ptr, _len);
 			char* ptr = (char*)BX_REALLOC(*AllocatorT, 0 != m_len ? const_cast<char*>(m_ptr) : NULL, len+1);
 			char* ptr = (char*)BX_REALLOC(*AllocatorT, 0 != m_len ? const_cast<char*>(m_ptr) : NULL, len+1);
 			m_len = len;
 			m_len = len;
-			strlncpy(ptr + old, len-old+1, _ptr, _len);
+			strCopy(ptr + old, len-old+1, _ptr, _len);
 
 
 			*const_cast<char**>(&m_ptr) = ptr;
 			*const_cast<char**>(&m_ptr) = ptr;
 		}
 		}

+ 3 - 15
include/bx/string.h

@@ -133,14 +133,14 @@ namespace bx
 	int32_t strincmp(const char* _lhs, const char* _rhs, int32_t _max = INT32_MAX);
 	int32_t strincmp(const char* _lhs, const char* _rhs, int32_t _max = INT32_MAX);
 
 
 	///
 	///
-	int32_t strnlen(const char* _str, int32_t _max = INT32_MAX);
+	int32_t strLen(const char* _str, int32_t _max = INT32_MAX);
 
 
 	/// Copy _num characters from string _src to _dst buffer of maximum _dstSize capacity
 	/// Copy _num characters from string _src to _dst buffer of maximum _dstSize capacity
 	/// including zero terminator. Copy will be terminated with '\0'.
 	/// including zero terminator. Copy will be terminated with '\0'.
-	int32_t strlncpy(char* _dst, int32_t _dstSize, const char* _src, int32_t _num = INT32_MAX);
+	int32_t strCopy(char* _dst, int32_t _dstSize, const char* _src, int32_t _num = INT32_MAX);
 
 
 	///
 	///
-	int32_t strlncat(char* _dst, int32_t _dstSize, const char* _src, int32_t _num = INT32_MAX);
+	int32_t strCat(char* _dst, int32_t _dstSize, const char* _src, int32_t _num = INT32_MAX);
 
 
 	///
 	///
 	const char* strnchr(const char* _str, char _ch, int32_t _max = INT32_MAX);
 	const char* strnchr(const char* _str, char _ch, int32_t _max = INT32_MAX);
@@ -215,18 +215,6 @@ namespace bx
 	/// Convert size in bytes to human readable string.
 	/// Convert size in bytes to human readable string.
 	void prettify(char* _out, int32_t _count, uint64_t _size);
 	void prettify(char* _out, int32_t _count, uint64_t _size);
 
 
-	/// Copy src to string dst of size siz.  At most siz-1 characters
-	/// will be copied.  Always NUL terminates (unless siz == 0).
-	/// Returns strlen(src); if retval >= siz, truncation occurred.
-	int32_t strlcpy(char* _dst, const char* _src, int32_t _max);
-
-	/// Appends src to string dst of size siz (unlike strncat, siz is the
-	/// full size of dst, not space left).  At most siz-1 characters
-	/// will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
-	/// Returns strlen(src) + MIN(siz, strlen(initial dst)).
-	/// If retval >= siz, truncation occurred.
-	int32_t strlcat(char* _dst, const char* _src, int32_t _max);
-
 	///
 	///
 	int32_t toString(char* _out, int32_t _max, double _value);
 	int32_t toString(char* _out, int32_t _max, double _value);
 
 

+ 1 - 0
scripts/toolchain.lua

@@ -779,6 +779,7 @@ function toolchain(_buildDir, _libDir)
 			"-msse2",
 			"-msse2",
 			"-Wunused-value",
 			"-Wunused-value",
 			"-Wundef",
 			"-Wundef",
+			"-Wno-strict-overflow",
 		}
 		}
 		buildoptions_cpp {
 		buildoptions_cpp {
 			"-std=c++11",
 			"-std=c++11",

+ 1 - 1
src/commandline.cpp

@@ -270,7 +270,7 @@ namespace bx
 				++arg;
 				++arg;
 				if (_short == *arg)
 				if (_short == *arg)
 				{
 				{
-					if (1 == strnlen(arg) )
+					if (1 == strLen(arg) )
 					{
 					{
 						if (0 == _skip)
 						if (0 == _skip)
 						{
 						{

+ 6 - 6
src/crtnone.cpp

@@ -39,29 +39,29 @@ extern "C" int32_t memcmp(const void* _lhs, const void* _rhs, size_t _numBytes)
 
 
 extern "C" size_t strlen(const char* _str)
 extern "C" size_t strlen(const char* _str)
 {
 {
-	return bx::strnlen(_str);
+	return bx::strLen(_str);
 }
 }
 
 
-extern "C" size_t strnlen(const char* _str, size_t _max)
+extern "C" size_t strLen(const char* _str, size_t _max)
 {
 {
-	return bx::strnlen(_str, _max);
+	return bx::strLen(_str, _max);
 }
 }
 
 
 extern "C" void* strcpy(char* _dst, const char* _src)
 extern "C" void* strcpy(char* _dst, const char* _src)
 {
 {
-	bx::strlncpy(_dst, INT32_MAX, _src, INT32_MAX);
+	bx::strCopy(_dst, INT32_MAX, _src, INT32_MAX);
 	return _dst;
 	return _dst;
 }
 }
 
 
 extern "C" void* strncpy(char* _dst, const char* _src, size_t _num)
 extern "C" void* strncpy(char* _dst, const char* _src, size_t _num)
 {
 {
-	bx::strlncpy(_dst, INT32_MAX, _src, _num);
+	bx::strCopy(_dst, INT32_MAX, _src, _num);
 	return _dst;
 	return _dst;
 }
 }
 
 
 extern "C" char* strcat(char* _dst, const char* _src)
 extern "C" char* strcat(char* _dst, const char* _src)
 {
 {
-	bx::strlncat(_dst, INT32_MAX, _src, INT32_MAX);
+	bx::strCat(_dst, INT32_MAX, _src, INT32_MAX);
 	return _dst;
 	return _dst;
 }
 }
 
 

+ 3 - 3
src/dtoa.cpp

@@ -436,17 +436,17 @@ namespace bx
 
 
 		if (isNan(_value) )
 		if (isNan(_value) )
 		{
 		{
-			return (int32_t)strlncpy(_dst, _max, "nan") + sign;
+			return (int32_t)strCopy(_dst, _max, "nan") + sign;
 		}
 		}
 		else if (isInfinite(_value) )
 		else if (isInfinite(_value) )
 		{
 		{
-			return (int32_t)strlncpy(_dst, _max, "inf") + sign;
+			return (int32_t)strCopy(_dst, _max, "inf") + sign;
 		}
 		}
 
 
 		int32_t len;
 		int32_t len;
 		if (0.0 == _value)
 		if (0.0 == _value)
 		{
 		{
-			len = (int32_t)strlncpy(_dst, _max, "0.0");
+			len = (int32_t)strCopy(_dst, _max, "0.0");
 		}
 		}
 		else
 		else
 		{
 		{

+ 4 - 4
src/os.cpp

@@ -235,12 +235,12 @@ namespace bx
 		bool result = false;
 		bool result = false;
 		if (NULL != ptr)
 		if (NULL != ptr)
 		{
 		{
-			len = (uint32_t)strnlen(ptr);
+			len = (uint32_t)strLen(ptr);
 
 
 			result = len != 0 && len < *_inOutSize;
 			result = len != 0 && len < *_inOutSize;
 			if (len < *_inOutSize)
 			if (len < *_inOutSize)
 			{
 			{
-				strlncpy(_out, len, ptr);
+				strCopy(_out, len, ptr);
 			}
 			}
 		}
 		}
 
 
@@ -340,7 +340,7 @@ namespace bx
 		if (stat("/tmp", fi)
 		if (stat("/tmp", fi)
 		&&  FileInfo::Directory == fi.m_type)
 		&&  FileInfo::Directory == fi.m_type)
 		{
 		{
-			strlncpy(_out, *_inOutSize, "/tmp");
+			strCopy(_out, *_inOutSize, "/tmp");
 			*_inOutSize = 4;
 			*_inOutSize = 4;
 			return true;
 			return true;
 		}
 		}
@@ -418,7 +418,7 @@ namespace bx
 		int32_t total = 0;
 		int32_t total = 0;
 		for (uint32_t ii = 0; NULL != _argv[ii]; ++ii)
 		for (uint32_t ii = 0; NULL != _argv[ii]; ++ii)
 		{
 		{
-			total += (int32_t)strnlen(_argv[ii]) + 1;
+			total += (int32_t)strLen(_argv[ii]) + 1;
 		}
 		}
 
 
 		char* temp = (char*)alloca(total);
 		char* temp = (char*)alloca(total);

+ 17 - 27
src/string.cpp

@@ -70,7 +70,7 @@ namespace bx
 
 
 	void toLower(char* _inOutStr, int32_t _max)
 	void toLower(char* _inOutStr, int32_t _max)
 	{
 	{
-		const int32_t len = strnlen(_inOutStr, _max);
+		const int32_t len = strLen(_inOutStr, _max);
 		toLowerUnsafe(_inOutStr, len);
 		toLowerUnsafe(_inOutStr, len);
 	}
 	}
 
 
@@ -89,7 +89,7 @@ namespace bx
 
 
 	void toUpper(char* _inOutStr, int32_t _max)
 	void toUpper(char* _inOutStr, int32_t _max)
 	{
 	{
-		const int32_t len = strnlen(_inOutStr, _max);
+		const int32_t len = strLen(_inOutStr, _max);
 		toUpperUnsafe(_inOutStr, len);
 		toUpperUnsafe(_inOutStr, len);
 	}
 	}
 
 
@@ -134,7 +134,7 @@ namespace bx
 		return strCmp<toLower>(_lhs, _rhs, _max);
 		return strCmp<toLower>(_lhs, _rhs, _max);
 	}
 	}
 
 
-	int32_t strnlen(const char* _str, int32_t _max)
+	int32_t strLen(const char* _str, int32_t _max)
 	{
 	{
 		if (NULL == _str)
 		if (NULL == _str)
 		{
 		{
@@ -146,13 +146,13 @@ namespace bx
 		return int32_t(ptr - _str);
 		return int32_t(ptr - _str);
 	}
 	}
 
 
-	int32_t strlncpy(char* _dst, int32_t _dstSize, const char* _src, int32_t _num)
+	int32_t strCopy(char* _dst, int32_t _dstSize, const char* _src, int32_t _num)
 	{
 	{
 		BX_CHECK(NULL != _dst, "_dst can't be NULL!");
 		BX_CHECK(NULL != _dst, "_dst can't be NULL!");
 		BX_CHECK(NULL != _src, "_src can't be NULL!");
 		BX_CHECK(NULL != _src, "_src can't be NULL!");
 		BX_CHECK(0 < _dstSize, "_dstSize can't be 0!");
 		BX_CHECK(0 < _dstSize, "_dstSize can't be 0!");
 
 
-		const int32_t len = strnlen(_src, _num);
+		const int32_t len = strLen(_src, _num);
 		const int32_t max = _dstSize-1;
 		const int32_t max = _dstSize-1;
 		const int32_t num = (len < max ? len : max);
 		const int32_t num = (len < max ? len : max);
 		memCopy(_dst, _src, num);
 		memCopy(_dst, _src, num);
@@ -161,20 +161,20 @@ namespace bx
 		return num;
 		return num;
 	}
 	}
 
 
-	int32_t strlncat(char* _dst, int32_t _dstSize, const char* _src, int32_t _num)
+	int32_t strCat(char* _dst, int32_t _dstSize, const char* _src, int32_t _num)
 	{
 	{
 		BX_CHECK(NULL != _dst, "_dst can't be NULL!");
 		BX_CHECK(NULL != _dst, "_dst can't be NULL!");
 		BX_CHECK(NULL != _src, "_src can't be NULL!");
 		BX_CHECK(NULL != _src, "_src can't be NULL!");
 		BX_CHECK(0 < _dstSize, "_dstSize can't be 0!");
 		BX_CHECK(0 < _dstSize, "_dstSize can't be 0!");
 
 
 		const int32_t max = _dstSize;
 		const int32_t max = _dstSize;
-		const int32_t len = strnlen(_dst, max);
-		return strlncpy(&_dst[len], max-len, _src, _num);
+		const int32_t len = strLen(_dst, max);
+		return strCopy(&_dst[len], max-len, _src, _num);
 	}
 	}
 
 
 	const char* strnchr(const char* _str, char _ch, int32_t _max)
 	const char* strnchr(const char* _str, char _ch, int32_t _max)
 	{
 	{
-		for (int32_t ii = 0, len = strnlen(_str, _max); ii < len; ++ii)
+		for (int32_t ii = 0, len = strLen(_str, _max); ii < len; ++ii)
 		{
 		{
 			if (_str[ii] == _ch)
 			if (_str[ii] == _ch)
 			{
 			{
@@ -187,7 +187,7 @@ namespace bx
 
 
 	const char* strnrchr(const char* _str, char _ch, int32_t _max)
 	const char* strnrchr(const char* _str, char _ch, int32_t _max)
 	{
 	{
-		for (int32_t ii = strnlen(_str, _max); 0 < ii; --ii)
+		for (int32_t ii = strLen(_str, _max); 0 < ii; --ii)
 		{
 		{
 			if (_str[ii] == _ch)
 			if (_str[ii] == _ch)
 			{
 			{
@@ -203,8 +203,8 @@ namespace bx
 	{
 	{
 		const char* ptr = _str;
 		const char* ptr = _str;
 
 
-		int32_t       stringLen = strnlen(_str,  _strMax);
-		const int32_t findLen   = strnlen(_find, _findMax);
+		int32_t       stringLen = strLen(_str,  _strMax);
+		const int32_t findLen   = strLen(_find, _findMax);
 
 
 		for (; stringLen >= findLen; ++ptr, --stringLen)
 		for (; stringLen >= findLen; ++ptr, --stringLen)
 		{
 		{
@@ -251,7 +251,7 @@ namespace bx
 
 
 	const char* strnl(const char* _str)
 	const char* strnl(const char* _str)
 	{
 	{
-		for (; '\0' != *_str; _str += strnlen(_str, 1024) )
+		for (; '\0' != *_str; _str += strLen(_str, 1024) )
 		{
 		{
 			const char* eol = strnstr(_str, "\r\n", 1024);
 			const char* eol = strnstr(_str, "\r\n", 1024);
 			if (NULL != eol)
 			if (NULL != eol)
@@ -271,7 +271,7 @@ namespace bx
 
 
 	const char* streol(const char* _str)
 	const char* streol(const char* _str)
 	{
 	{
-		for (; '\0' != *_str; _str += strnlen(_str, 1024) )
+		for (; '\0' != *_str; _str += strLen(_str, 1024) )
 		{
 		{
 			const char* eol = strnstr(_str, "\r\n", 1024);
 			const char* eol = strnstr(_str, "\r\n", 1024);
 			if (NULL != eol)
 			if (NULL != eol)
@@ -348,7 +348,7 @@ namespace bx
 
 
 	const char* findIdentifierMatch(const char* _str, const char* _word)
 	const char* findIdentifierMatch(const char* _str, const char* _word)
 	{
 	{
-		int32_t len = strnlen(_word);
+		int32_t len = strLen(_word);
 		const char* ptr = strnstr(_str, _word);
 		const char* ptr = strnstr(_str, _word);
 		for (; NULL != ptr; ptr = strnstr(ptr + len, _word) )
 		for (; NULL != ptr; ptr = strnstr(ptr + len, _word) )
 		{
 		{
@@ -418,7 +418,7 @@ namespace bx
 		static int32_t write(WriterI* _writer, const char* _str, int32_t _len, const Param& _param, Error* _err)
 		static int32_t write(WriterI* _writer, const char* _str, int32_t _len, const Param& _param, Error* _err)
 		{
 		{
 			int32_t size = 0;
 			int32_t size = 0;
-			int32_t len = (int32_t)strnlen(_str, _len);
+			int32_t len = (int32_t)strLen(_str, _len);
 			int32_t padding = _param.width > len ? _param.width - len : 0;
 			int32_t padding = _param.width > len ? _param.width - len : 0;
 			bool sign = _param.sign && len > 1 && _str[0] != '-';
 			bool sign = _param.sign && len > 1 && _str[0] != '-';
 			padding = padding > 0 ? padding - sign : 0;
 			padding = padding > 0 ? padding - sign : 0;
@@ -574,7 +574,7 @@ namespace bx
 
 
 	int32_t write(WriterI* _writer, const char* _format, va_list _argList, Error* _err)
 	int32_t write(WriterI* _writer, const char* _format, va_list _argList, Error* _err)
 	{
 	{
-		MemoryReader reader(_format, uint32_t(strnlen(_format) ) );
+		MemoryReader reader(_format, uint32_t(strLen(_format) ) );
 
 
 		int32_t size = 0;
 		int32_t size = 0;
 
 
@@ -916,14 +916,4 @@ namespace bx
 		snprintf(_out, _count, "%0.2f %c%c", size, "BkMGTPEZY"[idx], idx > 0 ? 'B' : '\0');
 		snprintf(_out, _count, "%0.2f %c%c", size, "BkMGTPEZY"[idx], idx > 0 ? 'B' : '\0');
 	}
 	}
 
 
-	int32_t strlcpy(char* _dst, const char* _src, int32_t _max)
-	{
-		return strlncpy(_dst, _max, _src);
-	}
-
-	int32_t strlcat(char* _dst, const char* _src, int32_t _max)
-	{
-		return strlncat(_dst, _max, _src);
-	}
-
 } // namespace bx
 } // namespace bx

+ 16 - 16
tests/string_test.cpp

@@ -23,48 +23,48 @@ TEST_CASE("chars", "")
 	}
 	}
 }
 }
 
 
-TEST_CASE("strnlen", "")
+TEST_CASE("strLen", "")
 {
 {
 	const char* test = "test";
 	const char* test = "test";
 
 
-	REQUIRE(0 == bx::strnlen(test, 0) );
-	REQUIRE(2 == bx::strnlen(test, 2) );
-	REQUIRE(4 == bx::strnlen(test, INT32_MAX) );
+	REQUIRE(0 == bx::strLen(test, 0) );
+	REQUIRE(2 == bx::strLen(test, 2) );
+	REQUIRE(4 == bx::strLen(test, INT32_MAX) );
 }
 }
 
 
-TEST_CASE("strlncpy", "")
+TEST_CASE("strCopy", "")
 {
 {
 	char dst[128];
 	char dst[128];
 	size_t num;
 	size_t num;
 
 
-	num = bx::strlncpy(dst, 1, "blah");
+	num = bx::strCopy(dst, 1, "blah");
 	REQUIRE(num == 0);
 	REQUIRE(num == 0);
 
 
-	num = bx::strlncpy(dst, 3, "blah", 3);
+	num = bx::strCopy(dst, 3, "blah", 3);
 	REQUIRE(0 == bx::strncmp(dst, "bl") );
 	REQUIRE(0 == bx::strncmp(dst, "bl") );
 	REQUIRE(num == 2);
 	REQUIRE(num == 2);
 
 
-	num = bx::strlncpy(dst, sizeof(dst), "blah", 3);
+	num = bx::strCopy(dst, sizeof(dst), "blah", 3);
 	REQUIRE(0 == bx::strncmp(dst, "bla") );
 	REQUIRE(0 == bx::strncmp(dst, "bla") );
 	REQUIRE(num == 3);
 	REQUIRE(num == 3);
 
 
-	num = bx::strlncpy(dst, sizeof(dst), "blah");
+	num = bx::strCopy(dst, sizeof(dst), "blah");
 	REQUIRE(0 == bx::strncmp(dst, "blah") );
 	REQUIRE(0 == bx::strncmp(dst, "blah") );
 	REQUIRE(num == 4);
 	REQUIRE(num == 4);
 }
 }
 
 
-TEST_CASE("strlncat", "")
+TEST_CASE("strCat", "")
 {
 {
 	char dst[128] = { '\0' };
 	char dst[128] = { '\0' };
 
 
-	REQUIRE(0 == bx::strlncat(dst, 1, "cat") );
+	REQUIRE(0 == bx::strCat(dst, 1, "cat") );
 
 
-	REQUIRE(4 == bx::strlncpy(dst, 5, "copy") );
-	REQUIRE(3 == bx::strlncat(dst, 8, "cat") );
+	REQUIRE(4 == bx::strCopy(dst, 5, "copy") );
+	REQUIRE(3 == bx::strCat(dst, 8, "cat") );
 	REQUIRE(0 == bx::strncmp(dst, "copycat") );
 	REQUIRE(0 == bx::strncmp(dst, "copycat") );
 
 
-	REQUIRE(1 == bx::strlncat(dst, BX_COUNTOF(dst), "------", 1) );
-	REQUIRE(3 == bx::strlncat(dst, BX_COUNTOF(dst), "cat") );
+	REQUIRE(1 == bx::strCat(dst, BX_COUNTOF(dst), "------", 1) );
+	REQUIRE(3 == bx::strCat(dst, BX_COUNTOF(dst), "cat") );
 	REQUIRE(0 == bx::strncmp(dst, "copycat-cat") );
 	REQUIRE(0 == bx::strncmp(dst, "copycat-cat") );
 }
 }
 
 
@@ -134,7 +134,7 @@ static bool testToString(Ty _value, const char* _expected)
 {
 {
 	char tmp[1024];
 	char tmp[1024];
 	int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value);
 	int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value);
-	int32_t len = (int32_t)bx::strnlen(_expected);
+	int32_t len = (int32_t)bx::strLen(_expected);
 	if (0 == bx::strncmp(tmp, _expected)
 	if (0 == bx::strncmp(tmp, _expected)
 	&&  num == len)
 	&&  num == len)
 	{
 	{

+ 1 - 1
tests/vsnprintf_test.cpp

@@ -25,7 +25,7 @@ TEST_CASE("vsnprintf truncated", "Truncated output buffer.")
 
 
 static bool test(const char* _expected, const char* _format, ...)
 static bool test(const char* _expected, const char* _format, ...)
 {
 {
-	int32_t max = (int32_t)bx::strnlen(_expected) + 1;
+	int32_t max = (int32_t)bx::strLen(_expected) + 1;
 	char* temp = (char*)alloca(max);
 	char* temp = (char*)alloca(max);
 
 
 	va_list argList;
 	va_list argList;