Merge pull request #331 from libretro/string--trim

Add string functions
This commit is contained in:
Rob Loach 2018-10-12 18:01:23 -04:00 committed by GitHub
commit f2aa29c54f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 73 additions and 5 deletions

View File

@ -6,7 +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

@ -100,6 +100,24 @@ class String {
* @endcode
*/
std::string replace(const std::string& search, const std::string& replace);
/**
* Returns a trimmed version of the given string.
*
* @return A new string with trimmed left and right.
*
* @code
* var hello = " Hello World! "
* var result = hello.trim()
* // => "Hello World!"
* @endcode
*/
std::string trim();
/**
* Splits a string by the given token.
*/
std::string split(const std::string& token);
};
#endif // SRC_CHAILOVEDOCS_H_

View File

@ -76,6 +76,7 @@ chaiscript::Boxed_Value script::eval(const std::string& code, const std::string&
std::string contents = replaceString(code, "\t", " ");
return chai.eval(contents, Exception_Handler(), filename);
}
std::string script::evalString(const std::string& code, const std::string& filename) {
// Replace possible problematic tabs, and evaluate the script.
std::string contents = replaceString(code, "\t", " ");
@ -102,13 +103,43 @@ 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()
chai.add(fun([](const std::string& subject) {
std::string result(subject);
std::string chars = "\t\n\v\f\r ";
result.erase(0, result.find_first_not_of(chars));
result.erase(0, result.find_last_not_of(chars));
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);
@ -330,9 +361,9 @@ script::script(const std::string& file) {
chai.add(fun(&system::getVersion), "getVersion");
chai.add(fun(&system::getVersionString), "getVersionString");
chai.add(fun(&system::getUsername), "getUsername");
chai.add(fun(&system::execute), "execute");
chai.add(fun(&system::getClipboardText), "getClipboardText");
chai.add(fun(&system::setClipboardText), "setClipboardText");
chai.add(fun(&system::execute), "execute");
// Mouse
chai.add(fun(&mouse::getX), "getX");

View File

@ -96,7 +96,9 @@ class system {
/**
* Execute an operating system shell command. This is like the C system() function.
*
* @return True or False depending on whether or not the command started properly.
* @param command The command to run.
*
* @return Returns true or false depending on the process succeeded to execute.
*/
bool execute(const std::string& command);

View File

@ -16,6 +16,7 @@ assert(true, "love.system.getUsername() == '" + username + "'")
// getOS()
if (love.system.getOS() == "Linux") {
// Run in foreground.
var result = love.system.execute("uname")
assert(result, "love.system.execute('uname')")
}
@ -32,3 +33,15 @@ var newReplaceString = replaceSubject.replace("World", "Space")
assert_equal(newReplaceString, "Hello Space! Hello Space!", "string::replace(string, string)")
newReplaceString = replaceSubject.replace('!', '.')
assert_equal(newReplaceString, "Hello World. Hello World.", "string::replace(char, char)")
// 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")

View File

@ -4,4 +4,4 @@ assert(delta >= 0, "love.timer.getDelta()")
// getFPS()
var fps = love.timer.getFPS()
assert(fps >= 0, "love.love.timer.getFPS()")
assert(fps >= 0, "love.timer.getFPS()")