GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
caseless-str.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2007-2013 Shai Ayal
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_caseless_str_h)
24 #define octave_caseless_str_h 1
25 
26 #include <cctype>
27 #include <string>
28 
29 class caseless_str : public std::string
30 {
31 public:
32 
33  typedef std::string::iterator iterator;
34  typedef std::string::const_iterator const_iterator;
35 
36  caseless_str (void) : std::string () { }
37  caseless_str (const std::string& s) : std::string (s) { }
38  caseless_str (const char *s) : std::string (s) { }
39 
40  caseless_str (const caseless_str& name) : std::string (name) { }
41 
43  {
45  return *this;
46  }
47 
48  operator std::string (void) const { return *this; }
49 
50  bool operator < (const std::string& s) const
51  {
52  const_iterator p1 = begin ();
53  const_iterator p2 = s.begin ();
54 
55  while (p1 != end () && p2 != s.end ())
56  {
57  char lp1 = std::tolower (*p1), lp2 = std::tolower (*p2);
58 
59  if ( lp1 > lp2 )
60  return false;
61  if ( lp1 < lp2)
62  return true;
63 
64  p1++;
65  p2++;
66  }
67 
68  if ( length () >= s.length ())
69  return false;
70  else
71  return true;
72  }
73 
74  // Case-insensitive comparison.
75  bool compare (const std::string& s, size_t limit = std::string::npos) const
76  {
77  const_iterator p1 = begin ();
78  const_iterator p2 = s.begin ();
79 
80  size_t k = 0;
81 
82  while (p1 != end () && p2 != s.end () && k++ < limit)
83  {
84  if (std::tolower (*p1) != std::tolower (*p2))
85  return false;
86 
87  p1++;
88  p2++;
89  }
90 
91  return (limit == std::string::npos) ? size () == s.size () : k == limit;
92  }
93 };
94 
95 #endif