It seemed like a good idea.
commit beeba499793328cc118992884d8e175caffa6433 Author: Stephen R. van den Berg srb@cuci.nl Date: Thu Aug 21 13:17:21 2008 +0200
Optimise optimisation and move inline int2string from roxen.c to operators.c
diff --git a/src/modules/_Roxen/roxen.c b/src/modules/_Roxen/roxen.c index 253f2b3..dcf6086 100644 --- a/src/modules/_Roxen/roxen.c +++ b/src/modules/_Roxen/roxen.c @@ -456,33 +456,6 @@ static void f_html_encode_string( INT32 args ) void o_cast_to_string();
case PIKE_T_INT: - /* Optimization, this is basically a inlined cast_int_to_string */ - { - char buf[21], *b = buf+19; - int neg, i, j=0; - i = Pike_sp[-1].u.integer; - pop_stack(); - if( i < 0 ) - { - neg = 1; - i = -i; - } - else - neg = 0; - - buf[20] = 0; - - while( i >= 10 ) - { - b[ -j++ ] = '0'+(i%10); - i /= 10; - } - b[ -j++ ] = '0'+(i%10); - if( neg ) b[ -j++ ] = '-'; - push_text( b-j+1 ); - } - return; - case PIKE_T_FLOAT: /* Optimization, no need to check the resultstring for * unsafe characters. diff --git a/src/operators.c b/src/operators.c index 3d12cbd..d1eaf75 100644 --- a/src/operators.c +++ b/src/operators.c @@ -343,11 +343,10 @@ PMOD_EXPORT void o_cast_to_int(void) /* Special case for casting to string. */ PMOD_EXPORT void o_cast_to_string(void) { - char buf[200]; switch(sp[-1].type) { case PIKE_T_STRING: - return; + break;
case T_OBJECT: if(!sp[-1].u.object->prog) { @@ -387,10 +386,40 @@ PMOD_EXPORT void o_cast_to_string(void) get_name_of_type(sp[-1].type)); } } - return; + break; case T_INT: +#if 0 sprintf(buf, "%"PRINTPIKEINT"d", sp[-1].u.integer); +#else + { +#define sizeNUM(v) (8*sizeof(v)*4/10+1+1) + INT32 org; + char buf[sizeNUM(org)]; + register char*b = buf+sizeof buf-1; + register unsigned i; + org = sp[-1].u.integer; + pop_stack(); + *b-- = '\0'; + i = org; + + if( org < 0 ) + i = -i; + + goto jin; + do { + i /= 10; +jin: *b-- = '0'+(i%10); + } + while( i >= 10 ); + + if( org<0 ) + *b = '-'; + else + b++; + push_text( b ); + } +#endif break;
case T_ARRAY: @@ -462,18 +491,18 @@ PMOD_EXPORT void o_cast_to_string(void) pop_stack(); push_string(s); } - return; + break;
- case T_FLOAT: + case T_FLOAT: { + char buf[sizeNUM(double)*2]; sprintf(buf, "%f", (double)sp[-1].u.float_number); + push_text(buf); break; + }
default: Pike_error("Cannot cast %s to string.\n", get_name_of_type(sp[-1].type)); } - - sp[-1].type = PIKE_T_STRING; - sp[-1].u.string = make_shared_string(buf); }
PMOD_EXPORT void o_cast(struct pike_type *type, INT32 run_time_type)