What is the Java 1.4.2 equivalent of Pattern.quote()
Asked Answered
H

3

7

What would be a Java 1.4.2 equivalent of Pattern.quote?

I was using Pattern.quote() on a URI but now need to make it 1.4.2 compatible.

Honesty answered 15/6, 2010 at 19:47 Comment(0)
A
4

Well the source code of Pattern.quote is available and looks like this:

public static String quote(String s) {
    int slashEIndex = s.indexOf("\\E");
    if (slashEIndex == -1)
        return "\\Q" + s + "\\E";

    StringBuilder sb = new StringBuilder(s.length() * 2);
    sb.append("\\Q");
    slashEIndex = 0;
    int current = 0;
    while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
        sb.append(s.substring(current, slashEIndex));
        current = slashEIndex + 2;
        sb.append("\\E\\\\E\\Q");
    }
    sb.append(s.substring(current, s.length()));
    sb.append("\\E");
    return sb.toString();
}

Basically it relies on

\Q  Nothing, but quotes all characters until \E
\E  Nothing, but ends quoting started by \Q

and has a special treatement of the case in which \E is present in the string.

Alkalize answered 15/6, 2010 at 19:49 Comment(4)
That will actually do for me. Excuse my newbness, but how did you obtain the source?Honesty
The source is provided with the SDK, in eclipse you can shift-klick on a class to look at it's source.Mose
@Tobias, I think you mean Ctrl+Click, but I believe it requires that you specify the location of the source-zip file.Alkalize
Yes, should be click ;) You only need to use the JDK as JRE in Eclipse.Mose
M
2

This is the code of quote:

    public static String quote(String s) {
        int slashEIndex = s.indexOf("\\E");
        if (slashEIndex == -1)
            return "\\Q" + s + "\\E";

        StringBuilder sb = new StringBuilder(s.length() * 2);
        sb.append("\\Q");
        slashEIndex = 0;
        int current = 0;
        while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
            sb.append(s.substring(current, slashEIndex));
            current = slashEIndex + 2;
            sb.append("\\E\\\\E\\Q");
        }
        sb.append(s.substring(current, s.length()));
        sb.append("\\E");
        return sb.toString();
    }

Seems not hard copying or implementing by your self or?

Edit: aiobee was faster, sry

Mose answered 15/6, 2010 at 19:51 Comment(1)
You can add value to your reply by replacing the StringBuilder with a StringBuffer; StringBuilder wasn't introduced until JDK 1.5.Ddene
P
1

Here's the GNU Classpath implementation (in case the Java license worries you):

  public static String quote(String str)
  {
    int eInd = str.indexOf("\\E");
    if (eInd < 0)
      {
        // No need to handle backslashes.
        return "\\Q" + str + "\\E";
      }

    StringBuilder sb = new StringBuilder(str.length() + 16);
    sb.append("\\Q"); // start quote

    int pos = 0;
    do
      {
        // A backslash is quoted by another backslash;
        // 'E' is not needed to be quoted.
        sb.append(str.substring(pos, eInd))
          .append("\\E" + "\\\\" + "E" + "\\Q");
        pos = eInd + 2;
      } while ((eInd = str.indexOf("\\E", pos)) >= 0);

    sb.append(str.substring(pos, str.length()))
      .append("\\E"); // end quote
    return sb.toString();
  }
Permutation answered 4/11, 2013 at 5:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.