Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StringUtils.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include "StringUtils.hh"
26 
27 void BIAS::StringUtils::Explode(const std::string &str,
28  const std::string &seperator,
29  std::vector<std::string> &ret)
30 {
31  ret.clear();
32 
33  const int sepLen = seperator.length();
34 
35  // these indices mark beginning and end of the current token
36  register int posL = 0;
37  register int posR = str.find(seperator);
38 
39  // iterate over string
40  while (posR > -1) {
41  // skip empty strings (this can happen, when the seperator occures multiple
42  // times successively)
43  if (posL != posR) {
44  ret.push_back(str.substr(posL, posR - posL));
45  }
46 
47  // get indices for next iteration
48  posL = posR + sepLen;
49  posR = str.find(seperator, posL);
50  }
51 
52  // if the string doesn't end with the seperator, push_back the last token
53  if (posR == -1 && (unsigned int)posL < str.length()) {
54  ret.push_back(str.substr(posL, str.length() - posL));
55  }
56 }
57 
58 
59 void BIAS::StringUtils::SplitByChars(const std::string &str,
60  std::vector<std::string> &ret,
61  const std::string &charsToSplit)
62 {
63  //std::cout << "StringUtils::SplitByChars() : ";
64  ret.clear();
65  size_t nextPos, firstPos = str.find_first_not_of(charsToSplit);
66  while (firstPos != std::string::npos) {
67  nextPos = str.find_first_of(charsToSplit, firstPos);
68  if (nextPos != std::string::npos) {
69  ret.push_back(str.substr(firstPos, nextPos-firstPos));
70  firstPos = str.find_first_not_of(charsToSplit, nextPos);
71  } else {
72  ret.push_back(str.substr(firstPos, str.size()-firstPos));
73  firstPos = std::string::npos;
74  }
75  //std::cout << "\" << tokens[tokens.size()-1] << "\" ";
76  }
77  //std::cout << endl;
78 }
79 
80 
81 void BIAS::StringUtils::Trim(const std::string &str,
82  std::string &ret,
83  const std::string &charsToTrim)
84 {
85  const int left = str.find_first_not_of(charsToTrim);
86  const int right = str.find_last_not_of(charsToTrim);
87 
88  if (left != -1 && right != -1) {
89  ret = str.substr(left, right - left + 1);
90  }
91  else { // string only consists of charsToTrim characters or is empty
92  ret = "";
93  }
94 }
static void Trim(const std::string &str, std::string &ret, const std::string &charsToTrim=" \t\n")
Trims the given string, i.e.
Definition: StringUtils.cpp:81
static void Explode(const std::string &str, const std::string &seperator, std::vector< std::string > &ret)
Explodes the given string into tokens which are separated by the given separator string.
Definition: StringUtils.cpp:27
static void SplitByChars(const std::string &str, std::vector< std::string > &ret, const std::string &charsToSplit="\t\n ,;")
Splits the given string into tokens which are separated by any of the given separator characters (sim...
Definition: StringUtils.cpp:59