Remove duplicate line counting code. Make the remaining implementation have a two element cache.

darcs-hash:20060205131035-ac50b-885c6ba87a6d16aa48dfa7ee4608ae8891c71724.gz
This commit is contained in:
axel 2006-02-05 23:10:35 +10:00
parent a53a9aa293
commit 58667673d9
4 changed files with 64 additions and 38 deletions

View file

@ -18,6 +18,7 @@
#include "intern.h" #include "intern.h"
#include "event.h" #include "event.h"
#include "reader.h" #include "reader.h"
#include "parse_util.h"
/** /**
@ -65,18 +66,6 @@ void function_destroy()
hash_destroy( &function ); 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, void function_add( const wchar_t *name,
const wchar_t *val, const wchar_t *val,
@ -88,11 +77,10 @@ void function_add( const wchar_t *name,
wchar_t *cmd_end; wchar_t *cmd_end;
function_data_t *d; function_data_t *d;
if( function_exists( name ) ) function_remove( name );
function_remove( name );
d = malloc( sizeof( function_data_t ) ); 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 ); d->cmd = wcsdup( val );
cmd_end = d->cmd + wcslen(d->cmd)-1; cmd_end = d->cmd + wcslen(d->cmd)-1;
@ -103,7 +91,7 @@ void function_add( const wchar_t *name,
d->desc = desc?wcsdup( desc ):0; d->desc = desc?wcsdup( desc ):0;
d->is_binding = is_binding; 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 ); hash_put( &function, intern(name), d );
@ -125,18 +113,18 @@ void function_remove( const wchar_t *name )
function_data_t *d; function_data_t *d;
event_t ev; event_t ev;
ev.type=EVENT_ANY;
ev.function_name=name;
event_remove( &ev );
hash_remove( &function, hash_remove( &function,
name, name,
(const void **) &key, (const void **) &key,
(const void **)&d ); (const void **)&d );
if( !d ) if( !key )
return; return;
ev.type=EVENT_ANY;
ev.function_name=name;
event_remove( &ev );
clear_function_entry( key, d ); clear_function_entry( key, d );
} }

View file

@ -21,6 +21,51 @@
#include "tokenizer.h" #include "tokenizer.h"
#include "parse_util.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, int parse_util_locate_cmdsubst( const wchar_t *in,
const wchar_t **begin, const wchar_t **begin,
const wchar_t **end, const wchar_t **end,

View file

@ -47,4 +47,7 @@ void parse_util_token_extent( const wchar_t *buff,
const wchar_t **prev_begin, const wchar_t **prev_begin,
const wchar_t **prev_end ); const wchar_t **prev_end );
int parse_util_lineno( const wchar_t *str, int len );
#endif #endif

View file

@ -37,6 +37,7 @@ The fish parser. Contains functions for parsing code.
#include "event.h" #include "event.h"
#include "translate.h" #include "translate.h"
#include "intern.h" #include "intern.h"
#include "parse_util.h"
/** /**
Maximum number of block levels in code. This is not the same as Maximum number of block levels in code. This is not the same as
@ -1123,10 +1124,12 @@ int parser_get_lineno()
const wchar_t *whole_str; const wchar_t *whole_str;
const wchar_t *function_name; const wchar_t *function_name;
static const wchar_t *prev_str = 0; int lineno;
/* static const wchar_t *prev_str = 0;
static int i=0; static int i=0;
static int lineno=1; static int lineno=1;
*/
if( !current_tokenizer ) if( !current_tokenizer )
return -1; return -1;
@ -1135,20 +1138,7 @@ int parser_get_lineno()
if( !whole_str ) if( !whole_str )
return -1; return -1;
if( whole_str != prev_str || i>current_tokenizer_pos ) lineno = parse_util_lineno( whole_str, 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++;
}
}
if( (function_name = is_function()) ) if( (function_name = is_function()) )
{ {