• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007, 2008 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 "AtomicString.h"
25 #include <wtf/OwnPtr.h>
26 #include <wtf/Vector.h>
27 
28 namespace WebCore {
29 
30     class SpaceSplitStringData : public Noncopyable {
31     public:
SpaceSplitStringData(const String & string,bool shouldFoldCase)32         SpaceSplitStringData(const String& string, bool shouldFoldCase)
33             : m_string(string), m_shouldFoldCase(shouldFoldCase), m_createdVector(false)
34         {
35         }
36 
contains(const AtomicString & string)37         bool contains(const AtomicString& string)
38         {
39             ensureVector();
40             size_t size = m_vector.size();
41             for (size_t i = 0; i < size; ++i) {
42                 if (m_vector[i] == string)
43                     return true;
44             }
45             return false;
46         }
47 
48         bool containsAll(SpaceSplitStringData&);
49 
size()50         size_t size() { ensureVector(); return m_vector.size(); }
51         const AtomicString& operator[](size_t i) { ensureVector(); ASSERT(i < size()); return m_vector[i]; }
52 
53     private:
ensureVector()54         void ensureVector() { if (!m_createdVector) createVector(); }
55         void createVector();
56 
57         typedef Vector<AtomicString, 8> StringVector;
58         String m_string;
59         StringVector m_vector;
60         bool m_shouldFoldCase;
61         bool m_createdVector;
62     };
63 
64     class SpaceSplitString {
65     public:
SpaceSplitString()66         SpaceSplitString() { }
SpaceSplitString(const String & string,bool shouldFoldCase)67         SpaceSplitString(const String& string, bool shouldFoldCase) : m_data(new SpaceSplitStringData(string, shouldFoldCase)) { }
68 
set(const String & string,bool shouldFoldCase)69         void set(const String& string, bool shouldFoldCase) { m_data.set(new SpaceSplitStringData(string, shouldFoldCase)); }
clear()70         void clear() { m_data.clear(); }
71 
contains(const AtomicString & string)72         bool contains(const AtomicString& string) const { return m_data && m_data->contains(string); }
containsAll(const SpaceSplitString & names)73         bool containsAll(const SpaceSplitString& names) const { return !names.m_data || (m_data && m_data->containsAll(*names.m_data)); }
74 
size()75         size_t size() const { return m_data ? m_data->size() : 0; }
76         const AtomicString& operator[](size_t i) const { ASSERT(i < size()); return (*m_data)[i]; }
77 
78     private:
79         OwnPtr<SpaceSplitStringData> m_data;
80     };
81 
isClassWhitespace(UChar c)82     inline bool isClassWhitespace(UChar c)
83     {
84         return c == ' ' || c == '\r' || c == '\n' || c == '\t' || c == '\f';
85     }
86 
87 } // namespace WebCore
88 
89 #endif // SpaceSplitString_h
90