From 5359400c08f817f8c980c034fa1de4d55a43a65c Mon Sep 17 00:00:00 2001 From: graehl Date: Wed, 11 Aug 2010 22:40:01 +0000 Subject: itoa git-svn-id: https://ws10smt.googlecode.com/svn/trunk@529 ec762483-ff6d-05da-a07a-a48fb63a330f --- decoder/apply_fsa_models.cc | 5 ++- utils/static_utoa.h | 80 +---------------------------------- utils/utoa.h | 100 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 80 deletions(-) create mode 100755 utils/utoa.h diff --git a/decoder/apply_fsa_models.cc b/decoder/apply_fsa_models.cc index 3a1e8050..1c30eb90 100755 --- a/decoder/apply_fsa_models.cc +++ b/decoder/apply_fsa_models.cc @@ -9,6 +9,7 @@ #include #include "cfg.h" #include "hg_cfg.h" +#include "utoa.h" using namespace std; @@ -107,8 +108,8 @@ ApplyFsaBy::ApplyFsaBy(std::string const& n, int pop_limit) : pop_limit(pop_limi } ApplyFsaBy::ApplyFsaBy(int i, int pop_limit) : pop_limit(pop_limit) { - assert (i>=0); - assert (i=N_ALGORITHMS) + throw std::runtime_error("Unknown ApplyFsaBy type id: "+itos(i)+" - legal types: "+all_names()); algorithm=i; } diff --git a/utils/static_utoa.h b/utils/static_utoa.h index fe5f6d92..3af9fbb6 100755 --- a/utils/static_utoa.h +++ b/utils/static_utoa.h @@ -2,96 +2,20 @@ #define STATIC_UTOA_H #include "threadlocal.h" - - -#include -#include - -#define DIGIT_LOOKUP_TABLE 0 +#include "utoa.h" namespace { -THREADLOCAL char utoa_buf[] = "01234567890123456789"; // to put end of string character at buf[20] -const unsigned utoa_bufsize=sizeof(utoa_buf); -const unsigned utoa_bufsizem1=utoa_bufsize-1; -#ifdef DIGIT_LOOKUP_TABLE -char digits[] = "0123456789"; -#endif -} - -inline char digit_to_char(int d) { - return -#ifdef DIGIT_LOOKUP_TABLE - digits[d]; -#else - '0'+d; -#endif -} - -// returns n in string [return,num); *num=0 yourself before calling if you want a c_str -inline char *utoa(char *num,unsigned n) { - if ( !n ) { - *--num='0'; - } else { - unsigned rem; - // 3digit lookup table, divide by 1000 faster? - while ( n ) { -#if 1 - rem = n; - n /= 10; - rem -= 10*n; // maybe this is faster than mod because we are already dividing -#else - rem = n%10; // would optimizer combine these together? - n = n/10; -#endif - *--num = digit_to_char(rem); - } - } - return num; +THREADLOCAL char utoa_buf[utoa_bufsize]; // to put end of string character at buf[20] } inline char *static_utoa(unsigned n) { return utoa(utoa_buf+utoa_bufsizem1,n); } -//returns position of '\0' terminating number written starting at to -inline char* append_utoa(char *to,unsigned n) { - char *s=static_utoa(n); - int ns=(utoa_buf+utoa_bufsize)-s; - std::memcpy(to,s,ns); - return to+ns; -} - -// so named to avoid gcc segfault when named itoa -inline char *itoa(char *p,int n) { - if (n<0) { - p=utoa(p,-n); // TODO: check that (unsigned)(-INT_MIN) == 0x1000000 in 2s complement and not == 0 - *--p='-'; - return p; - } else - return utoa(p,n); -} - inline char *static_itoa(int n) { return itoa(utoa_buf+utoa_bufsizem1,n); } - -inline std::string utos(unsigned n) { - const int bufsz=20; - char buf[bufsz]; - char *end=buf+bufsz; - char *p=utoa(end,n); - return std::string(p,end); -} - -inline std::string itos(int n) { - const int bufsz=20; - char buf[bufsz]; - char *end=buf+bufsz; - char *p=itoa(end,n); - return std::string(p,end); -} - #ifdef ITOA_SAMPLE # include # include diff --git a/utils/utoa.h b/utils/utoa.h new file mode 100755 index 00000000..6c1cf2de --- /dev/null +++ b/utils/utoa.h @@ -0,0 +1,100 @@ +#ifndef UTOA_H +#define UTOA_H + +#include +#include + +#ifndef DIGIT_LOOKUP_TABLE +# define DIGIT_LOOKUP_TABLE 0 +#endif + +const unsigned utoa_bufsize=21; +const unsigned utoa_bufsizem1=utoa_bufsize-1; + +#ifdef DIGIT_LOOKUP_TABLE +namespace { +char digits[] = "0123456789"; +} +#endif + +inline char digit_to_char(int d) { + return +#ifdef DIGIT_LOOKUP_TABLE + digits[d]; +#else + '0'+d; +#endif +} + +// returns n in string [return,num); *num=0 yourself before calling if you want a c_str +inline char *utoa(char *num,unsigned n) { + if ( !n ) { + *--num='0'; + } else { + unsigned rem; + // 3digit lookup table, divide by 1000 faster? + while ( n ) { +#if 1 + rem = n; + n /= 10; + rem -= 10*n; // maybe this is faster than mod because we are already dividing +#else + rem = n%10; // would optimizer combine these together? + n = n/10; +#endif + *--num = digit_to_char(rem); + } + } + return num; +} + + +inline char *itoa(char *p,int n) { + if (n<0) { + p=utoa(p,-n); // TODO: check that (unsigned)(-INT_MIN) == 0x1000000 in 2s complement and not == 0 + *--p='-'; + return p; + } else + return utoa(p,n); +} + +inline std::string utos(unsigned n) { + char buf[utoa_bufsize]; + char *end=buf+utoa_bufsize; + char *p=utoa(end,n); + return std::string(p,end); +} + +inline std::string itos(int n) { + char buf[utoa_bufsize]; + char *end=buf+utoa_bufsize; + char *p=itoa(end,n); + return std::string(p,end); +} + +//returns position of '\0' terminating number written starting at to +inline char* append_utoa(char *to,unsigned n) { + char buf[utoa_bufsize]; + char *end=buf+utoa_bufsize; + char *s=utoa(end,n); + int ns=end-s; + std::memcpy(to,s,ns); + to+=ns; + *to++=0; + return to; +} + +//returns position of '\0' terminating number written starting at to +inline char* append_itoa(char *to,unsigned n) { + char buf[utoa_bufsize]; + char *end=buf+utoa_bufsize; + char *s=itoa(end,n); + int ns=end-s; + std::memcpy(to,s,ns); + to+=ns; + *to++=0; + return to; +} + + +#endif -- cgit v1.2.3