Return-Path: Delivered-To: apmail-commons-issues-archive@locus.apache.org Received: (qmail 66336 invoked from network); 17 Aug 2007 12:50:53 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 17 Aug 2007 12:50:53 -0000 Received: (qmail 2240 invoked by uid 500); 17 Aug 2007 12:50:49 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 2148 invoked by uid 500); 17 Aug 2007 12:50:49 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 2139 invoked by uid 99); 17 Aug 2007 12:50:49 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Aug 2007 05:50:49 -0700 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO brutus.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Aug 2007 12:50:51 +0000 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id 921A271420A for ; Fri, 17 Aug 2007 05:50:30 -0700 (PDT) Message-ID: <10703136.1187355030593.JavaMail.jira@brutus> Date: Fri, 17 Aug 2007 05:50:30 -0700 (PDT) From: "Stephen Colebourne (JIRA)" To: issues@commons.apache.org Subject: [jira] Commented: (LANG-352) New method replace(String text, Map replaceMap) in StringUtils In-Reply-To: <19583585.1187252131504.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org [ https://issues.apache.org/jira/browse/LANG-352?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12520544 ] Stephen Colebourne commented on LANG-352: ----------------------------------------- This looks suspiciously like StrSubstitutor > New method replace(String text, Map replaceMap) in StringUtils > -------------------------------------------------------------- > > Key: LANG-352 > URL: https://issues.apache.org/jira/browse/LANG-352 > Project: Commons Lang > Issue Type: New Feature > Affects Versions: 2.3, 3.0 > Reporter: Gerhard Maree > Priority: Minor > > 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 > /** > *

Replace all occurrences of multiple strings in a string.

> * > *

It is functionally equivalent to calling > * replaceAll(searchString, replaceString) repeatedly on text > * for all the strings you want to replace, but much faster

> * > *

replaceMap maps search strings to replacement strings. > * Each key in the map will be replaced by its value in text. > *

> * > * @param text String to replace strings in. May be null > * @param replaceMap Maps search strings to replacement strings. > * @return string with all values replaced. null 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)); > } -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.