Skip to content

Commit b617c59

Browse files
committed
Add string_methods
1 parent 7043c12 commit b617c59

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}")
179179

180180

181181

182-
set(CHAISCRIPT_BRANCH develop)
182+
set(CHAISCRIPT_BRANCH v6.1.0)
183183

184184
file(DOWNLOAD https://github.com/ChaiScript/ChaiScript/archive/${CHAISCRIPT_BRANCH}.tar.gz "${CMAKE_BINARY_DIR}/chaiscript/chaiscript-${CHAISCRIPT_BRANCH}.tar.gz"
185185
INACTIVITY_TIMEOUT 180 TIMEOUT 180 TLS_VERIFY off)
@@ -230,4 +230,6 @@ add_executable(string_id_test tests/string_id.cpp)
230230
target_link_libraries(string_id_test ${LIBS})
231231
ADD_CATCH_TESTS(string_id_test)
232232

233-
233+
add_executable(string_methods_test tests/string_methods.cpp)
234+
target_link_libraries(string_methods_test ${LIBS})
235+
ADD_CATCH_TESTS(string_methods_test)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <string>
2+
#include <vector>
3+
4+
#include <chaiscript/chaiscript.hpp>
5+
6+
namespace chaiscript {
7+
namespace extras {
8+
namespace string_methods {
9+
ModulePtr bootstrap(ModulePtr m = std::make_shared<Module>())
10+
{
11+
// string::replace(std::string search, std::string replace)
12+
chai.add(fun([](const std::string& subject, const std::string& search, const std::string& replace) {
13+
std::string result(subject);
14+
size_t pos = 0;
15+
while ((pos = result.find(search, pos)) != std::string::npos) {
16+
result.replace(pos, search.length(), replace);
17+
pos += replace.length();
18+
}
19+
return result;
20+
}), "replace");
21+
22+
// string::replace(char, char)
23+
m->add(fun([](const std::string& subject, char search, char replace) {
24+
std::string result(subject);
25+
std::replace(result.begin(), result.end(), search, replace);
26+
return result;
27+
}), "replace");
28+
29+
30+
// string::trim()
31+
m->add(fun([](const std::string& subject) {
32+
std::string result(subject);
33+
std::string chars = "\t\n\v\f\r ";
34+
result.erase(0, result.find_first_not_of(chars));
35+
result.erase(0, result.find_last_not_of(chars));
36+
return result;
37+
}), "trim");
38+
39+
// string::split()
40+
m->add(fun([](const std::string& subject, const std::string& token) {
41+
std::string str(subject);
42+
std::vector<std::string> result;
43+
while (str.size()) {
44+
int index = str.find(token);
45+
if (index != std::string::npos) {
46+
result.push_back(str.substr(0, index));
47+
str = str.substr(index + token.size());
48+
if (str.size() == 0) {
49+
result.push_back(str);
50+
}
51+
} else {
52+
result.push_back(str);
53+
str = "";
54+
}
55+
}
56+
return result;
57+
}), "split");
58+
59+
return m;
60+
}
61+
}
62+
}
63+
}

tests/string_methods.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
2+
#include <string>
3+
#include "catch.hpp"
4+
5+
#include <chaiscript/chaiscript.hpp>
6+
#include <chaiscript/chaiscript_stdlib.hpp>
7+
#include "../include/chaiscript/extras/string_methods.hpp"
8+
9+
#include <iostream>
10+
11+
TEST_CASE( "Math functions work", "[math]" ) {
12+
auto stringmethods = chaiscript::extras::string_methods::bootstrap();
13+
14+
chaiscript::ChaiScript chai;
15+
chai.add(stringmethods);
16+
17+
CHECK(chai.eval<std::string>("\" Hello World! \".trim()") == "Hello World!");
18+
}

0 commit comments

Comments
 (0)