|
| 1 | +/** |
| 2 | + * @file ChaiScript String Methods |
| 3 | + * |
| 4 | + * Adds some additional string methods to ChaiScript strings. |
| 5 | + * |
| 6 | + * string::replace(string search, string replace) |
| 7 | + * string::replace(char search, char replace) |
| 8 | + * string::trim() |
| 9 | + * string::split(string token) |
| 10 | + * string::toLowerCase() |
| 11 | + * string::toUpperCase() |
| 12 | + */ |
| 13 | +#include <algorithm> |
| 14 | +#include <string> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +#include <chaiscript/chaiscript.hpp> |
| 18 | + |
| 19 | +namespace chaiscript { |
| 20 | + namespace extras { |
| 21 | + namespace string_methods { |
| 22 | + ModulePtr bootstrap(ModulePtr m = std::make_shared<Module>()) |
| 23 | + { |
| 24 | + // Add lists of strings. |
| 25 | + m->add(bootstrap::standard_library::vector_type<std::vector<std::string>>("StringVector")); |
| 26 | + |
| 27 | + // string::replace(std::string search, std::string replace) |
| 28 | + m->add(fun([](const std::string& subject, const std::string& search, const std::string& replace) { |
| 29 | + std::string result(subject); |
| 30 | + size_t pos = 0; |
| 31 | + while ((pos = result.find(search, pos)) != std::string::npos) { |
| 32 | + result.replace(pos, search.length(), replace); |
| 33 | + pos += replace.length(); |
| 34 | + } |
| 35 | + return result; |
| 36 | + }), "replace"); |
| 37 | + |
| 38 | + // string::replace(char, char) |
| 39 | + m->add(fun([](const std::string& subject, char search, char replace) { |
| 40 | + std::string result(subject); |
| 41 | + std::replace(result.begin(), result.end(), search, replace); |
| 42 | + return result; |
| 43 | + }), "replace"); |
| 44 | + |
| 45 | + // string::trim() |
| 46 | + m->add(fun([](const std::string& subject) { |
| 47 | + std::string result(subject); |
| 48 | + std::string chars = "\t\n\v\f\r "; |
| 49 | + result.erase(0, result.find_first_not_of(chars)); |
| 50 | + result.erase(0, result.find_last_not_of(chars)); |
| 51 | + return result; |
| 52 | + }), "trim"); |
| 53 | + |
| 54 | + // string::split(string) |
| 55 | + m->add(fun([](const std::string& subject, const std::string& token) { |
| 56 | + std::string str(subject); |
| 57 | + std::vector<std::string> result; |
| 58 | + while (str.size()) { |
| 59 | + size_t index = str.find(token); |
| 60 | + if (index != std::string::npos) { |
| 61 | + result.push_back(str.substr(0, index)); |
| 62 | + str = str.substr(index + token.size()); |
| 63 | + if (str.size() == 0) { |
| 64 | + result.push_back(str); |
| 65 | + } |
| 66 | + } else { |
| 67 | + result.push_back(str); |
| 68 | + str = ""; |
| 69 | + } |
| 70 | + } |
| 71 | + return result; |
| 72 | + }), "split"); |
| 73 | + |
| 74 | + // string::toLowerCase() |
| 75 | + m->add(fun([](const std::string& subject) { |
| 76 | + std::string result(subject); |
| 77 | + std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { |
| 78 | + return std::tolower(c); |
| 79 | + }); |
| 80 | + return result; |
| 81 | + }), "toLowerCase"); |
| 82 | + |
| 83 | + // string::toUpperCase |
| 84 | + m->add(fun([](const std::string& subject) { |
| 85 | + std::string result(subject); |
| 86 | + std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { |
| 87 | + return std::toupper(c); |
| 88 | + }); |
| 89 | + return result; |
| 90 | + }), "toUpperCase"); |
| 91 | + |
| 92 | + return m; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments