string_manip.cpp

Go to the documentation of this file.
00001 
00012 #include <sstream>
00013 #include <iomanip>
00014 
00015 #include <cstdlib>
00016 #include <cmath>
00017 
00018 #include "string_manip.h"
00019 
00020 using namespace std;
00021 
00022 
00023 string erase_to_last_of(string const & str, char ch)
00024 {
00025     string result = str;
00026     string::size_type pos = result.find_last_of(ch);
00027     if (pos != string::npos)
00028         result.erase(0, pos + 1);
00029 
00030     return result;
00031 }
00032 
00033 
00034 string split(string & s, char c)
00035 {
00036     string::size_type i = s.find_first_of(c);
00037     if (i == string::npos)
00038         return string();
00039 
00040     string const tail = s.substr(i + 1);
00041     s = s.substr(0, i);
00042     return tail;
00043 }
00044 
00045 
00046 bool is_prefix(string const & s, string const & prefix)
00047 {
00048     // gcc 2.95 and below don't have this
00049     // return s.compare(0, prefix.length(), prefix) == 0;
00050     return s.find(prefix) == 0;
00051 }
00052 
00053 
00054 vector<string> separate_token(string const & str, char sep)
00055 {
00056     vector<string> result;
00057     string next;
00058 
00059     for (size_t pos = 0 ; pos != str.length() ; ++pos) {
00060         char ch = str[pos];
00061         if (ch == '\\') {
00062             if (pos < str.length() - 1 && str[pos + 1] == sep) {
00063                 ++pos;
00064                 next += sep;
00065             } else {
00066                 next += '\\';
00067             }
00068         } else if (ch == sep) {
00069             result.push_back(next);
00070             // some stl lacks string::clear()
00071             next.erase(next.begin(), next.end());
00072         } else {
00073             next += ch;
00074         }
00075     }
00076 
00077     if (!next.empty())
00078         result.push_back(next);
00079 
00080     return result;
00081 }
00082 
00083 
00084 string ltrim(string const & str, string const & totrim)
00085 {
00086     string result(str);
00087 
00088     return result.erase(0, result.find_first_not_of(totrim));
00089 }
00090 
00091 
00092 string rtrim(string const & str, string const & totrim)
00093 {
00094     string result(str);
00095 
00096     return result.erase(result.find_last_not_of(totrim) + 1);
00097 }
00098 
00099 
00100 string trim(string const & str, string const & totrim)
00101 {
00102     return rtrim(ltrim(str, totrim), totrim);
00103 }
00104 
00105 
00106 string const
00107 format_percent(double value, size_t int_width, size_t fract_width, bool showpos)
00108 {
00109     ostringstream os;
00110 
00111     if (value == 0.0)
00112         return string(int_width + fract_width, ' ') + "0";
00113 
00114     if (showpos)
00115         os.setf(ios::showpos);
00116 
00117     if (fabs(value) > .001) {
00118         // os << fixed << value unsupported by gcc 2.95
00119         os.setf(ios::fixed, ios::floatfield);
00120         os << setw(int_width + fract_width + 1)
00121            << setprecision(fract_width) << value;
00122     } else {
00123         // os << scientific << value unsupported by gcc 2.95
00124         os.setf(ios::scientific, ios::floatfield);
00125         os << setw(int_width + fract_width + 1)
00126            // - 3 to count exponent part
00127            << setprecision(fract_width - 3) << value;
00128     }
00129 
00130     string formatted = os.str();
00131     if (is_prefix(formatted, "100."))
00132         formatted.erase(formatted.size() - 1);
00133     return formatted;
00134 }
00135 
00136 
00137 template <>
00138 unsigned int op_lexical_cast<unsigned int, string>(string const & str)
00139 {
00140     char* endptr;
00141 
00142     // 2.91.66 fix
00143     unsigned long ret = 0;
00144     ret = strtoul(str.c_str(), &endptr, 0);
00145     if (*endptr)
00146         throw invalid_argument("op_lexical_cast(\""+ str +"\")");
00147     return ret;
00148 }

Generated on 8 Nov 2012 for Oprofile by  doxygen 1.6.1