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