Skip to content

Commit 806c6fd

Browse files
committed
Added tests for wide character construction for string class
1 parent bfea516 commit 806c6fd

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

engine/utils/String.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,8 @@ String::String(const wchar_t* string) : memory()
123123
{
124124
size_t size = std::wcslen(string) + 1;
125125
char* convertedStr = static_cast<char*>(alloca(size));
126-
memset(convertedStr, 0, size + 1);
127126
std::wcstombs(convertedStr, string, size);
128-
convertedStr[size] = '\0';
127+
convertedStr[size - 1] = '\0';
129128
Assign(convertedStr);
130129
}
131130

@@ -232,9 +231,8 @@ String& String::operator+=(const wchar_t* rhs)
232231
{
233232
size_t size = std::wcslen(rhs) + 1;
234233
char* convertedStr = static_cast<char*>(alloca(size));
235-
memset(convertedStr, 0, size + 1);
236234
std::wcstombs(convertedStr, rhs, size);
237-
convertedStr[size] = '\0';
235+
convertedStr[size - 1] = '\0';
238236

239237
Append(convertedStr);
240238
return *this;
@@ -723,6 +721,18 @@ String operator+(const char* lhs, const String& rhs)
723721
return {cstr};
724722
}
725723

724+
String operator+(const wchar_t* lhs, const String& rhs)
725+
{
726+
size_t lhsLength = std::wcslen(lhs);
727+
size_t rhsLength = rhs.Size();
728+
729+
char* cstr = static_cast<char*>(alloca(lhsLength + rhsLength + 1));
730+
std::wcstombs(cstr, lhs, lhsLength);
731+
strcpy(cstr + lhsLength, rhs.Str());
732+
733+
return {cstr};
734+
}
735+
726736
String operator+(char lhs, const String& rhs)
727737
{
728738
size_t rhsLength = rhs.Size();

engine/utils/String.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,14 @@ bool operator!=(const char* lhs, const String& rhs);
656656
*/
657657
String operator+(const char* lhs, const String& rhs);
658658

659+
/**
660+
* Reversed wide c-string addition operator overload for prepending strings
661+
* @param lhs - the string to append to
662+
* @param rhs - the string to append
663+
* @return a new string object with the appended value
664+
*/
665+
String operator+(const wchar_t* lhs, const String& rhs);
666+
659667
/**
660668
* Reversed character addition operator overload for prepending characters
661669
* @param lhs - the character to append to

tests/src/utils/test_String.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ UTEST(test_String, DefaultConstruct)
3737
ASSERT_EQ(5, s.Size());
3838
ASSERT_STREQ("hello", s.Str());
3939
ASSERT_EQ(0, std::strcmp(s.Str(), "hello"));
40+
41+
// Assignment of wide c-strings via assignment operator should apply
42+
s = L"hello";
43+
ASSERT_FALSE(s.IsEmpty());
44+
ASSERT_EQ(5, s.Size());
45+
ASSERT_STREQ("hello", s.Str());
46+
ASSERT_EQ(0, std::strcmp(s.Str(), "hello"));
4047
}
4148

4249
UTEST(test_String, CopyConstruct)
@@ -148,6 +155,16 @@ UTEST(test_String, Append)
148155
ASSERT_STREQ("goodbyehellogoodbye", s2.Str());
149156
ASSERT_EQ(19, s2.Size());
150157

158+
// The string should correctly append wide c-strings
159+
Siege::String s6;
160+
s6 += L"hello";
161+
ASSERT_STREQ("hello", s6.Str());
162+
Siege::String s7 = L"goodbye" + s6;
163+
ASSERT_STREQ("goodbyehello", s7.Str());
164+
s7.Append(L"goodbye");
165+
ASSERT_STREQ("goodbyehellogoodbye", s7.Str());
166+
ASSERT_EQ(19, s7.Size());
167+
151168
// The string should correctly append characters
152169
Siege::String s5;
153170
s5 += 'h';

0 commit comments

Comments
 (0)