diff --git a/include/bx/inline/string.inl b/include/bx/inline/string.inl index 82d428b..4ee2400 100644 --- a/include/bx/inline/string.inl +++ b/include/bx/inline/string.inl @@ -147,26 +147,20 @@ namespace bx inline StringT::StringT(const StringT& _rhs) : StringView() { - set(_rhs.m_ptr, _rhs.m_len); + set(_rhs); } template inline StringT& StringT::operator=(const StringT& _rhs) { - set(_rhs.m_ptr, _rhs.m_len); + set(_rhs); return *this; } - template - inline StringT::StringT(const char* _ptr, int32_t _len) - { - set(_ptr, _len); - } - template inline StringT::StringT(const StringView& _rhs) { - set(_rhs.getPtr(), _rhs.getLength() ); + set(_rhs); } template @@ -176,22 +170,22 @@ namespace bx } template - inline void StringT::set(const char* _ptr, int32_t _len) + inline void StringT::set(const StringView& _str) { clear(); - append(_ptr, _len); + append(_str); } template - inline void StringT::append(const char* _ptr, int32_t _len) + inline void StringT::append(const StringView& _str) { - if (0 != _len) + if (0 != _str.getLength() ) { int32_t old = m_len; - int32_t len = m_len + strLen(_ptr, _len); + int32_t len = m_len + strLen(_str); char* ptr = (char*)BX_REALLOC(*AllocatorT, 0 != m_len ? const_cast(m_ptr) : NULL, len+1); m_len = len; - strCopy(ptr + old, len-old+1, _ptr, _len); + strCopy(ptr + old, len-old+1, _str); *const_cast(&m_ptr) = ptr; } diff --git a/include/bx/string.h b/include/bx/string.h index d006d3c..331bda8 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -84,9 +84,6 @@ namespace bx /// StringT& operator=(const StringT& _rhs); - /// - StringT(const char* _ptr, int32_t _len = INT32_MAX); - /// StringT(const StringView& _rhs); @@ -94,10 +91,10 @@ namespace bx ~StringT(); /// - void set(const char* _ptr, int32_t _len = INT32_MAX); + void set(const StringView& _str); /// - void append(const char* _ptr, int32_t _len = INT32_MAX); + void append(const StringView& _str); /// void clear(); @@ -163,9 +160,6 @@ namespace bx /// void toUpper(char* _inOutStr, int32_t _max = INT32_MAX); - /// - bool toBool(const char* _str); - /// String compare. int32_t strCmp(const StringView& _lhs, const StringView& _rhs, int32_t _max = INT32_MAX); @@ -267,6 +261,9 @@ namespace bx /// Convert size in bytes to human readable string kibi units. int32_t prettify(char* _out, int32_t _count, uint64_t _value, Units::Enum _units = Units::Kibi); + /// + int32_t toString(char* _out, int32_t _max, bool _value); + /// int32_t toString(char* _out, int32_t _max, double _value); @@ -282,6 +279,9 @@ namespace bx /// int32_t toString(char* _out, int32_t _max, uint64_t _value, uint32_t _base = 10); + /// + bool fromString(bool* _out, const StringView& _str); + /// bool fromString(float* _out, const StringView& _str); diff --git a/src/dtoa.cpp b/src/dtoa.cpp index 7a2ab2f..6945210 100644 --- a/src/dtoa.cpp +++ b/src/dtoa.cpp @@ -1043,6 +1043,20 @@ namespace bx return hd.d; } + int32_t toString(char* _out, int32_t _max, bool _value) + { + StringView str(_value ? "true" : "false"); + strCopy(_out, _max, str); + return str.getLength(); + } + + bool fromString(bool* _out, const StringView& _str) + { + char ch = toLower(_str.getPtr()[0]); + *_out = ch == 't' || ch == '1'; + return 0 != _str.getLength(); + } + bool fromString(float* _out, const StringView& _str) { double dbl; diff --git a/src/string.cpp b/src/string.cpp index a225413..5fe2725 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -153,12 +153,6 @@ namespace bx toUpperUnsafe(_inOutStr, len); } - bool toBool(const char* _str) - { - char ch = toLower(_str[0]); - return ch == 't' || ch == '1'; - } - typedef char (*CharFn)(char _ch); inline char toNoop(char _ch) diff --git a/tests/string_test.cpp b/tests/string_test.cpp index 3bbbb53..d848f3d 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -352,7 +352,7 @@ TEST_CASE("StringView", "") st.append("test"); REQUIRE(8 == st.getLength() ); - st.append("test", 2); + st.append(bx::StringView("test", 2) ); REQUIRE(10 == st.getLength() ); REQUIRE(0 == bx::strCmp(st.getPtr(), "testtestte") );