// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.

#ifndef tools_strip
#define tools_strip

#include <vector>
#include <string>

namespace tools {

enum what { leading, trailing, both };

inline void strip(std::string& a_string,what a_type = both,char a_char = ' '){
  std::string::size_type l = a_string.length();
  if(l==0) return;

  switch ( a_type ) {
  case leading:{
    std::string::size_type i;
    char* pos = (char*)a_string.c_str();
    for(i=0;i<l;i++,pos++) {
      if(*pos!=a_char) {
        a_string = a_string.substr(i,l-i);
        return;
      }
    }
    }break;
  case trailing:{
    std::string::size_type i;
    char* pos = (char*)a_string.c_str();
    pos += (l-1);
    for(i=l-1;;i--,pos--) {
      if(*pos!=a_char) {
        a_string = a_string.substr(0,i+1);
        return;
      }
    }
    }break;
  case both:
    strip(a_string,leading,a_char);
    strip(a_string,trailing,a_char);
    break;
  //default:break;
  }
}

inline std::string strp(const std::string& a_string,what a_type = both,char a_char = ' '){
  std::string s(a_string);
  strip(s,a_type,a_char);
  return s;
}

inline void strip(std::vector<std::string>& a_strings,what a_type = both,char a_char = ' ') {
  std::vector<std::string>::iterator it;
  for(it=a_strings.begin();it!=a_strings.end();++it) {
    strip(*it,a_type,a_char);
  }
}

}

#endif
