Skip to content

Commit 1454aa5

Browse files
committed
Implement operator_contains
1 parent 84d109c commit 1454aa5

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/dev/blocks/operatorblocks.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void OperatorBlocks::registerBlocks(IEngine *engine)
3535
engine->addCompileFunction(this, "operator_join", &compileJoin);
3636
engine->addCompileFunction(this, "operator_letter_of", &compileLetterOf);
3737
engine->addCompileFunction(this, "operator_length", &compileLength);
38+
engine->addCompileFunction(this, "operator_contains", &compileContains);
3839
}
3940

4041
CompilerValue *OperatorBlocks::compileAdd(Compiler *compiler)
@@ -114,6 +115,13 @@ CompilerValue *OperatorBlocks::compileLength(Compiler *compiler)
114115
return compiler->addFunctionCall("operator_length", Compiler::StaticType::Number, { Compiler::StaticType::String }, { string });
115116
}
116117

118+
CompilerValue *OperatorBlocks::compileContains(Compiler *compiler)
119+
{
120+
auto string1 = compiler->addInput("STRING1");
121+
auto string2 = compiler->addInput("STRING2");
122+
return compiler->addFunctionCall("operator_contains", Compiler::StaticType::Bool, { Compiler::StaticType::String, Compiler::StaticType::String }, { string1, string2 });
123+
}
124+
117125
extern "C" char *operator_join(const char *string1, const char *string2)
118126
{
119127
const size_t len1 = strlen(string1);
@@ -155,3 +163,13 @@ extern "C" double operator_length(const char *string)
155163
// TODO: Rewrite this
156164
return utf8::utf8to16(std::string(string)).size();
157165
}
166+
167+
extern "C" bool operator_contains(const char *string1, const char *string2)
168+
{
169+
// TODO: Rewrite this
170+
std::u16string u16string1 = utf8::utf8to16(std::string(string1));
171+
std::u16string u16string2 = utf8::utf8to16(std::string(string2));
172+
std::transform(u16string1.begin(), u16string1.end(), u16string1.begin(), ::tolower);
173+
std::transform(u16string2.begin(), u16string2.end(), u16string2.begin(), ::tolower);
174+
return (u16string1.find(u16string2) != std::u16string::npos);
175+
}

src/dev/blocks/operatorblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class OperatorBlocks : public IExtension
3232
static CompilerValue *compileJoin(Compiler *compiler);
3333
static CompilerValue *compileLetterOf(Compiler *compiler);
3434
static CompilerValue *compileLength(Compiler *compiler);
35+
static CompilerValue *compileContains(Compiler *compiler);
3536
};
3637

3738
} // namespace libscratchcpp

test/dev/blocks/operator_blocks_test.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,64 @@ TEST_F(OperatorBlocksTest, Length)
449449
ASSERT_EQ(Value(values[1]), 11);
450450
ASSERT_EQ(Value(values[2]), 9);
451451
}
452+
453+
TEST_F(OperatorBlocksTest, Contains)
454+
{
455+
auto target = std::make_shared<Sprite>();
456+
ScriptBuilder builder(m_extension.get(), m_engine, target);
457+
458+
builder.addBlock("operator_contains");
459+
builder.addValueInput("STRING1", "abc");
460+
builder.addValueInput("STRING2", "a");
461+
builder.captureBlockReturnValue();
462+
463+
builder.addBlock("operator_contains");
464+
builder.addValueInput("STRING1", "abc");
465+
builder.addValueInput("STRING2", "e");
466+
builder.captureBlockReturnValue();
467+
468+
builder.addBlock("operator_contains");
469+
builder.addValueInput("STRING1", "abc");
470+
builder.addValueInput("STRING2", "C");
471+
builder.captureBlockReturnValue();
472+
473+
builder.addBlock("operator_contains");
474+
builder.addValueInput("STRING1", "Hello world");
475+
builder.addValueInput("STRING2", "ello");
476+
builder.captureBlockReturnValue();
477+
478+
builder.addBlock("operator_contains");
479+
builder.addValueInput("STRING1", "Hello world");
480+
builder.addValueInput("STRING2", "olld");
481+
builder.captureBlockReturnValue();
482+
483+
builder.addBlock("operator_contains");
484+
builder.addValueInput("STRING1", "ábČ");
485+
builder.addValueInput("STRING2", "á");
486+
builder.captureBlockReturnValue();
487+
488+
builder.addBlock("operator_contains");
489+
builder.addValueInput("STRING1", "ábČ");
490+
builder.addValueInput("STRING2", "");
491+
builder.captureBlockReturnValue();
492+
493+
builder.addBlock("operator_contains");
494+
builder.addValueInput("STRING1", "ábČ");
495+
builder.addValueInput("STRING2", "ďá");
496+
builder.captureBlockReturnValue();
497+
498+
builder.build();
499+
builder.run();
500+
501+
List *valueList = builder.capturedValues();
502+
ValueData *values = valueList->data();
503+
ASSERT_EQ(valueList->size(), 8);
504+
ASSERT_EQ(Value(values[0]), true);
505+
ASSERT_EQ(Value(values[1]), false);
506+
ASSERT_EQ(Value(values[2]), true);
507+
ASSERT_EQ(Value(values[3]), true);
508+
ASSERT_EQ(Value(values[4]), false);
509+
ASSERT_EQ(Value(values[5]), true);
510+
ASSERT_EQ(Value(values[6]), true);
511+
ASSERT_EQ(Value(values[7]), false);
512+
}

0 commit comments

Comments
 (0)