--- parser3/src/lib/cord/cordxtra.c 2003/03/20 09:41:45 1.1.2.2 +++ parser3/src/lib/cord/cordxtra.c 2013/07/19 22:07:57 1.16 @@ -18,12 +18,11 @@ * cord_basics. */ /* Boehm, December 8, 1995 1:53 pm PST */ -# include -# include -# include -# include -# include "cord.h" -# include "ec.h" + +#include "pa_config_includes.h" +#include "cord.h" +#include "ec.h" + # define I_HIDE_POINTERS /* So we get access to allocation lock. */ /* We use this for lazy file reading, */ /* so that we remain independent */ @@ -141,6 +140,7 @@ int CORD_cmp(CORD x, CORD y) if (y == CORD_EMPTY) return(x != CORD_EMPTY); if (x == CORD_EMPTY) return(-1); + if (x == y) return (0); if (CORD_IS_STRING(y) && CORD_IS_STRING(x)) return(strcmp(x,y)); CORD_set_pos(xpos, x, 0); CORD_set_pos(ypos, y, 0); @@ -221,10 +221,11 @@ int CORD_ncmp(CORD x, size_t x_start, CO return(0); } -char * CORD_to_char_star(CORD x) +char * CORD_to_char_star(CORD x, size_t len) { - register size_t len = CORD_len(x); - char * result = GC_MALLOC_ATOMIC(len + 1); + char * result; + if(0 == len) len = CORD_len(x); + result = GC_MALLOC_ATOMIC(len + 1); if (result == 0) OUT_OF_MEMORY; CORD_fill_buf(x, 0, len, result); @@ -244,11 +245,11 @@ CORD CORD_from_char_star(const char* s) return(result); } -const char* CORD_to_const_char_star(CORD x) +const char* CORD_to_const_char_star(CORD x, size_t len) { if (x == 0) return(""); if (CORD_IS_STRING(x)) return((const char* )x); - return(CORD_to_char_star(x)); + return(CORD_to_char_star(x, len)); } char CORD_fetch(CORD x, size_t i) @@ -298,7 +299,7 @@ int CORD_chr_proc(char c, void * client_ (d -> pos) ++; return(0); } - + int CORD_rchr_proc(char c, void * client_data) { register chr_data * d = (chr_data *)client_data; @@ -354,10 +355,9 @@ size_t CORD_rchr(CORD x, size_t i, int c /* and call CORD_ncmp whenever there is a partial match. */ /* This has the advantage that we allocate very little, or not at all. */ /* It's very fast if there are few close misses. */ -size_t CORD_str(CORD x, size_t start, CORD s) +size_t CORD_str(CORD x, size_t start, CORD s, size_t xlen) { CORD_pos xpos; - size_t xlen = CORD_len(x); size_t slen; register size_t start_len; const char* s_start; @@ -374,7 +374,7 @@ size_t CORD_str(CORD x, size_t start, CO s_start = s; slen = strlen(s); } else { - s_start = CORD_to_char_star(CORD_substr(s, 0, sizeof(unsigned long))); + s_start = CORD_to_char_star(CORD_substr(s, 0, sizeof(unsigned long), 0), 0); slen = CORD_len(s); } if (xlen < start || xlen - start < slen) return(CORD_NOT_FOUND); @@ -385,9 +385,9 @@ size_t CORD_str(CORD x, size_t start, CO mask <<= 8; mask |= 0xff; s_buf <<= 8; - s_buf |= s_start[i]; + s_buf |= (unsigned char)s_start[i]; x_buf <<= 8; - x_buf |= CORD_pos_fetch(xpos); + x_buf |= (unsigned char)CORD_pos_fetch(xpos); CORD_next(xpos); } for (match_pos = start; ; match_pos++) { @@ -402,7 +402,7 @@ size_t CORD_str(CORD x, size_t start, CO return(CORD_NOT_FOUND); } x_buf <<= 8; - x_buf |= CORD_pos_fetch(xpos); + x_buf |= (unsigned char)CORD_pos_fetch(xpos); CORD_next(xpos); } } @@ -414,6 +414,7 @@ void CORD_ec_flush_buf(CORD_ec x) if (len == 0) return; s = GC_MALLOC_ATOMIC(len+1); + if (s == 0) OUT_OF_MEMORY; memcpy(s, x[0].ec_buf, len); s[len] = '\0'; x[0].ec_cord = CORD_cat_char_star(x[0].ec_cord, s, len); @@ -432,10 +433,29 @@ char CORD_nul_func(size_t i, void * clie return((char)(unsigned long)client_data); } +#ifdef CORD_CHARS_CACHE +static char* cord_chars_cache[256][15]={0}; +#endif CORD CORD_chars(char c, size_t i) { - return(CORD_from_fn(CORD_nul_func, (void *)(unsigned long)c, i)); + if (i>0 && i<16 /* SHORT_LIMIT */) { + register char* result; +#ifdef CORD_CHARS_CACHE + if(cord_chars_cache[(unsigned char)c][i]) + return((CORD) cord_chars_cache[(unsigned char)c][i]); +#endif + result=GC_MALLOC_ATOMIC(i+1); + if(result==0) OUT_OF_MEMORY; + memset(result, c, i); + result[i] = '\0'; +#ifdef CORD_CHARS_CACHE + cord_chars_cache[(unsigned char)c][i]=result; +#endif + return((CORD) result); + } else { + return(CORD_from_fn(CORD_nul_func, (void *)(unsigned long)c, i)); + } } CORD CORD_from_file_eager(FILE * f) @@ -560,6 +580,7 @@ void CORD_lf_close_proc(void * obj, void } } +#ifndef PA_DEBUG_DISABLE_GC CORD CORD_from_file_lazy_inner(FILE * f, size_t len) { register lf_state * state = GC_NEW(lf_state); @@ -619,3 +640,4 @@ CORD CORD_from_file(FILE * f) return(CORD_from_file_lazy_inner(f, (size_t)len)); } } +#endif