Skip to content

Commit

Permalink
Add ltrim/rtrim hoc functions to avoid some regex (#2603)
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino authored Nov 8, 2023
1 parent fa132e4 commit c1ac797
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ stages:

# Jobs to build OSX wheels natively
- job: 'MacOSWheels'
timeoutInMinutes: 45
timeoutInMinutes: 60
pool:
vmImage: 'macOS-11'
strategy:
Expand Down
28 changes: 28 additions & 0 deletions src/ivoc/strfun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))));
Expand Down Expand Up @@ -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},
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 c1ac797

Please sign in to comment.