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.node.Node; 
    19  import org.as2lib.regexp.node.TreeInfo;
    20  
    21  /**
    22   * {@code BackRefA} refers to a group in the regular expression 
    23   * using IGNORE CASE flag. 
    24   * Attempts to match whatever the group referred to last matched.
    25   * 
    26   * @author Igor Sadovskiy
    27   */
    28  
    29  class org.as2lib.regexp.node.BackRefA extends Node {
    30  	
    31      private var groupIndex:Number;
    32      
    33      public function BackRefA(groupCount:Number) {
    34          super();
    35          groupIndex = groupCount + groupCount;
    36      }
    37      
    38      public function match(matcher:Object, i:Number, seq:String):Boolean {
    39          var j:Number = matcher.groups[groupIndex];
    40          var k:Number = matcher.groups[groupIndex+1];
    41  
    42          var groupSize:Number = k - j;
    43  
    44          // If the referenced group didn't match, neither can this
    45          if (j < 0) return false;
    46  
    47          // If there isn't enough input left no match
    48          if (i + groupSize > matcher.to) return false;
    49  
    50          // Check each new char to make sure it matches what the group
    51          // referenced matched last time around
    52          for (var index=0; index<groupSize; index++) {
    53              var c1:Number = seq.charCodeAt(i+index);
    54              var c2:Number = seq.charCodeAt(j+index);
    55              if (c1 != c2) {
    56                  c1 = AsciiUtil.toUpper(c1);
    57                  c2 = AsciiUtil.toUpper(c2);
    58                  if (c1 != c2) {
    59                      c1 = AsciiUtil.toLower(c1);
    60                      c2 = AsciiUtil.toLower(c2);
    61                      if (c1 != c2) return false;
    62                  }
    63              }
    64          }
    65  
    66          return next.match(matcher, i+groupSize, seq);
    67      }
    68      
    69      public function study(info:TreeInfo):Boolean {
    70          info.maxValid = false;
    71          return next.study(info);
    72      }
    73  }
    74  
    75