Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
String.h
Go to the documentation of this file.
1 
9 #ifndef _LIMBO_STRING_STRING
10 #define _LIMBO_STRING_STRING
11 
12 #include <iostream>
13 #include <string>
14 #include <cctype>
15 #include <limbo/string/ToString.h>
16 
18 namespace limbo
19 {
20 
21 using std::cout;
22 using std::endl;
23 using std::string;
24 
28 inline bool is_integer(string const& s)
29 {
30  if (s.empty()) return false;
31  for (string::const_iterator it = s.begin(); it != s.end(); ++it)
32  {
33  if (!isdigit(*it)) return false;
34  }
35  return true;
36 }
37 
41 inline bool is_float(string const& s)
42 {
43  if (s.empty()) return false;
44  short dp_cnt = 0; // count how many times the decimal point appears
45  // if it appears more than 1, then s is not a floating point number
46  for (string::const_iterator it = s.begin(); it != s.end(); ++it)
47  {
48  if (!isdigit(*it))
49  {
50  if (*it == '.' && dp_cnt < 1) // dp_cnt should be <= 1
51  ++dp_cnt;
52  else return false;
53  }
54  }
55  // for floating point we assume decimal point should be there
56  return dp_cnt == 1;
57 }
58 
62 inline bool is_number(string const& s)
63 {
64  if (s.empty()) return false;
65  short dp_cnt = 0; // count how many times the decimal point appears
66  // if it appears more than 1, then s is not a floating point number
67  for (string::const_iterator it = s.begin(); it != s.end(); ++it)
68  {
69  if (!isdigit(*it))
70  {
71  if (*it == '.' && dp_cnt < 1) // dp_cnt should be <= 1
72  ++dp_cnt;
73  else return false;
74  }
75  }
76  // no requirement for floating point here
77  return true;
78 }
79 
83 inline string toupper(string const& s)
84 {
85  string s_up;
86  s_up.reserve(s.size());
87  for (string::const_iterator it = s.begin(); it != s.end(); ++it)
88  s_up.push_back(::toupper(*it));
89  return s_up;
90 }
91 
95 inline string tolower(string const& s)
96 {
97  string s_low;
98  s_low.reserve(s.size());
99  for (string::const_iterator it = s.begin(); it != s.end(); ++it)
100  s_low.push_back(::tolower(*it));
101  return s_low;
102 }
103 
108 inline bool iequals(string const& s1, string const& s2)
109 {
110  string s1_up = toupper(s1);
111  string s2_up = toupper(s2);
112  return s1_up == s2_up;
113 }
114 
118 inline string get_file_path(const string& s)
119 {
120  size_t found = s.rfind('/');
121  if (found != string::npos) return s.substr(0, found);
122  else return ".";
123 }
127 inline string get_file_name(const string& s)
128 {
129  size_t found = s.rfind('/');
130  if (found != string::npos) return s.substr(found+1);
131  else return s;
132 }
136 inline string get_file_suffix(const string& s)
137 {
138  size_t found = s.rfind('.');
139  if (found != string::npos) return s.substr(found+1);
140  else return string("");
141 }
145 inline string trim_file_suffix(string const& s)
146 {
147  size_t found = s.rfind('.');
148  if (found != string::npos) return s.substr(0, found);
149  else return s;
150 }
154 inline string get_first_word(string const& str)
155 {
156  size_t pos1 = std::string::npos;
157  size_t pos2 = std::string::npos;
158  for (size_t i = 0; i < str.size(); i++)
159  {
160  if (pos1 == std::string::npos && str[i] != ' ' && str[i] != '\t')
161  pos1 = i;
162  else if (pos1 != std::string::npos && (str[i] == ' ' || str[i] == '\t'))
163  {
164  pos2 = i;
165  break;
166  }
167  }
168  return str.substr(pos1, pos2 - pos1);
169 }
170 
171 } // namespace limbo
172 
173 #endif
string trim_file_suffix(string const &s)
trim the suffix of a file
Definition: String.h:145
string get_file_path(const string &s)
get relative path of a file
Definition: String.h:118
string toupper(string const &s)
convert string to upper case
Definition: String.h:83
bool is_integer(string const &s)
check whether string represents an integer
Definition: String.h:28
string get_file_suffix(const string &s)
get suffix of a file
Definition: String.h:136
bool is_number(string const &s)
check whether string represents a number, either integer or floating point number ...
Definition: String.h:62
string get_file_name(const string &s)
get pure name of a file (no path)
Definition: String.h:127
convert metadata type to string
namespace for Limbo
Definition: GraphUtility.h:22
bool iequals(string const &s1, string const &s2)
check two strings equal, case-insensitive
Definition: String.h:108
bool is_float(string const &s)
check whether string represents an float
Definition: String.h:41
string tolower(string const &s)
convert string to lower case
Definition: String.h:95
string get_first_word(string const &str)
fetch the first word of a string, assume delimiter is space or tab
Definition: String.h:154