Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
StringSplitter |
|
| 3.0;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 delimiter. |
|
19 | * A fast, simple version of {@link java.util.StringTokenizer}. |
|
20 | * |
|
21 | * @author Howard Lewis Ship |
|
22 | */ |
|
23 | ||
24 | public class StringSplitter |
|
25 | { |
|
26 | ||
27 | private char _delimiter; |
|
28 | ||
29 | public StringSplitter(char delimiter) |
|
30 | 1 | { |
31 | 1 | this._delimiter = delimiter; |
32 | 1 | } |
33 | ||
34 | public char getDelimiter() |
|
35 | { |
|
36 | 0 | return _delimiter; |
37 | } |
|
38 | ||
39 | /** |
|
40 | * Splits a string on the delimter into an array of String tokens. The |
|
41 | * delimiters are not included in the tokens. Null tokens (caused by two |
|
42 | * consecutive delimiter) are reduced to an empty string. Leading delimiters |
|
43 | * are ignored. |
|
44 | */ |
|
45 | ||
46 | public String[] splitToArray(String value) |
|
47 | { |
|
48 | char[] buffer; |
|
49 | int i; |
|
50 | String[] result; |
|
51 | 1 | int resultCount = 0; |
52 | int start; |
|
53 | int length; |
|
54 | String token; |
|
55 | String[] newResult; |
|
56 | 1 | boolean first = true; |
57 | ||
58 | 1 | buffer = value.toCharArray(); |
59 | ||
60 | 1 | result = new String[3]; |
61 | ||
62 | 1 | start = 0; |
63 | 1 | length = 0; |
64 | ||
65 | 11 | for(i = 0; i < buffer.length; i++) |
66 | { |
|
67 | 10 | if (buffer[i] != _delimiter) |
68 | { |
|
69 | 9 | length++; |
70 | 9 | continue; |
71 | } |
|
72 | ||
73 | // This is used to ignore leading delimiter(s). |
|
74 | ||
75 | 1 | if (length > 0 || !first) |
76 | { |
|
77 | 1 | token = new String(buffer, start, length); |
78 | ||
79 | 1 | if (resultCount == result.length) |
80 | { |
|
81 | 0 | newResult = new String[result.length * 2]; |
82 | ||
83 | 0 | System.arraycopy(result, 0, newResult, 0, result.length); |
84 | ||
85 | 0 | result = newResult; |
86 | } |
|
87 | ||
88 | 1 | result[resultCount++] = token; |
89 | ||
90 | 1 | first = false; |
91 | } |
|
92 | ||
93 | 1 | start = i + 1; |
94 | 1 | length = 0; |
95 | } |
|
96 | ||
97 | // Special case: if the string contains no delimiters |
|
98 | // then it isn't really split. Wrap the input string |
|
99 | // in an array and return. This is a little optimization |
|
100 | // to prevent a new String instance from being |
|
101 | // created unnecessarily. |
|
102 | ||
103 | 1 | if (start == 0 && length == buffer.length) |
104 | { |
|
105 | 0 | result = new String[1]; |
106 | 0 | result[0] = value; |
107 | 0 | return result; |
108 | } |
|
109 | ||
110 | // If the string is all delimiters, then this |
|
111 | // will result in a single empty token. |
|
112 | ||
113 | 1 | token = new String(buffer, start, length); |
114 | ||
115 | 1 | newResult = new String[resultCount + 1]; |
116 | 1 | System.arraycopy(result, 0, newResult, 0, resultCount); |
117 | 1 | newResult[resultCount] = token; |
118 | ||
119 | 1 | return newResult; |
120 | } |
|
121 | } |