/* * Copyright (C) 2002 - David W. Durham * * This file is not part of any particular application. * * istring is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2 of the License, * or (at your option) any later version. * * istring is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #ifndef __istring_h__ #define __istring_h__ #include "../../config/common.h" /* --- istring class - "improved" string * * - This file contains a derived class, istring, to be used in place of the * normal C++ STL string whenever a string with more functionality is required. * It was written in response to the STL string's utter lacking of useful * methods, for instance: converting a number to a string, or trimming white * space . Many of the operations provided in istring are possible with the * STL but are often quite awkward, roundabout, and wordy to accomplish. - * Hopefully, this class will become obsolete when/if the * C++-ANSI-standard-making-people add more functionality to the STL string * class. * * - TODO: - Make this a template derived from basic_string and typedef istring * as i_basic_string just like string is I didn't do this already because * I need to make sure that if I did that, string and istring would be * completly interchangable just as it is being *derived* from string. - * Depending on the method, it should probably use the char_traits class to do * some work, but I'm not familiar enough with that class to use it. - The * formating of numbers to string has a problem when you right justify and * zeropad a negative number, the '-' sign comes after the padding. I don't * know how this should be fixed other than having the code handling the sign * character itself. */ #include #include #include #include namespace std { class istring : public std::string { public: // --- constructors explicit istring(const allocator_type &a=allocator_type()) : string(){} istring(const string &str,size_type pos=0,size_type n=npos, const allocator_type &a=allocator_type()) : string(str,pos,n){} istring(const char *s,size_type n,const allocator_type &a=allocator_type()) : string(s,n) { } istring(const char *s,const allocator_type &a=allocator_type()) : string(s) { } istring(size_type n,char c,const allocator_type &a=allocator_type()) : string(n,c) { } template istring(InputIterator begin,InputIterator end,const allocator_type &a=allocator_type()) : string(begin,end) { } // --- explicit number conversion constructors // integer or floating point type to string template explicit istring(const T val) { ostringstream ss; ss << val; assign(ss.str()); } // formatted integer type to string (min_width can be <0 to left justify or >=0 to right justify) template explicit istring(const T val,const int min_width,const bool zero_pad=false) { ostringstream ss; ss.width((min_width<0) ? -min_width : min_width); ss.setf((min_width<0) ? ios::left : ios::right); ss.fill((zero_pad && min_width>=0) ? '0' : ' '); ss << val; assign(ss.str()); } // formatted floating point type to string (min_width can be <0 to left justify or >=0 to right justify, precision is the number of place to show after the decimal place) template explicit istring(const T val,const int min_width,const int precision,const bool zero_pad=false) { ostringstream ss; ss.width((min_width<0) ? -min_width : min_width); ss.precision(precision); ss.setf(ios::fixed); ss.setf((min_width<0) ? ios::left : ios::right); ss.fill((zero_pad && min_width>=0) ? '0' : ' '); ss << val; assign(ss.str()); } // --- auto-cast to const char * operator const char * const() const { return c_str(); } // --- trim/pad operations // remove white-space from the left side of the string istring <rim() { const size_type len=length(); size_type i=0; while(i0) { i--; while(i>0 && isspace(operator[](i))) i--; if(i==0 && isspace(operator[](0))) erase(); else erase(i+1,(len-i)-1); } return *this; } // remove white-space from the left and right side of the string istring &trim() { return ltrim().rtrim(); } // add pad_char characters to the left side of the string such that the string is no shorter than min_length istring &lpad(const size_type min_length,const char pad_char=' ') { const size_type len=length(); if(lenmax_length) erase(max_length,size()-max_length); return *this; } // --- case manipulation // make all the characters in the string upper case istring &upper(const size_type pos=0,const size_type len=npos) { const size_type last= (len==npos) ? pos+length() : pos+len; for(size_type t=pos;tstr_len) last-=str_len; else return 0; size_type count=0; for(size_type t=pos;t