From c338725229d354d6cbab5aefddd0896ace1d1b9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sun, 1 Oct 2017 18:03:25 -0700 Subject: [PATCH] Switching code to use StringView. --- include/bx/string.h | 30 ++++------- src/filepath.cpp | 4 +- src/string.cpp | 112 +++++++++++++++++++++++++++--------------- tests/string_test.cpp | 6 ++- 4 files changed, 87 insertions(+), 65 deletions(-) diff --git a/include/bx/string.h b/include/bx/string.h index d6af333..5e78b44 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -169,38 +169,26 @@ namespace bx bool toBool(const char* _str); /// String compare. - int32_t strCmp(const char* _lhs, const char* _rhs, int32_t _max = INT32_MAX); - - /// String compare. - int32_t strCmp(const char* _lhs, const StringView& _rhs); + int32_t strCmp(const StringView& _lhs, const StringView& _rhs, int32_t _max = INT32_MAX); /// Case insensitive string compare. - int32_t strCmpI(const char* _lhs, const char* _rhs, int32_t _max = INT32_MAX); - - /// Case insensitive string compare. - int32_t strCmpI(const char* _lhs, const StringView& _rhs); + int32_t strCmpI(const StringView& _lhs, const StringView& _rhs, int32_t _max = INT32_MAX); // Compare as strings holding indices/version numbers. - int32_t strCmpV(const char* _lhs, const char* _rhs, int32_t _max = INT32_MAX); - - // Compare as strings holding indices/version numbers. - int32_t strCmpV(const char* _lhs, const StringView& _rhs); + int32_t strCmpV(const StringView& _lhs, const StringView& _rhs, int32_t _max = INT32_MAX); /// Get string length. int32_t strLen(const char* _str, int32_t _max = INT32_MAX); + /// Get string length. + int32_t strLen(const StringView& _str, int32_t _max = INT32_MAX); + /// Copy _num characters from string _src to _dst buffer of maximum _dstSize capacity /// including zero terminator. Copy will be terminated with '\0'. - int32_t strCopy(char* _dst, int32_t _dstSize, const char* _src, int32_t _num = INT32_MAX); - - /// - int32_t strCopy(char* _dst, int32_t _dstSize, const StringView& _str); + int32_t strCopy(char* _dst, int32_t _dstSize, const StringView& _str, int32_t _num = INT32_MAX); /// Concatinate string. - int32_t strCat(char* _dst, int32_t _dstSize, const char* _src, int32_t _num = INT32_MAX); - - /// - int32_t strCat(char* _dst, int32_t _dstSize, const StringView& _str); + int32_t strCat(char* _dst, int32_t _dstSize, const StringView& _str, int32_t _num = INT32_MAX); /// Find character in string. Limit search to _max characters. const char* strFind(const char* _str, int32_t _max, char _ch); @@ -282,7 +270,7 @@ namespace bx Ty replaceAll(const Ty& _str, const char* _from, const char* _to); /// Convert size in bytes to human readable string kibi units. - int32_t prettify(char* _out, int32_t _count, uint64_t _size, Units::Enum _units = Units::Kibi); + int32_t prettify(char* _out, int32_t _count, uint64_t _value, Units::Enum _units = Units::Kibi); /// int32_t toString(char* _out, int32_t _max, double _value); diff --git a/src/filepath.cpp b/src/filepath.cpp index e0159a3..b46619f 100644 --- a/src/filepath.cpp +++ b/src/filepath.cpp @@ -264,7 +264,7 @@ namespace bx const StringView fileName = getFileName(); if (!fileName.isEmpty() ) { - const char* ext = strFind(fileName.getPtr(), '.', fileName.getLength() ); + const char* ext = strFind(fileName, '.'); if (ext != NULL) { return StringView(fileName.getPtr(), ext); @@ -279,7 +279,7 @@ namespace bx const StringView fileName = getFileName(); if (!fileName.isEmpty() ) { - const char* ext = strFind(fileName.getPtr(), '.', fileName.getLength() ); + const char* ext = strFind(fileName, '.'); return StringView(ext); } diff --git a/src/string.cpp b/src/string.cpp index 58829bc..01ed8ee 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -63,7 +63,7 @@ namespace bx typedef bool (*CharTestFn)(char _ch); template - static bool isCharTest(const StringView& _str) + inline bool isCharTest(const StringView& _str) { bool result = true; @@ -165,11 +165,19 @@ namespace bx } template - int32_t strCmp(const char* _lhs, const char* _rhs, int32_t _max) + inline int32_t strCmp(const char* _lhs, int32_t _lhsMax, const char* _rhs, int32_t _rhsMax) { + int32_t max = _lhsMax; + + if (_lhsMax != _rhsMax) + { + max = _lhsMax < _rhsMax ? _lhsMax : _rhsMax; + return fn(_lhs[max]) - fn(_rhs[max]); + } + for ( - ; 0 < _max && fn(*_lhs) == fn(*_rhs) - ; ++_lhs, ++_rhs, --_max + ; 0 < max && fn(*_lhs) == fn(*_rhs) + ; ++_lhs, ++_rhs, --max ) { if (*_lhs == '\0' @@ -179,38 +187,52 @@ namespace bx } } - return 0 == _max ? 0 : fn(*_lhs) - fn(*_rhs); + return 0 == max ? 0 : fn(*_lhs) - fn(*_rhs); } - int32_t strCmp(const char* _lhs, const char* _rhs, int32_t _max) + template + inline Ty min(Ty _a, Ty _b) { - return strCmp(_lhs, _rhs, _max); + return _a > _b ? _b : _a; } - int32_t strCmp(const char* _lhs, const StringView& _rhs) + int32_t strCmp(const StringView& _lhs, const StringView& _rhs, int32_t _max) { - return strCmp(_lhs, _rhs.getPtr(), _rhs.getLength() ); + return strCmp( + _lhs.getPtr() + , min(_lhs.getLength(), _max) + , _rhs.getPtr() + , min(_rhs.getLength(), _max) + ); } - int32_t strCmpI(const char* _lhs, const char* _rhs, int32_t _max) + int32_t strCmpI(const StringView& _lhs, const StringView& _rhs, int32_t _max) { - return strCmp(_lhs, _rhs, _max); + return strCmp( + _lhs.getPtr() + , min(_lhs.getLength(), _max) + , _rhs.getPtr() + , min(_rhs.getLength(), _max) + ); } - int32_t strCmpI(const char* _lhs, const StringView& _rhs) + inline int32_t strCmpV(const char* _lhs, int32_t _lhsMax, const char* _rhs, int32_t _rhsMax) { - return strCmpI(_lhs, _rhs.getPtr(), _rhs.getLength() ); - } + int32_t max = _lhsMax; + + if (_lhsMax != _rhsMax) + { + max = _lhsMax < _rhsMax ? _lhsMax : _rhsMax; + return _lhs[max] - _rhs[max]; + } - int32_t strCmpV(const char* _lhs, const char* _rhs, int32_t _max) - { int32_t ii = 0; int32_t idx = 0; bool zero = true; for ( - ; 0 < _max && _lhs[ii] == _rhs[ii] - ; ++ii, --_max + ; 0 < max && _lhs[ii] == _rhs[ii] + ; ++ii, --max ) { const uint8_t ch = _lhs[ii]; @@ -231,7 +253,7 @@ namespace bx } } - if (0 == _max) + if (0 == max) { return 0; } @@ -241,8 +263,8 @@ namespace bx { int32_t jj = 0; for (jj = ii - ; 0 < _max && isNumeric(_lhs[jj]) - ; ++jj, --_max + ; 0 < max && isNumeric(_lhs[jj]) + ; ++jj, --max ) { if (!isNumeric(_rhs[jj]) ) @@ -263,12 +285,17 @@ namespace bx return (_lhs[ii] - '0') - (_rhs[ii] - '0'); } - return 0 == _max ? 0 : _lhs[ii] - _rhs[ii]; + return 0 == max ? 0 : _lhs[ii] - _rhs[ii]; } - int32_t strCmpV(const char* _lhs, const StringView& _rhs) + int32_t strCmpV(const StringView& _lhs, const StringView& _rhs, int32_t _max) { - return strCmpV(_lhs, _rhs.getPtr(), _rhs.getLength() ); + return strCmpV( + _lhs.getPtr() + , min(_lhs.getLength(), _max) + , _rhs.getPtr() + , min(_rhs.getLength(), _max) + ); } int32_t strLen(const char* _str, int32_t _max) @@ -283,7 +310,12 @@ namespace bx return int32_t(ptr - _str); } - int32_t strCopy(char* _dst, int32_t _dstSize, const char* _src, int32_t _num) + int32_t strLen(const StringView& _str, int32_t _max) + { + return strLen(_str.getPtr(), min(_str.getLength(), _max) ); + } + + inline 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 != _src, "_src can't be NULL!"); @@ -298,12 +330,12 @@ namespace bx return num; } - int32_t strCopy(char* _dst, int32_t _dstSize, const StringView& _str) + int32_t strCopy(char* _dst, int32_t _dstSize, const StringView& _str, int32_t _num) { - return strCopy(_dst, _dstSize, _str.getPtr(), _str.getLength() ); + return strCopy(_dst, _dstSize, _str.getPtr(), min(_str.getLength(), _num) ); } - int32_t strCat(char* _dst, int32_t _dstSize, const char* _src, int32_t _num) + inline 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 != _src, "_src can't be NULL!"); @@ -314,9 +346,9 @@ namespace bx return strCopy(&_dst[len], max-len, _src, _num); } - int32_t strCat(char* _dst, int32_t _dstSize, const StringView& _str) + int32_t strCat(char* _dst, int32_t _dstSize, const StringView& _str, int32_t _num) { - return strCat(_dst, _dstSize, _str.getPtr(), _str.getLength() ); + return strCat(_dst, _dstSize, _str.getPtr(), min(_str.getLength(), _num) ); } const char* strFind(const char* _str, int32_t _max, char _ch) @@ -356,7 +388,7 @@ namespace bx } template - static const char* strFind(const char* _str, int32_t _strMax, const char* _find, int32_t _findMax) + inline const char* strFind(const char* _str, int32_t _strMax, const char* _find, int32_t _findMax) { const char* ptr = _str; @@ -1065,33 +1097,33 @@ namespace bx static const char s_units[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' }; template - static int32_t prettify(char* _out, int32_t _count, uint64_t _size) + inline int32_t prettify(char* _out, int32_t _count, uint64_t _value) { uint8_t idx = 0; - double size = double(_size); - while (_size != (_size&0x7ff) + double value = double(_value); + while (_value != (_value&0x7ff) && idx < BX_COUNTOF(s_units) ) { - _size /= Kilo; - size *= 1.0/double(Kilo); + _value /= Kilo; + value *= 1.0/double(Kilo); ++idx; } - return snprintf(_out, _count, "%0.2f %c%c%c", size + return snprintf(_out, _count, "%0.2f %c%c%c", value , s_units[idx] , idx > 0 ? KiloCh0 : '\0' , KiloCh1 ); } - int32_t prettify(char* _out, int32_t _count, uint64_t _size, Units::Enum _units) + int32_t prettify(char* _out, int32_t _count, uint64_t _value, Units::Enum _units) { if (Units::Kilo == _units) { - return prettify<1000, 'B', '\0'>(_out, _count, _size); + return prettify<1000, 'B', '\0'>(_out, _count, _value); } - return prettify<1024, 'i', 'B'>(_out, _count, _size); + return prettify<1024, 'i', 'B'>(_out, _count, _value); } } // namespace bx diff --git a/tests/string_test.cpp b/tests/string_test.cpp index 9f055d1..c0ba4ab 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -49,6 +49,7 @@ TEST_CASE("strCopy", "") REQUIRE(num == 3); num = bx::strCopy(dst, sizeof(dst), "blah"); + REQUIRE(0 == bx::strCmp(dst, "bl", 2) ); REQUIRE(0 == bx::strCmp(dst, "blah") ); REQUIRE(num == 4); } @@ -62,6 +63,7 @@ TEST_CASE("strCat", "") REQUIRE(4 == bx::strCopy(dst, 5, "copy") ); REQUIRE(3 == bx::strCat(dst, 8, "cat") ); REQUIRE(0 == bx::strCmp(dst, "copycat") ); + REQUIRE(0 == bx::strCmp(dst, "copy", 4) ); REQUIRE(1 == bx::strCat(dst, BX_COUNTOF(dst), "------", 1) ); REQUIRE(3 == bx::strCat(dst, BX_COUNTOF(dst), "cat") ); @@ -316,8 +318,8 @@ TEST_CASE("fromString int32_t", "") REQUIRE(testFromString(1389, "1389") ); REQUIRE(testFromString(1389, " 1389") ); REQUIRE(testFromString(1389, "+1389") ); - REQUIRE(testFromString(-1389, "-1389") ); - REQUIRE(testFromString(-1389, " -1389") ); + REQUIRE(testFromString(-1389, "-1389") ); + REQUIRE(testFromString(-1389, " -1389") ); REQUIRE(testFromString(555333, "555333") ); REQUIRE(testFromString(-21, "-021") ); }