From d5fb2fe6b14f823dc81de3d70456bbccdbb83a92 Mon Sep 17 00:00:00 2001 From: mdb Date: Thu, 7 Feb 2002 08:41:59 +0000 Subject: [PATCH] Rewrote split() and parseStringArray() not to use StringTokenizer because it does undesirable things like not report multiple separators and skip separators that show up at the beginning of the string. We prefer the more rigorous approach of interpreting something like ",," as three empty strings rather than nothing at all which is what StringTokenizer would report. git-svn-id: https://samskivert.googlecode.com/svn/trunk@567 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/StringUtil.java | 61 ++++++++++++++----- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java index b05ad5e0..07b30bd1 100644 --- a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java @@ -1,5 +1,5 @@ // -// $Id: StringUtil.java,v 1.27 2002/02/06 00:56:57 mdb Exp $ +// $Id: StringUtil.java,v 1.28 2002/02/07 08:41:59 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -413,19 +413,32 @@ public class StringUtil */ public static String[] parseStringArray (String source) { + int tcount = 0, tpos = -1, tstart = 0; + // sort out escaped commas source = replace(source, ",,", "%COMMA%"); - // now tokenize the string - StringTokenizer tok = new StringTokenizer(source, ","); - String[] vals = new String[tok.countTokens()]; - for (int i = 0; tok.hasMoreTokens(); i++) { - // trim the whitespace from the token - String token = tok.nextToken().trim(); - // undo the comma escaping - token = replace(token, "%COMMA%", ","); - vals[i] = token; + + // count up the number of tokens + while ((tpos = source.indexOf(",", tpos+1)) != -1) { + tcount++; } - return vals; + + String[] tokens = new String[tcount+1]; + tpos = -1; tcount = 0; + + // do the split + while ((tpos = source.indexOf(",", tpos+1)) != -1) { + tokens[tcount] = source.substring(tstart, tpos); + tokens[tcount] = replace(tokens[tcount].trim(), "%COMMA%", ","); + tstart = tpos+1; + tcount++; + } + + // grab the last token + tokens[tcount] = source.substring(tstart); + tokens[tcount] = replace(tokens[tcount].trim(), "%COMMA%", ","); + + return tokens; } /** @@ -451,15 +464,31 @@ public class StringUtil /** * Splits the supplied string into components based on the specified - * separator character. + * separator string. */ public static String[] split (String source, String sep) { - StringTokenizer tok = new StringTokenizer(source, sep); - String[] tokens = new String[tok.countTokens()]; - for (int i = 0; tok.hasMoreTokens(); i++) { - tokens[i] = tok.nextToken(); + int tcount = 0, tpos = -1, tstart = 0; + + // count up the number of tokens + while ((tpos = source.indexOf(sep, tpos+1)) != -1) { + tcount++; } + + String[] tokens = new String[tcount+1]; + tpos = -1; tcount = 0; + + // do the split + while ((tpos = source.indexOf(sep, tpos+1)) != -1) { + tokens[tcount] = source.substring(tstart, tpos); + tstart = tpos+1; + tcount++; + } + + // grab the last token + tokens[tcount] = source.substring(tstart); + tokens[tcount] = replace(tokens[tcount].trim(), "%COMMA%", ","); + return tokens; }