Merge remote-tracking branch 'origin/topic/timw/178-string-functions'

* origin/topic/timw/178-string-functions:
  GH-178: Add new string bif methods based on python string utilities
This commit is contained in:
Tim Wojtulewicz 2020-08-14 10:00:07 -07:00
commit b89935107d
6 changed files with 469 additions and 3 deletions

View file

@ -0,0 +1,66 @@
Justification (input string 'abc')
----------------------------------
ljust: 'abc'
ljust: 'abc'
ljust: 'abc '
ljust: 'abc--'
rjust: 'abc'
rjust: 'abc'
rjust: ' abc'
rjust: '--abc'
zfill: 'abc'
zfill: 'abc'
zfill: '00abc'
Content checking
----------------
is_num abc : 0
is_num 123 : 1
is_alpha ab : 1
is_alpha 1a : 0
is_alpha a1 : 0
is_alnum ab : 1
is_alnum 1a : 1
is_alnum a1 : 1
is_alnum 12 : 1
is_alnum ##12: 0
String counting (input str 'aabbaa')
------------------------------------
count_substr aa: 2
count_substr bb: 1
count_substr cc: 0
Starts/endswith
---------------
starts_with bro: 1
starts_with ids: 0
ends_with ids: 1
ends_with bro: 0
Transformations
---------------
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_suffix 'banana'/'na' : ba
remove_suffix 'bantana'/'na': banta
find_str/rfind_str (input string 'abcdefghi')
-----------------------------------------------------
find_str: 0
find_str: -1
find_str: -1
find_str: 4
find_str: 4
find_str: -1
find_str: 0
find_str: -1
find_str: -1
find_str: 4
find_str: 4
find_str: -1

View file

@ -0,0 +1,82 @@
# @TEST-EXEC: zeek -b %INPUT >out
# @TEST-EXEC: btest-diff out
event zeek_init()
{
print "Justification (input string 'abc')";
print "----------------------------------";
local s1 : string = "abc";
print fmt("ljust: '%s'", ljust(s1, 2, " ")); # 'abc'
print fmt("ljust: '%s'", ljust(s1, 3, " ")); # 'abc'
print fmt("ljust: '%s'", ljust(s1, 5)); # 'abc '
print fmt("ljust: '%s'", ljust(s1, 5, "-")); # 'abc--'
print fmt("ljust: '%s'", ljust(s1, 2, "--")); # This should return an error
print fmt("rjust: '%s'", rjust(s1, 2, " ")); # 'abc'
print fmt("rjust: '%s'", rjust(s1, 3, " ")); # 'abc'
print fmt("rjust: '%s'", rjust(s1, 5)); # ' abc'
print fmt("rjust: '%s'", rjust(s1, 5, "-")); # '--abc'
print fmt("rjust: '%s'", rjust(s1, 2, "--")); # This should return an error
print fmt("zfill: '%s'", zfill(s1, 2)); # 'abc'
print fmt("zfill: '%s'", zfill(s1, 3)); # 'abc'
print fmt("zfill: '%s'", zfill(s1, 5)); # '00abc'
print "";
print "Content checking";
print "----------------";
print fmt("is_num abc : %d", is_num("abc"));
print fmt("is_num 123 : %d", is_num("123"));
print fmt("is_alpha ab : %d", is_alpha("ab"));
print fmt("is_alpha 1a : %d", is_alpha("1a"));
print fmt("is_alpha a1 : %d", is_alpha("a1"));
print fmt("is_alnum ab : %d", is_alnum("ab"));
print fmt("is_alnum 1a : %d", is_alnum("1a"));
print fmt("is_alnum a1 : %d", is_alnum("a1"));
print fmt("is_alnum 12 : %d", is_alnum("12"));
print fmt("is_alnum ##12: %d", is_alnum("##12"));
print "";
print "String counting (input str 'aabbaa')";
print "------------------------------------";
local s2 : string = "aabbaa";
print fmt("count_substr aa: %d", count_substr(s2, "aa"));
print fmt("count_substr bb: %d", count_substr(s2, "bb"));
print fmt("count_substr cc: %d", count_substr(s2, "cc"));
print "";
print "Starts/endswith";
print "---------------";
local s3: string = "abcdefghi";
print fmt("starts_with bro: %d", starts_with(s3, "abc"));
print fmt("starts_with ids: %d", starts_with(s3, "ghi"));
print fmt("ends_with ids: %d", ends_with(s3, "ghi"));
print fmt("ends_with bro: %d", ends_with(s3, "abc"));
print "";
print "Transformations";
print "---------------";
print fmt("swap_case 'aBc': %s", swap_case("aBc"));
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_suffix 'banana'/'na' : %s", remove_suffix("banana", "na"));
print fmt("remove_suffix 'bantana'/'na': %s", remove_suffix("bantana", "na"));
print "";
print fmt("find_str/rfind_str (input string '%s')", s3);
print "-----------------------------------------------------";
print fmt("find_str: %d", find_str(s3, "abcd"));
print fmt("find_str: %d", find_str(s3, "abcd", 1));
print fmt("find_str: %d", find_str(s3, "abcd", 0, 2));
print fmt("find_str: %d", find_str(s3, "efg"));
print fmt("find_str: %d", find_str(s3, "efg", 2, 6));
print fmt("find_str: %d", find_str(s3, "efg", 6, 2));
print fmt("find_str: %d", rfind_str(s3, "abcd"));
print fmt("find_str: %d", rfind_str(s3, "abcd", 1));
print fmt("find_str: %d", rfind_str(s3, "abcd", 0, 2));
print fmt("find_str: %d", rfind_str(s3, "efg"));
print fmt("find_str: %d", rfind_str(s3, "efg", 2, 6));
print fmt("find_str: %d", rfind_str(s3, "efg", 6, 2));
print "";
}