001package votorola.g.web.gwt; // Copyright 2012, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003
004/** {@linkplain com.google.gwt.regexp.shared.RegExp RegExp} utilities.
005  */
006public final class RegExpX
007{
008
009
010    private RegExpX() {}
011
012
013
014    /** Appends the character to the string builder, preceded by any quoting necessary to
015      * convert it from a meta-character to a character literal.
016      */
017    public static void appendQuoted( final StringBuilder b, final char ch )
018    {
019        if( ch == '\\'
020         || ch == '.'
021         || ch == '*'
022         || ch == '+'
023         || ch == '?'
024         || ch == '='
025         || ch == '!'
026         || ch == ':'
027         || ch == '|'
028         || ch == '(' || ch == ')'
029         || ch == '{' || ch == '}'
030         || ch == '$'
031         || ch == '^'
032         || ch == '[' || ch == ']' ) b.append( '\\' );
033        b.append( ch );
034    }
035
036
037
038    /** Translates all meta-characters in the string to literals by quoting where
039      * necessary and appends the result to the string builder.
040      */
041    public static void appendQuoted( final StringBuilder b, final String s )
042    {
043        appendQuoted( b, s, /*start*/0, /*end*/s.length() );
044    }
045
046
047
048    /** Translates all meta-characters in the substring to literals by quoting where
049      * necessary and appends the result to the string builder.  The substring is taken
050      * from s and extends from start to end-1.
051      *
052      *     @param start the index of the first character of the substring (inclusive).
053      *     @param end the index of the substring end (exclusive).
054      */
055    public static void appendQuoted( final StringBuilder b, final String s, final int start,
056      final int end )
057    {
058        for( int c = start; c < end; ++c ) appendQuoted( b, s.charAt( c ));
059    }
060
061
062}