Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

strutils.h

00001 /***************************************************************************
00002     copyright            : (C) 2002-2005 by Stefano Barbato
00003     email                : stefano@codesink.org
00004 
00005     $Id: strutils.h,v 1.8 2005/02/23 10:26:14 tat Exp $
00006  ***************************************************************************/
00007 
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #ifndef _MIMETIC_STRINGUTILS_H_
00017 #define _MIMETIC_STRINGUTILS_H_
00018 #include <string>
00019 #include <iostream>
00020 #include <algorithm>
00021 #include <cstring>
00022 
00023 namespace mimetic
00024 {
00025 
00026 extern const std::string nullstring;
00027 
00028 struct ichar_traits : public std::char_traits<char>
00029 {
00030     static bool eq (const char_type & c1, const char_type& c2)
00031     {    return (toupper(c1) == toupper(c2));    }
00032     static bool ne (const char_type& c1, const char_type& c2)
00033     {    return (toupper(c1) != toupper(c2));    }
00034     static bool lt (const char_type& c1, const char_type& c2)
00035     {    return (toupper(c1) < toupper(c2));    }
00036     static int compare (const char_type* s1, const char_type* s2, size_t n)
00037     {
00038         for(size_t i=0; i < n; ++i)
00039             if(toupper(s1[i]) != toupper(s2[i]))
00040                 return (toupper(s1[i]) < toupper(s2[i])) ?-1: 1;
00041         return 0;
00042     }
00043     static const char* find( const char* s, int n, char a ) 
00044     {
00045         while( n-- > 0 && tolower(*s) != tolower(a) ) 
00046                          ++s;
00047         return s;
00048     }
00049 };
00050 
00051 //typedef std::istring <char, ichar_traits> istring;
00052 using std::string;
00053 
00054 struct istring: public string
00055 {
00056     istring()
00057     {}
00058     //typedef std::string::allocator_type allocator_type;
00059     istring(const std::string& right)
00060     : string(right)
00061     {}
00062     explicit istring(const allocator_type& al)
00063     : string(al)
00064     {}
00065     istring(const istring& right)
00066     : string(right)
00067     {}
00068     istring(const istring& right, size_type roff, size_type count = npos)
00069     : string(right, roff, count)
00070     {}
00071     istring(const istring& right, size_type roff, size_type count, 
00072         const allocator_type& al)
00073     : string(right, roff, count, al)
00074     {}
00075     istring(const value_type *ptr, size_type count)
00076     : string(ptr, count)
00077     {}
00078     istring(const value_type *ptr, size_type count,const allocator_type& al)
00079     : string(ptr, count, al)
00080     {}
00081     istring(const value_type *ptr)
00082     : string(ptr)
00083     {}
00084     istring(const value_type *ptr,const allocator_type& al)
00085     : string(ptr, al)
00086     {}
00087     istring(size_type count, value_type ch)
00088     : string(count,ch)
00089     {}
00090     istring(size_type count, value_type ch,const allocator_type& al)
00091     : string(count,ch,al)
00092     {}
00093     template <class InIt>
00094     istring(InIt first, InIt last)
00095     : string(first, last)
00096     {}
00097     template <class InIt>
00098     istring(InIt first, InIt last,const allocator_type& al)
00099     : string(first, last, al)
00100     {}
00101 };
00102 
00103 
00104 inline bool operator==(const istring& is, const std::string& s)
00105 {
00106     return (0 == ichar_traits::compare(is.c_str(),s.c_str(),
00107             std::max(is.length(),s.length())) );
00108 }
00109 
00110 inline bool operator!=(const istring& is, const std::string& s)
00111 {
00112     return (0 != ichar_traits::compare(is.c_str(),s.c_str(),
00113             std::max(is.length(),s.length())) );
00114 }
00115 
00116 inline bool operator!=(const istring& is, const char* str)
00117 {
00118     return (0 != ichar_traits::compare(is.c_str(),str,
00119             std::max(is.length(),::strlen(str))) );
00120 }
00121 
00122 inline bool operator==(const istring& is, const char* str)
00123 {
00124     return (0 == ichar_traits::compare(is.c_str(),str,
00125             std::max(is.length(),::strlen(str))) );
00126 }
00127 
00128 inline std::string dquoted(const std::string& s)
00129 {    
00130     return "\"" + s + "\"";
00131 }
00132 
00133 inline std::string parenthed(const std::string& s)
00134 {    
00135     return "(" + s + ")";
00136 }
00137 
00138 /// removes double quotes
00139 inline std::string remove_dquote(const std::string& s)
00140 {
00141     int len = s.length();
00142     if( len < 2)
00143         return s;
00144     if(s[0] == '"' && s[len-1] == '"')
00145         return std::string(s, 1, len-2);
00146     return s;
00147 }
00148 
00149 /**
00150  * returns the \e canonical representation of \p s (see RFC822)
00151  * if \p no_ws is true removes all blanks from the resulting string
00152  */
00153 std::string canonical(const std::string& s, bool no_ws = false);
00154 
00155 /// removes leading and trailing blanks
00156 inline std::string remove_external_blanks(const std::string& in)
00157 {
00158     if(!in.length())
00159         return in;
00160     std::string s = in;
00161     int beg = 0, end = s.length();
00162     for(; beg < end; ++beg)
00163         if(s[beg] != ' ' && s[beg] != '\t')
00164             break;
00165     end = s.length() - 1;
00166     for(; end > beg; --end)
00167         if(s[end] != ' ' && s[end] != '\t')
00168             break;
00169     s.assign(std::string(s, beg, end - beg + 1));
00170     return s;
00171 }
00172 
00173 }
00174 
00175 #endif
00176