You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oro-dev@jakarta.apache.org by "Mark F. Murphy" <ma...@tyrell.com> on 2001/05/10 22:15:41 UTC

[PATCH] [Upper|Lower]Case For Perl5Substitution

--- Perl5Substitution.java.orig	Thu May 10 12:48:14 2001
+++ Perl5Substitution.java	Thu May 10 13:11:12 2001
@@ -57,7 +57,7 @@
   * by Daniel F. Savarese. We appreciate his contributions.
   */

-import java.util.*;
+import java.util.Vector;

  /**
   * Perl5Substitution implements a Substitution consisting of a
@@ -135,70 +135,141 @@
     */
    public static final int INTERPOLATE_NONE = -1;

+  /**
+   * A constant declaring opcode for copy operation
+   */
+  public static final int OPCODE_COPY = -1;
+
+  /**
+   * A constant declaring opcode for lowercase char operation
+   */
+  public static final int OPCODE_LOWERCASE_CHAR = -2;
+
+  /**
+   * A constant declaring opcode for uppercase char operation
+   */
+  public static final int OPCODE_UPPERCASE_CHAR = -3;
+
+  /**
+   * A constant declaring opcode for lowercase mode operation
+   */
+  public static final int OPCODE_LOWERCASE_MODE = -4;
+
+  /**
+   * A constant declaring opcode for lowercase mode operation
+   */
+  public static final int OPCODE_UPPERCASE_MODE = -5;
+
+  /**
+   * A constant declaring opcode for lowercase mode operation
+   */
+  public static final int OPCODE_ENDCASE_MODE = -6;
+
    int _numInterpolations;
-  ArrayList _substitutions;
+  Vector _substitutions;
    transient String _lastInterpolation;

