5 // Arina Itkes <arinai@mainsoft.com>
7 // Copyright (C) 2007 Mainsoft, Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections.Generic;
33 using System.Collections;
35 using java.util.regex;
38 namespace System.Text.RegularExpressions
40 sealed class JvmReMachine : BaseMachine
42 readonly PatternData _patternData;
44 internal JvmReMachine (PatternData patternData) {
45 this._patternData = patternData;
50 internal PatternData PatternData {
51 get { return _patternData; }
54 internal Pattern JavaPattern {
55 get { return _patternData.JavaPattern; }
58 internal IDictionary Mapping {
59 get { return _patternData.GroupNameToNumberMap; }
60 set { throw new NotImplementedException ("Mapping setter of JvmReMachine should not be called."); }//We must implement the setter of interface but it is not in use
63 internal string [] NamesMapping {
64 get { return _patternData.GroupNumberToNameMap; }
65 set { throw new NotImplementedException ("NamesMapping setter of JvmReMachine should not be called."); }//We must implement the setter of interface but it is not in use
68 internal int GroupCount {
69 get { return _patternData.GroupCount; }
74 #region Implementations of IMachine Interface
76 public override Match Scan (Regex regex, string text, int start, int end) {
81 Matcher m = JavaPattern.matcher (((CharSequence) (object) text).subSequence(0, end));
83 if (!m.find (start)) {
84 return System.Text.RegularExpressions.Match.Empty;
87 GroupCollection groups = new GroupCollection (regex.GroupCount + 1);
88 Match match = new Match (regex, this, groups, text, text.Length,
89 m.start (), m.end () - m.start ());
90 for (int javaGroupNumber = 1; javaGroupNumber <= m.groupCount (); ++javaGroupNumber) {
91 AddGroup (m, groups, javaGroupNumber, text, match);
97 public override string [] Split (Regex regex, string input, int count, int startat) {
99 string [] splitArray = JavaPattern.split ((CharSequence) (object) input.Substring (startat), count);
101 if (regex.GroupCount == 0 || splitArray.Length == 1) {
106 count = Int32.MaxValue;
108 Matcher m = JavaPattern.matcher ((CharSequence) (object) input.Substring (startat));
110 int splitArrayIndex = 1;
112 List<string> splits = new List<string> (splitArray.Length * (1 + regex.GroupCount));
113 splits.Add (splitArray [0]);
115 for (int number = 0; number < count; ++number) {
120 for (int i = 1; i <= m.groupCount (); ++i) {
121 splits.Add (m.group (i));
124 splits.Add (splitArray [splitArrayIndex++]);
127 return splits.ToArray ();
130 public override string Replace (Regex regex, string input, string replacement, int count, int startat) {
132 if (regex.SameGroupNamesFlag) {
133 return base.Replace (regex, input, replacement, count, startat);
137 count = Int32.MaxValue;
140 string replacementPattern = ReplacementData.Reformat (regex, replacement);
141 Matcher m = JavaPattern.matcher ((CharSequence) (object) input);
143 StringBuffer sb = new StringBuffer ();
144 if (count > 0 && m.find (startat)) {
145 ReplacementData.ReplaceMatch (replacementPattern, m, sb, input, _patternData);
148 for (int matchesCounter = 1; matchesCounter < count; ++matchesCounter) {
152 ReplacementData.ReplaceMatch (replacementPattern, m, sb, input, _patternData);
157 return sb.ToString ();
161 public override string Result (string replacement, Match match) {
162 if (match.Length == 0)
165 string result = Replace (match.Regex, match.Text, replacement, 1, 0);
166 return result.Substring (match.Index, result.Length - (match.Text.Length - match.Length));
170 #endregion Implementations of IMachine Interface
172 private void AddGroup (Matcher m, GroupCollection groups, int javaGroupNumber, string text, Match match) {
173 int netGroupNumber = _patternData.JavaToNetGroupNumbersMap [javaGroupNumber];
174 if (netGroupNumber == -1) {
178 int index = m.start (javaGroupNumber);
181 if(groups[netGroupNumber] == null)
182 groups.SetValue (new Group (), netGroupNumber);
186 Group group = new Group (text, index, m.end (javaGroupNumber) - index, match, netGroupNumber);
188 groups.SetValue (group, netGroupNumber);