commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gerhard Maree" <Gerhard.Ma...@fidentia.co.za>
Subject New method in org.apache.commons.lang.StringUtils
Date Wed, 15 Aug 2007 13:59:32 GMT

Hi

I have a method that I would like to contribute to the StringUtils
class. I unfortunately can't access a CVS server on the internet, so I
included the method and its test in this mail. I'd appreciate it if
someone can have a look and add the source if it is acceptable. 

The method basically replaces multiple strings in a String, but is very
efficient. The description of the method is in the Javadoc.

Regards,
Gerhard
 
Here is method to add to org.apache.commons.lang.StringUtils

    /**
     * <p>Replace all occurrences of multiple strings in a string.</p>
     * 
     * <p>It is functionally equivalent to calling 
     *  replaceAll(searchString, replaceString) repeatedly on
<code>text</code> 
     *  for all the strings you want to replace, but much faster </p>
     *  
     * <p><code>replaceMap</code> maps search strings to replacement
strings.
     * Each key in the map will be replaced by its value in
<code>text</code>.
     * </p>
     * 
     * @param text String to replace strings in. May be null
     * @param replaceMap Maps search strings to replacement strings.  
     * @return string with all values replaced. <code>null</code> if 
     *          text was null
     */
    public static String replace(String text, Map replaceMap) {
      if(isEmpty(text)) {
        return text;
      }
      StringBuffer buff = new StringBuffer(text.length());
      //map each replace string and it's next position in text
      TreeMap indexMap = new TreeMap();
      //populate indexMap with it's initial values
      for (Iterator iter = replaceMap.keySet().iterator();
iter.hasNext();) {
        String key = (String) iter.next();
        if(isBlank(key)) {
          continue;
        }
        int idx = text.indexOf(key);
        if(idx >= 0) {
          indexMap.put(new Integer(idx), key);
        }
      }
      
      //if there is nothing to replace
      if(indexMap.isEmpty()) return text;
      
      int prevIdx = 0;
      while(indexMap.size() > 0) {
        Integer idxI = (Integer)indexMap.firstKey();
        int idx = idxI.intValue();
        String keyS = (String)indexMap.remove(idxI);
        buff.append(text.substring(prevIdx, idx));
        buff.append((String)replaceMap.get(keyS));
        prevIdx = idx + keyS.length();
        idx = text.indexOf(keyS, prevIdx);
        if(idx > 0) {
          indexMap.put(new Integer(idx), keyS);
        }
      }
      buff.append(text.substring(prevIdx));
      return buff.toString();
    }


Here is the test method to add to StringUtilsTest.


    public void testReplace_StringMap() {
      Map replaceMapEmpty = new HashMap();
      Map replaceMap1 = new HashMap();
      replaceMap1.put("foo", "bar");
      replaceMap1.put("boo", "far");
      Map replaceMap2 = new HashMap();
      replaceMap2.put("foo", "");
      Map replaceMap3 = new HashMap();
      replaceMap3.put("", "foo");
      
      assertEquals(null, StringUtils.replace(null,  replaceMapEmpty));
      assertEquals(null, StringUtils.replace(null, replaceMap1));

      assertEquals("", StringUtils.replace("", replaceMapEmpty));
      assertEquals("", StringUtils.replace("", replaceMap1));

      assertEquals("foo", StringUtils.replace("foo", replaceMapEmpty));
      assertEquals("bar", StringUtils.replace("foo", replaceMap1));
      assertEquals("", StringUtils.replace("foo", replaceMap2));
      assertEquals("foo", StringUtils.replace("foo", replaceMap3));
      
      assertEquals("foobar", StringUtils.replace("foobar",
replaceMapEmpty));
      assertEquals("barbar", StringUtils.replace("foobar",
replaceMap1));
      assertEquals("bar", StringUtils.replace("bar", replaceMap2));

      assertEquals("fobar", StringUtils.replace("fobar", replaceMap1));
      assertEquals("barobar", StringUtils.replace("fooobar",
replaceMap1));
      assertEquals("barbar", StringUtils.replace("foofoo",
replaceMap1));
      assertEquals("barfar", StringUtils.replace("fooboo",
replaceMap1));
      assertEquals("barfarbarfar", StringUtils.replace("fooboofooboo",
replaceMap1));
      assertEquals("barbarfarfar", StringUtils.replace("foofoobooboo",
replaceMap1));
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message