mirror of
https://github.com/zeek/zeek.git
synced 2025-10-10 10:38:20 +00:00
Convert remove_prefix/suffix BIFs to use std::string_view
This commit is contained in:
parent
94ad676db0
commit
c77f8cc898
3 changed files with 15 additions and 8 deletions
|
@ -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:
|
// 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
|
// 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.
|
// 2) lstrip() searches for any character in the string, not the string as a whole.
|
||||||
string s = str->ToStdString();
|
auto s = str->ToStdStringView();
|
||||||
string sub_s = sub->ToStdString();
|
auto sub_s = sub->ToStdStringView();
|
||||||
|
|
||||||
size_t pos = s.find(sub_s);
|
size_t pos = s.find(sub_s);
|
||||||
if ( pos != 0 )
|
if ( pos != 0 )
|
||||||
|
@ -1584,12 +1584,15 @@ function remove_prefix%(str: string, sub: string%) : string
|
||||||
function remove_suffix%(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.
|
// See the note in removeprefix for why this doesn't just call rstrip.
|
||||||
string s = str->ToStdString();
|
auto s = str->ToStdStringView();
|
||||||
string sub_s = sub->ToStdString();
|
auto sub_s = sub->ToStdStringView();
|
||||||
|
|
||||||
size_t pos = s.rfind(sub_s);
|
size_t pos = s.rfind(sub_s);
|
||||||
size_t next_pos = s.size() - sub_s.size();
|
size_t next_pos = s.size() - sub_s.size();
|
||||||
|
|
||||||
|
if ( pos != next_pos )
|
||||||
|
return zeek::IntrusivePtr<zeek::StringVal>(NewRef{}, str);
|
||||||
|
|
||||||
while ( pos == next_pos )
|
while ( pos == next_pos )
|
||||||
{
|
{
|
||||||
next_pos -= sub_s.size();
|
next_pos -= sub_s.size();
|
||||||
|
|
|
@ -49,10 +49,12 @@ swap_case 'aBc': AbC
|
||||||
to_title 'bro is a very neat ids': 'Bro Is A Very Neat Ids'
|
to_title 'bro is a very neat ids': 'Bro Is A Very Neat Ids'
|
||||||
to_title ' ': ' '
|
to_title ' ': ' '
|
||||||
to_title ' a c ': ' A C '
|
to_title ' a c ': ' A C '
|
||||||
remove_prefix 'ananab'/'an' : ab
|
remove_prefix 'banana'/'ba' : nana
|
||||||
remove_prefix 'anatnab'/'an': atnab
|
remove_prefix 'bantana'/'ba': ntana
|
||||||
|
remove_prefix 'bantana'/'ab': bantana
|
||||||
remove_suffix 'banana'/'na' : ba
|
remove_suffix 'banana'/'na' : ba
|
||||||
remove_suffix 'bantana'/'na': banta
|
remove_suffix 'bantana'/'na': banta
|
||||||
|
remove_suffix 'bantana'/'an': bantana
|
||||||
|
|
||||||
find_str/rfind_str (input string 'abcdefghi')
|
find_str/rfind_str (input string 'abcdefghi')
|
||||||
-----------------------------------------------------
|
-----------------------------------------------------
|
||||||
|
|
|
@ -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 '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 ' ': '%s'", to_title(" "));
|
||||||
print fmt("to_title ' a c ': '%s'", to_title(" a c "));
|
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 'banana'/'ba' : %s", remove_prefix("banana", "ba"));
|
||||||
print fmt("remove_prefix 'anatnab'/'an': %s", remove_prefix("anatnab", "an"));
|
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 'banana'/'na' : %s", remove_suffix("banana", "na"));
|
||||||
print fmt("remove_suffix 'bantana'/'na': %s", remove_suffix("bantana", "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 "";
|
||||||
|
|
||||||
print fmt("find_str/rfind_str (input string '%s')", s3);
|
print fmt("find_str/rfind_str (input string '%s')", s3);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue