diff --git a/include/bx/inline/string.inl b/include/bx/inline/string.inl index 12273bc..0683f37 100644 --- a/include/bx/inline/string.inl +++ b/include/bx/inline/string.inl @@ -58,9 +58,9 @@ namespace bx clear(); } - inline StringView::StringView(const StringView& _rhs) + inline StringView::StringView(const StringView& _rhs, int32_t _start, int32_t _len) { - set(_rhs.m_ptr, _rhs.m_len); + set(_rhs, _start, _len); } inline StringView& StringView::operator=(const char* _rhs) @@ -71,7 +71,7 @@ namespace bx inline StringView& StringView::operator=(const StringView& _rhs) { - set(_rhs.m_ptr, _rhs.m_len); + set(_rhs); return *this; } @@ -135,12 +135,14 @@ namespace bx template inline void StringView::set(const Ty& _container) { - set(_container.data(), _container.length() ); + set(_container.data(), int32_t(_container.length() ) ); } - inline void StringView::set(const StringView& _str) + inline void StringView::set(const StringView& _str, int32_t _start, int32_t _len) { - set(_str.m_ptr, _str.m_len); + const int32_t start = min(_start, _str.m_len); + const int32_t len = clamp(_str.m_len - start, 0, min(_len, _str.m_len) ); + set(_str.m_ptr + start, len); } inline void StringView::clear() diff --git a/include/bx/string.h b/include/bx/string.h index 9830b76..132afa1 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -28,7 +28,7 @@ namespace bx StringView(); /// - StringView(const StringView& _rhs); + StringView(const StringView& _rhs, int32_t _start = 0, int32_t _len = INT32_MAX); /// StringView& operator=(const char* _rhs); @@ -68,7 +68,7 @@ namespace bx void set(const char* _ptr, const char* _term); /// - void set(const StringView& _str); + void set(const StringView& _str, int32_t _start = 0, int32_t _len = INT32_MAX); /// template diff --git a/src/string.cpp b/src/string.cpp index f6f7cf9..649323a 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -690,7 +690,7 @@ namespace bx Param() : width(0) , base(10) - , prec(6) + , prec(INT32_MAX) , fill(' ') , bits(0) , left(false) @@ -701,8 +701,8 @@ namespace bx } int32_t width; - uint32_t base; - uint32_t prec; + int32_t base; + int32_t prec; char fill; uint8_t bits; bool left; @@ -760,7 +760,7 @@ namespace bx static int32_t write(WriterI* _writer, const char* _str, const Param& _param, Error* _err) { - return write(_writer, _str, INT32_MAX, _param, _err); + return write(_writer, _str, _param.prec, _param, _err); } static int32_t write(WriterI* _writer, int32_t _i, const Param& _param, Error* _err) @@ -833,10 +833,11 @@ namespace bx const char* dot = strFind(str, INT32_MAX, '.'); if (NULL != dot) { + const int32_t prec = INT32_MAX == _param.prec ? 6 : _param.prec; const int32_t precLen = int32_t( dot - + uint32_min(_param.prec + _param.spec, 1) - + _param.prec + + uint32_min(prec + _param.spec, 1) + + prec - str ); if (precLen > len) diff --git a/tests/vsnprintf_test.cpp b/tests/vsnprintf_test.cpp index ad73887..035c17b 100644 --- a/tests/vsnprintf_test.cpp +++ b/tests/vsnprintf_test.cpp @@ -164,4 +164,12 @@ TEST_CASE("vsnprintf", "") REQUIRE(test("hello ", "%-20s", "hello") ); REQUIRE(test("hello, world!", "%s, %s!", "hello", "world") ); + + bx::StringView str("0hello1world2"); + bx::StringView hello(str, 1, 5); + bx::StringView world(str, 7, 5); + REQUIRE(test("hello, world!", "%.*s, %.*s!" + , hello.getLength(), hello.getPtr() + , world.getLength(), world.getPtr() + ) ); }