bpp-core3  3.0.0
StringTokenizer.cpp
Go to the documentation of this file.
1 //
2 // File: StringTokenizer.cpp
3 // Authors:
4 // Julien Dutheil
5 // Sylvain Gaillard
6 // Last modified: 2004-09-20 00:00:00
7 //
8 
9 /*
10  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
11 
12  This software is a computer program whose purpose is to provide utilitary
13  classes. This file belongs to the Bio++ Project.
14 
15  This software is governed by the CeCILL license under French law and
16  abiding by the rules of distribution of free software. You can use,
17  modify and/ or redistribute the software under the terms of the CeCILL
18  license as circulated by CEA, CNRS and INRIA at the following URL
19  "http://www.cecill.info".
20 
21  As a counterpart to the access to the source code and rights to copy,
22  modify and redistribute granted by the license, users are provided only
23  with a limited warranty and the software's author, the holder of the
24  economic rights, and the successive licensors have only limited
25  liability.
26 
27  In this respect, the user's attention is drawn to the risks associated
28  with loading, using, modifying and/or developing or reproducing the
29  software by the user in light of its specific status of free software,
30  that may mean that it is complicated to manipulate, and that also
31  therefore means that it is reserved for developers and experienced
32  professionals having in-depth computer knowledge. Users are therefore
33  encouraged to load and test the software's suitability as regards their
34  requirements in conditions enabling the security of their systems and/or
35  data to be ensured and, more generally, to use and operate it in the
36  same conditions as regards security.
37 
38  The fact that you are presently reading this means that you have had
39  knowledge of the CeCILL license and that you accept its terms.
40 */
41 
42 
43 #include "StringTokenizer.h"
44 
45 using namespace bpp;
46 using namespace std;
47 
48 StringTokenizer::StringTokenizer(const std::string& s, const std::string& delimiters, bool solid, bool allowEmptyTokens) :
49  tokens_(),
50  splits_(),
51  currentPosition_(0)
52 {
53  if (!solid)
54  {
55  string::size_type index = s.find_first_not_of(delimiters, 0);
56  while (index != s.npos)
57  {
58  string::size_type newIndex = s.find_first_of(delimiters, index);
59  if (newIndex != s.npos)
60  {
61  tokens_.push_back(s.substr(index, newIndex - index));
62  if (!allowEmptyTokens)
63  index = s.find_first_not_of(delimiters, newIndex);
64  else
65  index = newIndex + 1;
66  splits_.push_back(s.substr(newIndex, index - newIndex));
67  }
68  else
69  {
70  tokens_.push_back(s.substr(index));
71  index = newIndex;
72  }
73  }
74  }
75  else
76  {
77  string::size_type index = 0;
78  while (index != s.npos)
79  {
80  string::size_type newIndex = s.find(delimiters, index);
81  if (newIndex != s.npos)
82  {
83  tokens_.push_back(s.substr(index, newIndex - index));
84  if (!allowEmptyTokens)
85  {
86  index = newIndex + delimiters.size();
87  while (index != string::npos && s.substr(index, delimiters.size()) == delimiters)
88  index += delimiters.size();
89  }
90  else
91  index = newIndex + delimiters.size();
92  splits_.push_back(s.substr(newIndex, index - newIndex));
93  }
94  else
95  {
96  tokens_.push_back(s.substr(index));
97  index = newIndex;
98  }
99  }
100  }
101 }
102 
104 {
105  for (size_t i = tokens_.size(); i > currentPosition_; i--)
106  {
107  if (tokens_[i - 1] == "")
108  tokens_.erase(tokens_.begin() + static_cast<ptrdiff_t>(i - 1));
109  }
110 }
111 
113 {
114  string s;
115  for (size_t i = currentPosition_; i < tokens_.size() - 1; ++i)
116  {
117  s += tokens_[i] + splits_[i];
118  }
119  if (numberOfRemainingTokens() > 0)
120  s += tokens_.back();
121  return s;
122 }
size_t numberOfRemainingTokens() const
Tell how many tokens are available.
size_t currentPosition_
the current position in the token list.
std::string unparseRemainingTokens() const
std::deque< std::string > splits_
std::deque< std::string > tokens_
Where the tokens are stored.
void removeEmptyTokens()
remove all empty token from the current position.