From 7b995b33489371fcd9ecc17e9b0b6fe187fb445c Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Sat, 26 Mar 2022 00:05:02 +0100 Subject: [PATCH] Add some string functions --- src/component/string.cpp | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/component/string.cpp diff --git a/src/component/string.cpp b/src/component/string.cpp new file mode 100644 index 0000000..9922bfb --- /dev/null +++ b/src/component/string.cpp @@ -0,0 +1,46 @@ +#include +#include "loader/component_loader.hpp" + +#include "gsc.hpp" + +#include + +namespace string +{ + class component final : public component_interface + { + public: + void post_unpack() override + { + gsc::function::add("toupper", [](const gsc::function_args& args) + { + return utils::string::to_upper(args[0].as()); + }); + + gsc::function::add("getchar", [](const gsc::function_args& args) + { + auto index = 0; + if (args.size() > 1) + { + index = args[1].as(); + } + + const auto string = args[0].as(); + if (index >= static_cast(string.size())) + { + throw std::runtime_error("Char index out of bounds"); + } + + return static_cast(string[index]); + }); + + gsc::function::add("chartostring", [](const gsc::function_args& args) + { + const auto char_ = static_cast(args[0].as()); + return std::string(1, char_); + }); + } + }; +} + +REGISTER_COMPONENT(string::component) \ No newline at end of file