Home | History | Annotate | Download | only in wrec
      1 /*
      2  * Copyright (C) 2008 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef CharacterClassConstructor_h
     27 #define CharacterClassConstructor_h
     28 
     29 #include <wtf/Platform.h>
     30 
     31 #if ENABLE(WREC)
     32 
     33 #include "CharacterClass.h"
     34 #include <wtf/AlwaysInline.h>
     35 #include <wtf/Vector.h>
     36 #include <wtf/unicode/Unicode.h>
     37 
     38 namespace JSC { namespace WREC {
     39 
     40     class CharacterClassConstructor {
     41     public:
     42         CharacterClassConstructor(bool isCaseInsensitive)
     43             : m_charBuffer(-1)
     44             , m_isPendingDash(false)
     45             , m_isCaseInsensitive(isCaseInsensitive)
     46             , m_isUpsideDown(false)
     47         {
     48         }
     49 
     50         void flush();
     51 
     52         // We need to flush prior to an escaped hyphen to prevent it as being treated as indicating
     53         // a range, e.g. [a\-c] we flush prior to adding the hyphen so that this is not treated as
     54         // [a-c].  However, we do not want to flush if we have already seen a non escaped hyphen -
     55         // e.g. [+-\-] should be treated the same as [+--], producing a range that will also match
     56         // a comma.
     57         void flushBeforeEscapedHyphen()
     58         {
     59             if (!m_isPendingDash)
     60                 flush();
     61         }
     62 
     63         void put(UChar ch);
     64         void append(const CharacterClass& other);
     65 
     66         bool isUpsideDown() { return m_isUpsideDown; }
     67 
     68         ALWAYS_INLINE CharacterClass charClass()
     69         {
     70             CharacterClass newCharClass = {
     71                 m_matches.begin(), m_matches.size(),
     72                 m_ranges.begin(), m_ranges.size(),
     73                 m_matchesUnicode.begin(), m_matchesUnicode.size(),
     74                 m_rangesUnicode.begin(), m_rangesUnicode.size(),
     75             };
     76 
     77             return newCharClass;
     78         }
     79 
     80     private:
     81         void addSorted(Vector<UChar>& matches, UChar ch);
     82         void addSortedRange(Vector<CharacterRange>& ranges, UChar lo, UChar hi);
     83 
     84         int m_charBuffer;
     85         bool m_isPendingDash;
     86         bool m_isCaseInsensitive;
     87         bool m_isUpsideDown;
     88 
     89         Vector<UChar> m_matches;
     90         Vector<CharacterRange> m_ranges;
     91         Vector<UChar> m_matchesUnicode;
     92         Vector<CharacterRange> m_rangesUnicode;
     93     };
     94 
     95 } } // namespace JSC::WREC
     96 
     97 #endif // ENABLE(WREC)
     98 
     99 #endif // CharacterClassConstructor_h
    100