diff options
author | axel <axel@liljencrantz.se> | 2006-02-05 23:10:35 +1000 |
---|---|---|
committer | axel <axel@liljencrantz.se> | 2006-02-05 23:10:35 +1000 |
commit | 58667673d921a782f4a531255344983bbadb9288 (patch) | |
tree | 5c49d356be69c5b2e9b841191fb6cd7a8c8c0bae | |
parent | a53a9aa2932a1581f278f97f9f2c0d55a031de48 (diff) |
Remove duplicate line counting code. Make the remaining implementation have a two element cache.
darcs-hash:20060205131035-ac50b-885c6ba87a6d16aa48dfa7ee4608ae8891c71724.gz
-rw-r--r-- | function.c | 32 | ||||
-rw-r--r-- | parse_util.c | 45 | ||||
-rw-r--r-- | parse_util.h | 3 | ||||
-rw-r--r-- | parser.c | 22 |
4 files changed, 64 insertions, 38 deletions
@@ -18,6 +18,7 @@ #include "intern.h" #include "event.h" #include "reader.h" +#include "parse_util.h" /** @@ -65,18 +66,6 @@ void function_destroy() hash_destroy( &function ); } -static int count_lineno( const wchar_t *str, int len ) -{ - int res = 0; - int i; - for( i=0; i<len; i++ ) - { - if( str[i] == L'\n' ) - res++; - } - return res; -} - void function_add( const wchar_t *name, const wchar_t *val, @@ -88,13 +77,12 @@ void function_add( const wchar_t *name, wchar_t *cmd_end; function_data_t *d; - if( function_exists( name ) ) - function_remove( name ); + function_remove( name ); d = malloc( sizeof( function_data_t ) ); - d->definition_offset = count_lineno( parser_get_buffer(), current_block->tok_pos ); + d->definition_offset = parse_util_lineno( parser_get_buffer(), current_block->tok_pos ); d->cmd = wcsdup( val ); - + cmd_end = d->cmd + wcslen(d->cmd)-1; while( (cmd_end>d->cmd) && wcschr( L"\n\r\t ", *cmd_end ) ) { @@ -103,7 +91,7 @@ void function_add( const wchar_t *name, d->desc = desc?wcsdup( desc ):0; d->is_binding = is_binding; - d->definition_file = reader_current_filename()?intern(reader_current_filename()):0; + d->definition_file = intern(reader_current_filename()); hash_put( &function, intern(name), d ); @@ -125,18 +113,18 @@ void function_remove( const wchar_t *name ) function_data_t *d; event_t ev; - ev.type=EVENT_ANY; - ev.function_name=name; - event_remove( &ev ); - hash_remove( &function, name, (const void **) &key, (const void **)&d ); - if( !d ) + if( !key ) return; + ev.type=EVENT_ANY; + ev.function_name=name; + event_remove( &ev ); + clear_function_entry( key, d ); } diff --git a/parse_util.c b/parse_util.c index d58af719..908f46da 100644 --- a/parse_util.c +++ b/parse_util.c @@ -21,6 +21,51 @@ #include "tokenizer.h" #include "parse_util.h" +int parse_util_lineno( const wchar_t *str, int len ) +{ + static int res = 1; + static int i=0; + static const wchar_t *prev_str = 0; + + static const wchar_t *prev_str2 = 0; + static int i2 = 0; + static int res2 = 1; + + if( str != prev_str || i>len ) + { + if( prev_str2 == str && i2 <= len ) + { + const wchar_t *tmp_str = prev_str; + int tmp_i = i; + int tmp_res = res; + prev_str = prev_str2; + i=i2; + res=res2; + + prev_str2 = tmp_str; + i2 = tmp_i; + res2 = tmp_res; + } + else + { + prev_str2 = prev_str; + i2 = i; + res2=res; + + prev_str = str; + i=0; + res=1; + } + } + + for( ; i<len; i++ ) + { + if( str[i] == L'\n' ) + res++; + } + return res; +} + int parse_util_locate_cmdsubst( const wchar_t *in, const wchar_t **begin, const wchar_t **end, diff --git a/parse_util.h b/parse_util.h index 30f47ef8..3b21e09b 100644 --- a/parse_util.h +++ b/parse_util.h @@ -47,4 +47,7 @@ void parse_util_token_extent( const wchar_t *buff, const wchar_t **prev_begin, const wchar_t **prev_end ); +int parse_util_lineno( const wchar_t *str, int len ); + + #endif @@ -37,6 +37,7 @@ The fish parser. Contains functions for parsing code. #include "event.h" #include "translate.h" #include "intern.h" +#include "parse_util.h" /** Maximum number of block levels in code. This is not the same as @@ -1122,11 +1123,13 @@ int parser_get_lineno() { const wchar_t *whole_str; const wchar_t *function_name; + + int lineno; - static const wchar_t *prev_str = 0; +/* static const wchar_t *prev_str = 0; static int i=0; static int lineno=1; - +*/ if( !current_tokenizer ) return -1; @@ -1135,20 +1138,7 @@ int parser_get_lineno() if( !whole_str ) return -1; - if( whole_str != prev_str || i>current_tokenizer_pos ) - { - prev_str = whole_str; - i=0; - lineno = 0; - } - - for( ; i<current_tokenizer_pos && whole_str[i]; i++ ) - { - if( whole_str[i] == L'\n' ) - { - lineno++; - } - } + lineno = parse_util_lineno( whole_str, current_tokenizer_pos ); if( (function_name = is_function()) ) { |