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 df...@apache.org on 2001/05/17 23:16:23 UTC

cvs commit: jakarta-oro/src/java/org/apache/oro/text/regex Substitution.java Perl5Substitution.java StringSubstitution.java Util.java

dfs         01/05/17 14:16:23

  Modified:    src/java/org/apache/oro/text/regex Substitution.java
                        Perl5Substitution.java StringSubstitution.java
                        Util.java
  Log:
    Made a backwards-incompatible change in the Substitution interface.
    The input parameter is now a PatternMatcherInput instance instead
    of a String.  A new substitute method was added to Util to allow
    programmers to reduce the number of string copies in the existing
    substitute() implementation and also to provide the oft-requested
    ability to know how many substitutions were performed.  This
    required that the Substitution interface be changed.  The existing
    Substitution implementing classes do not use the input parameter and
    it is very easy for anyone implementing the Substitution interface in
    a custom class to update their class to the new interface.  A
    deprecation phase was skipped because it would have still required
    implementors of the interface to implement a method with the new signature.
  
  Revision  Changes    Path
  1.4       +11 -6     jakarta-oro/src/java/org/apache/oro/text/regex/Substitution.java
  
  Index: Substitution.java
  ===================================================================
  RCS file: /home/cvs/jakarta-oro/src/java/org/apache/oro/text/regex/Substitution.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Substitution.java	2001/05/09 18:22:10	1.3
  +++ Substitution.java	2001/05/17 21:16:07	1.4
  @@ -57,6 +57,10 @@
    * by Daniel F. Savarese. We appreciate his contributions.
    */
   
  +/*
  + * $Id: Substitution.java,v 1.4 2001/05/17 21:16:07 dfs Exp $
  + */
  +
   /**
    * The Substitution interface provides a means for you to control how
    * a substitution is performed when using the
  @@ -66,10 +70,10 @@
    * achieve custom control over the behavior of substitutions, you can 
    * create your own implementations.  A common use for customization is
    * to make a substitution a function of a match.
  -
  - @author <a href="mailto:oro-dev@jakarta.apache.org">Daniel F. Savarese</a>
  - @version $Id: Substitution.java,v 1.3 2001/05/09 18:22:10 dfs Exp $
  -
  + *
  + * @author <a href="mailto:oro-dev@jakarta.apache.org">Daniel F. Savarese</a>
  + * @version @version@
  + * @since 1.1
    * @see Util
    * @see Util#substitute
    * @see StringSubstitution
  @@ -110,11 +114,12 @@
      * @param substitutionCount  The number of substitutions that have been
      *  performed so far by Util.substitute.
      * @param originalInput The original input upon which the substitutions are
  -   * being performed.
  +   * being performed.  The Substitution must treat this parameter as read only.
      * @param matcher The PatternMatcher used to find the current match.
      * @param pattern The Pattern used to find the current match.
      */
     public void appendSubstitution(StringBuffer appendBuffer, MatchResult match,
  -				 int substitutionCount, String originalInput, 
  +				 int substitutionCount,
  +				 PatternMatcherInput originalInput, 
   				 PatternMatcher matcher, Pattern pattern);
   }
  
  
  
  1.6       +4 -3      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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Perl5Substitution.java	2001/05/17 18:59:47	1.5
  +++ Perl5Substitution.java	2001/05/17 21:16:11	1.6
  @@ -58,7 +58,7 @@
    */
   
   /*
  - * $Id: Perl5Substitution.java,v 1.5 2001/05/17 18:59:47 dfs Exp $
  + * $Id: Perl5Substitution.java,v 1.6 2001/05/17 21:16:11 dfs Exp $
    */
   import java.util.*;
   
  @@ -506,12 +506,13 @@
      * @param substitutionCount  The number of substitutions that have been
      *  performed so far by Util.substitute.
      * @param originalInput The original input upon which the substitutions are
  -   * being performed.
  +   * being performed.  This is a read-only parameter and is not modified.
      * @param matcher The PatternMatcher used to find the current match.
      * @param pattern The Pattern used to find the current match.
      */
     public void appendSubstitution(StringBuffer appendBuffer, MatchResult match,
  -				 int substitutionCount, String originalInput, 
  +				 int substitutionCount,
  +				 PatternMatcherInput originalInput, 
   				 PatternMatcher matcher, Pattern pattern)
     {
       if(_subOpcodes == null) {
  
  
  
  1.4       +4 -3      jakarta-oro/src/java/org/apache/oro/text/regex/StringSubstitution.java
  
  Index: StringSubstitution.java
  ===================================================================
  RCS file: /home/cvs/jakarta-oro/src/java/org/apache/oro/text/regex/StringSubstitution.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- StringSubstitution.java	2001/05/09 18:22:08	1.3
  +++ StringSubstitution.java	2001/05/17 21:16:13	1.4
  @@ -63,7 +63,7 @@
    * {@link Util#substitute Util.substitute}.
   
    @author <a href="mailto:oro-dev@jakarta.apache.org">Daniel F. Savarese</a>
  - @version $Id: StringSubstitution.java,v 1.3 2001/05/09 18:22:08 dfs Exp $
  + @version $Id: StringSubstitution.java,v 1.4 2001/05/17 21:16:13 dfs Exp $
   
    * @see Substitution
    * @see Util
  @@ -133,12 +133,13 @@
      * @param substitutionCount  The number of substitutions that have been
      *  performed so far by Util.substitute.
      * @param originalInput The original input upon which the substitutions are
  -   * being performed.
  +   * being performed.  This is a read-only parameter and is not modified.
      * @param matcher The PatternMatcher used to find the current match.
      * @param pattern The Pattern used to find the current match.
      */
     public void appendSubstitution(StringBuffer appendBuffer, MatchResult match,
  -				 int substitutionCount, String originalInput, 
  +				 int substitutionCount,
  +				 PatternMatcherInput originalInput, 
   				 PatternMatcher matcher, Pattern pattern)
     {
       if(_subLength == 0) 
  
  
  
  1.6       +49 -33    jakarta-oro/src/java/org/apache/oro/text/regex/Util.java
  
  Index: Util.java
  ===================================================================
  RCS file: /home/cvs/jakarta-oro/src/java/org/apache/oro/text/regex/Util.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Util.java	2001/05/09 18:22:11	1.5
  +++ Util.java	2001/05/17 21:16:14	1.6
  @@ -58,6 +58,10 @@
    */
   import java.util.*;
   
  +/*
  + * $Id: Util.java,v 1.6 2001/05/17 21:16:14 dfs Exp $
  + */
  +
   /**
    * The Util class is a holder for useful static utility methods that can
    * be generically applied to Pattern and PatternMatcher instances.
  @@ -83,10 +87,10 @@
    *          are retrieved, which is more efficient than storing them all in a
    *          Vector and then accessing them.
    * </ol>
  -
  - @author <a href="mailto:oro-dev@jakarta.apache.org">Daniel F. Savarese</a>
  - @version $Id: Util.java,v 1.5 2001/05/09 18:22:11 dfs Exp $
  -
  + *
  + * @author <a href="mailto:oro-dev@jakarta.apache.org">Daniel F. Savarese</a>
  + * @version @version@
  + * @since 1.0
    * @see Pattern
    * @see PatternMatcher
    */
  @@ -155,7 +159,7 @@
      *               the pattern.  You should use the <b>SPLIT_ALL</b> constant
      *               to achieve this behavior instead of relying on the default
      *               behavior associated with non-positive limit values.
  -   * @return A <code>Vector</code>
  +   * @since 2.0
      */
     public static void split(Collection results, PatternMatcher matcher,
   			   Pattern pattern, String input, int limit)
  @@ -211,6 +215,7 @@
      * @param matcher The regular expression matcher to execute the split.
      * @param pattern The regular expression to use as a split delimiter.
      * @param input  The <code>String</code> to split.
  +   * @since 2.0
      */
     public static void split(Collection results,  PatternMatcher matcher,
   			   Pattern pattern, String input)
  @@ -306,6 +311,7 @@
      * @param input  The <code>String</code> to split.
      * @return A <code>Vector</code> containing all the substrings of the input
      *         that occur between the regular expression delimiter occurences.
  +   * @since 1.0
      */
     public static Vector split( PatternMatcher matcher, Pattern pattern,
   			      String input)
  @@ -334,39 +340,21 @@
      * @return A String comprising the input string with the substitutions,
      *         if any, made.  If no substitutions are made, the returned String
      *         is the original input String.
  +   * @since 1.0
      */
     public static String substitute(PatternMatcher matcher, Pattern pattern,
   				  Substitution sub, String input, int numSubs)
     {
  -    int beginOffset, subCount;
  -    MatchResult currentResult;
  -    PatternMatcherInput pinput;
  +    int substitutions;
       StringBuffer buffer = new StringBuffer(input.length());
  -
  -    pinput = new PatternMatcherInput(input);
  -    beginOffset = subCount = 0;
  -
  -    // Must be != 0 because SUBSTITUTE_ALL is represented by -1.
  -    // Do NOT change to numSubs > 0.
  -    while(numSubs != 0 && matcher.contains(pinput, pattern)) {
  -      --numSubs;
  -      ++subCount;
  -      currentResult = matcher.getMatch();
  -      buffer.append(input.substring(beginOffset,
  -				    currentResult.beginOffset(0)));
  -      sub.appendSubstitution(buffer, currentResult, subCount,
  -			     input, matcher, pattern);
  -      beginOffset = currentResult.endOffset(0);
  -    }
  -
  -    // No substitutions performed.  There's no point in duplicating 
  -    // the string as would happen if this check were omitted.
  -    if(subCount == 0)
  -      return input;
  -
  -    buffer.append(input.substring(beginOffset, input.length()));
  -
  -    return buffer.toString();
  +    PatternMatcherInput pinput = new PatternMatcherInput(input);
  +    
  +    // Users have indicated that they expect the result to be the
  +    // original input string, rather than a copy, if no substitutions
  +    // are performed, 
  +    if(substitute(buffer, matcher, pattern, sub, pinput, numSubs) != 0)
  +      return buffer.toString();
  +    return input;
     }
   
     /**
  @@ -387,6 +375,7 @@
      * @return A String comprising the input string with the substitutions,
      *         if any, made.  If no substitutions are made, the returned String
      *         is the original input String.
  +   * @since 1.0
      */
     public static String substitute(PatternMatcher matcher, Pattern pattern,
   				  Substitution sub, String input)
  @@ -394,4 +383,31 @@
       return substitute(matcher, pattern, sub, input, 1);
     }
   
  +  public static int substitute(StringBuffer result,
  +			       PatternMatcher matcher, Pattern pattern,
  +			       Substitution sub, PatternMatcherInput input,
  +			       int numSubs)
  +  {
  +    int beginOffset, subCount;
  +    char[] inputBuffer;
  +
  +    subCount    = 0;
  +    beginOffset = input.getBeginOffset(); 
  +    inputBuffer = input.getBuffer();
  +
  +    // Must be != 0 because SUBSTITUTE_ALL is represented by -1.
  +    // Do NOT change to numSubs > 0.
  +    while(numSubs != 0 && matcher.contains(input, pattern)) {
  +      --numSubs;
  +      ++subCount;
  +      result.append(inputBuffer, beginOffset,
  +		    input.getMatchBeginOffset() - beginOffset);
  +      sub.appendSubstitution(result, matcher.getMatch(), subCount,
  +			     input, matcher, pattern);
  +      beginOffset = input.getMatchEndOffset();
  +    }
  +
  +    result.append(inputBuffer, beginOffset, input.length() - beginOffset);
  +    return subCount;
  +  }
   }