summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorgraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-08-12 01:19:27 +0000
committergraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-08-12 01:19:27 +0000
commite52c3fd7f441b2bd7c1dbf3972de7f672d897745 (patch)
tree5a0729270a5f064ad173fb673e360bea9d594d98 /utils
parentc8e90b60480f450745d347e2975b0456563e93e3 (diff)
fidget
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@532 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'utils')
-rw-r--r--utils/stringlib.h14
-rwxr-xr-xutils/utoa.h24
2 files changed, 20 insertions, 18 deletions
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