Skip to content

Commit

Permalink
Change string.trimCharacters to specify different leading and trailin…
Browse files Browse the repository at this point in the history
…g trim sets
  • Loading branch information
ghewgill committed Oct 11, 2018
1 parent 44caff8 commit b4d2ba5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/string.cpp
Expand Up @@ -92,11 +92,11 @@ std::vector<utf8string> splitLines(const utf8string &ss)
return r;
}

utf8string trimCharacters(const utf8string &ss, const utf8string &trimChars)
utf8string trimCharacters(const utf8string &ss, const utf8string &trimLeadingChars, const utf8string &trimTrailingChars)
{
const std::string &s = ss.str(); // TODO: utf8
std::string::size_type first = s.find_first_not_of(trimChars.str());
std::string::size_type last = s.find_last_not_of(trimChars.str());
std::string::size_type first = s.find_first_not_of(trimLeadingChars.str());
std::string::size_type last = s.find_last_not_of(trimTrailingChars.str());
if (first == std::string::npos || last == std::string::npos) {
return utf8string("");
}
Expand Down
4 changes: 2 additions & 2 deletions lib/string.neon
Expand Up @@ -106,15 +106,15 @@ DECLARE NATIVE FUNCTION splitLines(s: String): Array<String>
|
| Trim given characters from the start and end of a string.
|%
DECLARE NATIVE FUNCTION trimCharacters(s: String, trimChars: String): String
DECLARE NATIVE FUNCTION trimCharacters(s: String, trimLeadingChars, trimTrailingChars: String): String

%|
| Function: trim
|
| Trim spaces from the start and end of a string.
|%
FUNCTION trim(s: String): String
RETURN trimCharacters(s, " ")
RETURN trimCharacters(s, " ", " ")
END FUNCTION

%|
Expand Down
10 changes: 8 additions & 2 deletions t/string-test.neon
Expand Up @@ -27,10 +27,16 @@ ASSERT string.splitLines("a\nb") = ["a", "b"]
ASSERT string.splitLines("a\nb\n") = ["a", "b"]
ASSERT string.splitLines("hello\nworld\r\neverybody\n") = ["hello", "world", "everybody"]

print(string.trimCharacters(" Hello World ", " "))
print(string.trimCharacters(" Hello World ", " ", " "))
%= Hello World

print(string.trimCharacters(" ", " "))
print(string.trimCharacters(" Hello World ", " ", ""))
%= Hello World

print(string.trimCharacters(" Hello World ", "", " "))
%= Hello World

print(string.trimCharacters(" ", " ", " "))
%=

print(string.upper("Hello World"))
Expand Down
4 changes: 2 additions & 2 deletions tools/helium.py
Expand Up @@ -2650,8 +2650,8 @@ def neon_string_splitLines(env, s):
s = re.sub("\n$", "", s, 1)
return s.split("\n")

def neon_string_trimCharacters(env, s, t):
return s.strip(t)
def neon_string_trimCharacters(env, s, leading, trailing):
return s.lstrip(leading).rstrip(trailing)

def neon_string_upper(env, s):
return s.upper()
Expand Down

0 comments on commit b4d2ba5

Please sign in to comment.