Home | History | Annotate | Download | only in regex
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
      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 package java.util.regex;
     18 
     19 /**
     20  * Holds the results of a successful match of a regular expression against a
     21  * given string. Only used internally, thus sparsely documented (though the
     22  * defining public interface has full documentation).
     23  *
     24  * @see java.util.regex.MatchResult
     25  */
     26 class MatchResultImpl implements MatchResult {
     27 
     28     /**
     29      * Holds the original input text.
     30      */
     31     private String text;
     32 
     33     /**
     34      * Holds the offsets of the groups in the input text. The first two
     35      * elements specifiy start and end of the zero group, the next two specify
     36      * group 1, and so on.
     37      */
     38     private int[] offsets;
     39 
     40     MatchResultImpl(String text, int[] offsets) {
     41         this.text = text;
     42         this.offsets = offsets.clone();
     43     }
     44 
     45     public int end() {
     46         return end(0);
     47     }
     48 
     49     public int end(int group) {
     50         return offsets[2 * group + 1];
     51     }
     52 
     53     public String group() {
     54         return text.substring(start(), end());
     55     }
     56 
     57     public String group(int group) {
     58         int from = offsets[group * 2];
     59         int to = offsets[(group * 2) + 1];
     60         if (from == -1 || to == -1) {
     61             return null;
     62         } else {
     63             return text.substring(from, to);
     64         }
     65     }
     66 
     67     public int groupCount() {
     68         return (offsets.length / 2) - 1;
     69     }
     70 
     71     public int start() {
     72         return start(0);
     73     }
     74 
     75     public int start(int group) {
     76         return offsets[2 * group];
     77     }
     78 
     79 }
     80