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#pragma once
28
29#include <AK/RefPtr.h>
30#include <AK/RefCounted.h>
31#include <AK/Types.h>
32#include <AK/kmalloc.h>
33
34namespace AK {
35
36enum ShouldChomp {
37 NoChomp,
38 Chomp
39};
40
41class StringImpl : public RefCounted<StringImpl> {
42public:
43 static NonnullRefPtr<StringImpl> create_uninitialized(size_t length, char*& buffer);
44 static RefPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);
45 static RefPtr<StringImpl> create(const char* cstring, size_t length, ShouldChomp = NoChomp);
46 NonnullRefPtr<StringImpl> to_lowercase() const;
47 NonnullRefPtr<StringImpl> to_uppercase() const;
48
49 void operator delete(void* ptr)
50 {
51 kfree(ptr);
52 }
53
54 static StringImpl& the_empty_stringimpl();
55
56 ~StringImpl();
57
58 size_t length() const { return m_length; }
59 const char* characters() const { return &m_inline_buffer[0]; }
60 char operator[](size_t i) const
61 {
62 ASSERT(i < m_length);
63 return characters()[i];
64 }
65
66 unsigned hash() const
67 {
68 if (!m_has_hash)
69 compute_hash();
70 return m_hash;
71 }
72
73private:
74 enum ConstructTheEmptyStringImplTag {
75 ConstructTheEmptyStringImpl
76 };
77 explicit StringImpl(ConstructTheEmptyStringImplTag)
78 {
79 m_inline_buffer[0] = '\0';
80 }
81
82 enum ConstructWithInlineBufferTag {
83 ConstructWithInlineBuffer
84 };
85 StringImpl(ConstructWithInlineBufferTag, size_t length);
86
87 void compute_hash() const;
88
89 size_t m_length { 0 };
90 mutable unsigned m_hash { 0 };
91 mutable bool m_has_hash { false };
92 char m_inline_buffer[0];
93};
94
95inline constexpr u32 string_hash(const char* characters, size_t length)
96{
97 u32 hash = 0;
98 for (size_t i = 0; i < length; ++i) {
99 hash += (u32)characters[i];
100 hash += (hash << 10);
101 hash ^= (hash >> 6);
102 }
103 hash += hash << 3;
104 hash ^= hash >> 11;
105 hash += hash << 15;
106 return hash;
107}
108
109}
110
111using AK::Chomp;
112using AK::string_hash;
113using AK::StringImpl;