Login | Register
My pages Projects Community openCollabNet

Discussions > dev > EscapedString

Project highlights: Architectural Overview

joist
Discussion topic

Back to topic list

EscapedString

Author dlr
Full name Daniel Rall
Date 2000-10-20 16:36:18 PDT
Message Greg McNulty wrote:
>
> one notable point: we were using
> org.joist.util.Escap​edString.from() in an
> unsafe way. this method makes the following call
>
> Util.substitute(matcher, pattern, backslashDoubled, input, limit,
> nointerp)
>
> which, as indicated, is a class (static) method. Our _threads_
> called from(), and appeared to rewrite each others variables. If
> you're using org.joist.util.Escap​edString.from() please keep this in
> mind.

Greg, it looked like this hadn't been corrected yet. Since
EscapedString is used all throughout our source code, it is critical
that it functions correctly. I've rewritten it for efficiency, and
think found the threading issue in the reuse of the Perl5Matcher
instance.

The ORO Matcher is now a Jakarta project, and its source code is readily
available. Also, there is the GNU regexp package as well (which we are
using), and yet another regexp package at Jakarta if all else fails.
Anyhow, I've committed my changes, but thought that I'd post'em here so
you can see what I did and comment on whether you think it will help.


Index: EscapedString.java
====================​====================​====================​=======
RCS file: /cvs/joist/java/org/​joist/util/EscapedSt​ring.java,v
retrieving revision 1.7
diff -u -u -r1.7 EscapedString.java
--- EscapedString.java 2000/10/07 18:00:14 1.7
+++ EscapedString.java 2000/10/20 23:32:48
@@ -56,6 +56,7 @@
   * doubling it ("\\").
   *
   * @author David C. Pellegrini <A
HREF="mailto:davidp@​dataweb-systems.com"​>davidp@dataweb-s​ystems.com</A>​
+ * @author <a href="mailto:dlr@col​lab.net">Daniel Rall</a>
   * @version $Revision: 1.7 $
   *
   * Copyright (c) 1999 dataweb systems, inc.
@@ -64,14 +65,24 @@
 
 public class EscapedString
 {
- static final private PatternCompiler compiler = new
Perl5Compiler();
- static final private PatternMatcher matcher = new Perl5Matcher();
- static private Pattern pattern = null;
- static final private String backslashRegEx = "\\\\";
- static final private String backslashDoubled = "\\\\";
- static final private int limit = Util.SUBSTITUTE_ALL;
- static final private int nointerp = Util.INTERPOLATE_NONE;
+ private static final Pattern PATTERN;
+ private static final String BACKSLASH_RE = "\\\\";
+ private static final String BACKSLASH_DOUBLED = "\\\\";
+ private static final int LIMIT = Util.SUBSTITUTE_ALL;
+ private static final int NO_INTERP = Util.INTERPOLATE_NONE;
 
+ static
+ {
+ try
+ {
+ PATTERN = new Perl5Compiler().comp​ile(BACKSLASH_RE);
+ }
+ catch (MalformedPatternException ignored)
+ {
+ // This is not likely to happen.
+ PATTERN = null;
+ }
+ }
 
     /**
      * Tests the operation of the from method.
@@ -79,14 +90,13 @@
      *
      * @param args A variable of type String
      */
- public static final void
- main (String[] args)
+ public static final void main (String[] args)
     {
- String input = args[0];
+ String input = args[0];
 
- System.out.println("original: " + input);
- String subbed = from(input);
- System.out.println("​substituted: " + subbed);
+ System.out.println("original: " + input);
+ String subbed = from(input);
+ System.out.println("​substituted: " + subbed);
     }
 
     /**
@@ -99,20 +109,9 @@
      * needing escaping.
      * @return A string with all escape characters escaped.
      */
- public static String
- from (String input)
+ public static final String from (String input)
     {
- // Bootstrap the first time through ...
- if (pattern == null) {
- synchronized(compiler) {
- if (pattern == null) // if it's STILL null ;-)
- try {
- pattern = compiler.compile(bac​kslashRegEx);
- } catch (MalformedPatternException ignored){};
- }
- }
-
- return Util.substitute(matcher, pattern, backslashDoubled,
input,
- limit, nointerp);
+ return Util.substitute(new Perl5Matcher(), PATTERN,
BACKSLASH_DOUBLED,
+ input, LIMIT, NO_INTERP);
     }
 }
--

Daniel Rall <dlr at finemaltcoding dot com>

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

EscapedString dlr Daniel Rall 2000-10-20 16:36:18 PDT
Messages per page: