29static std::vector<std::string>
sbPool;
47 return static_cast<int64_t
>(idx);
53 int64_t idx = args[0].asInt();
54 if (idx < 0 || idx >=
static_cast<int64_t
>(
sbPool.size()))
55 throw std::runtime_error(
"Invalid StringBuilder handle");
57 sbPool[idx] += args[1].toString();
64 int64_t idx = args[0].asInt();
65 if (idx < 0 || idx >=
static_cast<int64_t
>(
sbPool.size()))
66 throw std::runtime_error(
"Invalid StringBuilder handle");
74 size_t idx = args[0].asInt();
75 std::string value =
sbPool[idx];
83 size_t idx = args[0].asInt();
85 throw std::runtime_error(
"Invalid StringBuilder handle");
93 if (args[0].isString())
95 const std::string &s = args[0].asString();
96 int64_t idx = args[1].asInt();
97 if (idx < 0 || idx >=
static_cast<int64_t
>(s.length()))
99 return Value(std::string(1, s[idx]));
101 throw std::runtime_error(
"char_at() expects a string");
107 if (args.size() < 2 || args.size() > 3)
109 throw std::runtime_error(
"substr() expects 2 or 3 arguments");
111 std::string s = args[0].asString();
112 int64_t start = args[1].asInt();
113 int64_t len = args.size() == 3 ? args[2].asInt() : s.length() - start;
115 if (start < 0 || start >=
static_cast<int64_t
>(s.length()))
120 return Value(s.substr(start, len));
126 std::string result =
"";
127 for (
const auto &arg : args)
129 result += arg.toString();
131 return Value(result);
137 std::string s = args[0].toString();
138 return static_cast<int64_t
>(s.length());
144 std::string s = args[0].asString();
145 std::transform(s.begin(), s.end(), s.begin(), ::toupper);
152 std::string s = args[0].asString();
153 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
160 std::string s = args[0].asString();
161 std::string prefix = args[1].asString();
162 if (s.length() >= prefix.length())
164 return Value(s.compare(0, prefix.length(), prefix) == 0);
172 std::string s = args[0].asString();
173 std::string suffix = args[1].asString();
174 if (s.length() >= suffix.length())
176 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 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_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 registerStringFunctions(const std::vector< Value > &args, VM *vm)
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