/src/serenity/AK/StringImpl.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/CharacterTypes.h> |
8 | | #include <AK/FlyString.h> |
9 | | #include <AK/HashTable.h> |
10 | | #include <AK/Memory.h> |
11 | | #include <AK/StdLibExtras.h> |
12 | | #include <AK/StringHash.h> |
13 | | #include <AK/StringImpl.h> |
14 | | #include <AK/kmalloc.h> |
15 | | |
16 | | namespace AK { |
17 | | |
18 | | static StringImpl* s_the_empty_stringimpl = nullptr; |
19 | | |
20 | | StringImpl& StringImpl::the_empty_stringimpl() |
21 | 0 | { |
22 | 0 | if (!s_the_empty_stringimpl) { |
23 | 0 | void* slot = kmalloc(sizeof(StringImpl) + sizeof(char)); |
24 | 0 | s_the_empty_stringimpl = new (slot) StringImpl(ConstructTheEmptyStringImpl); |
25 | 0 | } |
26 | 0 | return *s_the_empty_stringimpl; |
27 | 0 | } |
28 | | |
29 | | StringImpl::StringImpl(ConstructWithInlineBufferTag, size_t length) |
30 | | : m_length(length) |
31 | 364 | { |
32 | 364 | } |
33 | | |
34 | | StringImpl::~StringImpl() |
35 | 364 | { |
36 | 364 | if (m_fly) |
37 | 0 | FlyString::did_destroy_impl({}, *this); |
38 | 364 | } |
39 | | |
40 | | NonnullRefPtr<StringImpl> StringImpl::create_uninitialized(size_t length, char*& buffer) |
41 | 364 | { |
42 | 364 | VERIFY(length); |
43 | 364 | void* slot = kmalloc(allocation_size_for_stringimpl(length)); |
44 | 364 | VERIFY(slot); |
45 | 0 | auto new_stringimpl = adopt_ref(*new (slot) StringImpl(ConstructWithInlineBuffer, length)); |
46 | 364 | buffer = const_cast<char*>(new_stringimpl->characters()); |
47 | 364 | buffer[length] = '\0'; |
48 | 364 | return new_stringimpl; |
49 | 364 | } |
50 | | |
51 | | RefPtr<StringImpl> StringImpl::create(char const* cstring, size_t length, ShouldChomp should_chomp) |
52 | 364 | { |
53 | 364 | if (!cstring) |
54 | 0 | return nullptr; |
55 | | |
56 | 364 | if (should_chomp) { |
57 | 0 | while (length) { |
58 | 0 | char last_ch = cstring[length - 1]; |
59 | 0 | if (!last_ch || last_ch == '\n' || last_ch == '\r') |
60 | 0 | --length; |
61 | 0 | else |
62 | 0 | break; |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | 364 | if (!length) |
67 | 0 | return the_empty_stringimpl(); |
68 | | |
69 | 364 | char* buffer; |
70 | 364 | auto new_stringimpl = create_uninitialized(length, buffer); |
71 | 364 | memcpy(buffer, cstring, length * sizeof(char)); |
72 | | |
73 | 364 | return new_stringimpl; |
74 | 364 | } |
75 | | |
76 | | RefPtr<StringImpl> StringImpl::create(char const* cstring, ShouldChomp shouldChomp) |
77 | 182 | { |
78 | 182 | if (!cstring) |
79 | 0 | return nullptr; |
80 | | |
81 | 182 | if (!*cstring) |
82 | 0 | return the_empty_stringimpl(); |
83 | | |
84 | 182 | return create(cstring, strlen(cstring), shouldChomp); |
85 | 182 | } |
86 | | |
87 | | RefPtr<StringImpl> StringImpl::create(ReadonlyBytes bytes, ShouldChomp shouldChomp) |
88 | 0 | { |
89 | 0 | return StringImpl::create(reinterpret_cast<char const*>(bytes.data()), bytes.size(), shouldChomp); |
90 | 0 | } |
91 | | |
92 | | RefPtr<StringImpl> StringImpl::create_lowercased(char const* cstring, size_t length) |
93 | 0 | { |
94 | 0 | if (!cstring) |
95 | 0 | return nullptr; |
96 | 0 | if (!length) |
97 | 0 | return the_empty_stringimpl(); |
98 | 0 | char* buffer; |
99 | 0 | auto impl = create_uninitialized(length, buffer); |
100 | 0 | for (size_t i = 0; i < length; ++i) |
101 | 0 | buffer[i] = (char)to_ascii_lowercase(cstring[i]); |
102 | 0 | return impl; |
103 | 0 | } |
104 | | |
105 | | RefPtr<StringImpl> StringImpl::create_uppercased(char const* cstring, size_t length) |
106 | 0 | { |
107 | 0 | if (!cstring) |
108 | 0 | return nullptr; |
109 | 0 | if (!length) |
110 | 0 | return the_empty_stringimpl(); |
111 | 0 | char* buffer; |
112 | 0 | auto impl = create_uninitialized(length, buffer); |
113 | 0 | for (size_t i = 0; i < length; ++i) |
114 | 0 | buffer[i] = (char)to_ascii_uppercase(cstring[i]); |
115 | 0 | return impl; |
116 | 0 | } |
117 | | |
118 | | NonnullRefPtr<StringImpl> StringImpl::to_lowercase() const |
119 | 0 | { |
120 | 0 | for (size_t i = 0; i < m_length; ++i) { |
121 | 0 | if (is_ascii_upper_alpha(characters()[i])) |
122 | 0 | return create_lowercased(characters(), m_length).release_nonnull(); |
123 | 0 | } |
124 | 0 | return const_cast<StringImpl&>(*this); |
125 | 0 | } |
126 | | |
127 | | NonnullRefPtr<StringImpl> StringImpl::to_uppercase() const |
128 | 0 | { |
129 | 0 | for (size_t i = 0; i < m_length; ++i) { |
130 | 0 | if (is_ascii_lower_alpha(characters()[i])) |
131 | 0 | return create_uppercased(characters(), m_length).release_nonnull(); |
132 | 0 | } |
133 | 0 | return const_cast<StringImpl&>(*this); |
134 | 0 | } |
135 | | |
136 | | unsigned StringImpl::case_insensitive_hash() const |
137 | 0 | { |
138 | 0 | return case_insensitive_string_hash(characters(), length()); |
139 | 0 | } |
140 | | |
141 | | void StringImpl::compute_hash() const |
142 | 0 | { |
143 | 0 | if (!length()) |
144 | 0 | m_hash = 0; |
145 | 0 | else |
146 | 0 | m_hash = string_hash(characters(), m_length); |
147 | 0 | m_has_hash = true; |
148 | 0 | } |
149 | | |
150 | | } |