diff --git a/src/strings.bif b/src/strings.bif index 90811a6a4e..ed574bbea5 100644 --- a/src/strings.bif +++ b/src/strings.bif @@ -1562,8 +1562,8 @@ function remove_prefix%(str: string, sub: string%) : string // This could just use repeated calls to lstrip(), except for a couple of reasons: // 1) lstrip() creates a StringVal at the end, and that would mean repeated recreation of objects // 2) lstrip() searches for any character in the string, not the string as a whole. - string s = str->ToStdString(); - string sub_s = sub->ToStdString(); + auto s = str->ToStdStringView(); + auto sub_s = sub->ToStdStringView(); size_t pos = s.find(sub_s); if ( pos != 0 ) @@ -1584,12 +1584,15 @@ function remove_prefix%(str: string, sub: string%) : string function remove_suffix%(str: string, sub: string%) : string %{ // See the note in removeprefix for why this doesn't just call rstrip. - string s = str->ToStdString(); - string sub_s = sub->ToStdString(); + auto s = str->ToStdStringView(); + auto sub_s = sub->ToStdStringView(); size_t pos = s.rfind(sub_s); size_t next_pos = s.size() - sub_s.size(); + if ( pos != next_pos ) + return zeek::IntrusivePtr(NewRef{}, str); + while ( pos == next_pos ) { next_pos -= sub_s.size(); diff --git a/testing/btest/Baseline/bifs.string_utils/out b/testing/btest/Baseline/bifs.string_utils/out index 4ff7df3070..6277a24be3 100644 --- a/testing/btest/Baseline/bifs.string_utils/out +++ b/testing/btest/Baseline/bifs.string_utils/out @@ -49,10 +49,12 @@ swap_case 'aBc': AbC to_title 'bro is a very neat ids': 'Bro Is A Very Neat Ids' to_title ' ': ' ' to_title ' a c ': ' A C ' -remove_prefix 'ananab'/'an' : ab -remove_prefix 'anatnab'/'an': atnab +remove_prefix 'banana'/'ba' : nana +remove_prefix 'bantana'/'ba': ntana +remove_prefix 'bantana'/'ab': bantana remove_suffix 'banana'/'na' : ba remove_suffix 'bantana'/'na': banta +remove_suffix 'bantana'/'an': bantana find_str/rfind_str (input string 'abcdefghi') ----------------------------------------------------- diff --git a/testing/btest/bifs/string_utils.zeek b/testing/btest/bifs/string_utils.zeek index d31051555a..9178f90d70 100644 --- a/testing/btest/bifs/string_utils.zeek +++ b/testing/btest/bifs/string_utils.zeek @@ -60,10 +60,12 @@ event zeek_init() print fmt("to_title 'bro is a very neat ids': '%s'", to_title("bro is a very neat ids")); print fmt("to_title ' ': '%s'", to_title(" ")); print fmt("to_title ' a c ': '%s'", to_title(" a c ")); - print fmt("remove_prefix 'ananab'/'an' : %s", remove_prefix("ananab", "an")); - print fmt("remove_prefix 'anatnab'/'an': %s", remove_prefix("anatnab", "an")); + print fmt("remove_prefix 'banana'/'ba' : %s", remove_prefix("banana", "ba")); + print fmt("remove_prefix 'bantana'/'ba': %s", remove_prefix("bantana", "ba")); + print fmt("remove_prefix 'bantana'/'ab': %s", remove_prefix("bantana", "ab")); print fmt("remove_suffix 'banana'/'na' : %s", remove_suffix("banana", "na")); print fmt("remove_suffix 'bantana'/'na': %s", remove_suffix("bantana", "na")); + print fmt("remove_suffix 'bantana'/'an': %s", remove_suffix("bantana", "an")); print ""; print fmt("find_str/rfind_str (input string '%s')", s3);