dfs         02/03/14 13:12:53

  Modified:    .        CHANGES
               src/java/org/apache/oro/text/regex Perl5Substitution.java
  Log:
  Added $& to Perl5Substitution based on Takashi's patch.
  
  Revision  Changes    Path
  1.25      +6 -1      jakarta-oro/CHANGES
  
  Index: CHANGES
  ===================================================================
  RCS file: /home/cvs/jakarta-oro/CHANGES,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- CHANGES   19 Feb 2002 04:54:29 -0000      1.24
  +++ CHANGES   14 Mar 2002 21:12:53 -0000      1.25
  @@ -1,6 +1,11 @@
  -$Id: CHANGES,v 1.24 2002/02/19 04:54:29 dfs Exp $
  +$Id: CHANGES,v 1.25 2002/03/14 21:12:53 dfs Exp $
   
   Version 2.0.x
  +
  +o Added $& as a valid interpolation variable for Perl5Substitution.
  +  The behavior of $0 was changed from undefined to the same as $&,
  +  but it should be avoided since $0 no longer means the same thing
  +  as $& in Perl.  Only use $& if possible.
   
   o Removed some leftover references to OROMatcher in the Perl5Util javadocs.
   
  
  
  
  1.8       +28 -11    
jakarta-oro/src/java/org/apache/oro/text/regex/Perl5Substitution.java
  
  Index: Perl5Substitution.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-oro/src/java/org/apache/oro/text/regex/Perl5Substitution.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Perl5Substitution.java    1 Feb 2002 09:38:20 -0000       1.7
  +++ Perl5Substitution.java    14 Mar 2002 21:12:53 -0000      1.8
  @@ -58,7 +58,7 @@
    */
   
   /*
  - * $Id: Perl5Substitution.java,v 1.7 2002/02/01 09:38:20 dfs Exp $
  + * $Id: Perl5Substitution.java,v 1.8 2002/03/14 21:12:53 dfs Exp $
    */
   import java.util.*;
   
  @@ -71,7 +71,7 @@
    * The substitution string may contain variable interpolations referring
    * to the saved parenthesized groups of the search pattern.
    * A variable interpolation is denoted by <b>$1</b>, or <b>$2</b>,
  - * or <b>$3</b>, etc.  If you do not want such expressions to be
  + * or <b>$3</b>, etc.  If you want such expressions to be
    * interpreted literally, you should set the <b> numInterpolations </b>
    * parameter to <b> INTERPOLATE_NONE </b>.  It is easiest to explain
    * what an interpolated variable does by giving an example:
  @@ -112,9 +112,14 @@
    * example, and a substitution expression of <b>a$2-</b>, the result
    * of the last sample input would be:
    * <pre><b>Tank a- 85  Tank a- 32  Tank a- 22</b></pre>
  - * Also, the result of substituting <b>$0</b> or $ followed by an
  - * non-positive integer is undefined.  In order to include a $ in a
  - * substitution, it should be escaped with a backslash (e.g., <b>"\\$0"</b>).
  + * The special substitution <b>$&</b> will interpolate the entire portion
  + * of the input matched by the regular expression.  <b>$0</b> will
  + * do the same, but it is recommended that it be avoided because the
  + * latest versions of Perl use <b>$0</b> to store the program name rather
  + * than duplicate the behavior of <b>$&</b>.
  + * Also, the result of substituting $ followed by a non-positive integer
  + * is undefined.  In order to include a $ in a  substitution, it should
  + * be escaped with a backslash (e.g., <b>"\\$0"</b>).
    * <p>
    * Perl5 double-quoted string case modification is also supported in
    * the substitution.  The following escape sequences are supported:
  @@ -131,6 +136,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel F. Savarese</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Mark Murphy</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Takashi Okamoto</a>
    * @version @version@
    * @since 1.1
    * @see Substitution
  @@ -202,6 +208,10 @@
   
     transient String _lastInterpolation;
   
  +  private static final boolean __isInterpolationCharacter(char ch) {
  +    return (Character.isDigit(ch) || ch == '&');
  +  }
  +
     private void __addElement(int value) {
       int len = _subOpcodes.length;
       if (_subOpcodesCount == len) {
  @@ -212,7 +222,7 @@
       _subOpcodes[_subOpcodesCount++] = value;
     }
   
  -  private void _parseSubs(String sub) {
  +  private void __parseSubs(String sub) {
       boolean saveDigits, escapeMode, caseMode;
       int posParam;
       int offset;
  @@ -236,7 +246,13 @@
           
         // Save digits
         if (saveDigits) {
  -     int digit = Character.digit(c, 10);
  +     int digit;
  +
  +     if(c == '&')
  +       digit = 0;
  +     else
  +       digit = Character.digit(c, 10);
  +
        if (digit > -1) {
          if (posParam <= __MAX_GROUPS) {
            posParam *= 10;
  @@ -247,10 +263,11 @@
          }
          continue;
        }
  +
        __addElement(posParam);
        posParam = 0;
        saveDigits = false;
  -      }
  +      } 
   
         if ((c != '$' && c != '\\') || escapeMode) {
        escapeMode = false;
  @@ -278,7 +295,7 @@
   
         // Positional params
         if (c == '$') {
  -     saveDigits = (nextc != '0' && Character.isDigit(nextc));
  +     saveDigits = __isInterpolationCharacter(nextc);
         } else if (c == '\\') { // Escape codes
        if (nextc == 'l') {
          if (!caseMode){
  @@ -334,7 +351,7 @@
   
         // If we have a group, set up interpolation, else
         // interpret op code.
  -      if(value > 0 && value < result.groups()) {
  +      if(value >= 0 && value < result.groups()) {
        int end, len;
        offset = result.begin(value);
        
  @@ -483,7 +500,7 @@
   
       if(numInterpolations != INTERPOLATE_NONE && 
          (substitution.indexOf('$') != -1 || substitution.indexOf('\\') != -1))
  -      _parseSubs(substitution);
  +      __parseSubs(substitution);
       else
         _subOpcodes = null;
       _lastInterpolation = null;
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to