29 static std::vector<PhsString> pool;
35 static std::vector<size_t> freeIndices;
47 i64 start = args[2].asInt();
48 pos =
s.find(sub, start);
51 return static_cast<i64>(pos);
55 else if (args.size() == 4)
57 i64 start = args[2].asInt();
58 i64 end = args[3].asInt();
59 pos =
s.find(sub, start);
62 return static_cast<i64>(pos);
88 return static_cast<i64>(idx);
94 i64 idx = args[0].asInt();
95 if (idx < 0 || idx >=
static_cast<i64>(
getSbPool().size()))
96 throw std::runtime_error(
"Invalid StringBuilder handle");
105 i64 idx = args[0].asInt();
106 if (idx < 0 || idx >=
static_cast<i64>(
getSbPool().size()))
107 throw std::runtime_error(
"Invalid StringBuilder handle");
115 size_t idx = args[0].asInt();
124 size_t idx = args[0].asInt();
126 throw std::runtime_error(
"Invalid StringBuilder handle");
134 if (args[0].isString())
137 i64 idx = args[1].asInt();
138 if (idx < 0 || idx >=
static_cast<i64>(
s.length()))
142 throw std::runtime_error(
"char_at() expects a string");
150 throw std::runtime_error(
"substr() expects 2 or 3 arguments");
153 i64 start = args[1].asInt();
154 i64 len = (
i64)args.size() == 3 ? args[2].asInt() : (
i64)
s.length() - start;
156 if (start < 0 || start >=
static_cast<i64>(
s.length()))
161 return Value(
s.substr(start, len));
168 for (
const auto &arg : args)
170 result += arg.toString();
179 return static_cast<i64>(
s.length());
186 std::transform(
s.begin(),
s.end(),
s.begin(), ::toupper);
194 std::transform(
s.begin(),
s.end(),
s.begin(), ::tolower);
201 std::string
s = args[0].string();
202 std::string prefix = args[1].string();
203 if (
s.length() >= prefix.length())
205 return Value(
s.compare(0, prefix.length(), prefix) == 0);
213 std::string
s = args[0].string();
214 std::string suffix = args[1].string();
215 if (
s.length() >= suffix.length())
217 return Value(
s.compare(
s.length() - suffix.length(), suffix.length(), suffix) == 0);
static constexpr std::size_t npos
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 i64 sb_new(const std::vector< Value > &args, VM *vm)
Create new string builder.
static void registerStringFunctions(VM *vm)
static PhsString sb_free(const std::vector< Value > &args, VM *vm)
Free string builder.
static i64 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 PhsString sb_to_string(const std::vector< Value > &args, VM *vm)
Convert string builder to string.
static PhsString str_upper(const std::vector< Value > &args, VM *vm)
Convert to uppercase.
static PhsString str_lower(const std::vector< Value > &args, VM *vm)
Convert to lowercase.
static i64 str_len(const std::vector< Value > &args, VM *vm)
Get string length.
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 PhsString str_concat(const std::vector< Value > &args, VM *vm)
Concatenate strings.
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< PhsString > & getSbPool()
static std::vector< size_t > & getSbFreeIndices()