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