Skip to content

Commit 2499ca0

Browse files
committed
Use new string API in blocks
1 parent 76d2a0e commit 2499ca0

File tree

6 files changed

+54
-57
lines changed

6 files changed

+54
-57
lines changed

src/blocks/controlblocks.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include <scratchcpp/istacktimer.h>
1212
#include <scratchcpp/variable.h>
1313
#include <scratchcpp/sprite.h>
14+
#include <scratchcpp/stringptr.h>
15+
#include <scratchcpp/string_functions.h>
16+
#include <utf8.h>
1417

1518
#include "controlblocks.h"
1619

@@ -215,13 +218,15 @@ extern "C" void control_create_clone_by_index(ExecutionContext *ctx, double inde
215218
static_cast<Sprite *>(target)->clone();
216219
}
217220

218-
extern "C" void control_create_clone(ExecutionContext *ctx, const char *spriteName)
221+
extern "C" void control_create_clone(ExecutionContext *ctx, const StringPtr *spriteName)
219222
{
220-
if (strcmp(spriteName, "_myself_") == 0)
223+
static const StringPtr myself("_myself_");
224+
if (string_compare_case_sensitive(spriteName, &myself) == 0)
221225
control_create_clone_of_myself(ctx->thread()->target());
222226
else {
223227
IEngine *engine = ctx->engine();
224-
auto index = engine->findTarget(spriteName);
228+
// TODO: Use UTF-16 in engine
229+
auto index = engine->findTarget(utf8::utf16to8(std::u16string(spriteName->data)));
225230
Target *target = engine->targetAt(index);
226231

227232
if (!target->isStage())

src/blocks/eventblocks.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <scratchcpp/thread.h>
1010
#include <scratchcpp/compilerconstant.h>
1111
#include <scratchcpp/promise.h>
12+
#include <scratchcpp/stringptr.h>
13+
#include <utf8.h>
1214

1315
#include "eventblocks.h"
1416

@@ -125,11 +127,12 @@ CompilerValue *EventBlocks::compileWhenKeyPressed(Compiler *compiler)
125127
return nullptr;
126128
}
127129

128-
extern "C" void event_broadcast(ExecutionContext *ctx, const char *name, bool wait)
130+
extern "C" void event_broadcast(ExecutionContext *ctx, const StringPtr *name, bool wait)
129131
{
130132
Thread *thread = ctx->thread();
131133
IEngine *engine = thread->engine();
132-
std::vector<int> broadcasts = engine->findBroadcasts(name);
134+
// TODO: Use UTF-16 in engine
135+
std::vector<int> broadcasts = engine->findBroadcasts(utf8::utf16to8(std::u16string(name->data)));
133136

134137
for (int index : broadcasts)
135138
engine->broadcast(index, thread, wait);

src/blocks/operatorblocks.cpp

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <scratchcpp/compilerconstant.h>
66
#include <scratchcpp/field.h>
77
#include <scratchcpp/value.h>
8+
#include <scratchcpp/string_pool.h>
9+
#include <scratchcpp/string_functions.h>
10+
#include <scratchcpp/stringptr.h>
811
#include <utf8.h>
912

1013
#include "operatorblocks.h"
@@ -129,7 +132,7 @@ CompilerValue *OperatorBlocks::compileContains(Compiler *compiler)
129132
{
130133
auto string1 = compiler->addInput("STRING1");
131134
auto string2 = compiler->addInput("STRING2");
132-
return compiler->addFunctionCall("operator_contains", Compiler::StaticType::Bool, { Compiler::StaticType::String, Compiler::StaticType::String }, { string1, string2 });
135+
return compiler->addFunctionCall("string_contains_case_insensitive", Compiler::StaticType::Bool, { Compiler::StaticType::String, Compiler::StaticType::String }, { string1, string2 });
133136
}
134137

135138
CompilerValue *OperatorBlocks::compileMod(Compiler *compiler)
@@ -180,54 +183,34 @@ CompilerValue *OperatorBlocks::compileMathOp(Compiler *compiler)
180183
return compiler->addConstValue(Value());
181184
}
182185

183-
extern "C" char *operator_join(const char *string1, const char *string2)
186+
extern "C" StringPtr *operator_join(const StringPtr *string1, const StringPtr *string2)
184187
{
185-
const size_t len1 = strlen(string1);
186-
const size_t len2 = strlen(string2);
187-
188-
char *ret = (char *)malloc((len1 + len2 + 1) * sizeof(char));
189-
size_t i;
190-
191-
for (i = 0; i < len1; i++)
192-
ret[i] = string1[i];
193-
194-
for (i = 0; i < len2 + 1; i++) // +1: null-terminate
195-
ret[len1 + i] = string2[i];
196-
188+
StringPtr *ret = string_pool_new(true);
189+
ret->size = string1->size + string2->size;
190+
string_alloc(ret, ret->size);
191+
memcpy(ret->data, string1->data, string1->size * sizeof(typeof(*string1->data)));
192+
memcpy(ret->data + string1->size, string2->data, (string2->size + 1) * sizeof(typeof(*string2->data))); // +1: null-terminate
197193
return ret;
198194
}
199195

200-
extern "C" char *operator_letter_of(double letter, const char *string)
196+
extern "C" StringPtr *operator_letter_of(double letter, const StringPtr *string)
201197
{
202-
const size_t len = strlen(string);
198+
StringPtr *ret = string_pool_new();
203199

204-
if (letter < 1 || letter > len) {
205-
char *ret = (char *)malloc(sizeof(char));
206-
ret[0] = '\0';
200+
if (letter < 1 || letter > string->size) {
201+
string_alloc(ret, 0);
202+
ret->data[0] = u'\0';
207203
return ret;
208204
}
209205

210-
// TODO: Rewrite this
211-
std::u16string u16 = utf8::utf8to16(std::string(string));
212-
std::string str = utf8::utf16to8(std::u16string({ u16[(size_t)letter - 1] }));
213-
char *ret = (char *)malloc((str.size() + 1) * sizeof(char));
214-
strcpy(ret, str.c_str());
215-
206+
string_alloc(ret, 1);
207+
ret->data[0] = string->data[static_cast<size_t>(letter - 1)];
208+
ret->data[1] = u'\0';
209+
ret->size = 1;
216210
return ret;
217211
}
218212

219-
extern "C" double operator_length(const char *string)
220-
{
221-
// TODO: Rewrite this
222-
return utf8::utf8to16(std::string(string)).size();
223-
}
224-
225-
extern "C" bool operator_contains(const char *string1, const char *string2)
213+
extern "C" double operator_length(const StringPtr *string)
226214
{
227-
// TODO: Rewrite this
228-
std::u16string u16string1 = utf8::utf8to16(std::string(string1));
229-
std::u16string u16string2 = utf8::utf8to16(std::string(string2));
230-
std::transform(u16string1.begin(), u16string1.end(), u16string1.begin(), ::tolower);
231-
std::transform(u16string2.begin(), u16string2.end(), u16string2.begin(), ::tolower);
232-
return (u16string1.find(u16string2) != std::u16string::npos);
215+
return string->size;
233216
}

test/blocks/operator_blocks_test.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ TEST_F(OperatorBlocksTest, Join)
375375
List *valueList = builder.capturedValues();
376376
ValueData *values = valueList->data();
377377
ASSERT_EQ(valueList->size(), 2);
378-
ASSERT_EQ(Value(values[0]), "abcdef");
379-
ASSERT_EQ(Value(values[1]), "Hello world");
378+
ASSERT_EQ(Value(values[0]).toString(), "abcdef");
379+
ASSERT_EQ(Value(values[1]).toString(), "Hello world");
380380
}
381381

382382
TEST_F(OperatorBlocksTest, LetterOf)
@@ -415,11 +415,11 @@ TEST_F(OperatorBlocksTest, LetterOf)
415415
List *valueList = builder.capturedValues();
416416
ValueData *values = valueList->data();
417417
ASSERT_EQ(valueList->size(), 5);
418-
ASSERT_EQ(Value(values[0]), "b");
419-
ASSERT_EQ(Value(values[1]), "w");
420-
ASSERT_EQ(Value(values[2]), "");
421-
ASSERT_EQ(Value(values[3]), "");
422-
ASSERT_EQ(Value(values[4]), "Á");
418+
ASSERT_EQ(Value(values[0]).toString(), "b");
419+
ASSERT_EQ(Value(values[1]).toString(), "w");
420+
ASSERT_EQ(Value(values[2]).toString(), "");
421+
ASSERT_EQ(Value(values[3]).toString(), "");
422+
ASSERT_EQ(Value(values[4]).toString(), "Á");
423423
}
424424

