Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/FlyString.h>
28#include <AK/Memory.h>
29#include <AK/StdLibExtras.h>
30#include <AK/String.h>
31#include <AK/StringBuilder.h>
32#include <AK/StringView.h>
33#include <AK/Vector.h>
34
35#ifndef KERNEL
36# include <inttypes.h>
37#endif
38
39#ifdef KERNEL
40extern "C" char* strstr(const char* haystack, const char* needle);
41#endif
42
43namespace AK {
44
45String::String(const StringView& view)
46{
47 if (view.m_impl)
48 m_impl = *view.m_impl;
49 else
50 m_impl = StringImpl::create(view.characters_without_null_termination(), view.length());
51}
52
53bool String::operator==(const FlyString& fly_string) const
54{
55 return *this == String(fly_string.impl());
56}
57
58bool String::operator==(const String& other) const
59{
60 if (!m_impl)
61 return !other.m_impl;
62
63 if (!other.m_impl)
64 return false;
65
66 if (length() != other.length())
67 return false;
68
69 return !memcmp(characters(), other.characters(), length());
70}
71
72bool String::operator==(const StringView& other) const
73{
74 if (!m_impl)
75 return !other.m_characters;
76
77 if (!other.m_characters)
78 return false;
79
80 if (length() != other.length())
81 return false;
82
83 return !memcmp(characters(), other.characters_without_null_termination(), length());
84}
85
86bool String::operator<(const String& other) const
87{
88 if (!m_impl)
89 return other.m_impl;
90
91 if (!other.m_impl)
92 return false;
93
94 return strcmp(characters(), other.characters()) < 0;
95}
96
97bool String::operator>(const String& other) const
98{
99 if (!m_impl)
100 return other.m_impl;
101
102 if (!other.m_impl)
103 return false;
104
105 return strcmp(characters(), other.characters()) > 0;
106}
107
108String String::empty()
109{
110 return StringImpl::the_empty_stringimpl();
111}
112
113String String::isolated_copy() const
114{
115 if (!m_impl)
116 return {};
117 if (!m_impl->length())
118 return empty();
119 char* buffer;
120 auto impl = StringImpl::create_uninitialized(length(), buffer);
121 memcpy(buffer, m_impl->characters(), m_impl->length());
122 return String(move(*impl));
123}
124
125String String::substring(size_t start, size_t length) const
126{
127 if (!length)
128 return {};
129 ASSERT(m_impl);
130 ASSERT(start + length <= m_impl->length());
131 // FIXME: This needs some input bounds checking.
132 return { characters() + start, length };
133}
134
135StringView String::substring_view(size_t start, size_t length) const
136{
137 ASSERT(m_impl);
138 ASSERT(start + length <= m_impl->length());
139 // FIXME: This needs some input bounds checking.
140 return { characters() + start, length };
141}
142
143Vector<String> String::split(char separator, bool keep_empty) const
144{
145 return split_limit(separator, 0, keep_empty);
146}
147
148Vector<String> String::split_limit(char separator, size_t limit, bool keep_empty) const
149{
150 if (is_empty())
151 return {};
152
153 Vector<String> v;
154 size_t substart = 0;
155 for (size_t i = 0; i < length() && (v.size() + 1) != limit; ++i) {
156 char ch = characters()[i];
157 if (ch == separator) {
158 size_t sublen = i - substart;
159 if (sublen != 0 || keep_empty)
160 v.append(substring(substart, sublen));
161 substart = i + 1;
162 }
163 }
164 size_t taillen = length() - substart;
165 if (taillen != 0 || keep_empty)
166 v.append(substring(substart, taillen));
167 return v;
168}
169
170Vector<StringView> String::split_view(const char separator, bool keep_empty) const
171{
172 if (is_empty())
173 return {};
174
175 Vector<StringView> v;
176 size_t substart = 0;
177 for (size_t i = 0; i < length(); ++i) {
178 char ch = characters()[i];
179 if (ch == separator) {
180 size_t sublen = i - substart;
181 if (sublen != 0 || keep_empty)
182 v.append(substring_view(substart, sublen));
183 substart = i + 1;
184 }
185 }
186 size_t taillen = length() - substart;
187 if (taillen != 0 || keep_empty)
188 v.append(substring_view(substart, taillen));
189 return v;
190}
191
192ByteBuffer String::to_byte_buffer() const
193{
194 if (!m_impl)
195 return nullptr;
196 return ByteBuffer::copy(reinterpret_cast<const u8*>(characters()), length());
197}
198
199int String::to_int(bool& ok) const
200{
201 return StringUtils::convert_to_int(this->view(), ok);
202}
203
204unsigned String::to_uint(bool& ok) const
205{
206 return StringUtils::convert_to_uint(this->view(), ok);
207}
208
209String String::number(unsigned long long value)
210{
211 int size;
212 char buffer[32];
213 size = sprintf(buffer, "%llu", value);
214 return String(buffer, size);
215}
216
217String String::number(unsigned long value)
218{
219 int size;
220 char buffer[32];
221 size = sprintf(buffer, "%lu", value);
222 return String(buffer, size);
223}
224
225String String::number(unsigned value)
226{
227 char buffer[32];
228 int size = sprintf(buffer, "%u", value);
229 return String(buffer, size);
230}
231
232String String::number(long long value)
233{
234 char buffer[32];
235 int size = sprintf(buffer, "%lld", value);
236 return String(buffer, size);
237}
238
239String String::number(long value)
240{
241 char buffer[32];
242 int size = sprintf(buffer, "%ld", value);
243 return String(buffer, size);
244}
245
246String String::number(int value)
247{
248 char buffer[32];
249 int size = sprintf(buffer, "%d", value);
250 return String(buffer, size);
251}
252
253String String::format(const char* fmt, ...)
254{
255 StringBuilder builder;
256 va_list ap;
257 va_start(ap, fmt);
258 builder.appendvf(fmt, ap);
259 va_end(ap);
260 return builder.to_string();
261}
262
263bool String::starts_with(const StringView& str) const
264{
265 if (str.is_empty())
266 return true;
267 if (is_empty())
268 return false;
269 if (str.length() > length())
270 return false;
271 return !memcmp(characters(), str.characters_without_null_termination(), str.length());
272}
273
274bool String::starts_with(char ch) const
275{
276 if (is_empty())
277 return false;
278 return characters()[0] == ch;
279}
280
281bool String::ends_with(const StringView& str) const
282{
283 if (str.is_empty())
284 return true;
285 if (is_empty())
286 return false;
287 if (str.length() > length())
288 return false;
289 return !memcmp(characters() + (length() - str.length()), str.characters_without_null_termination(), str.length());
290}
291
292bool String::ends_with(char ch) const
293{
294 if (is_empty())
295 return false;
296 return characters()[length() - 1] == ch;
297}
298String String::repeated(char ch, size_t count)
299{
300 if (!count)
301 return empty();
302 char* buffer;
303 auto impl = StringImpl::create_uninitialized(count, buffer);
304 memset(buffer, ch, count);
305 return *impl;
306}
307
308bool String::matches(const StringView& mask, CaseSensitivity case_sensitivity) const
309{
310 return StringUtils::matches(*this, mask, case_sensitivity);
311}
312
313bool String::contains(const String& needle) const
314{
315 return strstr(characters(), needle.characters());
316}
317
318bool String::equals_ignoring_case(const StringView& other) const
319{
320 return StringUtils::equals_ignoring_case(view(), other);
321}
322
323int String::replace(const String& needle, const String& replacement, bool all_occurences)
324{
325 if (is_empty())
326 return 0;
327
328 Vector<size_t> positions;
329 size_t start = 0, pos;
330 for (;;) {
331 const char* ptr = strstr(characters() + start, needle.characters());
332 if (!ptr)
333 break;
334
335 pos = ptr - characters();
336 positions.append(pos);
337 if (!all_occurences)
338 break;
339
340 start = pos + 1;
341 }
342
343 if (!positions.size())
344 return 0;
345
346 StringBuilder b;
347 size_t lastpos = 0;
348 for (auto& pos : positions) {
349 b.append(substring_view(lastpos, pos - lastpos));
350 b.append(replacement);
351 lastpos = pos + needle.length();
352 }
353 b.append(substring_view(lastpos, length() - lastpos));
354 m_impl = StringImpl::create(b.build().characters());
355 return positions.size();
356}
357
358String escape_html_entities(const StringView& html)
359{
360 StringBuilder builder;
361 for (size_t i = 0; i < html.length(); ++i) {
362 if (html[i] == '<')
363 builder.append("<");
364 else if (html[i] == '>')
365 builder.append(">");
366 else if (html[i] == '&')
367 builder.append("&");
368 else
369 builder.append(html[i]);
370 }
371 return builder.to_string();
372}
373
374String::String(const FlyString& string)
375 : m_impl(string.impl())
376{
377}
378
379String String::to_lowercase() const
380{
381 if (!m_impl)
382 return {};
383 return m_impl->to_lowercase();
384}
385
386String String::to_uppercase() const
387{
388 if (!m_impl)
389 return {};
390 return m_impl->to_uppercase();
391}
392
393bool operator<(const char* characters, const String& string)
394{
395 if (!characters)
396 return !string.is_null();
397
398 if (string.is_null())
399 return false;
400
401 return __builtin_strcmp(characters, string.characters()) < 0;
402}
403
404bool operator>=(const char* characters, const String& string)
405{
406 return !(characters < string);
407}
408
409bool operator>(const char* characters, const String& string)
410{
411 if (!characters)
412 return !string.is_null();
413
414 if (string.is_null())
415 return false;
416
417 return __builtin_strcmp(characters, string.characters()) > 0;
418}
419
420bool operator<=(const char* characters, const String& string)
421{
422 return !(characters > string);
423}
424
425bool String::operator==(const char* cstring) const
426{
427 if (is_null())
428 return !cstring;
429 if (!cstring)
430 return false;
431 return !__builtin_strcmp(characters(), cstring);
432}
433
434StringView String::view() const
435{
436 return { characters(), length() };
437}
438
439}