Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JLangToolkit.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JLANGTOOLKIT__
2 #define __JLANG__JLANGTOOLKIT__
3 
4 #include <string>
5 #include <sstream>
6 #include <iterator>
7 #include <ctype.h>
8 
9 
10 /**
11  * \author mdejong
12  */
13 
14 /**
15  * Auxiliary classes and methods for language specific functionality.
16  */
17 namespace JLANG {}
18 namespace JPP { using namespace JLANG; }
19 
20 namespace JLANG {
21 
22  using std::size_t;
23 
24 
25  /**
26  * Get size of c-array.
27  *
28  * \param array array
29  * \return size
30  */
31  template<class T, size_t N>
32  inline size_t getSize(T (&array)[N])
33  {
34  return N;
35  }
36 
37 
38  /**
39  * Check if two objects are indentical.
40  *
41  * \param first first object
42  * \param second second object
43  * \return true if addresses are equal; else false
44  */
45  template<class JFirst_t, class JSecond_t>
46  inline bool is_identical(JFirst_t& first, JSecond_t& second)
47  {
48  return (void*) &first == (void*) &second;
49  }
50 
51 
52  /**
53  * Check if string is an integer.\n
54  *
55  * \param buffer input string
56  * \return true if integer; else false
57  */
58  inline bool is_integer(const std::string& buffer)
59  {
60  using namespace std;
61 
62  for (string::const_iterator i = buffer. begin(); i != buffer.end(); ++i) {
63  if (!isdigit(*i)) {
64  return false;
65  }
66  }
67 
68  return !buffer.empty();
69  }
70 
71 
72  /**
73  * Trim string.\n
74  * Returns a copy of the string, with leading and trailing white spaces omitted.
75  *
76  * \param buffer input string
77  * \return modified string
78  */
79  inline std::string trim(const std::string& buffer)
80  {
81  using namespace std;
82 
83  string::const_iterator p = buffer. begin();
84  string::const_reverse_iterator q = buffer.rbegin();
85 
86  while (p != q.base() && isspace(*p)) { ++p; }
87  while (p != q.base() && isspace(*q)) { ++q; }
88 
89  return string(p,q.base());
90  }
91 
92 
93  /**
94  * Trim string.\n
95  * Returns a copy of the string, with leading and trailing target characters omitted.
96  *
97  * \param buffer input string
98  * \param c strip character
99  * \return modified string
100  */
101  inline std::string trim(const std::string& buffer, const char c)
102  {
103  using namespace std;
104 
105  string::const_iterator p = buffer. begin();
106  string::const_reverse_iterator q = buffer.rbegin();
107 
108  while (p != q.base() && *p == c) { ++p; }
109  while (p != q.base() && *q == c) { ++q; }
110 
111  return string(p,q.base());
112  }
113 
114 
115  /**
116  * Replace tokens in string.\n
117  * Returns a copy of the string, with all occurences of <tt>target</tt> replaced by <tt>replacement</tt>.
118  *
119  * \param input input string
120  * \param target target string
121  * \param replacement replacement string
122  * \return modified string
123  */
124  inline std::string replace(const std::string& input, const std::string& target, const std::string& replacement)
125  {
126  using namespace std;
127 
128  string buffer = input;
129 
130  for (std::size_t i = buffer.find(target); i != std::string::npos; i = buffer.find(target,i)) {
131  buffer.replace(buffer.begin() + i, buffer.begin() + i + target.length(), replacement);
132  }
133 
134  return buffer;
135  }
136 
137 
138  /**
139  * Trim string.\n
140  * Returns a copy of the string, with leading and trailing target characters omitted.
141  *
142  * \param buffer input string
143  * \param target strip character(s)
144  * \return modified string
145  */
146  inline std::string trim(const std::string& buffer, const std::string& target)
147  {
148  using namespace std;
149 
150  string::const_iterator p = buffer. begin();
151  string::const_reverse_iterator q = buffer.rbegin();
152 
153  while (p != q.base() && target.find(*p) != string::npos) { ++p; }
154  while (p != q.base() && target.find(*q) != string::npos) { ++q; }
155 
156  return string(p,q.base());
157  }
158 
159 
160  /**
161  * Convert value to string.
162  *
163  * \param value value
164  * \return string
165  */
166  template<class T>
167  inline std::string to_string(const T& value)
168  {
169  using namespace std;
170 
171  ostringstream os;
172 
173  os << value;
174 
175  return os.str();
176  }
177 
178 
179  /**
180  * Convert all character to upper case.
181  *
182  * \param value value
183  * \return string
184  */
185  inline std::string to_upper(const std::string& value)
186  {
187  using namespace std;
188 
189  string buffer(value);
190 
191  for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
192  *i = toupper(*i);
193  }
194 
195  return buffer;
196  }
197 
198 
199  /**
200  * Convert all character to lower case.
201  *
202  * \param value value
203  * \return string
204  */
205  inline std::string to_lower(const std::string& value)
206  {
207  using namespace std;
208 
209  string buffer(value);
210 
211  for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
212  *i = tolower(*i);
213  }
214 
215  return buffer;
216  }
217 
218 
219  /**
220  * Count number of white space separated tokens.
221  *
222  * \param buffer input string
223  * \return number of tokens
224  */
225  inline size_t get_number_of_tokens(const std::string& buffer)
226  {
227  using namespace std;
228 
229  istringstream is(buffer);
230 
231  return distance(istream_iterator<string>(is), istream_iterator<string>());
232  }
233 
234 
235  /**
236  * Check quotation.
237  *
238  * \param value value
239  * \return true if quoted; else false
240  */
241  inline bool is_single_quoted(const std::string& value)
242  {
243  return (value.size() > 1 && *value.begin() == '\'' && *value.rbegin() == '\'');
244  }
245 
246 
247  /**
248  * Check quotation.
249  *
250  * \param value value
251  * \return true if quoted; else false
252  */
253  inline bool is_double_quoted(const std::string& value)
254  {
255  return (value.size() > 1 && *value.begin() == '\"' && *value.rbegin() == '\"');
256  }
257 
258 
259  /**
260  * Quote string.
261  *
262  * \param value value
263  * \return string
264  */
265  inline std::string single_quote(const std::string& value)
266  {
267  if (!is_single_quoted(value))
268  return "\'" + value + "\'";
269  else
270  return value;
271  }
272 
273 
274  /**
275  * Quote string.
276  *
277  * \param value value
278  * \return string
279  */
280  inline std::string double_quote(const std::string& value)
281  {
282  if (!is_double_quoted(value))
283  return "\"" + value + "\"";
284  else
285  return value;
286  }
287 }
288 
289 #endif
std::string to_lower(const std::string &value)
Convert all character to lower case.
std::string single_quote(const std::string &value)
Quote string.
std::string replace(const std::string &input, const std::string &target, const std::string &replacement)
Replace tokens in string.
bool is_double_quoted(const std::string &value)
Check quotation.
std::string double_quote(const std::string &value)
Quote string.
size_t getSize(T(&array)[N])
Get size of c-array.
Definition: JLangToolkit.hh:32
std::string trim(const std::string &buffer)
Trim string.
Definition: JLangToolkit.hh:79
bool is_identical(JFirst_t &first, JSecond_t &second)
Check if two objects are indentical.
Definition: JLangToolkit.hh:46
bool is_single_quoted(const std::string &value)
Check quotation.
size_t get_number_of_tokens(const std::string &buffer)
Count number of white space separated tokens.
bool is_integer(const std::string &buffer)
Check if string is an integer.
Definition: JLangToolkit.hh:58
std::string to_upper(const std::string &value)
Convert all character to upper case.
std::string to_string(const T &value)
Convert value to string.