libbio
Loading...
Searching...
No Matches
ImmutableString.h
Go to the documentation of this file.
1/*
2 * This file is a part of the Biology project by eons LLC.
3 * Biology (aka Develop Biology) is a framework for approaching software
4 * development from a natural sciences perspective.
5 *
6 * Copyright (C) 2022 Séon O'Shannon & eons LLC
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
25
26#include <string>
27#include <cstring>
28#include <cstdlib>
29
30namespace bio {
31
38{
39 friend class String; //makes life easy, no need to use this-> or define extra access methods here.
40
41public:
42
48 mLength(0)
49 {
50
51 }
52
56 BIO_CONSTEXPR ImmutableString(const char* string) :
57 mString(string),
58 mLength(strlen(string))
59 {
60
61 }
62
68 explicit ImmutableString(const char* string, const ::std::size_t length) :
69 mString(string),
71 {
72
73 }
74
84
85 #if BIO_CPP_VERSION >= 11
92 {
93
94 }
95 #endif
96
97 #if BIO_CPP_VERSION >= 11
103 {
104 mString = toMove.mString;
105 mLength = toMove.mLength;
106 return *this;
107 }
108 #endif
109
115 {
116 mString = toCopy.mString;
117 mLength = toCopy.mLength;
118 return *this;
119 }
120
124 BIO_CONSTEXPR const char* Data() const
125 {
126 return mString;
127 }
128
132 BIO_CONSTEXPR ::std::size_t Length() const
133 {
134 return mLength;
135 }
136
140 BIO_CONSTEXPR bool Empty() const
141 {
142 return mLength == 0;
143 }
144
150 BIO_CONSTEXPR ::std::size_t Find(const ImmutableString& substring) const
151 {
152 //We can almost use strstr here. However, our use of mLength means we would need strnstr, which does not appear to be standard.
153 //We could also simplify this by making the inner loop use strncmp but meh.
154
155 ::std::size_t start = 0;
156 ::std::size_t str = 0;
157 ::std::size_t sub = 0;
158 while (start < mLength)
159 {
160 if (mString[start] == substring.mString[0])
161 {
162 str = start + 1;
163 sub = 1;
164 if (sub == substring.mLength) //mLength is 1.
165 {
166 return start;
167 }
168 while (mString[str] == substring.mString[sub])
169 {
170 ++str;
171 ++sub;
172 if (sub == substring.mLength)
173 {
174 return start;
175 }
176 else if (str == mLength)
177 {
178 return 0;
179 }
180 }
181 }
182 ++start;
183 }
184 return 0;
185 }
186
195 BIO_CONSTEXPR ::std::size_t FirstDifference(const ImmutableString& other) const
196 {
197 ::std::size_t i = 0;
198 while (i < mLength && i < other.mLength && mString[i] == other.mString[i])
199 {
200 ++i;
201 }
202 return i;
203 }
204
211 BIO_CONSTEXPR ImmutableString GetImmutableSubString(const ::std::size_t start, const ::std::size_t length) const
212 {
214 ,
215 return ImmutableString()
216 )
218 ret.mString = &mString[start];
219 ret.mLength = length;
220 return ret;
221 }
222
223
224protected:
225 const char* mString;
226 ::std::size_t mLength;
227};
228
229} //bio namespace
#define BIO_CONSTEXPR
Definition KeywordMacros.h:29
#define BIO_SANITIZE(test, success, failure)
Definition SanitizeMacros.h:94
Definition ImmutableString.h:38
BIO_CONSTEXPR ImmutableString GetImmutableSubString(const ::std::size_t start, const ::std::size_t length) const
Definition ImmutableString.h:211
BIO_CONSTEXPR::std::size_t Length() const
Definition ImmutableString.h:132
BIO_CONSTEXPR bool Empty() const
Definition ImmutableString.h:140
BIO_CONSTEXPR ImmutableString()
Definition ImmutableString.h:46
BIO_CONSTEXPR ImmutableString(const ImmutableString &toCopy)
Definition ImmutableString.h:78
BIO_CONSTEXPR ImmutableString(const char *string)
Definition ImmutableString.h:56
::std::size_t mLength
Definition ImmutableString.h:226
BIO_CONSTEXPR ImmutableString & operator=(const ImmutableString &toCopy)
Definition ImmutableString.h:114
ImmutableString(const char *string, const ::std::size_t length)
Definition ImmutableString.h:68
const char * mString
Definition ImmutableString.h:225
BIO_CONSTEXPR::std::size_t Find(const ImmutableString &substring) const
Definition ImmutableString.h:150
BIO_CONSTEXPR::std::size_t FirstDifference(const ImmutableString &other) const
Definition ImmutableString.h:195
BIO_CONSTEXPR const char * Data() const
Definition ImmutableString.h:124
Definition Pointer.h:115
Definition String.h:55
Definition FinalCell.h:29