caseless-str.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2007-2012 Shai Ayal
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_caseless_str_h)
00024 #define octave_caseless_str_h 1
00025 
00026 #include <string>
00027 
00028 class caseless_str : public std::string
00029 {
00030 public:
00031 
00032   typedef std::string::iterator iterator;
00033   typedef std::string::const_iterator const_iterator;
00034 
00035   caseless_str (void) : std::string () { }
00036   caseless_str (const std::string& s) : std::string (s) { }
00037   caseless_str (const char *s) : std::string (s) { }
00038 
00039   caseless_str (const caseless_str& name) : std::string (name) { }
00040 
00041   caseless_str& operator = (const caseless_str& pname)
00042   {
00043     std::string::operator = (pname);
00044     return *this;
00045   }
00046 
00047   operator std::string (void) const { return *this; }
00048 
00049   bool operator < (const std::string& s) const
00050   {
00051     const_iterator p1 = begin ();
00052     const_iterator p2 = s.begin ();
00053 
00054     while (p1 != end () && p2 != s.end ())
00055       {
00056         char lp1 = std::tolower (*p1), lp2 = std::tolower (*p2);
00057 
00058         if ( lp1 > lp2 )
00059           return false;
00060         if ( lp1 < lp2)
00061           return true;
00062 
00063         p1++;
00064         p2++;
00065       }
00066 
00067     if ( length () >= s.length ())
00068       return false;
00069     else
00070       return true;
00071   }
00072 
00073   // Case-insensitive comparison.
00074   bool compare (const std::string& s, size_t limit = std::string::npos) const
00075   {
00076     const_iterator p1 = begin ();
00077     const_iterator p2 = s.begin ();
00078 
00079     size_t k = 0;
00080 
00081     while (p1 != end () && p2 != s.end () && k++ < limit)
00082       {
00083         if (std::tolower (*p1) != std::tolower (*p2))
00084           return false;
00085 
00086         p1++;
00087         p2++;
00088       }
00089 
00090     return (limit == std::string::npos) ? size () == s.size () : k == limit;
00091   }
00092 };
00093 
00094 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines