Skip to content

Commit 6f6b595

Browse files
committed
Add string_methods
1 parent b617c59 commit 6f6b595

File tree

4 files changed

+111
-63
lines changed

4 files changed

+111
-63
lines changed

CMakeLists.txt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,14 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
175175
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}")
176176
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}")
177177

178-
179-
180-
181-
182-
set(CHAISCRIPT_BRANCH v6.1.0)
183-
178+
# ChaiScript
179+
set(CHAISCRIPT_BRANCH v5.8.6)
180+
set(CHAISCRIPT_VERSION 5.8.6)
184181
file(DOWNLOAD https://github.com/ChaiScript/ChaiScript/archive/${CHAISCRIPT_BRANCH}.tar.gz "${CMAKE_BINARY_DIR}/chaiscript/chaiscript-${CHAISCRIPT_BRANCH}.tar.gz"
185182
INACTIVITY_TIMEOUT 180 TIMEOUT 180 TLS_VERIFY off)
186-
187183
execute_process(COMMAND ${CMAKE_COMMAND} -E tar -xf "${CMAKE_BINARY_DIR}/chaiscript/chaiscript-${CHAISCRIPT_BRANCH}.tar.gz" "${CMAKE_BINARY_DIR}/chaiscript"
188184
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/chaiscript")
189-
190-
include_directories("${CMAKE_BINARY_DIR}/chaiscript/ChaiScript-${CHAISCRIPT_BRANCH}/include")
191-
185+
include_directories("${CMAKE_BINARY_DIR}/chaiscript/ChaiScript-${CHAISCRIPT_VERSION}/include")
192186

193187
# Add catch tests macro
194188
macro(ADD_CATCH_TESTS executable)

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
In this repository will live user contributed wrappers
2-
for ChaiScript.
1+
# ChaiScript Extras
32

3+
User contributed wrappers for ChaiScript.
44

5+
## Modules
6+
7+
- Math: Adds common math methods to ChaiScript.
8+
- String ID: String hashing with [string_id](https://github.com/foonathan/string_id)
9+
- String Methods: Introduces some extra string methods to ChaiScript strings

include/chaiscript/extras/string_methods.hpp

Lines changed: 80 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
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>
114
#include <string>
215
#include <vector>
316

@@ -8,53 +21,73 @@ namespace chaiscript {
821
namespace string_methods {
922
ModulePtr bootstrap(ModulePtr m = std::make_shared<Module>())
1023
{
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");
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");
5891

5992
return m;
6093
}

tests/string_methods.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,29 @@
66
#include <chaiscript/chaiscript_stdlib.hpp>
77
#include "../include/chaiscript/extras/string_methods.hpp"
88

9-
#include <iostream>
9+
TEST_CASE( "string_methods functions work", "[string_methods]" ) {
10+
auto stdlib = chaiscript::Std_Lib::library();
11+
chaiscript::ChaiScript chai(stdlib);
1012

11-
TEST_CASE( "Math functions work", "[math]" ) {
13+
// Add the string_methods module.
1214
auto stringmethods = chaiscript::extras::string_methods::bootstrap();
13-
14-
chaiscript::ChaiScript chai;
1515
chai.add(stringmethods);
1616

17+
// replace(string, string)
18+
CHECK(chai.eval<std::string>("\"Hello World!\".replace(\"Hello\", \"Goodbye\")") == "Goodbye World!");
19+
20+
// replace(char, char)
21+
CHECK(chai.eval<std::string>("\"Hello World!\".replace('e', 'i')") == "Hillo World!");
22+
23+
// trim()
1724
CHECK(chai.eval<std::string>("\" Hello World! \".trim()") == "Hello World!");
25+
26+
// split()
27+
CHECK(chai.eval<std::string>("\"Hello,World,How,Are,You\".split(\",\")[1]") == "World");
28+
29+
// toLowerCase()
30+
CHECK(chai.eval<std::string>("\"HeLLO WoRLD!\".toLowerCase()") == "hello world!");
31+
32+
// toUpperCase()
33+
CHECK(chai.eval<std::string>("\"Hello World!\".toUpperCase()") == "HELLO WORLD!");
1834
}

0 commit comments

Comments
 (0)