1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.regexp.AsciiUtil;
    18  import org.as2lib.regexp.Pattern;
    19  import org.as2lib.regexp.node.Node;
    20  import org.as2lib.regexp.node.TreeInfo;
    21  
    22  /**
    23   *  {@code BitClass} creates a bit vector for matching ASCII values.
    24   *  
    25   *  @author Igor Sadovskiy
    26   */
    27   
    28  class org.as2lib.regexp.node.BitClass extends Node {
    29  	
    30      private var bits:Array; 
    31      private var complementMe:Boolean;
    32      
    33      public function BitClass(flag:Boolean, newBits:Array) {
    34      	complementMe = (flag != null) ? flag : false; 
    35      	bits = (newBits != null) ? newBits : new Array(256);
    36      }
    37      
    38      public function addChar(c:Number, f:Number):Node {
    39          if ((f & Pattern.CASE_INSENSITIVE) == 0) {
    40              bits[c] = true;
    41              return this;
    42          }
    43          if (c < 128) {
    44              bits[c] = true;
    45              if (AsciiUtil.isUpper(c)) {
    46                  c += 0x20;
    47                  bits[c] = true;
    48              } else if (AsciiUtil.isLower(c)) {
    49                  c -= 0x20;
    50                  bits[c] = true;
    51              }
    52              return this;
    53          }
    54          c = AsciiUtil.toLower(c);
    55          bits[c] = true;
    56          c = AsciiUtil.toUpper(c);
    57          bits[c] = true;
    58          return this;
    59      }
    60      
    61      public function dup(flag:Boolean):Node {
    62          return new BitClass(flag, bits);
    63      }
    64      
    65      public function match(matcher:Object, i:Number, seq:String):Boolean {
    66          if (i >= matcher.to) return false;
    67          var c:Number = seq.charCodeAt(i);
    68          var charMatches:Boolean = (c > 255) ? 
    69          	complementMe : Boolean(Number(bits[c]) ^ Number(complementMe));
    70          return charMatches && next.match(matcher, i+1, seq);
    71      }
    72      
    73      public function study(info:TreeInfo):Boolean {
    74          info.minLength++;
    75          info.maxLength++;
    76          return next.study(info);
    77      }
    78  }
    79