[stringtemplate-interest] [ST4 Beta4] Bug: IndexOutOfBounds Exception when using "cap" format on empty string

Udo Borkowski ub at abego-software.de
Fri Feb 18 02:41:21 PST 2011


Bug: IndexOutOfBounds Exception when using "cap" format on empty string

E.g. 

main(s="") ::= <<
<s;format="cap">
>>


Testcase:

	public void testIt() throws Exception {
		String templates = "main(s=\"\") ::= <<\n<s;format=\"cap\">\n>>";

		writeFile(tmpdir, "t.stg", templates);
		org.stringtemplate.v4.STGroup group = new org.stringtemplate.v4.STGroupFile(
				tmpdir + "/t.stg");
		group.registerRenderer(String.class, new StringRenderer());

		org.stringtemplate.v4.ST st = group.getInstanceOf("main");
		// Render the string (this led to an IndexOutOfBounds exception)
		String s = st.render();
		
		Assert.assertEquals("", s);
	}

Fix:

--- new/org/stringtemplate/v4/StringRenderer.java	2011-02-05 12:58:28.000000000 +0100
+++ mine/org/stringtemplate/v4/StringRenderer.java	2011-02-07 11:02:38.000000000 +0100
@@ -30,18 +30,19 @@
 import java.net.URLEncoder;
 import java.util.Locale;
 
 /** This render knows to perform a few operations on String objects:
  *  upper, lower, cap, url-encode, xml-encode.
  */
 public class StringRenderer implements AttributeRenderer {
     // trim(s) and strlen(s) built-in funcs; these are format options
     public String toString(Object o, String formatString, Locale locale) {
         String s = (String)o;
         if ( formatString==null ) return s; 
         if ( formatString.equals("upper") ) return s.toUpperCase(locale);
         if ( formatString.equals("lower") ) return s.toLowerCase(locale);
         if ( formatString.equals("cap") ) {
-            return Character.toUpperCase(s.charAt(0))+s.substring(1);
+            return (s.length() > 0) ? Character.toUpperCase(s.charAt(0))
+					+ s.substring(1) : "";
         }
         if ( formatString.equals("url-encode") ) {
             return URLEncoder.encode(s);


Udo


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110218/8779e311/attachment.html 


More information about the stringtemplate-interest mailing list