Unverified Commit aaa88afa authored by RobLoach's avatar RobLoach
Browse files

Add string::trim()

parent 7c10d261
...@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ...@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixes ### Fixes
- Fixed save_dir mounting - Fixed save_dir mounting
### Features
- Added `string::trim()`
## 0.28.0 - 2018-10-07 ## 0.28.0 - 2018-10-07
### Features ### Features
- `love.timer.step()` now returns `dt` - `love.timer.step()` now returns `dt`
......
...@@ -100,6 +100,18 @@ class String { ...@@ -100,6 +100,18 @@ class String {
* @endcode * @endcode
*/ */
std::string replace(const std::string& search, const std::string& replace); 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_ #endif // SRC_CHAILOVEDOCS_H_
...@@ -76,6 +76,7 @@ chaiscript::Boxed_Value script::eval(const std::string& code, const std::string& ...@@ -76,6 +76,7 @@ chaiscript::Boxed_Value script::eval(const std::string& code, const std::string&
std::string contents = replaceString(code, "\t", " "); std::string contents = replaceString(code, "\t", " ");
return chai.eval(contents, Exception_Handler(), filename); return chai.eval(contents, Exception_Handler(), filename);
} }
std::string script::evalString(const std::string& code, const std::string& filename) { std::string script::evalString(const std::string& code, const std::string& filename) {
// Replace possible problematic tabs, and evaluate the script. // Replace possible problematic tabs, and evaluate the script.
std::string contents = replaceString(code, "\t", " "); std::string contents = replaceString(code, "\t", " ");
...@@ -102,6 +103,7 @@ script::script(const std::string& file) { ...@@ -102,6 +103,7 @@ 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);
...@@ -109,6 +111,15 @@ script::script(const std::string& file) { ...@@ -109,6 +111,15 @@ script::script(const std::string& file) {
return newSubject; return newSubject;
}), "replace"); }), "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 // 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);
...@@ -330,9 +341,9 @@ script::script(const std::string& file) { ...@@ -330,9 +341,9 @@ script::script(const std::string& file) {
chai.add(fun(&system::getVersion), "getVersion"); chai.add(fun(&system::getVersion), "getVersion");
chai.add(fun(&system::getVersionString), "getVersionString"); chai.add(fun(&system::getVersionString), "getVersionString");
chai.add(fun(&system::getUsername), "getUsername"); chai.add(fun(&system::getUsername), "getUsername");
chai.add(fun(&system::execute), "execute");
chai.add(fun(&system::getClipboardText), "getClipboardText"); chai.add(fun(&system::getClipboardText), "getClipboardText");
chai.add(fun(&system::setClipboardText), "setClipboardText"); chai.add(fun(&system::setClipboardText), "setClipboardText");
chai.add(fun(&system::execute), "execute");
// Mouse // Mouse
chai.add(fun(&mouse::getX), "getX"); chai.add(fun(&mouse::getX), "getX");
......
...@@ -96,7 +96,9 @@ class system { ...@@ -96,7 +96,9 @@ class system {
/** /**
* Execute an operating system shell command. This is like the C system() function. * 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); bool execute(const std::string& command);
......
...@@ -16,6 +16,7 @@ assert(true, "love.system.getUsername() == '" + username + "'") ...@@ -16,6 +16,7 @@ assert(true, "love.system.getUsername() == '" + username + "'")
// getOS() // getOS()
if (love.system.getOS() == "Linux") { if (love.system.getOS() == "Linux") {
// Run in foreground.
var result = love.system.execute("uname") var result = love.system.execute("uname")
assert(result, "love.system.execute('uname')") assert(result, "love.system.execute('uname')")
} }
...@@ -32,3 +33,7 @@ var newReplaceString = replaceSubject.replace("World", "Space") ...@@ -32,3 +33,7 @@ var newReplaceString = replaceSubject.replace("World", "Space")
assert_equal(newReplaceString, "Hello Space! Hello Space!", "string::replace(string, string)") assert_equal(newReplaceString, "Hello Space! Hello Space!", "string::replace(string, string)")
newReplaceString = replaceSubject.replace('!', '.') newReplaceString = replaceSubject.replace('!', '.')
assert_equal(newReplaceString, "Hello World. Hello World.", "string::replace(char, char)") 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()")
...@@ -4,4 +4,4 @@ assert(delta >= 0, "love.timer.getDelta()") ...@@ -4,4 +4,4 @@ assert(delta >= 0, "love.timer.getDelta()")
// getFPS() // getFPS()
var fps = love.timer.getFPS() var fps = love.timer.getFPS()
assert(fps >= 0, "love.love.timer.getFPS()") assert(fps >= 0, "love.timer.getFPS()")
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