inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/strip
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_strip
00005 #define inlib_strip
00006 
00007 #include <vector>
00008 #include <string>
00009 
00010 namespace inlib {
00011 
00012 enum what { leading, trailing, both };
00013 
00014 inline void strip(std::string& a_string,what a_type = both,char a_char = ' '){
00015   std::string::size_type l = a_string.length();
00016   if(l==0) return;
00017 
00018   switch ( a_type ) {
00019   case leading:{
00020     std::string::size_type i;
00021     char* pos = (char*)a_string.c_str();
00022     for(i=0;i<l;i++,pos++) {
00023       if(*pos!=a_char) {
00024         a_string = a_string.substr(i,l-i);
00025         return;
00026       }
00027     }
00028     }break;
00029   case trailing:{
00030     std::string::size_type i;
00031     char* pos = (char*)a_string.c_str();
00032     pos += (l-1);
00033     for(i=l-1;;i--,pos--) {
00034       if(*pos!=a_char) {
00035         a_string = a_string.substr(0,i+1);
00036         return;
00037       }
00038     }
00039     }break;
00040   case both:
00041     strip(a_string,leading,a_char);
00042     strip(a_string,trailing,a_char);
00043     break;
00044   //default:break;
00045   }
00046 }
00047 
00048 inline std::string strp(const std::string& a_string,what a_type = both,char a_char = ' '){
00049   std::string s(a_string);
00050   strip(s,a_type,a_char);
00051   return s;
00052 }
00053 
00054 inline void strip(std::vector<std::string>& a_strings,what a_type = both,char a_char = ' ') {
00055   std::vector<std::string>::iterator it;
00056   for(it=a_strings.begin();it!=a_strings.end();++it) {
00057     strip(*it,a_type,a_char);
00058   }
00059 }
00060 
00061 }
00062 
00063 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines