diff --git a/src/ivoc/strfun.cpp b/src/ivoc/strfun.cpp index 4c1358fa50..cb5375eb82 100644 --- a/src/ivoc/strfun.cpp +++ b/src/ivoc/strfun.cpp @@ -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()); @@ -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()); diff --git a/test/unit_tests/hoc_python/test_StringFunctions.py b/test/unit_tests/hoc_python/test_StringFunctions.py index 0cfcb44dc4..ecea51955d 100644 --- a/test/unit_tests/hoc_python/test_StringFunctions.py +++ b/test/unit_tests/hoc_python/test_StringFunctions.py @@ -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)