source: rrlib_util/sStringUtils.cpp @ 123:68ba619b821a

14.08
Last change on this file since 123:68ba619b821a was 98:ea0238bf0832, checked in by Tobias Föhst <foehst@…>, 6 years ago

Added and updated license information

File size: 5.9 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
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.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/util/sStringUtils.cpp
23 *
24 * \author  Bernd Helge Schaefer
25 *
26 * \date    2005-11-15
27 *
28 */
29//----------------------------------------------------------------------
30// Includes
31//----------------------------------------------------------------------
32
33#include "rrlib/util/sStringUtils.h"
34
35// boost includes
36//#include <boost/algorithm/string.hpp> // trim
37
38//----------------------------------------------------------------------
39// Namespace declaration
40//----------------------------------------------------------------------
41namespace rrlib
42{
43namespace util
44{
45
46const size_t sStringUtils::max_chars;
47
48void sStringUtils::Tokenize(const std::string& str,
49                            std::vector<std::string>& tokens,
50                            const std::string& delimiters)
51{
52  // Skip delimiters at beginning.
53  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
54  // Find first "non-delimiter".
55  std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
56
57  while (std::string::npos != pos || std::string::npos != lastPos)
58  {
59    // Found a token, add it to the vector.
60    tokens.push_back(str.substr(lastPos, pos - lastPos));
61    // Skip delimiters.  Note the "not_of"
62    lastPos = str.find_first_not_of(delimiters, pos);
63    // Find next "non-delimiter"
64    pos = str.find_first_of(delimiters, lastPos);
65  }
66}
67
68void sStringUtils::RemoveBracketsAndSpaces(const char* input_str, char* output_str, const char replace_token)
69{
70
71  unsigned int num_chars = std::min(strlen(input_str), max_chars);
72
73  /*  if (num_chars > max_chars)
74  {
75    fprintf(stderr, "%s::RemoveBracketsAndSpaces>> string length (%d) exceeds max chars (%d)\n",
76            Description(), num_chars, max_chars);
77    fprintf(stderr, "Due to security reasons this is not permitted.\nOnly %d chars will be converted.\n",
78            max_chars);
79    num_chars = max_chars;
80    }*/
81  //@todo: line feeds, tabs etc are not handles so far ...
82  for (unsigned int i = 0; i < num_chars; i++)
83  {
84    if (input_str [i] != '(' &&
85        input_str [i] != ')' &&
86        input_str [i] != '{' &&
87        input_str [i] != '}' &&
88        input_str [i] != '[' &&
89        input_str [i] != ']' &&
90        input_str [i] != ' ' &&
91        input_str [i] != '.')
92    {
93      output_str [i] = input_str [i];
94    }
95    else
96    {
97      output_str [i] = replace_token;
98    }
99  }
100  output_str [num_chars] = '\0';
101}
102
103void sStringUtils::Replace(std::string &input_str, const char* target_token, const char* replace_token)
104{
105  size_t pos = input_str.find(target_token);
106  while (pos != std::string::npos)
107  {
108    input_str.replace(pos, strlen(target_token), replace_token);
109    pos = input_str.find(target_token);
110  }
111}
112
113void sStringUtils::Replace(char *input_str, char target_token, char replace_token)
114{
115  for (unsigned int i = 0; i < strlen(input_str); i++)
116  {
117    if (input_str[i] == target_token)
118    {
119      input_str[i] = replace_token;
120    }
121  }
122}
123
124std::string sStringUtils::ConstReplace(const std::string &input_str, const char* target_token, const char* replace_token)
125{
126  std::string output_str = input_str;
127  size_t pos = output_str.find(target_token);
128  while (pos != std::string::npos)
129  {
130    output_str.replace(pos, strlen(target_token), replace_token);
131    pos = output_str.find(target_token);
132  }
133  return output_str;
134}
135
136void sStringUtils::TrimWhitespace(std::string &text)
137{
138  std::string check(text);
139  std::string::size_type pos(text.size());
140
141  //trim right side
142  while (pos > 0 && std::isspace(text[pos - 1]))
143    --pos;
144  text.erase(pos);
145
146  // trim left side
147  pos = 0;
148  while (pos < text.size() && std::isspace(text[pos]))
149    ++pos;
150  text.erase(0, pos);
151  /*
152  boost::trim(check);
153  assert(text == check && "Internal Error: hand-coded string trimming does not return same value as boost::trim. Please go and tell tim.");
154  */
155}
156
157void sStringUtils::RemoveMultipleWhitespaces(std::string &text)
158{
159  // search from left
160  std::string::size_type counter = 0;
161  while (counter < text.size() - 1)
162  {
163    if (std::isspace(text.at(counter)) && std::isspace(text.at(counter + 1)))
164    {
165      text.erase(counter, 1);
166    }
167    else
168    {
169      counter++;
170    }
171  }
172}
173
174
175bool sStringUtils::EndsWith(const std::string& text, const std::string& element)
176{
177  if (text.length() < element.length()) // if element is longer than text, the text can not contain the element
178  {
179    return false;
180  }
181
182  std::size_t element_pos = text.rfind(element);
183
184  if (element_pos != std::string::npos)
185  {
186    return (element_pos == (text.length() - element.length())); // check whether the last characters equal the element
187  }
188  else
189  {
190    return false; // if element was not found in text
191  }
192
193}
194
195bool sStringUtils::BeginsWith(const std::string& text, const std::string& element)
196{
197  return (text.find(element) == 0);
198}
199
200bool sStringUtils::StringToBool(std::string s)
201{
202  if ((s == "true") || (s == "1"))
203    return true;
204  else
205    return false;
206}
207
208//----------------------------------------------------------------------
209// End of namespace declaration
210//----------------------------------------------------------------------
211}
212}
Note: See TracBrowser for help on using the repository browser.