GNU Octave  4.2.1
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-2017 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 "octave-config.h"
27 
28 #include <cctype>
29 #include <string>
30 
31 class caseless_str : public std::string
32 {
33 public:
34 
35  typedef std::string::iterator iterator;
36  typedef std::string::const_iterator const_iterator;
37 
38  caseless_str (void) : std::string () { }
39  caseless_str (const std::string& s) : std::string (s) { }
40  caseless_str (const char *s) : std::string (s) { }
41 
42  caseless_str (const caseless_str& name) : std::string (name) { }
43 
45  {
46  std::string::operator = (pname);
47  return *this;
48  }
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:40
for large enough k
Definition: lu.cc:606
STL namespace.
bool operator<(const std::string &s) const
Definition: caseless-str.h:50
s
Definition: file-io.cc:2682
caseless_str(const caseless_str &name)
Definition: caseless-str.h:42
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
std::string::const_iterator const_iterator
Definition: caseless-str.h:36
caseless_str & operator=(const caseless_str &pname)
Definition: caseless-str.h:44
std::string pname
Definition: graphics.cc:11207
caseless_str(void)
Definition: caseless-str.h:38
bool compare(const std::string &s, size_t limit=std::string::npos) const
Definition: caseless-str.h:76
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable or any other valid Octave code The number of return their size
Definition: input.cc:871
std::string::iterator iterator
Definition: caseless-str.h:35
caseless_str(const std::string &s)
Definition: caseless-str.h:39
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854