neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
string_ci.hpp
Go to the documentation of this file.
1// string_ci.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <string>
41
42namespace neolib
43{
44 template <typename Traits>
45 struct ci_char_traits : Traits
46 {
47 public:
48 typedef typename Traits::char_type char_type;
49 typedef typename Traits::int_type int_type;
50 public:
51 static int compare(const char_type* s1, const char_type* s2, std::size_t n)
52 {
53 for(;n-- > 0;++s1, ++s2)
54 {
55 if (eq(*s1, *s2))
56 continue;
57 else if (lt(*s1, *s2))
58 return -1;
59 else
60 return 1;
61 }
62 return 0;
63 }
64 static const char_type* find(const char_type* str, std::size_t n, const char_type& c)
65 {
66 while(n > 0)
67 {
68 if (eq(*str, c))
69 return str;
70 ++str;
71 --n;
72 }
73 return 0;
74 }
75 static bool eq(const char_type& c1, const char_type& c2)
76 {
77 return lower(c1) == lower(c2);
78 }
79 static bool lt(const char_type& c1, const char_type& c2)
80 {
81 return lower(c1) < lower(c2);
82 }
84 {
85 return neolib::to_lower(c);
86 }
87 };
88
89 typedef std::basic_string<char, ci_char_traits<std::char_traits<char> > > ci_string;
90 typedef std::basic_string<char16_t, ci_char_traits<std::char_traits<char16_t> > > ci_u16string;
91
92 inline ci_string make_ci_string(const std::string& s)
93 {
94 return ci_string(s.begin(), s.end());
95 }
96 inline ci_u16string make_ci_string(const std::u16string& s)
97 {
98 return ci_u16string(s.begin(), s.end());
99 }
100
101 inline std::string make_string(const ci_string& s)
102 {
103 return std::string(s.begin(), s.end());
104 }
105 inline std::u16string make_string(const ci_u16string & s)
106 {
107 return std::u16string(s.begin(), s.end());
108 }
109
110 inline bool operator==(const ci_string& s1, const std::string& s2)
111 {
112 return s1 == ci_string(s2.begin(), s2.end());
113 }
114 inline bool operator==(const std::string& s1, const ci_string& s2)
115 {
116 return ci_string(s1.begin(), s1.end()) == s2;
117 }
118 inline bool operator!=(const ci_string& s1, const std::string& s2)
119 {
120 return s1 != ci_string(s2.begin(), s2.end());
121 }
122 inline bool operator!=(const std::string& s1, const ci_string& s2)
123 {
124 return ci_string(s1.begin(), s1.end()) != s2;
125 }
126 inline bool operator<(const ci_string& s1, const std::string& s2)
127 {
128 return s1 < ci_string(s2.begin(), s2.end());
129 }
130 inline bool operator<(const std::string& s1, const ci_string& s2)
131 {
132 return ci_string(s1.begin(), s1.end()) < s2;
133 }
134
135 template <typename CharT, typename Traits, typename Alloc>
136 inline bool lexicographical_compare_ignoring_case(const std::basic_string<CharT, Traits, Alloc>& s1,
137 const std::basic_string<CharT, Traits, Alloc>& s2)
138 {
139 typedef std::basic_string<CharT, Traits, Alloc> string_type;
140 typedef typename string_type::size_type size_type;
141 size_type count = std::min(s1.size(), s2.size());
142 size_type answer = ci_char_traits<std::char_traits<CharT> >::compare(s1.c_str(), s2.c_str(), count);
143 return static_cast<int>(answer) < 0 || (answer == 0 && s1.size() < s2.size());
144 }
145}
std::basic_string< char16_t, ci_char_traits< std::char_traits< char16_t > > > ci_u16string
Definition string_ci.hpp:90
ci_string make_ci_string(const std::string &s)
Definition string_ci.hpp:92
std::basic_string< char, ci_char_traits< std::char_traits< char > > > ci_string
Definition string_ci.hpp:89
bool lexicographical_compare_ignoring_case(const std::basic_string< CharT, Traits, Alloc > &s1, const std::basic_string< CharT, Traits, Alloc > &s2)
std::basic_string< CharT, Traits, Alloc > to_lower(const std::basic_string< CharT, Traits, Alloc > &aString)
std::string make_string(const ci_string &s)
Traits::char_type char_type
Definition string_ci.hpp:48
static const char_type * find(const char_type *str, std::size_t n, const char_type &c)
Definition string_ci.hpp:64
Traits::int_type int_type
Definition string_ci.hpp:49
static int compare(const char_type *s1, const char_type *s2, std::size_t n)
Definition string_ci.hpp:51
static int_type lower(char_type c)
Definition string_ci.hpp:83
static bool eq(const char_type &c1, const char_type &c2)
Definition string_ci.hpp:75
static bool lt(const char_type &c1, const char_type &c2)
Definition string_ci.hpp:79