commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alex Chaffee / Purple Technology <g...@stinky.com>
Subject [lang] Adding Purple to StringUtils
Date Wed, 05 Mar 2003 18:24:27 GMT
For many years, I've published my personal source code libraries as
open source.  By far the most heavily-downloaded class was Utils.java,
containing my string processing routines.  I'm psyched that Jakarta
Commons now exists, and I'd like to donate my code to
Lang.StringUtils.

You can see the code at http://www.purpletech.com/code, specifically
http://www.purpletech.com/code/src/com/purpletech/util/Utils.java and
http://www.purpletech.com/code/src/com/purpletech/util/UtilsTest.java
(a set of unit tests that may help clarify the usage of the API.

I'll list these independently, so we can start haggling over 

1. yea or nay
2. naming
3. API / method signature
4. appropriate package (if lang.StringUtils is not the right place)

for each in turn.  Naturally, I'm open to any negotiation; the methods
I'd really like to lobby for are htmlescape and strdiffVerbose.

 - Alex


* isWhitespace(String str)

Returns true if the str contains only whitespace characters.

Fills a gap in the isAlphanumeric family.  (Funny that isWhitespace is
the only one of the bunch that I had occasion to write!)


* abbreviate(String str, int max)

Turn "Now is the time for all good men" into "Now is the time for..."

Specifically:

If str is less than max characters long, return it.
Else abbreviate it to (substring(str, 0, max-3) + "...").
If max is less than 3, throw an IllegalArgumentException.
In no case will it return a string of length greater than max.


* String strdiff(String s1, String s2)

Compare two strings, and return the portion where they differ.  (More
precisely, return the remainder of the second string, starting from
where it's different from the first.)  
e.g. strdiff("i am a machine", "i am a robot") -> "robot"


* int strdiffat(String s1, String s2)

Compare two strings, and return the index at which the strings begin
to diverge.
E.g. strdiff("i am a machine", "i am a robot") -> 7<p>
return -1 if they are the same


* String strdiffVerbose(String expected, String actual)

Compare two strings, and return a verbose description of how
they differ. Shows a window around the location to provide
context.  

E.g. strdiffVerbose("i am a robot", "i am a machine") might return a
string containing

strings differ at character 7
Expected: ...am a robot
  Actual: ...am a machine

This was developed in order to provide some sanity to JUnit's
assertEquals display.


* String rtrim(String orig)
* String ltrim(String orig)

Trim the whitespace off the right or left side of a String
only. (Probably rename trimRight and trimLeft.)


* int maxLength(Iterator i)

return the length of the longest string in i.  If i contains other
than strings, uses toString() value.


* htmlescape(String)     (should rename: escapeHtml)
* htmlunescape(String)   (should rename: unescapeHtml)

Turns funky characters into HTML entity equivalents (and vice versa).
This is far and away the most-used function I have.  There is
definitely a need for it to be in Commons.  Apparently there's some
debate as to whether it's appropriate for StringUtils.

My code supports all the named entities I know about. See 
http://hotwired.lycos.com/webmonkey/reference/special_characters/


* String jsEscape    (should rename: escapeJavaScript)

JavaScript's string escaping is nearly identical to Java's, and it's
sort of a no-brainer to support it as well as Java.  (In JS, a single
quote is escaped; in Java it's not.)


* uncurlQuotes(String)

Turn Windows and Mac curly-quotes into their non-curly ASCII equivalents.


* capitalize

Properly-spelled version of capitalise :-)


* lowerize

Symmetrical with capitalize. Turn the first character into a
lower-case letter.


* pluralize

turn String s into a plural English noun (doing the right thing with
"story" -> "stories" and "mess" -> "messes")


* toUnderscore

Converts camelCaseVersusC to camel_case_versus_c 

I'd love to get some ideas on a better name for this.  Also, it'd be
nice to have a full, symmetrical set of camelCase vs. under_score
vs. CONSTANT_NAMING vs. "separate words" naming style converters.


* getStackTrace(Throwable t)

Convenience method to get a stack trace as a string:
        StringWriter s = new StringWriter();
        PrintWriter p = new PrintWriter(s);
        t.printStackTrace(p);
        p.close();
        return s.toString();


-- 
Alex Chaffee                               mailto:alex@jguru.com
Purple Technology - Code and Consulting    http://www.purpletech.com/
jGuru - Java News and FAQs                 http://www.jguru.com/alex/
Gamelan - the Original Java site           http://www.gamelan.com/
Stinky - Art and Angst                     http://www.stinky.com/

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


Mime
View raw message