From b977e76ad5f4581a824a3c01687dad1ad2b35898 Mon Sep 17 00:00:00 2001 From: Christian Kreibich Date: Mon, 28 Feb 2022 13:02:36 -0800 Subject: [PATCH] The is_num(), is_alpha(), and is_alnum() BiFs now return F on empty string --- NEWS | 2 ++ src/strings.bif | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index 0acb7f13d0..34c6e3c611 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,8 @@ New Functionality Changed Functionality --------------------- +- The is_num(), is_alpha(), and is_alnum() BiFs now return F for the empty string. + Deprecated Functionality ------------------------ diff --git a/src/strings.bif b/src/strings.bif index 7bc45996c1..11a38deaa3 100644 --- a/src/strings.bif +++ b/src/strings.bif @@ -1254,13 +1254,17 @@ function ends_with%(str: string, sub: string%) : bool return zeek::val_mgr->Bool(s.rfind(sub_s) == (s.size() - sub_s.size())); %} -## Returns whether an entire string consists only of digits. +## Returns whether a string consists entirely of digits. +## The empty string is not numeric. ## function is_num%(str: string%) : bool %{ // Python's version of this method (which this is based on) just checks to see if every // character in the string is a numeric value. If something more than this is desired, we // could use something like std::from_chars or std::strto{ul,f} to check it. + if ( str->Len() == 0 ) + return zeek::val_mgr->False(); + const char* s = str->CheckString(); for ( int i = 0; i < str->Len(); i++ ) if ( ! std::isdigit(s[i]) ) @@ -1269,10 +1273,14 @@ function is_num%(str: string%) : bool return zeek::val_mgr->True(); %} -## Returns whether an entire string is alphabetic characters. +## Returns whether a string consists entirely of alphabetic characters. +## The empty string is not alphabetic. ## function is_alpha%(str: string%) : bool %{ + if ( str->Len() == 0 ) + return zeek::val_mgr->False(); + const char* s = str->CheckString(); for ( int i = 0; i < str->Len(); i++ ) if ( ! std::isalpha(s[i]) ) @@ -1281,10 +1289,14 @@ function is_alpha%(str: string%) : bool return zeek::val_mgr->True(); %} -## Returns whether an entire string is alphanumeric characters +## Returns whether a string consists entirely of alphanumeric characters. +## The empty string is not alphanumeric. ## function is_alnum%(str: string%) : bool %{ + if ( str->Len() == 0 ) + return zeek::val_mgr->False(); + const char* s = str->CheckString(); for ( int i = 0; i < str->Len(); i++ ) if ( ! std::isalnum(s[i]) )