JavaScriptCore/runtime/RegExpKey.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 University of Szeged
       
     3  * Copyright (C) 2010 Renata Hodovan (hodovan@inf.u-szeged.hu)
       
     4  * All rights reserved.
       
     5  *
       
     6  * Redistribution and use in source and binary forms, with or without
       
     7  * modification, are permitted provided that the following conditions
       
     8  * are met:
       
     9  * 1. Redistributions of source code must retain the above copyright
       
    10  *    notice, this list of conditions and the following disclaimer.
       
    11  * 2. Redistributions in binary form must reproduce the above copyright
       
    12  *    notice, this list of conditions and the following disclaimer in the
       
    13  *    documentation and/or other materials provided with the distribution.
       
    14  *
       
    15  * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
       
    16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
       
    19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    26  */
       
    27 
       
    28 #include "UString.h"
       
    29 
       
    30 #ifndef RegExpKey_h
       
    31 #define RegExpKey_h
       
    32 
       
    33 namespace JSC {
       
    34 
       
    35 struct RegExpKey {
       
    36     int flagsValue;
       
    37     RefPtr<UString::Rep> pattern;
       
    38 
       
    39     RegExpKey()
       
    40         : flagsValue(0)
       
    41     {
       
    42     }
       
    43 
       
    44     RegExpKey(int flags)
       
    45         : flagsValue(flags)
       
    46     {
       
    47     }
       
    48 
       
    49     RegExpKey(int flags, const UString& pattern)
       
    50         : flagsValue(flags)
       
    51         , pattern(pattern.rep())
       
    52     {
       
    53     }
       
    54 
       
    55     RegExpKey(int flags, const PassRefPtr<UString::Rep> pattern)
       
    56         : flagsValue(flags)
       
    57         , pattern(pattern)
       
    58     {
       
    59     }
       
    60 
       
    61     RegExpKey(const UString& flags, const UString& pattern)
       
    62         : pattern(pattern.rep())
       
    63     {
       
    64         flagsValue = getFlagsValue(flags);
       
    65     }
       
    66 
       
    67     int getFlagsValue(const UString flags) 
       
    68     {
       
    69         flagsValue = 0;
       
    70         if (flags.find('g') != UString::NotFound)
       
    71             flagsValue += 4;
       
    72         if (flags.find('i') != UString::NotFound)
       
    73             flagsValue += 2;
       
    74         if (flags.find('m') != UString::NotFound)
       
    75             flagsValue += 1;
       
    76         return flagsValue;
       
    77     }
       
    78 };
       
    79 
       
    80 inline bool operator==(const RegExpKey& a, const RegExpKey& b) 
       
    81 {
       
    82     if (a.flagsValue != b.flagsValue)
       
    83         return false;
       
    84     if (!a.pattern)
       
    85         return !b.pattern;
       
    86     if (!b.pattern)
       
    87         return false;
       
    88     return equal(a.pattern.get(), b.pattern.get());
       
    89 }
       
    90 
       
    91 } // namespace JSC
       
    92 
       
    93 namespace WTF {
       
    94 template<typename T> struct DefaultHash;
       
    95 template<typename T> struct RegExpHash;
       
    96 
       
    97 template<> struct RegExpHash<JSC::RegExpKey> {
       
    98     static unsigned hash(const JSC::RegExpKey& key) { return key.pattern->hash(); }
       
    99     static bool equal(const JSC::RegExpKey& a, const JSC::RegExpKey& b) { return a == b; }
       
   100     static const bool safeToCompareToEmptyOrDeleted = false;
       
   101 };
       
   102 
       
   103 template<> struct DefaultHash<JSC::RegExpKey> {
       
   104     typedef RegExpHash<JSC::RegExpKey> Hash;
       
   105 };
       
   106 
       
   107 template<> struct HashTraits<JSC::RegExpKey> : GenericHashTraits<JSC::RegExpKey> {
       
   108     static void constructDeletedValue(JSC::RegExpKey& slot) { slot.flagsValue = -1; }
       
   109     static bool isDeletedValue(const JSC::RegExpKey& value) { return value.flagsValue == -1; }
       
   110 };
       
   111 } // namespace WTF
       
   112 
       
   113 #endif // RegExpKey_h