Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

strutils.h

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