1 19 20 package gnu.regexp; 21 import java.io.Serializable ; 22 23 31 public final class REMatch implements Serializable , Cloneable { 32 private String matchedText; 33 34 int eflags; 37 int offset; 40 41 int anchor; 45 46 int index; int[] start; int[] end; REMatch next; 52 public Object clone() { 53 try { 54 REMatch copy = (REMatch) super.clone(); 55 copy.next = null; 56 57 copy.start = (int[]) start.clone(); 58 copy.end = (int[]) end.clone(); 59 60 return copy; 61 } catch (CloneNotSupportedException e) { 62 throw new Error (); } 64 } 65 66 void assignFrom(REMatch other) { 67 start = other.start; 68 end = other.end; 69 index = other.index; 70 next = other.next; 72 } 73 74 REMatch(int subs, int anchor, int eflags) { 75 start = new int[subs+1]; 76 end = new int[subs+1]; 77 this.anchor = anchor; 78 this.eflags = eflags; 79 clear(anchor); 80 } 81 82 void finish(CharIndexed text) { 83 start[0] = 0; 84 StringBuffer sb = new StringBuffer (); 85 int i; 86 for (i = 0; i < end[0]; i++) 87 sb.append(text.charAt(i)); 88 matchedText = sb.toString(); 89 for (i = 0; i < start.length; i++) { 90 if ((start[i] == -1) ^ (end[i] == -1)) { 93 start[i] = -1; 94 end[i] = -1; 95 } 96 } 97 next = null; } 99 100 101 void clear(int index) { 102 offset = index; 103 this.index = 0; 104 for (int i = 0; i < start.length; i++) { 105 start[i] = end[i] = -1; 106 } 107 next = null; } 109 110 119 public String toString() { 120 return matchedText; 121 } 122 123 127 public int getStartIndex() { 128 return offset + start[0]; 129 } 130 131 148 public int getEndIndex() { 149 return offset + end[0]; 150 } 151 152 160 public String toString(int sub) { 161 if ((sub >= start.length) || (start[sub] == -1)) return ""; 162 return (matchedText.substring(start[sub],end[sub])); 163 } 164 165 173 public int getSubStartIndex(int sub) { 174 if (sub >= start.length) return -1; 175 int x = start[sub]; 176 return (x == -1) ? x : offset + x; 177 } 178 179 187 public int getStartIndex(int sub) { 188 if (sub >= start.length) return -1; 189 int x = start[sub]; 190 return (x == -1) ? x : offset + x; 191 } 192 193 201 public int getSubEndIndex(int sub) { 202 if (sub >= start.length) return -1; 203 int x = end[sub]; 204 return (x == -1) ? x : offset + x; 205 } 206 207 214 public int getEndIndex(int sub) { 215 if (sub >= start.length) return -1; 216 int x = end[sub]; 217 return (x == -1) ? x : offset + x; 218 } 219 220 229 public String substituteInto(String input) { 230 StringBuffer output = new StringBuffer (); 232 int pos; 233 for (pos = 0; pos < input.length()-1; pos++) { 234 if ((input.charAt(pos) == '$') && (Character.isDigit(input.charAt(pos+1)))) { 235 int val = Character.digit(input.charAt(++pos),10); 236 if (val < start.length) { 237 output.append(toString(val)); 238 } 239 } else output.append(input.charAt(pos)); 240 } 241 if (pos < input.length()) output.append(input.charAt(pos)); 242 return output.toString(); 243 } 244 } 245 | Popular Tags |