Skip to content

Commit 24e2d49

Browse files
committed
improve string concat
1 parent 7970c6c commit 24e2d49

File tree

4 files changed

+62
-48
lines changed

4 files changed

+62
-48
lines changed

include/rift/value.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <unordered_map>
77
#include <variant>
88
#include <vector>
9+
#include <fmt/format.h>
10+
#include <fmt/ranges.h>
911

1012
#include <Geode/Result.hpp>
1113

@@ -256,4 +258,43 @@ namespace rift {
256258
};
257259
}
258260

261+
template <>
262+
struct fmt::formatter<rift::Value> {
263+
constexpr auto parse(format_parse_context& ctx) noexcept {
264+
return ctx.begin();
265+
}
266+
267+
template <typename FormatContext>
268+
auto format(rift::Value const& value, FormatContext& ctx) const noexcept {
269+
switch (value.type()) {
270+
case rift::Value::Type::String:
271+
return fmt::format_to(ctx.out(), "{}", value.getString());
272+
case rift::Value::Type::Integer:
273+
return fmt::format_to(ctx.out(), "{}", value.getInteger());
274+
case rift::Value::Type::Float:
275+
return fmt::format_to(ctx.out(), "{:.2f}", value.getFloat());
276+
case rift::Value::Type::Boolean:
277+
return fmt::format_to(ctx.out(), "{}", value.getBoolean() ? "true" : "false");
278+
case rift::Value::Type::Array: {
279+
return fmt::format_to(ctx.out(), "[{}]", fmt::join(value.getArray(), ", "));;
280+
}
281+
case rift::Value::Type::Object: {
282+
auto& obj = value.getObject();
283+
size_t i = 0;
284+
auto count = obj.size();
285+
fmt::format_to(ctx.out(), "{{");
286+
for (auto const& [key, val] : obj) {
287+
fmt::format_to(ctx.out(), "{}: {}", key, val);
288+
if (i < count - 1) {
289+
fmt::format_to(ctx.out(), ", ");
290+
}
291+
}
292+
return fmt::format_to(ctx.out(), "}}");
293+
}
294+
default:
295+
return fmt::format_to(ctx.out(), "null");
296+
}
297+
}
298+
};
299+
259300
#endif // RIFT_VALUE_HPP

