You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2002/12/07 22:50:30 UTC

cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang StringUtilsTest.java

bayard      2002/12/07 13:50:30

  Modified:    lang     STATUS.html
               lang/src/java/org/apache/commons/lang StringUtils.java
               lang/src/test/org/apache/commons/lang StringUtilsTest.java
  Log:
  Added the StringUtils.unescape method, UnitTest and STATUS change.
  
  Revision  Changes    Path
  1.28      +3 -2      jakarta-commons/lang/STATUS.html
  
  Index: STATUS.html
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/STATUS.html,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- STATUS.html	15 Nov 2002 00:07:26 -0000	1.27
  +++ STATUS.html	7 Dec 2002 21:50:29 -0000	1.28
  @@ -72,6 +72,7 @@
   <ul>
   <li>CharRange.UNSET - will have problems if we introduce reverse ranges that go down to \u0000.</lI>
   <li>Null effects - the classes are not standardised in how they handle null.</li>
  +<li>When running the TestFactoryUtils test, sometimes the CPU speed is not quick enough and 'assertEquals((double) System.currentTimeMillis(), (double) ((Date) created).getTime(), 0.01d);' fails. </li>
   </ul>
   </p>
   
  @@ -90,7 +91,7 @@
   <li>DateRange</li>
   <li>CloneUtils - utility class to enable cloning via various different mechanisms. This code exists in [pattern] at present.</li>
   <li>StringUtils truncateNicely method - A substring with some extra power to choose where to cut off. It was in Avalon and was added separately to String Taglib from a code submission. This suggests it may have some commonality. [CODED]</li>
  -<li>StringUtils unescape method - String Taglib has shown that this method is missing from StringUtils. It would take a String with "\n" in and convert it to the Java character. unescape and escape should be symmetric. </li>
  +<li>StringUtils unescape method - String Taglib has shown that this method is missing from StringUtils. It would take a String with "\n" in and convert it to the Java character. unescape and escape should be symmetric - [DONE. Test symmetry] </li>
   <li>ArrayUtils - opinion seems to be that this belongs with [lang] and not [collections]
   <li>GUID and other Identifier generators - these may belong in [util], some code exists in [pattern] at the moment
   <li>CharUtils - Utilities to work on a char[] in the same way as a String
  
  
  
  1.28      +74 -2     jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java
  
  Index: StringUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- StringUtils.java	27 Nov 2002 22:54:29 -0000	1.27
  +++ StringUtils.java	7 Dec 2002 21:50:29 -0000	1.28
  @@ -55,9 +55,10 @@
    */
   
   import java.util.StringTokenizer;
  -
   import java.util.Iterator;
   
  +import org.apache.commons.lang.exception.NestableRuntimeException;
  +
   /**
    * <p>Common <code>String</code> manipulation routines.</p>
    *
  @@ -952,6 +953,77 @@
                           break;
                   }
               }
  +        }
  +        return buffer.toString();
  +    }
  +
  +    /**
  +     * Unescapes any Java literals found in the String. For example, 
  +     * it will turn a sequence of '\' and 'n' into a newline character, 
  +     * unless the '\' is preceded by another '\'.
  +     */
  +    public static String unescape(String str) {
  +        int sz = str.length();
  +        StringBuffer buffer = new StringBuffer(sz);
  +        StringBuffer unicode = new StringBuffer(4);
  +        boolean hadSlash = false;
  +        boolean inUnicode = false;
  +        for (int i = 0; i < sz; i++) {
  +            char ch = str.charAt(i);
  +            if(inUnicode) {
  +                // if in unicode, then we're reading unicode 
  +                // values in somehow
  +                if(unicode.length() == 4) {
  +                    // unicode now contains the four hex digits 
  +                    // which represents our unicode chacater
  +                    try {
  +                        int value = Integer.parseInt(unicode.toString(), 16);
  +                        buffer.append( (char)value );
  +                        unicode.setLength(0);
  +                        unicode.setLength(4);
  +                        inUnicode = false;
  +                        hadSlash = false;
  +                    } catch(NumberFormatException nfe) {
  +                        throw new NestableRuntimeException("Unable to parse unicode value: "+unicode, nfe);
  +                    }
  +                } else {
  +                    unicode.append(ch);
  +                    continue;
  +                }
  +            }
  +            if(hadSlash) {
  +                // handle an escaped value
  +                hadSlash = false;
  +                switch(ch) {
  +                    case '\\': buffer.append('\\'); break;
  +                    case '\'': buffer.append('\''); break;
  +                    case '\"': buffer.append('"'); break;
  +                    case 'r':  buffer.append('\r'); break;
  +                    case 'f':  buffer.append('\f'); break;
  +                    case 't':  buffer.append('\t'); break;
  +                    case 'n':  buffer.append('\n'); break;
  +                    case 'b':  buffer.append('\b'); break;
  +                    case 'u':  {
  +                        // uh-oh, we're in unicode country....
  +                        inUnicode=true;
  +                        break;
  +                    }
  +                    default :
  +                        buffer.append(ch);
  +                        break;
  +                }
  +                continue;
  +            } else
  +            if(ch == '\\') {
  +                hadSlash = true;
  +                continue;
  +            } 
  +            buffer.append(ch);
  +        }
  +        if(hadSlash) {
  +            // then we're in the weird case of a \ at the end of the 
  +            // string, let's output it anyway.
  +            buffer.append('\\');
           }
           return buffer.toString();
       }
  
  
  
  1.11      +9 -1      jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java
  
  Index: StringUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- StringUtilsTest.java	23 Nov 2002 00:51:34 -0000	1.10
  +++ StringUtilsTest.java	7 Dec 2002 21:50:30 -0000	1.11
  @@ -317,6 +317,14 @@
                        "\\u0234", StringUtils.escape("\u0234") );
           assertEquals("escape(String) failed",
                        "\\u00fd", StringUtils.escape("\u00fd") );
  +        assertEquals("unescape(String) failed", 
  +                     "", StringUtils.unescape("") );
  +        assertEquals("unescape(String) failed", 
  +                     "test", StringUtils.unescape("test") );
  +        assertEquals("unescape(String) failed", 
  +                     "\ntest\b", StringUtils.unescape("\\ntest\\b") );
  +        assertEquals("unescape(String) failed", 
  +                     "\u123425foo\ntest\b", StringUtils.unescape("\\u123425foo\\ntest\\b") );
       }
   
       public void testGetLevenshteinDistance() {
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>