|
5 | 5 | #include <scratchcpp/compilerconstant.h> |
6 | 6 | #include <scratchcpp/field.h> |
7 | 7 | #include <scratchcpp/value.h> |
| 8 | +#include <scratchcpp/string_pool.h> |
| 9 | +#include <scratchcpp/string_functions.h> |
| 10 | +#include <scratchcpp/stringptr.h> |
8 | 11 | #include <utf8.h> |
9 | 12 |
|
10 | 13 | #include "operatorblocks.h" |
@@ -129,7 +132,7 @@ CompilerValue *OperatorBlocks::compileContains(Compiler *compiler) |
129 | 132 | { |
130 | 133 | auto string1 = compiler->addInput("STRING1"); |
131 | 134 | 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 }); |
133 | 136 | } |
134 | 137 |
|
135 | 138 | CompilerValue *OperatorBlocks::compileMod(Compiler *compiler) |
@@ -180,54 +183,34 @@ CompilerValue *OperatorBlocks::compileMathOp(Compiler *compiler) |
180 | 183 | return compiler->addConstValue(Value()); |
181 | 184 | } |
182 | 185 |
|
183 | | -extern "C" char *operator_join(const char *string1, const char *string2) |
| 186 | +extern "C" StringPtr *operator_join(const StringPtr *string1, const StringPtr *string2) |
184 | 187 | { |
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 |
197 | 193 | return ret; |
198 | 194 | } |
199 | 195 |
|
200 | | -extern "C" char *operator_letter_of(double letter, const char *string) |
| 196 | +extern "C" StringPtr *operator_letter_of(double letter, const StringPtr *string) |
201 | 197 | { |
202 | | - const size_t len = strlen(string); |
| 198 | + StringPtr *ret = string_pool_new(); |
203 | 199 |
|
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'; |
207 | 203 | return ret; |
208 | 204 | } |
209 | 205 |
|
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; |
216 | 210 | return ret; |
217 | 211 | } |
218 | 212 |
|
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) |
226 | 214 | { |
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; |
233 | 216 | } |
0 commit comments