mirror of
https://github.com/bkaradzic/bx.git
synced 2026-02-17 20:52:37 +01:00
Fixed vsnprintf.
This commit is contained in:
@@ -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<typename Ty>
|
||||
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()
|
||||
|
||||
@@ -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<typename Ty>
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
) );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user