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