Skip to content

Commit 84d109c

Browse files
committed
Implement operator_length
1 parent 676cc83 commit 84d109c

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/dev/blocks/operatorblocks.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void OperatorBlocks::registerBlocks(IEngine *engine)
3434
engine->addCompileFunction(this, "operator_not", &compileNot);
3535
engine->addCompileFunction(this, "operator_join", &compileJoin);
3636
engine->addCompileFunction(this, "operator_letter_of", &compileLetterOf);
37+
engine->addCompileFunction(this, "operator_length", &compileLength);
3738
}
3839

3940
CompilerValue *OperatorBlocks::compileAdd(Compiler *compiler)
@@ -107,6 +108,12 @@ CompilerValue *OperatorBlocks::compileLetterOf(Compiler *compiler)
107108
return compiler->addFunctionCall("operator_letter_of", Compiler::StaticType::String, { Compiler::StaticType::Number, Compiler::StaticType::String }, { letter, string });
108109
}
109110

111+
CompilerValue *OperatorBlocks::compileLength(Compiler *compiler)
112+
{
113+
auto string = compiler->addInput("STRING");
114+
return compiler->addFunctionCall("operator_length", Compiler::StaticType::Number, { Compiler::StaticType::String }, { string });
115+
}
116+
110117
extern "C" char *operator_join(const char *string1, const char *string2)
111118
{
112119
const size_t len1 = strlen(string1);
@@ -142,3 +149,9 @@ extern "C" char *operator_letter_of(double letter, const char *string)
142149

143150
return ret;
144151
}
152+
153+
extern "C" double operator_length(const char *string)
154+
{
155+
// TODO: Rewrite this
156+
return utf8::utf8to16(std::string(string)).size();
157+
}

src/dev/blocks/operatorblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class OperatorBlocks : public IExtension
3131
static CompilerValue *compileNot(Compiler *compiler);
3232
static CompilerValue *compileJoin(Compiler *compiler);
3333
static CompilerValue *compileLetterOf(Compiler *compiler);
34+
static CompilerValue *compileLength(Compiler *compiler);
3435
};
3536

3637
} // namespace libscratchcpp

test/dev/blocks/operator_blocks_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,31 @@ TEST_F(OperatorBlocksTest, LetterOf)
421421
ASSERT_EQ(Value(values[3]), "");
422422
ASSERT_EQ(Value(values[4]), "Á");
423423
}
424+
425+
TEST_F(OperatorBlocksTest, Length)
426+
{
427+
auto target = std::make_shared<Sprite>();
428+
ScriptBuilder builder(m_extension.get(), m_engine, target);
429+
430+
builder.addBlock("operator_length");
431+
builder.addValueInput("STRING", "abc");
432+
builder.captureBlockReturnValue();
433+
434+
builder.addBlock("operator_length");
435+
builder.addValueInput("STRING", "Hello world");
436+
builder.captureBlockReturnValue();
437+
438+
builder.addBlock("operator_length");
439+
builder.addValueInput("STRING", "dOádčĐaší");
440+
builder.captureBlockReturnValue();
441+
442+
builder.build();
443+
builder.run();
444+
445+
List *valueList = builder.capturedValues();
446+
ValueData *values = valueList->data();
447+
ASSERT_EQ(valueList->size(), 3);
448+
ASSERT_EQ(Value(values[0]), 3);
449+
ASSERT_EQ(Value(values[1]), 11);
450+
ASSERT_EQ(Value(values[2]), 9);
451+
}

0 commit comments

Comments
 (0)