|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
StringSplitter.java | 80% | 89.5% | 66.7% | 86.3% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.tapestry.util;
|
|
16 |
|
|
17 |
/**
|
|
18 |
* Used to split a string into substrings based on a single character
|
|
19 |
* delimiter. A fast, simple version of
|
|
20 |
* {@link java.util.StringTokenizer}.
|
|
21 |
*
|
|
22 |
* @author Howard Lewis Ship
|
|
23 |
*
|
|
24 |
**/
|
|
25 |
|
|
26 |
public class StringSplitter |
|
27 |
{ |
|
28 |
private char delimiter; |
|
29 |
|
|
30 | 80 |
public StringSplitter(char delimiter) |
31 |
{ |
|
32 | 80 |
this.delimiter = delimiter;
|
33 |
} |
|
34 |
|
|
35 | 0 |
public char getDelimiter() |
36 |
{ |
|
37 | 0 |
return delimiter;
|
38 |
} |
|
39 |
|
|
40 |
/**
|
|
41 |
* Splits a string on the delimter into an array of String
|
|
42 |
* tokens. The delimiters are not included in the tokens. Null
|
|
43 |
* tokens (caused by two consecutive delimiter) are reduced to an
|
|
44 |
* empty string. Leading delimiters are ignored.
|
|
45 |
*
|
|
46 |
**/
|
|
47 |
|
|
48 | 80 |
public String[] splitToArray(String value)
|
49 |
{ |
|
50 | 80 |
char[] buffer;
|
51 | 80 |
int i;
|
52 | 80 |
String[] result; |
53 | 80 |
int resultCount = 0;
|
54 | 80 |
int start;
|
55 | 80 |
int length;
|
56 | 80 |
String token; |
57 | 80 |
String[] newResult; |
58 | 80 |
boolean first = true; |
59 |
|
|
60 | 80 |
buffer = value.toCharArray(); |
61 |
|
|
62 | 80 |
result = new String[3];
|
63 |
|
|
64 | 80 |
start = 0; |
65 | 80 |
length = 0; |
66 |
|
|
67 | 80 |
for (i = 0; i < buffer.length; i++)
|
68 |
{ |
|
69 | 486 |
if (buffer[i] != delimiter)
|
70 |
{ |
|
71 | 482 |
length++; |
72 | 482 |
continue;
|
73 |
} |
|
74 |
|
|
75 |
// This is used to ignore leading delimiter(s).
|
|
76 |
|
|
77 | 4 |
if (length > 0 || !first)
|
78 |
{ |
|
79 | 4 |
token = new String(buffer, start, length);
|
80 |
|
|
81 | 4 |
if (resultCount == result.length)
|
82 |
{ |
|
83 | 0 |
newResult = new String[result.length * 2];
|
84 |
|
|
85 | 0 |
System.arraycopy(result, 0, newResult, 0, result.length); |
86 |
|
|
87 | 0 |
result = newResult; |
88 |
} |
|
89 |
|
|
90 | 4 |
result[resultCount++] = token; |
91 |
|
|
92 | 4 |
first = false;
|
93 |
} |
|
94 |
|
|
95 | 4 |
start = i + 1; |
96 | 4 |
length = 0; |
97 |
} |
|
98 |
|
|
99 |
// Special case: if the string contains no delimiters
|
|
100 |
// then it isn't really split. Wrap the input string
|
|
101 |
// in an array and return. This is a little optimization
|
|
102 |
// to prevent a new String instance from being
|
|
103 |
// created unnecessarily.
|
|
104 |
|
|
105 | 80 |
if (start == 0 && length == buffer.length)
|
106 |
{ |
|
107 | 77 |
result = new String[1];
|
108 | 77 |
result[0] = value; |
109 | 77 |
return result;
|
110 |
} |
|
111 |
|
|
112 |
// If the string is all delimiters, then this
|
|
113 |
// will result in a single empty token.
|
|
114 |
|
|
115 | 3 |
token = new String(buffer, start, length);
|
116 |
|
|
117 | 3 |
newResult = new String[resultCount + 1];
|
118 | 3 |
System.arraycopy(result, 0, newResult, 0, resultCount); |
119 | 3 |
newResult[resultCount] = token; |
120 |
|
|
121 | 3 |
return newResult;
|
122 |
} |
|
123 |
} |
|