mirror of
https://github.com/libretro/libretro-chailove.git
synced 2024-12-17 21:37:20 +00:00
Add string::split()
This commit is contained in:
parent
aaa88afab9
commit
e6e69a705a
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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")
|
||||
|
Loading…
Reference in New Issue
Block a user