source: rrlib_util/sStringUtils.cpp @ 139:804baa38d03b

Last change on this file since 139:804baa38d03b was 139:804baa38d03b, checked in by Jens Wettach <wettach@…>, 4 years ago

moved sStringUtils::Tokenize to string.h, replaced some functions of sStringUtils with corresponding functions in string.h and marked the old ones as deprecated

File size: 4.6 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#include "rrlib/util/string.h"
35
36//----------------------------------------------------------------------
37// Namespace declaration
38//----------------------------------------------------------------------
39namespace rrlib
40{
41namespace util
42{
43
44const size_t sStringUtils::max_chars;
45
46void sStringUtils::Tokenize(const std::string& str,
47                            std::vector<std::string>& tokens,
48                            const std::string& delimiters)
49{
50  return rrlib::util::Tokenize(str, tokens, delimiters);
51}
52
53void sStringUtils::RemoveBracketsAndSpaces(const char* input_str, char* output_str, const char replace_token)
54{
55
56  unsigned int num_chars = std::min(strlen(input_str), max_chars);
57
58  /*  if (num_chars > max_chars)
59  {
60    fprintf(stderr, "%s::RemoveBracketsAndSpaces>> string length (%d) exceeds max chars (%d)\n",
61            Description(), num_chars, max_chars);
62    fprintf(stderr, "Due to security reasons this is not permitted.\nOnly %d chars will be converted.\n",
63            max_chars);
64    num_chars = max_chars;
65    }*/
66  //@todo: line feeds, tabs etc are not handles so far ...
67  for (unsigned int i = 0; i < num_chars; i++)
68  {
69    if (input_str [i] != '(' &&
70        input_str [i] != ')' &&
71        input_str [i] != '{' &&
72        input_str [i] != '}' &&
73        input_str [i] != '[' &&
74        input_str [i] != ']' &&
75        input_str [i] != ' ' &&
76        input_str [i] != '.')
77    {
78      output_str [i] = input_str [i];
79    }
80    else
81    {
82      output_str [i] = replace_token;
83    }
84  }
85  output_str [num_chars] = '\0';
86}
87
88void sStringUtils::Replace(std::string &input_str, const char* target_token, const char* replace_token)
89{
90  size_t pos = input_str.find(target_token);
91  while (pos != std::string::npos)
92  {
93    input_str.replace(pos, strlen(target_token), replace_token);
94    pos = input_str.find(target_token);
95  }
96}
97
98void sStringUtils::Replace(char *input_str, char target_token, char replace_token)
99{
100  for (unsigned int i = 0; i < strlen(input_str); i++)
101  {
102    if (input_str[i] == target_token)
103    {
104      input_str[i] = replace_token;
105    }
106  }
107}
108
109std::string sStringUtils::ConstReplace(const std::string &input_str, const char* target_token, const char* replace_token)
110{
111  std::string output_str = input_str;
112  size_t pos = output_str.find(target_token);
113  while (pos != std::string::npos)
114  {
115    output_str.replace(pos, strlen(target_token), replace_token);
116    pos = output_str.find(target_token);
117  }
118  return output_str;
119}
120
121void sStringUtils::TrimWhitespace(std::string &text)
122{
123  return util::TrimWhitespace(text);
124}
125
126void sStringUtils::RemoveMultipleWhitespaces(std::string &text)
127{
128  // search from left
129  std::string::size_type counter = 0;
130  while (counter < text.size() - 1)
131  {
132    if (std::isspace(text.at(counter)) && std::isspace(text.at(counter + 1)))
133    {
134      text.erase(counter, 1);
135    }
136    else
137    {
138      counter++;
139    }
140  }
141}
142
143
144bool sStringUtils::EndsWith(const std::string& text, const std::string& element)
145{
146  return util::EndsWith(text, element);
147}
148
149bool sStringUtils::BeginsWith(const std::string& text, const std::string& element)
150{
151  return util::StartsWith(text, element);
152}
153
154bool sStringUtils::StringToBool(std::string s)
155{
156  if ((s == "true") || (s == "1"))
157    return true;
158  else
159    return false;
160}
161
162//----------------------------------------------------------------------
163// End of namespace declaration
164//----------------------------------------------------------------------
165}
166}
Note: See TracBrowser for help on using the repository browser.