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.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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