-  static ArrayList _parseSubs(String sub) {
-    boolean saveDigits, storedInterpolation;
-    int current;
+  static Vector _parseSubs(String sub) {
+    boolean saveDigits, escapeMode, caseMode;
+    int posParam;
+    int offset;
      char[] str;
-    ArrayList subs;
-    StringBuffer numBuffer, strBuffer;
+    Vector subs;

-    subs = new ArrayList(5);
-    numBuffer = new StringBuffer(5);
-    strBuffer = new StringBuffer(10);
+    subs = new Vector(5);

      str = sub.toCharArray();
-    current = 0;
+    posParam = 0;
+    offset = -1;
      saveDigits = false;
-    storedInterpolation = false;
+    escapeMode = false;
+    caseMode = false;

-    while(current < str.length) {
-      if(saveDigits && Character.isDigit(str[current])) {
-	numBuffer.append(str[current]);
-
-	if(strBuffer.length() > 0) {
-	  subs.add(strBuffer.toString());
-	  strBuffer.setLength(0);
-	}
-      } else {
-	if(saveDigits) {
-	  try {
-	    subs.add(new Integer(numBuffer.toString()));
-	    storedInterpolation = true;
-	  } catch(NumberFormatException e) {
-	    subs.add(numBuffer.toString());
-	  }
+    for (int current = 0; current < str.length; current++) {

-	  numBuffer.setLength(0);
-	  saveDigits = false;
-	}
+        char c = str[current];
+        int next = current + 1;
+
+        // Save digits
+        if (saveDigits) {
+            int digit = Character.digit(c, 10);
+            if (digit > -1) {
+                if (posParam <= 32767) {
+                    posParam *= 10;
+                    posParam += digit;
+                }
+                if (next == str.length) {
+                    subs.addElement(new Integer(posParam));
+                }
+                continue;
+            }
+            subs.addElement(new Integer(posParam));
+            posParam = 0;
+            saveDigits = false;
+        }
+
+        // Copy
+        if ((c != '$' && c != '\\') || escapeMode) {
+            escapeMode = false;
+            if (offset < 0) {
+                offset = current;
+                subs.addElement(new Integer(OPCODE_COPY));
+                subs.addElement(new Integer(offset));
+            }
+            if (next == str.length) {
+                subs.addElement(new Integer(next - offset));
+            }
+            continue;
+        }
+
+        // End Copy
+        if (offset >= 0) {
+            subs.addElement(new Integer(current - offset));
+            offset = -1;
+        }
+
+        // Only do positional and escapes if we have a next char
+        if (next == str.length)
+            continue;
+        char nextc = str[next];
+
+        // Positional params
+        if (c == '$') {
+            saveDigits = (nextc != '0' && Character.isDigit(nextc));
+        }
+
+        // Escape codes
+        else if (c == '\\') {
+            if (nextc == 'l') {
+                if (!caseMode){
+                    subs.addElement(new Integer(OPCODE_LOWERCASE_CHAR));
+                    current++;
+                }
+            } else if (nextc == 'u') {
+                if (!caseMode) {
+                    subs.addElement(new Integer(OPCODE_UPPERCASE_CHAR));
+                    current++;
+                }
+            } else if (nextc == 'L') {
+                subs.addElement(new Integer(OPCODE_LOWERCASE_MODE));
+                current++;
+                caseMode = true;
+            } else if (nextc == 'U') {
+                subs.addElement(new Integer(OPCODE_UPPERCASE_MODE));
+                current++;
+                caseMode = true;
+            } else if (nextc == 'E') {
+                subs.addElement(new Integer(OPCODE_ENDCASE_MODE));
+                current++;
+                caseMode = false;
+            } else {
+                escapeMode = true;
+            }
+        }

-	if(str[current] == '$' &&
-	   current + 1 < str.length && str[current + 1] != '0' &&
-	   Character.isDigit(str[current + 1]))
-	  saveDigits = true;
-	else
-	  strBuffer.append(str[current]);
-      }
-
-      ++current;
-    } // end while
-
-
-    if(saveDigits) {
-      try {
-	subs.add(new Integer(numBuffer.toString()));
-	storedInterpolation = true;
-      } catch(NumberFormatException e) {
-	subs.add(numBuffer.toString());
-      }
-    } else if(strBuffer.length() > 0)
-      subs.add(strBuffer.toString());
+    }

-    return (storedInterpolation ? subs : null);
+    return subs;
    }


@@ -209,16 +280,20 @@
    }

    void _calcSub(StringBuffer buffer, MatchResult result) {
-    int size, value;
+    int size, element, value, offset, count, caseMode;
      Object obj;
      Integer integer;
      String group;
-    Iterator it;
+    char[] sub;

-    it = _substitutions.iterator();
+    caseMode = 0;
+    char[] str = getSubstitution().toCharArray();
+    char[] match = result.group(0).toCharArray();

-    while(it.hasNext()) {
-      obj = it.next();
+    size = _substitutions.size();
+
+    for(element=0; element < size; element++) {
+      obj = _substitutions.elementAt(element);

        if(obj instanceof String)
  	buffer.append(obj);
@@ -226,15 +301,80 @@
  	integer = (Integer)obj;
  	value = integer.intValue();

+        // Groups
  	if(value > 0 && value < result.groups()) {
-	  group = result.group(value);

-	  if(group != null)
-	    buffer.append(group);
-	} else {
-	  buffer.append('$');
-	  buffer.append(value);
+            offset = result.begin(value);
+            if (offset < 0) continue;
+            int end = result.end(value);
+            if (end < 0) continue;
+
+            int len = result.length();
+
+            if (offset >= len || end > len || offset >= end) continue;
+
+            count = end - offset;
+            sub = match;
+
+        }
+
+        // Copy
+        else if (value == OPCODE_COPY) {
+            element++;
+            if (element >= size) continue;
+            offset = ((Integer) _substitutions.elementAt(element)).intValue();
+            element++;
+            if (element >= size) continue;
+            count = ((Integer) _substitutions.elementAt(element)).intValue();
+            sub = str;
+        }
+
+        // Case char
+        else if (value == OPCODE_LOWERCASE_CHAR || value == 
OPCODE_UPPERCASE_CHAR) {
+            if (caseMode != OPCODE_LOWERCASE_MODE && caseMode != 
OPCODE_UPPERCASE_MODE) {
+                caseMode = value;
+            }
+            continue;
+        }
+
+        // Case mode
+        else if (value == OPCODE_LOWERCASE_MODE || value == 
OPCODE_UPPERCASE_MODE) {
+            caseMode = value;
+            continue;
+        }
+
+        // Case mode end
+        else if (value == OPCODE_ENDCASE_MODE) {
+            caseMode = 0;
+            continue;
+        }
+
+        // Unknown
+        else {
+            continue;
  	}
+
+        // Apply modes to buf
+        if (caseMode == OPCODE_LOWERCASE_CHAR) {
+            buffer.append(Character.toLowerCase(sub[offset++]));
+            buffer.append(sub, offset, --count);
+            caseMode = 0;
+        } else if (caseMode == OPCODE_UPPERCASE_CHAR) {
+            buffer.append(Character.toUpperCase(sub[offset++]));
+            buffer.append(sub, offset, --count);
+            caseMode = 0;
+        } else if (caseMode == OPCODE_LOWERCASE_MODE) {
+            for (int end = offset + count; offset < end; ) {
+                buffer.append(Character.toLowerCase(sub[offset++]));
+            }
+        } else if (caseMode == OPCODE_UPPERCASE_MODE) {
+            for (int end = offset + count; offset < end; ) {
+                buffer.append(Character.toUpperCase(sub[offset++]));
+            }
+        } else {
+            buffer.append(sub, offset, count);
+        }
+
        }
      }
    }