src/optimizer.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,23 +249,25 @@ namespace rift::optimizer {
249249
// Helper lambda to create a ValueNode
250250
#define MAKE_VALUE(...) std::make_unique<ValueNode>(Value(__VA_ARGS__), start, end);
251251

252+
using namespace std::literals::string_view_literals;
253+
252254
// Check if the name is a constant identifier
253-
if (name == "true" || name == "false") {
255+
if (name == "true"sv || name == "false"sv) {
254256
return MAKE_VALUE(name == "true");
255257
}
256-
if (name == "null") {
258+
if (name == "null"sv) {
257259
return MAKE_VALUE();
258260
}
259-
if (name == "PI") {
261+
if (name == "PI"sv) {
260262
return MAKE_VALUE(std::numbers::pi);
261263
}
262-
if (name == "E") {
264+
if (name == "E"sv) {
263265
return MAKE_VALUE(std::numbers::e);
264266
}
265-
if (name == "inf") {
267+
if (name == "inf"sv) {
266268
return MAKE_VALUE(std::numeric_limits<double>::infinity());
267269
}
268-
if (name == "nan") {
270+
if (name == "nan"sv) {
269271
return MAKE_VALUE(std::numeric_limits<double>::quiet_NaN());
270272
}
271273

src/value.cpp

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -83,36 +83,7 @@ namespace rift {
8383
#endif
8484

8585
std::string Value::toString() const noexcept {
86-
switch (m_type) {
87-
case Type::String:
88-
return std::get<std::string>(m_data);
89-
case Type::Integer:
90-
return std::to_string(std::get<int64_t>(m_data));
91-
case Type::Float:
92-
return fmt::format("{:.2f}", std::get<double>(m_data));
93-
case Type::Boolean:
94-
return std::get<bool>(m_data) ? "true" : "false";
95-
case Type::Array: {
96-
std::string result = "[";
97-
auto const& array = getArray();
98-
for (size_t i = 0; i < array.size(); ++i) {
99-
result += array[i].toString();
100-
if (i + 1 < array.size()) result += ", ";
101-
}
102-
return result + "]";
103-
}
104-
case Type::Object: {
105-
// print all key-value pairs in the object on separate lines
106-
std::string result = "{";
107-
for (auto const& [key, value] : getObject()) {
108-
result += fmt::format("{}: {}, ", key, value.toString());
109-
}
110-
if (!result.empty()) result.resize(result.size() - 2);
111-
return result + "}";
112-
}
113-
default:
114-
return "null";
115-
}
86+
return fmt::to_string(*this);
11687
}
11788

11889
int64_t Value::toInteger() const noexcept {
@@ -716,4 +687,4 @@ namespace rift {
716687
auto& object = std::get<Object>(m_data);
717688
return object[key];
718689
}
719-
}
690+
}

src/visitor.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ namespace rift {
4747
return res;
4848
}
4949

50-
result += res.unwrap().toString();
50+
fmt::format_to(std::back_inserter(result), "{}", res.unwrap());
5151
}
52-
return geode::Ok(Value(result));
52+
return geode::Ok(Value(std::move(result)));
5353
}
5454

5555
VisitorResult Visitor::visit(IdentifierNode const& node) noexcept {
@@ -94,7 +94,7 @@ namespace rift {
9494
if (res.isErr()) { \
9595
return node.error(fmt::format("RuntimeError: {}", res.unwrapErr())); \
9696
} \
97-
return geode::Ok(res.unwrap()); \
97+
return geode::Ok(std::move(res).unwrap()); \
9898
}
9999

100100

@@ -143,7 +143,7 @@ namespace rift {
143143
if (fmtRes.isErr()) {
144144
return node.error(fmt::format("SubExpressionError: {}", fmtRes.unwrapErr().message()));
145145
}
146-
return geode::Ok(Value(fmtRes.unwrap()));
146+
return geode::Ok(Value(std::move(fmtRes).unwrap()));
147147
}
148148
case TokenType::BOOL_CAST:
149149
return geode::Ok(res.unwrap().toBoolean());
@@ -204,7 +204,7 @@ namespace rift {
204204
return node.error(fmt::format("RuntimeError: {}", res.unwrapErr()));
205205
}
206206

207-
return geode::Ok(res.unwrap());
207+
return geode::Ok(std::move(res).unwrap());
208208
}
209209

210210
VisitorResult Visitor::visit(AccessorNode const& node) noexcept {
@@ -249,7 +249,7 @@ namespace rift {
249249
if (res.isErr()) {
250250
return res;
251251
}
252-
result += res.unwrap().toString();
252+
fmt::format_to(std::back_inserter(result), "{}", res.unwrap());
253253
}
254254
} break;
255255
case Value::Type::Array: {
@@ -263,7 +263,7 @@ namespace rift {
263263
if (res.isErr()) {
264264
return res;
265265
}
266-
result += res.unwrap().toString();
266+
fmt::format_to(std::back_inserter(result), "{}", res.unwrap());
267267
}
268268
} break;
269269
case Value::Type::Object: {
@@ -277,7 +277,7 @@ namespace rift {
277277
if (res.isErr()) {
278278
return res;
279279
}
280-
result += res.unwrap().toString();
280+
fmt::format_to(std::back_inserter(result), "{}", res.unwrap());
281281
}
282282
} break;
283283
case Value::Type::Null:
@@ -294,12 +294,12 @@ namespace rift {
294294
if (res.isErr()) {
295295
return res;
296296
}
297-
result += res.unwrap().toString();
297+
fmt::format_to(std::back_inserter(result), "{}", res.unwrap());
298298
}
299299
} break;
300300
}
301301

302-
return geode::Ok(Value(result));
302+
return geode::Ok(Value(std::move(result)));
303303
}
304304

305305
VisitorResult Visitor::visit(ScriptNode const& node) noexcept {
@@ -325,6 +325,6 @@ namespace rift {
325325
}
326326

327327
m_localVariables.get()[node.getName()] = value.unwrap();
328-
return geode::Ok(std::move(value.unwrap()));
328+
return geode::Ok(std::move(value).unwrap());
329329
}
330330
}

0 commit comments

Comments
 (0)