diff options
| -rwxr-xr-x | decoder/fast_lexical_cast.hpp | 1 | ||||
| -rw-r--r-- | utils/stringlib.h | 14 | ||||
| -rwxr-xr-x | utils/utoa.h | 24 | 
3 files changed, 21 insertions, 18 deletions
| diff --git a/decoder/fast_lexical_cast.hpp b/decoder/fast_lexical_cast.hpp index c1b15042..ae49c934 100755 --- a/decoder/fast_lexical_cast.hpp +++ b/decoder/fast_lexical_cast.hpp @@ -2,6 +2,7 @@  #define FAST_LEXICAL_CAST_HPP  #define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE +// This should make casting to/from string reasonably fast (see http://accu.org/index.php/journals/1375)  #include <boost/lexical_cast.hpp> diff --git a/utils/stringlib.h b/utils/stringlib.h index 84e95d44..4f79eb31 100644 --- a/utils/stringlib.h +++ b/utils/stringlib.h @@ -20,22 +20,22 @@  #include <sstream>  #include <algorithm> -inline std::size_t skip_ws(std::string const& s,std::size_t starting=0,char const* ws=" \t\n\r") { +namespace { +const char c_isspace[]=" \t\n\r\f\v"; // somewhat ridiculous, including characters nobody uses. +const char common_isspace[]=" \t\n\r"; // even \n\r is borderline, but maybe you pass multiline DOS format text. +} + +inline std::size_t skip_ws(std::string const& s,std::size_t starting=0,char const* ws=common_isspace) {    return s.find_first_not_of(ws,starting);  }  // returns position of end of all non-ws chars before ending, i.e. string(s.begin()+skip_ws(s),s.begin()+trailing_ws(s)) strips both ends -inline std::size_t trailing_ws(std::string const& s,std::size_t ending=std::string::npos,char const* ws=" \t\n\r") { +inline std::size_t trailing_ws(std::string const& s,std::size_t ending=std::string::npos,char const* ws=common_isspace) {    std::size_t n=s.find_last_not_of(ws,ending);    if (n==std::string::npos) return n;    else return n+1;  } -//TEST: if string is all whitespace, make sure that string(a+npos,a+npos) can't segfault (i.e. won't access any memory because begin==end) -inline std::string strip_ws(std::string const& s) { -  return std::string(s.begin()+skip_ws(s),s.begin()+trailing_ws(s)); -} -  inline bool is_single_line(std::string const& line) {    return std::count(line.begin(),line.end(),'\n')==0; // but we want to allow terminal newlines/blanks diff --git a/utils/utoa.h b/utils/utoa.h index 6c1cf2de..8d304f97 100755 --- a/utils/utoa.h +++ b/utils/utoa.h @@ -1,6 +1,7 @@  #ifndef UTOA_H  #define UTOA_H +#include <stdint.h>  #include <string>  #include <cstring> @@ -8,7 +9,10 @@  # define DIGIT_LOOKUP_TABLE 0  #endif -const unsigned utoa_bufsize=21; +// The largest 32-bit integer is 4294967295, that is 10 chars +// 1 more for sign, and 1 for 0-termination of string +// generally: 2 + std::numeric_limits<T>::is_signed + std::numeric_limits<T>::digits10 +const unsigned utoa_bufsize=12;  const unsigned utoa_bufsizem1=utoa_bufsize-1;  #ifdef DIGIT_LOOKUP_TABLE @@ -27,11 +31,11 @@ inline char digit_to_char(int d) {  }  // returns n in string [return,num); *num=0 yourself before calling if you want a c_str -inline char *utoa(char *num,unsigned n) { +inline char *utoa(char *num,uint32_t n) {    if ( !n ) {      *--num='0';    } else { -    unsigned rem; +    uint32_t rem;      // 3digit lookup table, divide by 1000 faster?      while ( n ) {  #if 1 @@ -48,24 +52,23 @@ inline char *utoa(char *num,unsigned n) {    return num;  } - -inline char *itoa(char *p,int n) { +inline char *itoa(char *p,int32_t n) {    if (n<0) { -    p=utoa(p,-n); // TODO: check that (unsigned)(-INT_MIN) == 0x1000000 in 2s complement and not == 0 +    p=utoa(p,-n); // (unsigned)(-INT_MIN) == 0x1000000 in 2s complement and not == 0.      *--p='-';      return p;    } else      return utoa(p,n);  } -inline std::string utos(unsigned n) { +inline std::string utos(uint32_t 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) { +inline std::string itos(int32_t n) {    char buf[utoa_bufsize];    char *end=buf+utoa_bufsize;    char *p=itoa(end,n); @@ -73,7 +76,7 @@ inline std::string itos(int n) {  }  //returns position of '\0' terminating number written starting at to -inline char* append_utoa(char *to,unsigned n) { +inline char* append_utoa(char *to,uint32_t n) {    char buf[utoa_bufsize];    char *end=buf+utoa_bufsize;    char *s=utoa(end,n); @@ -85,7 +88,7 @@ inline char* append_utoa(char *to,unsigned n) {  }  //returns position of '\0' terminating number written starting at to -inline char* append_itoa(char *to,unsigned n) { +inline char* append_itoa(char *to,int32_t n) {    char buf[utoa_bufsize];    char *end=buf+utoa_bufsize;    char *s=itoa(end,n); @@ -96,5 +99,4 @@ inline char* append_itoa(char *to,unsigned n) {    return to;  } -  #endif | 
