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