Add string::trim()

This commit is contained in:
Rob Loach 2018-10-10 23:51:00 -04:00
parent 7c10d261ab
commit aaa88afab9
No known key found for this signature in database
GPG Key ID: 627C60834A74A21A
6 changed files with 36 additions and 3 deletions

View File

@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixes
- Fixed save_dir mounting
### Features
- Added `string::trim()`
## 0.28.0 - 2018-10-07
### Features
- `love.timer.step()` now returns `dt`

View File

@ -100,6 +100,18 @@ 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!"
*/
std::string trim();
};
#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,6 +103,7 @@ script::script(const std::string& file) {
}
return newSubject;
}), "replace");
// string::replace(char search, char replace)
chai.add(fun([](const std::string& subject, char search, char replace) {
std::string newSubject(subject);
@ -109,6 +111,15 @@ script::script(const std::string& file) {
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");
// List
auto listModule = std::make_shared<chaiscript::Module>();
chaiscript::bootstrap::standard_library::list_type<std::list<chaiscript::Boxed_Value> >("List", *listModule);
@ -330,9 +341,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,7 @@ 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()")

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()")