g4tools  5.4.0
words
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_words
5 #define tools_words
6 
7 #include <string>
8 #include <vector>
9 
10 namespace tools {
11 
12 inline void words(const std::string& a_string,const std::string& a_sep,bool a_take_empty,
13  std::vector<std::string>& a_words,bool a_clear = true){
14  // If a_sep is for exa "|" and for "xxx||xxx" :
15  // - a_take_empty false : {"xxx","xxx"} will be created
16  // (and NOT {"xxx","","xxx"}).
17  // - a_take_empty true : {"xxx","","xxx"} will be created.
18  if(a_clear) a_words.clear();
19  if(a_string.empty()) return;
20  std::string::size_type lim = (a_take_empty?0:1);
21  if(a_sep.empty()) {
22  a_words.push_back(a_string);
23  } else {
24  std::string::size_type l = a_string.length();
25  std::string::size_type llimiter = a_sep.length();
26  std::string::size_type pos = 0;
27  while(true) {
28  std::string::size_type index = a_string.find(a_sep,pos);
29  if(index==std::string::npos){ // Last word.
30  if((l-pos)>=lim) a_words.push_back(a_string.substr(pos,l-pos));
31  break;
32  } else {
33  // abcxxxef
34  // 0 3 67
35  if((index-pos)>=lim) a_words.push_back(a_string.substr(pos,index-pos));
36  pos = index + llimiter;
37  }
38  }
39  }
40 }
41 
42 #ifdef TOOLS_DEPRECATED
43 inline std::vector<std::string> words(const std::string& a_string,const std::string& a_limiter,bool a_take_empty = false){
44  std::vector<std::string> v;
45  words(a_string,a_limiter,a_take_empty,v);
46  return v;
47 }
48 #endif //TOOLS_DEPRECATED
49 
50 inline void words(const std::string& a_string,
51  const std::string& a_sep,bool a_take_empty,
52  //output :
53  unsigned int& a_wn,
54  std::string::size_type a_wps[],
55  std::string::size_type a_wls[]){
56  //used to optimize tools::match().
57  a_wn = 0;
58  if(a_string.empty()) return;
59  std::string::size_type lim = (a_take_empty?0:1);
60  if(a_sep.empty()) {
61  //a_words.push_back(a_string);
62  a_wps[a_wn] = 0;
63  a_wls[a_wn] = a_string.length();
64  a_wn++;
65  } else {
66  std::string::size_type l = a_string.length();
67  std::string::size_type llimiter = a_sep.length();
68  std::string::size_type pos = 0;
69  while(true) {
70  std::string::size_type index = a_string.find(a_sep,pos);
71  if(index==std::string::npos){ // Last word.
72  if((l-pos)>=lim) {
73  //a_words.push_back(a_string.substr(pos,l-pos));
74  a_wps[a_wn] = pos;
75  a_wls[a_wn] = l-pos;
76  a_wn++;
77  }
78  break;
79  } else {
80  // abcxxxef
81  // 0 3 67
82  if((index-pos)>=lim) {
83  //a_words.push_back(a_string.substr(pos,index-pos));
84  a_wps[a_wn] = pos;
85  a_wls[a_wn] = index-pos;
86  a_wn++;
87  }
88  pos = index + llimiter;
89  }
90  }
91  }
92 }
93 
94 }
95 
96 #endif
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::words
void words(const std::string &a_string, const std::string &a_sep, bool a_take_empty, std::vector< std::string > &a_words, bool a_clear=true)
Definition: words:12