@@ -121,15 +121,33 @@ public Appendable getOut() {
121121 */
122122 public void print (final Object value ) throws IOException {
123123 // null values are considered empty
124- String strValue ;
124+ // Only call CharSequence.toString() if you have to, helps GC-free use cases.
125+ CharSequence charSequence ;
125126 if (value == null ) {
126127 final String nullString = format .getNullString ();
127- strValue = nullString == null ? Constants .EMPTY : nullString ;
128+ charSequence = nullString == null ? Constants .EMPTY : nullString ;
128129 } else {
129- strValue = value .toString ();
130+ charSequence = value instanceof CharSequence ? ( CharSequence ) value : value .toString ();
130131 }
131- strValue = format .getTrim () ? strValue .trim () : strValue ;
132- this .print (value , strValue , 0 , strValue .length ());
132+ charSequence = format .getTrim () ? trim (charSequence ) : charSequence ;
133+ this .print (value , charSequence , 0 , charSequence .length ());
134+ }
135+
136+ private CharSequence trim (final CharSequence charSequence ) {
137+ if (charSequence instanceof String ) {
138+ return ((String ) charSequence ).trim ();
139+ }
140+ final int count = charSequence .length ();
141+ int len = count ;
142+ int pos = 0 ;
143+
144+ while ((pos < len ) && (charSequence .charAt (pos ) <= ' ' )) {
145+ pos ++;
146+ }
147+ while ((pos < len ) && (charSequence .charAt (len - 1 ) <= ' ' )) {
148+ len --;
149+ }
150+ return (pos > 0 ) || (len < count ) ? charSequence .subSequence (pos , len ) : charSequence ;
133151 }
134152
135153 private void print (final Object object , final CharSequence value , final int offset , final int len )
0 commit comments