425425
TEST_F(OperatorBlocksTest, Length)

test/blocks/util.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include <scratchcpp/value.h>
55
#include <scratchcpp/field.h>
66
#include <scratchcpp/variable.h>
7+
#include <scratchcpp/stringptr.h>
8+
#include <scratchcpp/string_pool.h>
9+
#include <scratchcpp/string_functions.h>
10+
#include <utf8.h>
711
#include <iostream>
812

913
#include "util.h"
@@ -47,20 +51,20 @@ void registerBlocks(IEngine *engine, IExtension *extension)
4751
});
4852
}
4953

50-
extern "C" void test_print(const char *str)
54+
extern "C" void test_print(const StringPtr *str)
5155
{
52-
std::cout << str << std::endl;
56+
std::cout << utf8::utf16to8(std::u16string(str->data)) << std::endl;
5357
}
5458

5559
extern "C" bool test_condition()
5660
{
5761
return conditionReturnValue;
5862
}
5963

60-
extern "C" char *test_const_string(const char *str)
64+
extern "C" StringPtr *test_const_string(const StringPtr *str)
6165
{
62-
char *ret = (char *)malloc((strlen(str) + 1) * sizeof(char));
63-
strcpy(ret, str);
66+
StringPtr *ret = string_pool_new();
67+
string_assign(ret, str);
6468
return ret;
6569
}
6670

test/test_api/testextension.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <scratchcpp/value.h>
55
#include <scratchcpp/input.h>
66
#include <scratchcpp/field.h>
7+
#include <scratchcpp/stringptr.h>
8+
#include <utf8.h>
79
#include <gtest/gtest.h>
810
#include <iostream>
911

@@ -94,7 +96,7 @@ extern "C" void test_simple()
9496
std::cout << "test" << std::endl;
9597
}
9698

97-
extern "C" void test_print(const char *string)
99+
extern "C" void test_print(const StringPtr *string)
98100
{
99-
std::cout << string << std::endl;
101+
std::cout << utf8::utf16to8(std::u16string(string->data)) << std::endl;
100102
}

0 commit comments

Comments
 (0)