blob: 6035f7c609dbb425e31bbb03955322286ceaf168 [file] [log] [blame]
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +00001/*
2 * Copyright (C) 2007, 2008, 2010, 2011, 2012 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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 *
19 */
20
21#ifndef SpaceSplitString_h
22#define SpaceSplitString_h
23
Ben Murdoche69819b2013-07-17 14:56:49 +010024#include "wtf/RefCounted.h"
25#include "wtf/Vector.h"
26#include "wtf/text/AtomicString.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000027
28namespace WebCore {
29
30 class SpaceSplitStringData : public RefCounted<SpaceSplitStringData> {
31 public:
32 static PassRefPtr<SpaceSplitStringData> create(const AtomicString&);
33 static PassRefPtr<SpaceSplitStringData> createUnique(const SpaceSplitStringData&);
34
35 ~SpaceSplitStringData();
36
37 bool contains(const AtomicString& string)
38 {
39 size_t size = m_vector.size();
40 for (size_t i = 0; i < size; ++i) {
41 if (m_vector[i] == string)
42 return true;
43 }
44 return false;
45 }
46
47 bool containsAll(SpaceSplitStringData&);
48
49 void add(const AtomicString&);
50 void remove(unsigned index);
51
Ben Murdoch02772c62013-07-26 10:21:05 +010052 bool isUnique() const { return m_keyString.isNull(); }
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000053 size_t size() const { return m_vector.size(); }
Torne (Richard Coles)926b0012013-03-28 15:32:48 +000054 const AtomicString& operator[](size_t i) { ASSERT_WITH_SECURITY_IMPLICATION(i < size()); return m_vector[i]; }
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000055
56 private:
57 explicit SpaceSplitStringData(const AtomicString&);
58 explicit SpaceSplitStringData(const SpaceSplitStringData&);
59
60 void createVector(const String&);
61 template <typename CharacterType>
62 inline void createVector(const CharacterType*, unsigned);
63
64 AtomicString m_keyString;
65 Vector<AtomicString, 4> m_vector;
66 };
67
68 class SpaceSplitString {
69 public:
70 SpaceSplitString() { }
71 SpaceSplitString(const AtomicString& string, bool shouldFoldCase) { set(string, shouldFoldCase); }
72
Torne (Richard Coles)926b0012013-03-28 15:32:48 +000073 bool operator!=(const SpaceSplitString& other) const { return m_data != other.m_data; }
74
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000075 void set(const AtomicString&, bool shouldFoldCase);
76 void clear() { m_data.clear(); }
77
78 bool contains(const AtomicString& string) const { return m_data && m_data->contains(string); }
79 bool containsAll(const SpaceSplitString& names) const { return !names.m_data || (m_data && m_data->containsAll(*names.m_data)); }
80 void add(const AtomicString&);
81 bool remove(const AtomicString&);
82
83 size_t size() const { return m_data ? m_data->size() : 0; }
84 bool isNull() const { return !m_data; }
Torne (Richard Coles)926b0012013-03-28 15:32:48 +000085 const AtomicString& operator[](size_t i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < size()); return (*m_data)[i]; }
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000086
87 private:
88 void ensureUnique()
89 {
90 if (m_data && !m_data->isUnique())
91 m_data = SpaceSplitStringData::createUnique(*m_data);
92 }
93
94 RefPtr<SpaceSplitStringData> m_data;
95 };
96
97} // namespace WebCore
98
99#endif // SpaceSplitString_h