From c1ac797dfc4d366fc4ce95f32d57c0827cd35b3c Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Wed, 8 Nov 2023 17:19:12 +0100 Subject: [PATCH] Add ltrim/rtrim hoc functions to avoid some regex (#2603) --- azure-pipelines.yml | 2 +- src/ivoc/strfun.cpp | 28 +++++++++++++++++++ .../hoc_python/test_StringFunctions.py | 20 +++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index f98807a7f2..56de36855f 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -81,7 +81,7 @@ stages: # Jobs to build OSX wheels natively - job: 'MacOSWheels' - timeoutInMinutes: 45 + timeoutInMinutes: 60 pool: vmImage: 'macOS-11' strategy: diff --git a/src/ivoc/strfun.cpp b/src/ivoc/strfun.cpp index 5e8fd7192c..f21820b2d8 100644 --- a/src/ivoc/strfun.cpp +++ b/src/ivoc/strfun.cpp @@ -68,6 +68,32 @@ static double l_tail(void*) { return double(i); } +static double l_ltrim(void*) { + std::string s(gargstr(1)); + 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()); + return 0.; +} + +static double l_rtrim(void*) { + std::string s(gargstr(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()); + return 0.; +} + static double l_left(void*) { std::string text(gargstr(1)); std::string newtext = text.substr(0, int(chkarg(2, 0, strlen(gargstr(1))))); @@ -311,6 +337,8 @@ static Member_func l_members[] = {{"substr", l_substr}, {"len", l_len}, {"head", l_head}, {"tail", l_tail}, + {"ltrim", l_ltrim}, + {"rtrim", l_rtrim}, {"right", l_right}, {"left", l_left}, {"is_name", l_is_name}, 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)