GNU Octave  4.0.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-2015 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  {
44  std::string::operator = (pname);
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);
58  char lp2 = std::tolower (*p2);
59 
60  if (lp1 > lp2)
61  return false;
62  if (lp1 < lp2)
63  return true;
64 
65  p1++;
66  p2++;
67  }
68 
69  if (length () >= s.length ())
70  return false;
71  else
72  return true;
73  }
74 
75  // Case-insensitive comparison.
76  bool compare (const std::string& s, size_t limit = std::string::npos) const
77  {
78  const_iterator p1 = begin ();
79  const_iterator p2 = s.begin ();
80 
81  size_t k = 0;
82 
83  while (p1 != end () && p2 != s.end () && k++ < limit)
84  {
85  if (std::tolower (*p1) != std::tolower (*p2))
86  return false;
87 
88  p1++;
89  p2++;
90  }
91 
92  return (limit == std::string::npos) ? size () == s.size () : k == limit;
93  }
94 };
95 
96 #endif
caseless_str(const char *s)
Definition: caseless-str.h:38
STL namespace.
bool operator<(const std::string &s) const
Definition: caseless-str.h:50
caseless_str(const caseless_str &name)
Definition: caseless-str.h:40
std::string::const_iterator const_iterator
Definition: caseless-str.h:34
caseless_str & operator=(const caseless_str &pname)
Definition: caseless-str.h:42
size_t size(T const (&)[z])
Definition: help.cc:103
caseless_str(void)
Definition: caseless-str.h:36
bool compare(const std::string &s, size_t limit=std::string::npos) const
Definition: caseless-str.h:76
std::string::iterator iterator
Definition: caseless-str.h:33
caseless_str(const std::string &s)
Definition: caseless-str.h:37