Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
string.cpp
Go to the documentation of this file.
1#include "StdLib.hpp"
2#include <string>
3
4namespace Phasor
5{
6
25
26// StringBuilder Pool
27static std::vector<std::string> sbPool;
28static std::vector<size_t> sbFreeIndices;
29
30Value StdLib::str_find(const std::vector<Value> &args, VM *vm)
31{
32 checkArgCount(args, 2, "find", true);
33 std::string s = args[0].asString();
34 std::string sub = args[1].asString();
35 size_t pos;
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);
41 }
42 return -1;
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);
49 }
50 return -1;
51 } else {
52 pos = s.find(sub);
53 }
54 return pos != std::string::npos ? static_cast<int64_t>(pos) : false;
55}
56
57Value StdLib::sb_new(const std::vector<Value> &args, VM *vm)
58{
59 StdLib::checkArgCount(args, 0, "sb_new");
60 size_t idx;
61 if (!sbFreeIndices.empty())
62 {
63 idx = sbFreeIndices.back();
64 sbFreeIndices.pop_back();
65 sbPool[idx] = "";
66 }
67 else
68 {
69 idx = sbPool.size();
70 sbPool.push_back("");
71 }
72 return static_cast<int64_t>(idx);
73}
74
75Value StdLib::sb_append(const std::vector<Value> &args, VM *vm)
76{
77 StdLib::checkArgCount(args, 2, "sb_append");
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");
81
82 sbPool[idx] += args[1].toString();
83 return args[0]; // Return handle for chaining
84}
85
86Value StdLib::sb_to_string(const std::vector<Value> &args, VM *vm)
87{
88 StdLib::checkArgCount(args, 1, "sb_to_string");
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");
92
93 return Value(sbPool[idx]);
94}
95
96Value StdLib::sb_free(const std::vector<Value> &args, VM *vm)
97{
98 StdLib::checkArgCount(args, 1, "sb_free");
99 size_t idx = args[0].asInt();
100 std::string value = sbPool[idx];
101 sbFreeIndices.push_back(idx);
102 return value;
103}
104
105Value StdLib::sb_clear(const std::vector<Value> &args, VM *vm)
106{
107 StdLib::checkArgCount(args, 1, "sb_clear");
108 size_t idx = args[0].asInt();
109 if (idx >= sbPool.size())
110 throw std::runtime_error("Invalid StringBuilder handle");
111 sbPool[idx].clear();
112 return args[0]; // Return handle for chaining
113}
114
115Value StdLib::str_char_at(const std::vector<Value> &args, VM *vm)
116{
117 checkArgCount(args, 2, "char_at");
118 if (args[0].isString())
119 {
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()))
123 return Value("");
124 return Value(std::string(1, s[idx]));
125 }
126 throw std::runtime_error("char_at() expects a string");
127}
128
129Value StdLib::str_substr(const std::vector<Value> &args, VM *vm)
130{
131 checkArgCount(args, 2, "substr", true);
132 if (args.size() < 2 || args.size() > 3)
133 {
134 throw std::runtime_error("substr() expects 2 or 3 arguments");
135 }
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;
139
140 if (start < 0 || start >= static_cast<int64_t>(s.length()))
141 {
142 return Value("");
143 }
144
145 return Value(s.substr(start, len));
146}
147
148Value StdLib::str_concat(const std::vector<Value> &args, VM *vm)
149{
150 checkArgCount(args, 2, "concat");
151 std::string result = "";
152 for (const auto &arg : args)
153 {
154 result += arg.toString();
155 }
156 return Value(result);
157}
158
159Value StdLib::str_len(const std::vector<Value> &args, VM *vm)
160{
161 checkArgCount(args, 1, "len");
162 std::string s = args[0].toString();
163 return static_cast<int64_t>(s.length());
164}
165
166Value StdLib::str_upper(const std::vector<Value> &args, VM *vm)
167{
168 checkArgCount(args, 1, "to_upper");
169 std::string s = args[0].asString();
170 std::transform(s.begin(), s.end(), s.begin(), ::toupper);
171 return Value(s);
172}
173
174Value StdLib::str_lower(const std::vector<Value> &args, VM *vm)
175{
176 checkArgCount(args, 1, "to_lower");
177 std::string s = args[0].asString();
178 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
179 return Value(s);
180}
181
182Value StdLib::str_starts_with(const std::vector<Value> &args, VM *vm)
183{
184 checkArgCount(args, 2, "starts_with");
185 std::string s = args[0].asString();
186 std::string prefix = args[1].asString();
187 if (s.length() >= prefix.length())
188 {
189 return Value(s.compare(0, prefix.length(), prefix) == 0);
190 }
191 return Value(false);
192}
193
194Value StdLib::str_ends_with(const std::vector<Value> &args, VM *vm)
195{
196 checkArgCount(args, 2, "ends_with");
197 std::string s = args[0].asString();
198 std::string suffix = args[1].asString();
199 if (s.length() >= suffix.length())
200 {
201 return Value(s.compare(s.length() - suffix.length(), suffix.length(), suffix) == 0);
202 }
203 return Value(false);
204}
205} // namespace Phasor
static Value sb_clear(const std::vector< Value > &args, VM *vm)
Clear string builder.
Definition string.cpp:105
static Value str_char_at(const std::vector< Value > &args, VM *vm)
Get character at index.
Definition string.cpp:115
static void registerStringFunctions(VM *vm)
Definition string.cpp:7
static Value sb_free(const std::vector< Value > &args, VM *vm)
Free string builder.
Definition string.cpp:96
static Value str_concat(const std::vector< Value > &args, VM *vm)
Concatenate strings.
Definition string.cpp:148
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:44
static Value str_find(const std::vector< Value > &args, VM *vm)
Find string in string.
Definition string.cpp:30
static Value str_substr(const std::vector< Value > &args, VM *vm)
Get substring.
Definition string.cpp:129
static Value sb_new(const std::vector< Value > &args, VM *vm)
Create new string builder.
Definition string.cpp:57
static Value str_lower(const std::vector< Value > &args, VM *vm)
Convert to lowercase.
Definition string.cpp:174
static Value str_upper(const std::vector< Value > &args, VM *vm)
Convert to uppercase.
Definition string.cpp:166
static Value sb_to_string(const std::vector< Value > &args, VM *vm)
Convert string builder to string.
Definition string.cpp:86
static Value sb_append(const std::vector< Value > &args, VM *vm)
Append to string builder.
Definition string.cpp:75
static Value str_len(const std::vector< Value > &args, VM *vm)
Get string length.
Definition string.cpp:159
static Value str_ends_with(const std::vector< Value > &args, VM *vm)
Check if string ends with.
Definition string.cpp:194
static Value str_starts_with(const std::vector< Value > &args, VM *vm)
Check if string starts with.
Definition string.cpp:182
Virtual Machine.
Definition VM.hpp:30
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
Definition Native.cpp:5
A value in the Phasor VM.
Definition Value.hpp:67
The Phasor Programming Language and Runtime.
Definition AST.hpp:11
static std::vector< std::string > sbPool
Definition string.cpp:27
static std::vector< size_t > sbFreeIndices
Definition string.cpp:28