From 07571bbe5ceed78c4c16b2fcb6035e5c54453858 Mon Sep 17 00:00:00 2001 From: Vladimir Prus Date: Mon, 14 Sep 2009 16:57:57 +0000 Subject: [PATCH] Implement SPLIT_BY_CHARACTERS. This is much faster than regex.split if splitting by a single char, or by a set of one-char delimiters. [SVN r56191] --- src/engine/builtins.c | 26 ++++++++++++++++++++++++++ src/engine/builtins.h | 1 + 2 files changed, 27 insertions(+) diff --git a/src/engine/builtins.c b/src/engine/builtins.c index 27eb31251..9889bb15e 100644 --- a/src/engine/builtins.c +++ b/src/engine/builtins.c @@ -141,6 +141,12 @@ void load_builtins() bind_builtin( "MATCH", builtin_match, 0, 0 ) ); + { + char * args[] = { "string", ":", "delimiters" }; + bind_builtin( "SPLIT_BY_CHARACTERS", + builtin_split_by_characters, 0, 0 ); + } + duplicate_rule( "NoCare", bind_builtin( "NOCARE", builtin_flags, T_FLAG_NOCARE, 0 ) ); @@ -845,6 +851,26 @@ LIST * builtin_match( PARSE * parse, FRAME * frame ) return result; } +LIST * builtin_split_by_characters( PARSE * parse, FRAME * frame ) +{ + LIST * l1 = lol_get( frame->args, 0 ); + LIST * l2 = lol_get( frame->args, 1 ); + + LIST * result = 0; + + char* s = l1->string; + char* delimiters = l2->string; + char* t; + + t = strtok (s, delimiters); + while (t) + { + result = list_new(result, newstr(t)); + t = strtok (NULL, delimiters); + } + + return result; +} LIST * builtin_hdrmacro( PARSE * parse, FRAME * frame ) { diff --git a/src/engine/builtins.h b/src/engine/builtins.h index 8be6249d9..c996528a3 100644 --- a/src/engine/builtins.h +++ b/src/engine/builtins.h @@ -31,6 +31,7 @@ LIST *builtin_glob( PARSE *parse, FRAME *args ); LIST *builtin_glob_recursive( PARSE *parse, FRAME *frame ); LIST *builtin_subst( PARSE *parse, FRAME *args ); LIST *builtin_match( PARSE *parse, FRAME *args ); +LIST *builtin_split_by_characters( PARSE *parse, FRAME *args ); LIST *builtin_hdrmacro( PARSE *parse, FRAME *args ); LIST *builtin_rulenames( PARSE *parse, FRAME *args ); LIST *builtin_varnames( PARSE *parse, FRAME *args );