27static std::vector<std::string>
sbPool;
33 std::string
s = args[0].asString();
34 std::string sub = args[1].asString();
38 int64_t start = args[2].asInt();
39 pos =
s.find(sub, start);
40 if (pos != std::string::npos)
42 return static_cast<int64_t
>(pos);
46 else if (args.size() == 4)
48 int64_t start = args[2].asInt();
49 int64_t end = args[3].asInt();
50 pos =
s.find(sub, start);
51 if (pos != std::string::npos && pos <
static_cast<size_t>(end))
53 return static_cast<int64_t
>(pos);
61 return pos != std::string::npos ?
static_cast<int64_t
>(pos) :
false;
79 return static_cast<int64_t
>(idx);
85 int64_t idx = args[0].asInt();
86 if (idx < 0 || idx >=
static_cast<int64_t
>(
sbPool.size()))
87 throw std::runtime_error(
"Invalid StringBuilder handle");
89 sbPool[idx] += args[1].toString();
96 int64_t idx = args[0].asInt();
97 if (idx < 0 || idx >=
static_cast<int64_t
>(
sbPool.size()))
98 throw std::runtime_error(
"Invalid StringBuilder handle");
106 size_t idx = args[0].asInt();
107 std::string value =
sbPool[idx];
115 size_t idx = args[0].asInt();
117 throw std::runtime_error(
"Invalid StringBuilder handle");
125 if (args[0].isString())
127 const std::string &
s = args[0].asString();
128 int64_t idx = args[1].asInt();
129 if (idx < 0 || idx >=
static_cast<int64_t
>(
s.length()))
131 return Value(std::string(1,
s[idx]));
133 throw std::runtime_error(
"char_at() expects a string");
139 if (args.size() < 2 || args.size() > 3)
141 throw std::runtime_error(
"substr() expects 2 or 3 arguments");
143 std::string
s = args[0].asString();
144 int64_t start = args[1].asInt();
145 int64_t len = (int64_t)args.size() == 3 ? args[2].asInt() : (int64_t)
s.length() - start;
147 if (start < 0 || start >=
static_cast<int64_t
>(
s.length()))
152 return Value(
s.substr(start, len));
158 std::string result =
"";
159 for (
const auto &arg : args)
161 result += arg.toString();
169 std::string
s = args[0].toString();
170 return static_cast<int64_t
>(
s.length());
176 std::string
s = args[0].asString();
177 std::transform(
s.begin(),
s.end(),
s.begin(), ::toupper);
184 std::string
s = args[0].asString();
185 std::transform(
s.begin(),
s.end(),
s.begin(), ::tolower);
192 std::string
s = args[0].asString();
193 std::string prefix = args[1].asString();
194 if (
s.length() >= prefix.length())
196 return Value(
s.compare(0, prefix.length(), prefix) == 0);
204 std::string
s = args[0].asString();
205 std::string suffix = args[1].asString();
206 if (
s.length() >= suffix.length())
208 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 std::string str_concat(const std::vector< Value > &args, VM *vm)
Concatenate strings.
static void registerStringFunctions(VM *vm)
static int64_t str_len(const std::vector< Value > &args, VM *vm)
Get string length.
static std::string sb_free(const std::vector< Value > &args, VM *vm)
Free string builder.
static int64_t str_find(const std::vector< Value > &args, VM *vm)
Find string in string.
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
static Value str_substr(const std::vector< Value > &args, VM *vm)
Get substring.
static std::string str_upper(const std::vector< Value > &args, VM *vm)
Convert to uppercase.
static std::string str_lower(const std::vector< Value > &args, VM *vm)
Convert to lowercase.
static std::string 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_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.
static int64_t sb_new(const std::vector< Value > &args, VM *vm)
Create new string builder.
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