Convert remove_prefix/suffix BIFs to use std::string_view

This commit is contained in:
Tim Wojtulewicz 2024-01-16 15:11:43 -07:00 committed by Tim Wojtulewicz
parent 94ad676db0
commit c77f8cc898
3 changed files with 15 additions and 8 deletions

View file

@ -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<zeek::StringVal>(NewRef{}, str);
while ( pos == next_pos )
{
next_pos -= sub_s.size();