1/*
2 * Copyright (C) 2006-2019 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#pragma once
22
23#include <wtf/text/UniquedStringImpl.h>
24
25namespace WTF {
26
27class AtomStringTable;
28
29class AtomStringImpl : public UniquedStringImpl {
30public:
31 WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> lookUp(const LChar*, unsigned length);
32 WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> lookUp(const UChar*, unsigned length);
33 static RefPtr<AtomStringImpl> lookUp(StringImpl* string)
34 {
35 if (!string || string->isAtom())
36 return static_cast<AtomStringImpl*>(string);
37 return lookUpSlowCase(*string);
38 }
39
40 static void remove(AtomStringImpl*);
41
42 WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(const LChar*);
43 ALWAYS_INLINE static RefPtr<AtomStringImpl> add(const char* s) { return add(reinterpret_cast<const LChar*>(s)); };
44 WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(const LChar*, unsigned length);
45 WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(const UChar*, unsigned length);
46 ALWAYS_INLINE static RefPtr<AtomStringImpl> add(const char* s, unsigned length) { return add(reinterpret_cast<const LChar*>(s), length); };
47 WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(const UChar*);
48 WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(StringImpl*, unsigned offset, unsigned length);
49 ALWAYS_INLINE static RefPtr<AtomStringImpl> add(StringImpl* string)
50 {
51 if (!string)
52 return nullptr;
53 return add(*string);
54 }
55 WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(const StaticStringImpl*);
56 WTF_EXPORT_PRIVATE static Ref<AtomStringImpl> addLiteral(const char* characters, unsigned length);
57
58 // Returns null if the input data contains an invalid UTF-8 sequence.
59 static RefPtr<AtomStringImpl> addUTF8(const char* start, const char* end);
60
61#if USE(CF)
62 WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> add(CFStringRef);
63#endif
64
65 template<typename StringTableProvider>
66 ALWAYS_INLINE static RefPtr<AtomStringImpl> addWithStringTableProvider(StringTableProvider& stringTableProvider, StringImpl* string)
67 {
68 if (!string)
69 return nullptr;
70 return add(*stringTableProvider.atomStringTable(), *string);
71 }
72
73#if !ASSERT_DISABLED
74 WTF_EXPORT_PRIVATE static bool isInAtomStringTable(StringImpl*);
75#endif
76
77private:
78 AtomStringImpl() = delete;
79
80 ALWAYS_INLINE static Ref<AtomStringImpl> add(StringImpl& string)
81 {
82 if (string.isAtom()) {
83 ASSERT_WITH_MESSAGE(!string.length() || isInAtomStringTable(&string), "The atom string comes from an other thread!");
84 return static_cast<AtomStringImpl&>(string);
85 }
86 return addSlowCase(string);
87 }
88
89 ALWAYS_INLINE static Ref<AtomStringImpl> add(AtomStringTable& stringTable, StringImpl& string)
90 {
91 if (string.isAtom()) {
92 ASSERT_WITH_MESSAGE(!string.length() || isInAtomStringTable(&string), "The atom string comes from an other thread!");
93 return static_cast<AtomStringImpl&>(string);
94 }
95 return addSlowCase(stringTable, string);
96 }
97
98 WTF_EXPORT_PRIVATE static Ref<AtomStringImpl> addSlowCase(StringImpl&);
99 WTF_EXPORT_PRIVATE static Ref<AtomStringImpl> addSlowCase(AtomStringTable&, StringImpl&);
100
101 WTF_EXPORT_PRIVATE static RefPtr<AtomStringImpl> lookUpSlowCase(StringImpl&);
102};
103
104#if !ASSERT_DISABLED
105
106// AtomStringImpls created from StaticStringImpl will ASSERT in the generic ValueCheck<T>::checkConsistency,
107// as they are not allocated by fastMalloc. We don't currently have any way to detect that case, so we don't
108// do any consistency check for AtomStringImpl*.
109
110template<> struct ValueCheck<AtomStringImpl*> {
111 static void checkConsistency(const AtomStringImpl*) { }
112};
113
114template<> struct ValueCheck<const AtomStringImpl*> {
115 static void checkConsistency(const AtomStringImpl*) { }
116};
117
118#endif
119
120}
121
122using WTF::AtomStringImpl;
123