Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / SpaceSplitString.h
1 /*
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
24 #include "wtf/RefCounted.h"
25 #include "wtf/Vector.h"
26 #include "wtf/text/AtomicString.h"
27
28 namespace blink {
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
52         bool isUnique() const { return m_keyString.isNull(); }
53         size_t size() const { return m_vector.size(); }
54         const AtomicString& operator[](size_t i) { ASSERT_WITH_SECURITY_IMPLICATION(i < size()); return m_vector[i]; }
55
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
73         bool operator!=(const SpaceSplitString& other) const { return m_data != other.m_data; }
74
75         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; }
85         const AtomicString& operator[](size_t i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < size()); return (*m_data)[i]; }
86
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 blink
98
99 #endif // SpaceSplitString_h