Fix DqnFixedString len bug

This commit is contained in:
Doyle T 2018-07-07 01:10:43 +10:00
parent 6fc26c7bb8
commit 39d75ae5bf
2 changed files with 21 additions and 14 deletions

View File

@ -34,7 +34,7 @@ void DqnFixedString_Test()
DqnFixedString<512> str = DQN_SLICE("hello world"); DqnFixedString<512> str = DQN_SLICE("hello world");
DQN_ASSERT(str.Sprintf("hello %s", "sailor")); DQN_ASSERT(str.Sprintf("hello %s", "sailor"));
str += DQN_SLICE(".end"); str += DQN_SLICE(".end");
DQN_ASSERT(DqnStr_Cmp(str.str, "hello sailor.end") == 0); DQN_ASSERTM(DqnStr_Cmp(str.str, "hello sailor.end") == 0, "Result: %s", str.str);
} }
{ {
@ -65,4 +65,13 @@ void DqnFixedString_Test()
Log(Status::Ok, "Operator +"); Log(Status::Ok, "Operator +");
} }
{
DqnFixedString<512> str = "localhost";
str.SprintfAppend(":%d", 16832);
str += "/json_rpc";
DQN_ASSERT(str.len == 24 && DqnStr_Cmp("localhost:16832/json_rpc", str.str) == 0);
Log(Status::Ok, "Copy constructor, sprintf, operator +=");
}
} }

24
dqn.h
View File

@ -1856,10 +1856,10 @@ struct DqnFixedString
DqnFixedString(DqnSlice<char> const &other) { this->len = DqnFixedString__Append(this->str, MAX, other.data, other.len); } DqnFixedString(DqnSlice<char> const &other) { this->len = DqnFixedString__Append(this->str, MAX, other.data, other.len); }
DqnFixedString(DqnFixedString const &other) { if (this != &other) this->len = DqnFixedString__Append(this->str, MAX, other.str, other.len); } DqnFixedString(DqnFixedString const &other) { if (this != &other) this->len = DqnFixedString__Append(this->str, MAX, other.str, other.len); }
DqnFixedString &operator+=(char const *other) { this->len = DqnFixedString__Append(this->str + this->len, MAX - this->len, other); return *this; } DqnFixedString &operator+=(char const *other) { this->len += DqnFixedString__Append(this->str + this->len, MAX - this->len, other); return *this; }
DqnFixedString &operator+=(DqnSlice<char const> const &other) { this->len = DqnFixedString__Append(this->str + this->len, MAX - this->len, other.data, other.len); return *this; } DqnFixedString &operator+=(DqnSlice<char const> const &other) { this->len += DqnFixedString__Append(this->str + this->len, MAX - this->len, other.data, other.len); return *this; }
DqnFixedString &operator+=(DqnSlice<char> const &other) { this->len = DqnFixedString__Append(this->str + this->len, MAX - this->len, other.data, other.len); return *this; } DqnFixedString &operator+=(DqnSlice<char> const &other) { this->len += DqnFixedString__Append(this->str + this->len, MAX - this->len, other.data, other.len); return *this; }
DqnFixedString &operator+=(DqnFixedString const &other) { this->len = DqnFixedString__Append(this->str + this->len, MAX - this->len, other.str, other.len); return *this; } DqnFixedString &operator+=(DqnFixedString const &other) { this->len += DqnFixedString__Append(this->str + this->len, MAX - this->len, other.str, other.len); return *this; }
DqnFixedString operator+ (char const *other) { DqnFixedString result = *this; result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, other); return result; } DqnFixedString operator+ (char const *other) { DqnFixedString result = *this; result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, other); return result; }
DqnFixedString operator+ (DqnSlice<char const> const &other) { DqnFixedString result = *this; result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, other.data, other.len); return result; } DqnFixedString operator+ (DqnSlice<char const> const &other) { DqnFixedString result = *this; result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, other.data, other.len); return result; }
@ -1885,7 +1885,7 @@ DqnFixedString__VSprintf(DqnFixedString<MAX> *me, char const *fmt, va_list argLi
char *bufStart = me->str + bufOffset; char *bufStart = me->str + bufOffset;
i32 const remainingSpace = static_cast<i32>((me->str + MAX) - bufStart); i32 const remainingSpace = static_cast<i32>((me->str + MAX) - bufStart);
int result = Dqn_vsnprintf(bufStart, remainingSpace, fmt, argList); int result = Dqn_vsnprintf(bufStart, remainingSpace, fmt, argList);
me->len = result; me->len = bufOffset + result;
return result; return result;
} }
@ -6215,7 +6215,6 @@ wchar_t *DqnString::ToWChar(DqnMemAPI *api) const
// return: The number of bytes written to dest // return: The number of bytes written to dest
FILE_SCOPE int DqnFixedString__Append(char *dest, int destSize, char const *src, int len) FILE_SCOPE int DqnFixedString__Append(char *dest, int destSize, char const *src, int len)
{ {
i32 realLen = 0;
if (len <= -1) if (len <= -1)
{ {
char *ptr = dest; char *ptr = dest;
@ -6229,17 +6228,16 @@ FILE_SCOPE int DqnFixedString__Append(char *dest, int destSize, char const *src,
DQN_ASSERT(!src[0]); DQN_ASSERT(!src[0]);
} }
realLen = static_cast<i32>(ptr - dest); len = static_cast<i32>(ptr - dest);
} }
else else
{ {
realLen = DQN_MIN(len, destSize - 1); DqnMem_Copy(dest, src, len);
DqnMem_Copy(dest, src, realLen);
} }
DQN_ASSERT(realLen <= destSize && realLen >= 0); DQN_ASSERT(len < destSize && len >= 0);
dest[realLen] = 0; dest[len] = 0;
return realLen; return len;
} }
// #Dqn // #Dqn
@ -6364,7 +6362,7 @@ TryNext:
{ {
if (locate[-1] != '"' || locate[findPropertyLen] != '"') if (locate[-1] != '"' || locate[findPropertyLen] != '"')
{ {
bufLen -= ((locate - buf) + findPropertyLen); bufLen -= static_cast<i32>(((locate - buf) + findPropertyLen));
buf = locate + findPropertyLen; buf = locate + findPropertyLen;
goto TryNext; goto TryNext;
} }