Rework starts_with BIF similarly to ends_with changes in 1649e3e7cc

This commit is contained in:
Tim Wojtulewicz 2024-01-16 15:09:54 -07:00 committed by Tim Wojtulewicz
parent 822ca99e80
commit 94ad676db0

View file

@ -1355,8 +1355,14 @@ function rfind_str%(str: string, sub: string, start: count &default=0, end: int
##
function starts_with%(str: string, sub: string%) : bool
%{
string s = str->ToStdString();
return zeek::val_mgr->Bool(s.find(sub->ToStdString()) == 0);
if ( sub->Len() > str->Len() )
return zeek::val_mgr->Bool(false);
auto sub_s = sub->ToStdStringView();
auto s = str->ToStdStringView();
auto start_s = std::string_view{s.data(), sub_s.size()};
return zeek::val_mgr->Bool(start_s == sub_s);
%}
## Returns whether a string ends with a substring.