Add string::split()

This commit is contained in:
Rob Loach 2018-10-11 00:04:54 -04:00
parent aaa88afab9
commit e6e69a705a
No known key found for this signature in database
GPG Key ID: 627C60834A74A21A
3 changed files with 32 additions and 3 deletions

View File

@ -6,10 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## 0.28.1 - Unreleased
### Fixes
- Fixed save_dir mounting
- Fixed `/libretro/saves` mounting
### Features
- Added `string::trim()`
- Added `string::split()`
## 0.28.0 - 2018-10-07
### Features

View File

@ -104,14 +104,14 @@ script::script(const std::string& file) {
return newSubject;
}), "replace");
// string::replace(char search, char replace)
// string::replace(char search, char replace)
chai.add(fun([](const std::string& subject, char search, char replace) {
std::string newSubject(subject);
std::replace(newSubject.begin(), newSubject.end(), search, replace);
return newSubject;
}), "replace");
// string::trim()
// string::trim()
chai.add(fun([](const std::string& subject) {
std::string result(subject);
std::string chars = "\t\n\v\f\r ";
@ -120,6 +120,26 @@ script::script(const std::string& file) {
return result;
}), "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
auto listModule = std::make_shared<chaiscript::Module>();
chaiscript::bootstrap::standard_library::list_type<std::list<chaiscript::Boxed_Value> >("List", *listModule);

View File

@ -37,3 +37,11 @@ assert_equal(newReplaceString, "Hello World. Hello World.", "string::replace(cha
// string::trim()
var trimSubject = " Hello World! "
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")