Skip to content

Commit 4db9965

Browse files
committed
Add string constructor to StringPtr
1 parent ff1bcf1 commit 4db9965

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

include/scratchcpp/stringptr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#pragma once
44

55
#include "global.h"
6+
#include "string_functions.h"
67

78
namespace libscratchcpp
89
{
@@ -14,6 +15,7 @@ extern "C"
1415
{
1516
// NOTE: Constructors and destructors only work in C++ code and are not supposed to be used in LLVM IR
1617
StringPtr() = default;
18+
StringPtr(const std::string &str) { string_assign_cstring(this, str.c_str()); }
1719
StringPtr(const StringPtr &) = delete;
1820

1921
~StringPtr()

src/scratch/value_functions.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,10 @@ extern "C"
454454
const StringPtr *value_boolToStringPtr(bool v)
455455
{
456456
if (v) {
457-
static const std::u16string str = utf8::utf8to16(std::string("true"));
458-
static const StringPtr ret = { const_cast<char16_t *>(str.c_str()) };
457+
static const StringPtr ret("true");
459458
return &ret;
460459
} else {
461-
static const std::u16string str = utf8::utf8to16(std::string("false"));
462-
static const StringPtr ret = { const_cast<char16_t *>(str.c_str()) };
460+
static const StringPtr ret("false");
463461
return &ret;
464462
}
465463
}

test/scratch_classes/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ target_link_libraries(
121121

122122
gtest_discover_tests(value_test)
123123

124+
# stringptr_test
125+
add_executable(
126+
stringptr_test
127+
stringptr_test.cpp
128+
)
129+
130+
target_link_libraries(
131+
stringptr_test
132+
GTest::gtest_main
133+
scratchcpp
134+
)
135+
136+
gtest_discover_tests(stringptr_test)
137+
124138
# string_functions_test
125139
add_executable(
126140
string_functions_test

test/scratch_classes/string_functions_test.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ using namespace libscratchcpp;
88
TEST(StringFunctionsTest, AssignAlloc)
99
{
1010
StringPtr str1;
11-
ASSERT_FALSE(str1.data);
12-
ASSERT_EQ(str1.size, 0);
13-
ASSERT_EQ(str1.allocatedSize, 0);
14-
1511
string_assign_cstring(&str1, "test");
1612
ASSERT_EQ(str1.size, 4);
1713
ASSERT_GE(str1.allocatedSize, 5);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <scratchcpp/stringptr.h>
2+
3+
#include "../common.h"
4+
5+
using namespace libscratchcpp;
6+
7+
TEST(StringPtrTest, DefaultConstructor)
8+
{
9+
StringPtr str;
10+
ASSERT_EQ(str.data, nullptr);
11+
ASSERT_EQ(str.size, 0);
12+
ASSERT_EQ(str.allocatedSize, 0);
13+
}
14+
15+
TEST(StringPtrTest, StringConstructor)
16+
{
17+
StringPtr str("test");
18+
ASSERT_EQ(str.size, 4);
19+
ASSERT_GE(str.allocatedSize, 5);
20+
ASSERT_TRUE(str.data);
21+
22+
ASSERT_EQ(str.data[0], u't');
23+
ASSERT_EQ(str.data[1], u'e');
24+
ASSERT_EQ(str.data[2], u's');
25+
ASSERT_EQ(str.data[3], u't');
26+
ASSERT_EQ(str.data[4], u'\0');
27+
}

0 commit comments

Comments
 (0)