27static std::vector<std::string>
sbPool;
33 std::string s = args[0].asString();
34 std::string sub = args[1].asString();
36 if (args.size() == 3) {
37 int64_t start = args[2].asInt();
38 pos = s.find(sub, start);
39 if (pos != std::string::npos) {
40 return static_cast<int64_t
>(pos);
43 }
else if (args.size() == 4) {
44 int64_t start = args[2].asInt();
45 int64_t end = args[3].asInt();
46 pos = s.find(sub, start);
47 if (pos != std::string::npos && pos <
static_cast<size_t>(end)) {
48 return static_cast<int64_t
>(pos);
54 return pos != std::string::npos ?
static_cast<int64_t
>(pos) :
false;
72 return static_cast<int64_t
>(idx);
78 int64_t idx = args[0].asInt();
79 if (idx < 0 || idx >=
static_cast<int64_t
>(
sbPool.size()))
80 throw std::runtime_error(
"Invalid StringBuilder handle");
82 sbPool[idx] += args[1].toString();
89 int64_t idx = args[0].asInt();
90 if (idx < 0 || idx >=
static_cast<int64_t
>(
sbPool.size()))
91 throw std::runtime_error(
"Invalid StringBuilder handle");
99 size_t idx = args[0].asInt();
100 std::string value =
sbPool[idx];
108 size_t idx = args[0].asInt();
110 throw std::runtime_error(
"Invalid StringBuilder handle");
118 if (args[0].isString())
120 const std::string &s = args[0].asString();
121 int64_t idx = args[1].asInt();
122 if (idx < 0 || idx >=
static_cast<int64_t
>(s.length()))
124 return Value(std::string(1, s[idx]));
126 throw std::runtime_error(
"char_at() expects a string");
132 if (args.size() < 2 || args.size() > 3)
134 throw std::runtime_error(
"substr() expects 2 or 3 arguments");
136 std::string s = args[0].asString();
137 int64_t start = args[1].asInt();
138 int64_t len = (int64_t)args.size() == 3 ? args[2].asInt() : (int64_t)s.length() - start;
140 if (start < 0 || start >=
static_cast<int64_t
>(s.length()))
145 return Value(s.substr(start, len));
151 std::string result =
"";
152 for (
const auto &arg : args)
154 result += arg.toString();
156 return Value(result);
162 std::string s = args[0].toString();
163 return static_cast<int64_t
>(s.length());
169 std::string s = args[0].asString();
170 std::transform(s.begin(), s.end(), s.begin(), ::toupper);
177 std::string s = args[0].asString();
178 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
185 std::string s = args[0].asString();
186 std::string prefix = args[1].asString();
187 if (s.length() >= prefix.length())
189 return Value(s.compare(0, prefix.length(), prefix) == 0);
197 std::string s = args[0].asString();
198 std::string suffix = args[1].asString();
199 if (s.length() >= suffix.length())
201 return Value(s.compare(s.length() - suffix.length(), suffix.length(), suffix) == 0);
static Value sb_clear(const std::vector< Value > &args, VM *vm)
Clear string builder.
static Value str_char_at(const std::vector< Value > &args, VM *vm)
Get character at index.
static void registerStringFunctions(VM *vm)
static Value sb_free(const std::vector< Value > &args, VM *vm)
Free string builder.
static Value str_concat(const std::vector< Value > &args, VM *vm)
Concatenate strings.
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
static Value str_find(const std::vector< Value > &args, VM *vm)
Find string in string.
static Value str_substr(const std::vector< Value > &args, VM *vm)
Get substring.
static Value sb_new(const std::vector< Value > &args, VM *vm)
Create new string builder.
static Value str_lower(const std::vector< Value > &args, VM *vm)
Convert to lowercase.
static Value str_upper(const std::vector< Value > &args, VM *vm)
Convert to uppercase.
static Value sb_to_string(const std::vector< Value > &args, VM *vm)
Convert string builder to string.
static Value sb_append(const std::vector< Value > &args, VM *vm)
Append to string builder.
static Value str_len(const std::vector< Value > &args, VM *vm)
Get string length.
static Value str_ends_with(const std::vector< Value > &args, VM *vm)
Check if string ends with.
static Value str_starts_with(const std::vector< Value > &args, VM *vm)
Check if string starts with.
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
A value in the Phasor VM.
The Phasor Programming Language and Runtime.
static std::vector< std::string > sbPool
static std::vector< size_t > sbFreeIndices