bpp-core3  3.0.0
StringTokenizer.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "StringTokenizer.h"
6 
7 using namespace bpp;
8 using namespace std;
9 
10 StringTokenizer::StringTokenizer(const std::string& s, const std::string& delimiters, bool solid, bool allowEmptyTokens) :
11  tokens_(),
12  splits_(),
13  currentPosition_(0)
14 {
15  if (!solid)
16  {
17  string::size_type index = s.find_first_not_of(delimiters, 0);
18  while (index != s.npos)
19  {
20  string::size_type newIndex = s.find_first_of(delimiters, index);
21  if (newIndex != s.npos)
22  {
23  tokens_.push_back(s.substr(index, newIndex - index));
24  if (!allowEmptyTokens)
25  index = s.find_first_not_of(delimiters, newIndex);
26  else
27  index = newIndex + 1;
28  splits_.push_back(s.substr(newIndex, index - newIndex));
29  }
30  else
31  {
32  tokens_.push_back(s.substr(index));
33  index = newIndex;
34  }
35  }
36  }
37  else
38  {
39  string::size_type index = 0;
40  while (index != s.npos)
41  {
42  string::size_type newIndex = s.find(delimiters, index);
43  if (newIndex != s.npos)
44  {
45  tokens_.push_back(s.substr(index, newIndex - index));
46  if (!allowEmptyTokens)
47  {
48  index = newIndex + delimiters.size();
49  while (index != string::npos && s.substr(index, delimiters.size()) == delimiters)
50  index += delimiters.size();
51  }
52  else
53  index = newIndex + delimiters.size();
54  splits_.push_back(s.substr(newIndex, index - newIndex));
55  }
56  else
57  {
58  tokens_.push_back(s.substr(index));
59  index = newIndex;
60  }
61  }
62  }
63 }
64 
66 {
67  for (size_t i = tokens_.size(); i > currentPosition_; i--)
68  {
69  if (tokens_[i - 1] == "")
70  tokens_.erase(tokens_.begin() + static_cast<ptrdiff_t>(i - 1));
71  }
72 }
73 
75 {
76  string s;
77  for (size_t i = currentPosition_; i < tokens_.size() - 1; ++i)
78  {
79  s += tokens_[i] + splits_[i];
80  }
81  if (numberOfRemainingTokens() > 0)
82  s += tokens_.back();
83  return s;
84 }
STL namespace.
size_t numberOfRemainingTokens() const
Tell how many tokens are available.
void removeEmptyTokens()
remove all empty token from the current position.
std::deque< std::string > tokens_
Where the tokens are stored.
std::string unparseRemainingTokens() const
std::deque< std::string > splits_
size_t currentPosition_
the current position in the token list.