RegExpResult.java revision 953:221a84ef44c0
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.nashorn.internal.runtime.regexp;
27
28/**
29 * Match tuple to keep track of ongoing regexp match.
30 */
31public final class RegExpResult {
32    final Object[] groups;
33    final int      index;
34    final String   input;
35
36    /**
37     * Constructor
38     *
39     * @param input  regexp input
40     * @param index  index of match
41     * @param groups groups vector
42     */
43    public RegExpResult(final String input, final int index, final Object[] groups) {
44        this.input  = input;
45        this.index  = index;
46        this.groups = groups;
47    }
48
49    /**
50     * Get the groups for the match
51     * @return group vector
52     */
53    public Object[] getGroups() {
54        return groups;
55    }
56
57    /**
58     * Get the input for the map
59     * @return input
60     */
61    public String getInput() {
62        return input;
63    }
64
65    /**
66     * Get the index for the match
67     * @return index
68     */
69    public int getIndex() {
70        return index;
71    }
72
73    /**
74     * Get the length of the match
75     * @return length
76     */
77    public int length() {
78        return ((String)groups[0]).length();
79    }
80
81    /**
82     * Get the group with the given index or the empty string if group index is not valid.
83     * @param groupIndex the group index
84     * @return the group or ""
85     */
86    public Object getGroup(final int groupIndex) {
87        return groupIndex >= 0 && groupIndex < groups.length ? groups[groupIndex] : "";
88    }
89
90    /**
91     * Get the last parenthesis group, or the empty string if none exists.
92     * @return the last group or ""
93     */
94    public Object getLastParen() {
95        return groups.length > 1 ? groups[groups.length - 1] : "";
96    }
97
98}
99