2 * @brief string conversion utility functions for omega
4 /* Copyright 1999,2000,2001 BrightStation PLC
5 * Copyright 2003,2004,2006,2010,2011,2019 Olly Betts
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
28 #include <cstdio> // for snprintf
36 string_to_int(const string
&s
)
38 return atoi(s
.c_str());
42 double_to_string(double val
)
44 // This should be more than enough.
45 const int BUFSIZE
= 100;
47 int len
= snprintf(buf
, BUFSIZE
, "%f", val
);
48 return string(buf
, (len
== -1 || len
> BUFSIZE
) ? BUFSIZE
: len
);
54 string::size_type first_nonspace
;
55 first_nonspace
= s
.find_first_not_of(" \t\r\n\v");
56 if (first_nonspace
== string::npos
) {
57 // String is all whitespace.
60 // Remove any trailing whitespace.
61 string::size_type len
= s
.find_last_not_of(" \t\r\n\v");
62 assert(len
!= string::npos
);
63 if (len
< s
.size() - 1)
65 // Remove any leading whitespace.
66 if (first_nonspace
> 0)
67 s
.erase(0, first_nonspace
);