Verified Commit e6e69a70 authored by RobLoach's avatar RobLoach
Browse files

Add string::split()

parent aaa88afa
...@@ -6,10 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ...@@ -6,10 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## 0.28.1 - Unreleased ## 0.28.1 - Unreleased
### Fixes ### Fixes
- Fixed save_dir mounting - Fixed `/libretro/saves` mounting
### Features ### Features
- Added `string::trim()` - Added `string::trim()`
- Added `string::split()`
## 0.28.0 - 2018-10-07 ## 0.28.0 - 2018-10-07
### Features ### Features
......
...@@ -104,14 +104,14 @@ script::script(const std::string& file) { ...@@ -104,14 +104,14 @@ script::script(const std::string& file) {
return newSubject; return newSubject;
}), "replace"); }), "replace");
// string::replace(char search, char replace) // string::replace(char search, char replace)
chai.add(fun([](const std::string& subject, char search, char replace) { chai.add(fun([](const std::string& subject, char search, char replace) {
std::string newSubject(subject); std::string newSubject(subject);
std::replace(newSubject.begin(), newSubject.end(), search, replace); std::replace(newSubject.begin(), newSubject.end(), search, replace);
return newSubject; return newSubject;
}), "replace"); }), "replace");
// string::trim() // string::trim()
chai.add(fun([](const std::string& subject) { chai.add(fun([](const std::string& subject) {
std::string result(subject); std::string result(subject);
std::string chars = "\t\n\v\f\r "; std::string chars = "\t\n\v\f\r ";
...@@ -120,6 +120,26 @@ script::script(const std::string& file) { ...@@ -120,6 +120,26 @@ script::script(const std::string& file) {
return result; return result;
}), "trim"); }), "trim");
// string::split()
chai.add(fun([](const std::string& subject, const std::string& token) {
std::string str(subject);
std::vector<std::string> result;
while (str.size()) {
int index = str.find(token);
if (index != std::string::npos) {
result.push_back(str.substr(0, index));
str = str.substr(index + token.size());
if (str.size() == 0) {
result.push_back(str);
}
} else {
result.push_back(str);
str = "";
}
}
return result;
}), "split");
// List // List
auto listModule = std::make_shared<chaiscript::Module>(); auto listModule = std::make_shared<chaiscript::Module>();
chaiscript::bootstrap::standard_library::list_type<std::list<chaiscript::Boxed_Value> >("List", *listModule); chaiscript::bootstrap::standard_library::list_type<std::list<chaiscript::Boxed_Value> >("List", *listModule);
......
...@@ -37,3 +37,11 @@ assert_equal(newReplaceString, "Hello World. Hello World.", "string::replace(cha ...@@ -37,3 +37,11 @@ assert_equal(newReplaceString, "Hello World. Hello World.", "string::replace(cha
// string::trim() // string::trim()
var trimSubject = " Hello World! " var trimSubject = " Hello World! "
assert_equal(trimSubject.trim(), "Hello World!", "string::trim()") assert_equal(trimSubject.trim(), "Hello World!", "string::trim()")
// string::split()
var splitTest = "Hello|How|Are|You"
var splitResult = splitTest.split("|")
assert_equal(splitResult[1], "How", "string::split()")
splitTest = "Rob, John, Loach"
splitResult = splitTest.split(", ")
assert_equal(splitResult[1], "John", " - commas")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment