neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
i_string.hpp
Go to the documentation of this file.
1// i_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 <iostream>
42#include <boost/algorithm/string.hpp>
45
46namespace neolib
47{
49 {
50 typedef i_string self_type;
52 public:
54 public:
55 using typename base_type::value_type;
56 using typename base_type::size_type;
57 public:
58 virtual i_string& operator=(const i_string& aOther) = 0;
59 public:
60 virtual const value_type* c_str() const noexcept = 0;
61 virtual void assign(const i_string& aOther) = 0;
62 virtual void assign(const value_type* aSource, size_type aSourceLength) = 0;
63 virtual void append(const i_string& aOther) = 0;
64 virtual void append(const value_type* aSource, size_type aSourceLength) = 0;
65 public:
66 virtual void replace_all(const i_string& aSearch, const i_string& aReplace) = 0;
67 public:
68 operator std::string() const { return to_std_string(); }
69 i_string& operator=(const std::string& aOther) { assign(aOther); return *this; }
70 size_type length() const { return size(); }
71 void assign(const std::string& aSource) { assign(aSource.c_str(), aSource.size()); }
72 void assign(const std::string_view& aSource) { assign(aSource.data(), aSource.size()); }
73 void append(const std::string& aSource) { append(aSource.c_str(), aSource.size()); }
74 void append(const std::string_view& aSource) { append(aSource.data(), aSource.size()); }
75 std::string to_std_string() const { return std::string{ c_str(), size() }; }
76 std::string_view to_std_string_view() const noexcept { return std::string_view{ c_str(), size() }; }
77 };
78
79 inline bool operator==(const i_string& lhs, const i_string& rhs) noexcept
80 {
81 return lhs.to_std_string_view() == rhs.to_std_string_view();
82 }
83
84 inline std::strong_ordering operator<=>(const i_string& lhs, const i_string& rhs) noexcept
85 {
86 return lhs.to_std_string_view() <=> rhs.to_std_string_view();
87 }
88
89 inline i_string& operator+=(i_string& lhs, const i_string& rhs)
90 {
91 lhs.append(rhs);
92 return lhs;
93 }
94
96 {
97 bool operator()(const i_string& lhs, const i_string& rhs) const
98 {
99 return boost::iequals(lhs.to_std_string_view(), rhs.to_std_string_view());
100 }
101 };
102
103 struct ci_less
104 {
105 bool operator()(const i_string& lhs, const i_string& rhs) const
106 {
107 return boost::ilexicographical_compare(lhs.to_std_string_view(), rhs.to_std_string_view());
108 }
109 };
110
111 inline std::ostream& operator<<(std::ostream& aStream, const i_string& aString)
112 {
113 aStream << aString.to_std_string_view();
114 return aStream;
115 }
116
117 inline std::istream& operator>>(std::istream& aStream, i_string& aString)
118 {
119 std::string temp;
120 aStream >> temp;
121 aString.assign(temp.c_str(), temp.size());
122 return aStream;
123 }
124}
virtual size_type size() const noexcept=0
self_type abstract_type
Definition i_string.hpp:53
void assign(const std::string &aSource)
Definition i_string.hpp:71
i_string & operator=(const std::string &aOther)
Definition i_string.hpp:69
virtual void append(const i_string &aOther)=0
virtual void replace_all(const i_string &aSearch, const i_string &aReplace)=0
virtual void assign(const i_string &aOther)=0
std::string_view to_std_string_view() const noexcept
Definition i_string.hpp:76
virtual const value_type * c_str() const noexcept=0
void append(const std::string_view &aSource)
Definition i_string.hpp:74
std::string to_std_string() const
Definition i_string.hpp:75
void assign(const std::string_view &aSource)
Definition i_string.hpp:72
void append(const std::string &aSource)
Definition i_string.hpp:73
size_type length() const
Definition i_string.hpp:70
virtual i_string & operator=(const i_string &aOther)=0
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
Definition plf_hive.h:79
bool operator()(const i_string &lhs, const i_string &rhs) const
Definition i_string.hpp:97
bool operator()(const i_string &lhs, const i_string &rhs) const
Definition i_string.hpp:105