GH-1040: Add zero-indexed version of str_split

This commit is contained in:
Tim Wojtulewicz 2020-07-06 12:58:38 -07:00
parent e891c310fb
commit e6871ed3e9
7 changed files with 67 additions and 13 deletions

View file

@ -700,10 +700,10 @@ function str_smith_waterman%(s1: string, s2: string, params: sw_params%) : sw_su
##
## idx: The index vector (``vector of count``) with the cutting points.
##
## Returns: A vector of strings.
## Returns: A one-indexed vector of strings.
##
## .. zeek:see:: split_string split_string1 split_string_all split_string_n
function str_split%(s: string, idx: index_vec%): string_vec
function str_split%(s: string, idx: index_vec%): string_vec &deprecated="Remove in v4.1. Use str_split_indices."
%{
auto idx_v = idx->AsVector();
zeek::String::IdxVec indices(idx_v->size());
@ -730,6 +730,44 @@ function str_split%(s: string, idx: index_vec%): string_vec
return result_v;
%}
## Splits a string into substrings with the help of an index vector of cutting
## points. This differs from str_split() in that it does not return an empty element
## at the beginning of the result.
##
## s: The string to split.
##
## idx: The index vector (``vector of count``) with the cutting points
##
## Returns: A zero-indexed vector of strings.
##
## .. zeek:see:: split_string split_string1 split_string_all split_string_n
function str_split_indices%(s: string, idx: index_vec%): string_vec
%{
auto idx_v = idx->AsVector();
zeek::String::IdxVec indices(idx_v->size());
unsigned int i;
for ( i = 0; i < idx_v->size(); i++ )
indices[i] = (*idx_v)[i]->AsCount();
zeek::String::Vec* result = s->AsString()->Split(indices);
auto result_v = zeek::make_intrusive<zeek::VectorVal>(zeek::id::string_vec);
if ( result )
{
i = 0;
for ( zeek::String::VecIt it = result->begin();
it != result->end(); ++it, ++i )
result_v->Assign(i, zeek::make_intrusive<zeek::StringVal>(*it));
// StringVal now possesses string.
delete result;
}
return result_v;
%}
## Strips whitespace at both ends of a string.
##
## str: The string to strip the whitespace from.