Thrill  0.1
compare_icase.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/string/compare_icase.cpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2007-2017 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
12 #include <tlx/string/to_lower.hpp>
13 
14 #include <algorithm>
15 
16 namespace tlx {
17 
18 int compare_icase(const char* a, const char* b) {
19 
20  while (*a != 0 && *b != 0)
21  {
22  int ca = to_lower(*a++);
23  int cb = to_lower(*b++);
24 
25  if (ca == cb) continue;
26  if (ca < cb) return -1;
27  else return +1;
28  }
29 
30  if (*a == 0 && *b != 0) return +1;
31  else if (*a != 0 && *b == 0) return -1;
32  else return 0;
33 }
34 
35 int compare_icase(const char* a, const std::string& b) {
36 
37  std::string::const_iterator bi = b.begin();
38 
39  while (*a != 0 && bi != b.end())
40  {
41  int ca = to_lower(*a++);
42  int cb = to_lower(*bi++);
43 
44  if (ca == cb) continue;
45  if (ca < cb) return -1;
46  else return +1;
47  }
48 
49  if (*a == 0 && bi != b.end()) return +1;
50  else if (*a != 0 && bi == b.end()) return -1;
51  else return 0;
52 }
53 
54 int compare_icase(const std::string& a, const char* b) {
55  std::string::const_iterator ai = a.begin();
56 
57  while (ai != a.end() && *b != 0)
58  {
59  int ca = to_lower(*ai++);
60  int cb = to_lower(*b++);
61 
62  if (ca == cb) continue;
63  if (ca < cb) return -1;
64  else return +1;
65  }
66 
67  if (ai == a.end() && *b != 0) return +1;
68  else if (ai != a.end() && *b == 0) return -1;
69  else return 0;
70 }
71 
72 int compare_icase(const std::string& a, const std::string& b) {
73  std::string::const_iterator ai = a.begin();
74  std::string::const_iterator bi = b.begin();
75 
76  while (ai != a.end() && bi != b.end())
77  {
78  int ca = to_lower(*ai++);
79  int cb = to_lower(*bi++);
80 
81  if (ca == cb) continue;
82  if (ca < cb) return -1;
83  else return +1;
84  }
85 
86  if (ai == a.end() && bi != b.end()) return +1;
87  else if (ai != a.end() && bi == b.end()) return -1;
88  else return 0;
89 }
90 
91 } // namespace tlx
92 
93 /******************************************************************************/
char to_lower(char ch)
Transform the given character to lower case without any localization.
Definition: to_lower.cpp:17
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
int compare_icase(const char *a, const char *b)
returns +1/0/-1 like strcmp(a, b) but without regard for letter case