Skip to content

Commit

Permalink
Add optional chars
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino committed Nov 7, 2023
1 parent ecf4d32 commit 5ca689e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/ivoc/strfun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ static double l_tail(void*) {

static double l_ltrim(void*) {
std::string s(gargstr(1));
s.erase(0, s.find_first_not_of(" \r\n\t\f\v"));
std::string chars = " \r\n\t\f\v";
if (ifarg(3)) {
chars = gargstr(3);
}
s.erase(0, s.find_first_not_of(chars));

char** ret = hoc_pgargstr(2);
hoc_assign_str(ret, s.c_str());
Expand All @@ -100,7 +104,11 @@ static double l_ltrim(void*) {

static double l_rtrim(void*) {
std::string s(gargstr(1));
s.erase(s.find_last_not_of(" \r\n\t\f\v") + 1);
std::string chars = " \r\n\t\f\v";
if (ifarg(3)) {
chars = gargstr(3);
}
s.erase(s.find_last_not_of(chars) + 1);

char** ret = hoc_pgargstr(2);
hoc_assign_str(ret, s.c_str());
Expand Down
20 changes: 20 additions & 0 deletions test/unit_tests/hoc_python/test_StringFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ def test_tail():
assert tail[0] == ""


def text_rtrim():
text = "bar\t; \t\n"
out = h.ref("")
sf.rtrim(text, out)
assert out[0] == "bar\t;"

sf.rtrim(text, out, " \t\n\f\v\r;")
assert out[0] == "bar"


def test_ltrim():
text = " \t \n# foo"
out = h.ref("")
sf.ltrim(text, out)
assert out[0] == "# foo"

sf.ltrim(text, out, " \t\n\f\r\v#")
assert out[0] == "foo"


def test_right():
s = h.ref("foobarshi")
sf.right(s, 6)
Expand Down

0 comments on commit 5ca689e

Please sign in to comment.