inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/words
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_words
00005 #define inlib_words
00006 
00007 #include <string>
00008 #include <vector>
00009 
00010 namespace inlib {
00011 
00012 inline void words(const std::string& a_string,const std::string& a_sep,bool a_take_empty,std::vector<std::string>& a_words){
00013   //  If a_sep is for exa "|" and for "xxx||xxx" :
00014   //  - a_take_empty false : {"xxx","xxx"} will be created 
00015   //    (and NOT {"xxx","","xxx"}).
00016   //  - a_take_empty true : {"xxx","","xxx"} will be created.
00017   a_words.clear();
00018   if(a_string.empty()) return;
00019   std::string::size_type lim = (a_take_empty?0:1);
00020   if(a_sep.empty()) {
00021     a_words.push_back(a_string);
00022   } else {
00023     std::string::size_type l = a_string.length();
00024     std::string::size_type llimiter = a_sep.length();
00025     std::string::size_type pos = 0;
00026     while(true) {
00027       std::string::size_type index = a_string.find(a_sep,pos);
00028       if(index==std::string::npos){ // Last word.
00029         if((l-pos)>=lim) a_words.push_back(a_string.substr(pos,l-pos));
00030         break;
00031       } else {
00032         //     abcxxxef
00033         //     0  3  67
00034         if((index-pos)>=lim) a_words.push_back(a_string.substr(pos,index-pos));
00035         pos = index + llimiter;
00036       }
00037     }
00038   }
00039 }
00040 
00041 inline std::vector<std::string> words(const std::string& a_string,const std::string& a_limiter,bool a_take_empty = false){
00042   std::vector<std::string> v;
00043   words(a_string,a_limiter,a_take_empty,v);
00044   return v;
00045 }
00046 
00047 }
00048 
00049 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines