Skip to content

Commit

Permalink
Fixes zeek#4076; update BIFs to_int() and to_count() to accept option…
Browse files Browse the repository at this point in the history
…al 'base' argument for more easy conversions of strings representing (for example) hexadecimal, octal, or binary numbers
  • Loading branch information
Mark Overholser authored and Mark Overholser committed Feb 15, 2025
1 parent 9c41b6d commit 0b81bf5
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/zeek.bif
Original file line number Diff line number Diff line change
Expand Up @@ -2606,12 +2606,12 @@ function enum_to_int%(e: any%): int
## Returns: The :zeek:type:`string` *str* as :zeek:type:`int`.
##
## .. zeek:see:: to_addr to_port to_subnet
function to_int%(str: string%): int
function to_int%(str: string, base: count &default=10%): int
%{
const char* s = str->CheckString();
char* end_s;

zeek_int_t i = strtoll(s, &end_s, 10);
zeek_int_t i = strtoll(s, &end_s, base);

#if 0
// Not clear we should complain. For example, is " 205 "
Expand Down Expand Up @@ -2675,12 +2675,12 @@ function double_to_count%(d: double%): count
## an invalid format.
##
## .. zeek:see:: to_addr to_int to_port to_subnet
function to_count%(str: string%): count
function to_count%(str: string, base: count &default=10%): count
%{
const char* s = str->CheckString();
char* end_s;

uint64_t u = (uint64_t) strtoull(s, &end_s, 10);
uint64_t u = (uint64_t) strtoull(s, &end_s, base);

if ( s[0] == '\0' || end_s[0] != '\0' )
{
Expand Down
3 changes: 3 additions & 0 deletions testing/btest/Baseline/bifs.to_count/out
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
7
0
18446744073709551611
172
35
195
0
123
9223372036854775808 and 9223372036854775808 are the same
3 changes: 3 additions & 0 deletions testing/btest/Baseline/bifs.to_int/out
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
1
-1
188
39
243
4294967296
0
3
Expand Down
3 changes: 3 additions & 0 deletions testing/btest/bifs/to_count.zeek
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ event zeek_init()
print to_count("7");
print to_count("");
print to_count("-5");
print to_int("10101100", 2);
print to_int("43", 8);
print to_int("C3", 16);
print to_count("not a count");

local e: port = 123/tcp;
Expand Down
3 changes: 3 additions & 0 deletions testing/btest/bifs/to_int.zeek
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ event zeek_init()
{
print to_int("1");
print to_int("-1");
print to_int("10111100", 2);
print to_int("47", 8);
print to_int("F3", 16);
print to_int("4294967296");
print to_int("not an int");

Expand Down

0 comments on commit 0b81bf5

Please sign in to comment.