neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1// string.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>
40#include <string_view>
41#include <boost/algorithm/string/replace.hpp>
46
47using namespace std::literals::string_literals;
48
49namespace neolib
50{
51 class string : public reference_counted<i_string>
52 {
54 // types
55 public:
57 typedef char value_type;
58 typedef std::string std_type;
59 protected:
62 typedef typename container_const_iterator::abstract_const_iterator abstract_const_iterator;
63 typedef typename container_iterator::abstract_iterator abstract_iterator;
64 // construction
65 public:
66 string(size_type aCount, value_type aChar) : iString(aCount, aChar) {}
67 string(const char* aString) : iString{ aString } {}
68 string(const char* aString, std::size_t aLength) : iString{ aString, aLength } {}
69 string(const std::string& aString = {}) : iString{ aString } {}
70 string(const std::string_view& aStringView) : iString{ aStringView } {}
71 string(const neolib::quick_string& aOther) : iString{ aOther } {}
72 string(const string& aOther) : iString{ aOther.as_std_string() } {}
73 string(string&& aOther) : iString{ std::move(aOther.as_std_string()) } {}
74 string(const i_string& aOther) : iString{ aOther.to_std_string_view() } {}
75 template <typename Iter, typename SFINAE = std::enable_if_t<!std::is_scalar_v<Iter>, sfinae>>
76 string(Iter aBegin, Iter aEnd) : iString{ aBegin, aEnd } {}
78 string& operator=(const string& aOther) { assign(aOther); return *this; }
79 string& operator=(string&& aOther) { assign(std::move(aOther)); return *this; }
80 string& operator=(const i_string& aOther) final { assign(aOther); return *this; }
81 // operations
82 public:
83 const std_type& as_std_string() const { return iString; }
84 std_type& as_std_string() { return iString; }
85 std::string to_std_string() const { return iString; }
86 std::string_view to_std_string_view() const noexcept { return std::string_view{ iString }; }
87 // implementation
88 // from i_container
89 public:
90 size_type size() const noexcept final { return iString.size(); }
91 size_type max_size() const noexcept final { return iString.max_size(); }
92 void clear() final { iString.clear(); }
93 void assign(const i_container& aOther) final { if (&aOther == this) return; iString.assign(aOther.begin(), aOther.end()); }
94 // from i_container
95 private:
96 abstract_const_iterator* do_begin(void* memory) const final { return new (memory) container_const_iterator{ iString.begin() }; }
97 abstract_const_iterator* do_end(void* memory) const final { return new (memory) container_const_iterator{ iString.end() }; }
98 abstract_iterator* do_begin(void* memory) final { return new (memory) container_iterator{ iString.begin() }; }
99 abstract_iterator* do_end(void* memory) final { return new (memory) container_iterator{ iString.end() }; }
100 abstract_iterator* do_erase(void* memory, const abstract_const_iterator& aPosition) final { return new (memory) container_iterator{ iString.erase(static_cast<const container_const_iterator&>(aPosition)) }; }
101 abstract_iterator* do_erase(void* memory, const abstract_const_iterator& aFirst, const abstract_const_iterator& aLast) final { return new (memory) container_iterator{ iString.erase(static_cast<const container_const_iterator&>(aFirst), static_cast<const container_const_iterator&>(aLast)) }; }
102 // from i_sequence_container
103 public:
104 size_type capacity() const final { return iString.size(); }
105 void reserve(size_type aCapacity) final { iString.reserve(aCapacity); }
106 void resize(size_type aSize) final { iString.resize(aSize); }
107 void resize(size_type aSize, const value_type& aValue) final { iString.resize(aSize, aValue); }
108 void push_back(const value_type& aValue) final { iString.push_back(aValue); }
109 void pop_back() final { iString.erase(iString.end() - 1); }
110 const value_type& front() const final { return iString.front(); }
111 value_type& front() final { return iString.front(); }
112 const value_type& back() const final { return iString.back(); }
113 value_type& back() final { return iString.back(); }
114 private:
115 abstract_iterator* do_insert(void* memory, const abstract_const_iterator& aPosition, const value_type& aValue) final { return new (memory) container_iterator{ iString.insert(static_cast<const container_const_iterator&>(aPosition), aValue) }; }
116 // from i_random_access_container
117 public:
118 const value_type& at(size_type aIndex) const final { return iString.at(aIndex); }
119 value_type& at(size_type aIndex) final { return iString.at(aIndex); }
120 const value_type& operator[](size_type aIndex) const final { return iString[aIndex]; }
121 value_type& operator[](size_type aIndex) final { return iString[aIndex]; }
122 private:
123 std::ptrdiff_t iterator_offset() const final { return sizeof(char); }
124 // from i_string
125 public:
126 const char* cdata() const noexcept final { return iString.data(); }
127 const char* data() const noexcept final { return iString.data(); }
128 char* data() noexcept final { return iString.data(); }
129 const char* c_str() const noexcept final { return iString.c_str(); }
130 void assign(const string& aOther) { iString = aOther.to_std_string_view(); }
131 void assign(const i_string& aOther) final { iString = aOther.to_std_string_view(); }
132 void assign(const char* aSource, size_type aSourceLength) final { iString.assign(aSource, aSourceLength); }
133 void append(const string& aOther) { iString.append(aOther.to_std_string_view()); }
134 void append(const i_string& aOther) final { iString.append(aOther.to_std_string_view()); }
135 void append(const char* aSource, size_type aSourceLength) final { iString.append(aSource, aSourceLength); }
136 public:
137 void replace_all(const i_string& aSearch, const i_string& aReplace) final { boost::replace_all(iString, aSearch.to_std_string_view(), aReplace.to_std_string_view()); }
138 public:
139 using i_string::assign;
140 using i_string::append;
141 void assign(string&& aOther) { if (&aOther == this) return; iString.assign(std::move(aOther.to_std_string())); }
142 public:
143 // attributes
144 private:
145 std::string iString;
146 };
147
148 inline bool operator==(const string& lhs, const string& rhs) noexcept
149 {
150 return lhs.to_std_string_view() == rhs.to_std_string_view();
151 }
152
153 inline std::strong_ordering operator<=>(const string& lhs, const string& rhs) noexcept
154 {
155 return lhs.to_std_string_view() <=> rhs.to_std_string_view();
156 }
157
158 inline bool operator==(const string& lhs, const i_string& rhs) noexcept
159 {
160 return lhs.to_std_string_view() == rhs.to_std_string_view();
161 }
162
163 inline std::strong_ordering operator<=>(const string& lhs, const i_string& rhs) noexcept
164 {
165 return lhs.to_std_string_view() <=> rhs.to_std_string_view();
166 }
167
168 inline bool operator==(const i_string& lhs, const string& rhs) noexcept
169 {
170 return lhs.to_std_string_view() == rhs.to_std_string_view();
171 }
172
173 inline std::strong_ordering operator<=>(const i_string& lhs, const string& rhs) noexcept
174 {
175 return lhs.to_std_string_view() <=> rhs.to_std_string_view();
176 }
177
178 inline string operator+(const string& lhs, const string& rhs)
179 {
180 return lhs.as_std_string() + rhs.as_std_string();
181 }
182
183 inline string& operator+=(string& lhs, const i_string& rhs)
184 {
185 lhs.as_std_string() += rhs.to_std_string_view();
186 return lhs;
187 }
188
189 namespace string_literals
190 {
191 inline string operator"" _s(const char* str, size_t len)
192 {
193 return string{ str, len };
194 }
195 }
196}
virtual void append(const i_string &aOther)=0
virtual void assign(const i_string &aOther)=0
std::string_view to_std_string_view() const noexcept
Definition i_string.hpp:76
string & operator=(const string &aOther)
Definition string.hpp:78
void append(const char *aSource, size_type aSourceLength) final
Definition string.hpp:135
void append(const string &aOther)
Definition string.hpp:133
value_type & front() final
Definition string.hpp:111
std::string to_std_string() const
Definition string.hpp:85
size_type capacity() const final
Definition string.hpp:104
container::random_access_const_iterator< char, std::string::const_iterator > container_const_iterator
Definition string.hpp:60
const char * c_str() const noexcept final
Definition string.hpp:129
string & operator=(string &&aOther)
Definition string.hpp:79
size_type size() const noexcept final
Definition string.hpp:90
value_type & operator[](size_type aIndex) final
Definition string.hpp:121
value_type & back() final
Definition string.hpp:113
string(const string &aOther)
Definition string.hpp:72
void assign(const char *aSource, size_type aSourceLength) final
Definition string.hpp:132
void replace_all(const i_string &aSearch, const i_string &aReplace) final
Definition string.hpp:137
void assign(const i_container &aOther) final
Definition string.hpp:93
void resize(size_type aSize) final
Definition string.hpp:106
void reserve(size_type aCapacity) final
Definition string.hpp:105
const value_type & operator[](size_type aIndex) const final
Definition string.hpp:120
container_iterator::abstract_iterator abstract_iterator
Definition string.hpp:63
void append(const i_string &aOther) final
Definition string.hpp:134
std::string std_type
Definition string.hpp:58
void pop_back() final
Definition string.hpp:109
string(const std::string &aString={})
Definition string.hpp:69
const value_type & at(size_type aIndex) const final
Definition string.hpp:118
void push_back(const value_type &aValue) final
Definition string.hpp:108
string(const char *aString, std::size_t aLength)
Definition string.hpp:68
i_string abstract_type
Definition string.hpp:56
string(const std::string_view &aStringView)
Definition string.hpp:70
string(size_type aCount, value_type aChar)
Definition string.hpp:66
std_type & as_std_string()
Definition string.hpp:84
char value_type
Definition string.hpp:57
void assign(string &&aOther)
Definition string.hpp:141
void assign(const i_string &aOther) final
Definition string.hpp:131
container_const_iterator::abstract_const_iterator abstract_const_iterator
Definition string.hpp:62
string(const char *aString)
Definition string.hpp:67
const value_type & front() const final
Definition string.hpp:110
size_type max_size() const noexcept final
Definition string.hpp:91
void assign(const string &aOther)
Definition string.hpp:130
const char * cdata() const noexcept final
Definition string.hpp:126
const std_type & as_std_string() const
Definition string.hpp:83
std::string_view to_std_string_view() const noexcept
Definition string.hpp:86
string(string &&aOther)
Definition string.hpp:73
container::random_access_iterator< char, std::string::iterator, std::string::const_iterator > container_iterator
Definition string.hpp:61
const value_type & back() const final
Definition string.hpp:112
const char * data() const noexcept final
Definition string.hpp:127
value_type & at(size_type aIndex) final
Definition string.hpp:119
string & operator=(const i_string &aOther) final
Definition string.hpp:80
string(const neolib::quick_string &aOther)
Definition string.hpp:71
string(Iter aBegin, Iter aEnd)
Definition string.hpp:76
void resize(size_type aSize, const value_type &aValue) final
Definition string.hpp:107
string(const i_string &aOther)
Definition string.hpp:74
void clear() final
Definition string.hpp:92
char * data() noexcept final
Definition string.hpp:128
std::partial_ordering operator<=>(const i_container< T, ConstIteratorType, IteratorType > &lhs, const i_container< T, ConstIteratorType, IteratorType > &rhs)
i_string & operator+=(i_string &lhs, const i_string &rhs)
Definition i_string.hpp:89
basic_quick_string< char > quick_string
Definition plf_hive.h:79