Warning, /include/Geant4/tools/words is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_words
0005 #define tools_words
0006
0007 #include <string>
0008 #include <vector>
0009
0010 namespace tools {
0011
0012 inline void words(const std::string& a_string,const std::string& a_sep,bool a_take_empty,
0013 std::vector<std::string>& a_words,bool a_clear = true){
0014 // If a_sep is for exa "|" and for "xxx||xxx" :
0015 // - a_take_empty false : {"xxx","xxx"} will be created
0016 // (and NOT {"xxx","","xxx"}).
0017 // - a_take_empty true : {"xxx","","xxx"} will be created.
0018 if(a_clear) a_words.clear();
0019 if(a_string.empty()) return;
0020 std::string::size_type lim = (a_take_empty?0:1);
0021 if(a_sep.empty()) {
0022 a_words.push_back(a_string);
0023 } else {
0024 std::string::size_type l = a_string.length();
0025 std::string::size_type llimiter = a_sep.length();
0026 std::string::size_type pos = 0;
0027 while(true) {
0028 std::string::size_type index = a_string.find(a_sep,pos);
0029 if(index==std::string::npos){ // Last word.
0030 if((l-pos)>=lim) a_words.push_back(a_string.substr(pos,l-pos));
0031 break;
0032 } else {
0033 // abcxxxef
0034 // 0 3 67
0035 if((index-pos)>=lim) a_words.push_back(a_string.substr(pos,index-pos));
0036 pos = index + llimiter;
0037 }
0038 }
0039 }
0040 }
0041
0042 #ifdef TOOLS_DEPRECATED
0043 inline std::vector<std::string> words(const std::string& a_string,const std::string& a_limiter,bool a_take_empty = false){
0044 std::vector<std::string> v;
0045 words(a_string,a_limiter,a_take_empty,v);
0046 return v;
0047 }
0048 #endif //TOOLS_DEPRECATED
0049
0050 inline void words(const std::string& a_string,
0051 const std::string& a_sep,bool a_take_empty,
0052 //output :
0053 unsigned int& a_wn,
0054 std::string::size_type a_wps[],
0055 std::string::size_type a_wls[]){
0056 //used to optimize tools::match().
0057 a_wn = 0;
0058 if(a_string.empty()) return;
0059 std::string::size_type lim = (a_take_empty?0:1);
0060 if(a_sep.empty()) {
0061 //a_words.push_back(a_string);
0062 a_wps[a_wn] = 0;
0063 a_wls[a_wn] = a_string.length();
0064 a_wn++;
0065 } else {
0066 std::string::size_type l = a_string.length();
0067 std::string::size_type llimiter = a_sep.length();
0068 std::string::size_type pos = 0;
0069 while(true) {
0070 std::string::size_type index = a_string.find(a_sep,pos);
0071 if(index==std::string::npos){ // Last word.
0072 if((l-pos)>=lim) {
0073 //a_words.push_back(a_string.substr(pos,l-pos));
0074 a_wps[a_wn] = pos;
0075 a_wls[a_wn] = l-pos;
0076 a_wn++;
0077 }
0078 break;
0079 } else {
0080 // abcxxxef
0081 // 0 3 67
0082 if((index-pos)>=lim) {
0083 //a_words.push_back(a_string.substr(pos,index-pos));
0084 a_wps[a_wn] = pos;
0085 a_wls[a_wn] = index-pos;
0086 a_wn++;
0087 }
0088 pos = index + llimiter;
0089 }
0090 }
0091 }
0092 }
0093
0094 }
0095
0096 #endif