|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com 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 | 56 | public StringSplitter(char delimiter) |
31 | { | |
32 | 56 | 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 | 56 | public String[] splitToArray(String value) |
49 | { | |
50 | 56 | char[] buffer; |
51 | 56 | int i; |
52 | 56 | String[] result; |
53 | 56 | int resultCount = 0; |
54 | 56 | int start; |
55 | 56 | int length; |
56 | 56 | String token; |
57 | 56 | String[] newResult; |
58 | 56 | boolean first = true; |
59 | ||
60 | 56 | buffer = value.toCharArray(); |
61 | ||
62 | 56 | result = new String[3]; |
63 | ||
64 | 56 | start = 0; |
65 | 56 | length = 0; |
66 | ||
67 | 56 | for (i = 0; i < buffer.length; i++) |
68 | { | |
69 | 385 | if (buffer[i] != delimiter) |
70 | { | |
71 | 384 | length++; |
72 | 384 | continue; |
73 | } | |
74 | ||
75 | // This is used to ignore leading delimiter(s). | |
76 | ||
77 | 1 | if (length > 0 || !first) |
78 | { | |
79 | 1 | token = new String(buffer, start, length); |
80 | ||
81 | 1 | 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 | 1 | result[resultCount++] = token; |
91 | ||
92 | 1 | first = false; |
93 | } | |
94 | ||
95 | 1 | start = i + 1; |
96 | 1 | 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 | 56 | if (start == 0 && length == buffer.length) |
106 | { | |
107 | 55 | result = new String[1]; |
108 | 55 | result[0] = value; |
109 | 55 | return result; |
110 | } | |
111 | ||
112 | // If the string is all delimiters, then this | |
113 | // will result in a single empty token. | |
114 | ||
115 | 1 | token = new String(buffer, start, length); |
116 | ||
117 | 1 | newResult = new String[resultCount + 1]; |
118 | 1 | System.arraycopy(result, 0, newResult, 0, resultCount); |
119 | 1 | newResult[resultCount] = token; |
120 | ||
121 | 1 | return newResult; |
122 | } | |
123 | } |
|