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
28namespace blink {
29
30class SpaceSplitString {
31public:
32    SpaceSplitString() { }
33    SpaceSplitString(const AtomicString& string, bool shouldFoldCase) { set(string, shouldFoldCase); }
34
35    bool operator!=(const SpaceSplitString& other) const { return m_data != other.m_data; }
36
37    void set(const AtomicString&, bool shouldFoldCase);
38    void clear() { m_data.clear(); }
39
40    bool contains(const AtomicString& string) const { return m_data && m_data->contains(string); }
41    bool containsAll(const SpaceSplitString& names) const { return !names.m_data || (m_data && m_data->containsAll(*names.m_data)); }
42    void add(const AtomicString&);
43    bool remove(const AtomicString&);
44
45    size_t size() const { return m_data ? m_data->size() : 0; }
46    bool isNull() const { return !m_data; }
47    const AtomicString& operator[](size_t i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < size()); return (*m_data)[i]; }
48
49private:
50    class Data : public RefCounted<Data> {
51    public:
52        static PassRefPtr<Data> create(const AtomicString&);
53        static PassRefPtr<Data> createUnique(const Data&);
54
55        ~Data();
56
57        bool contains(const AtomicString& string)
58        {
59            size_t size = m_vector.size();
60            for (size_t i = 0; i < size; ++i) {
61                if (m_vector[i] == string)
62                    return true;
63            }
64            return false;
65        }
66
67        bool containsAll(Data&);
68
69        void add(const AtomicString&);
70        void remove(unsigned index);
71
72        bool isUnique() const { return m_keyString.isNull(); }
73        size_t size() const { return m_vector.size(); }
74        const AtomicString& operator[](size_t i) { ASSERT_WITH_SECURITY_IMPLICATION(i < size()); return m_vector[i]; }
75
76    private:
77        explicit Data(const AtomicString&);
78        explicit Data(const Data&);
79
80        void createVector(const String&);
81        template <typename CharacterType>
82        inline void createVector(const CharacterType*, unsigned);
83
84        AtomicString m_keyString;
85        Vector<AtomicString, 4> m_vector;
86    };
87    typedef HashMap<AtomicString, Data*> DataMap;
88
89    static DataMap& sharedDataMap();
90
91    void ensureUnique()
92    {
93        if (m_data && !m_data->isUnique())
94            m_data = Data::createUnique(*m_data);
95    }
96
97    RefPtr<Data> m_data;
98};
99
100} // namespace blink
101
102#endif // SpaceSplitString_h
103