1 | /*- | |
2 | * #%L | |
3 | * io.earcam.utilitarian.charstar | |
4 | * %% | |
5 | * Copyright (C) 2017 - 2018 earcam | |
6 | * %% | |
7 | * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT) | |
8 | * | |
9 | * You <b>must</b> choose to accept, in full - any individual or combination of | |
10 | * the following licenses: | |
11 | * <ul> | |
12 | * <li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li> | |
13 | * <li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li> | |
14 | * <li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li> | |
15 | * <li><a href="https://opensource.org/licenses/MIT">MIT</a></li> | |
16 | * </ul> | |
17 | * #L% | |
18 | */ | |
19 | package io.earcam.utilitarian.charstar; | |
20 | ||
21 | import static java.nio.charset.StandardCharsets.UTF_8; | |
22 | ||
23 | import java.nio.ByteBuffer; | |
24 | import java.nio.CharBuffer; | |
25 | import java.nio.charset.Charset; | |
26 | import java.util.Arrays; | |
27 | import java.util.Comparator; | |
28 | ||
29 | import javax.annotation.Nullable; | |
30 | import javax.annotation.ParametersAreNonnullByDefault; | |
31 | ||
32 | @ParametersAreNonnullByDefault | |
33 | public final class CharSequences { | |
34 | ||
35 | private CharSequences() | |
36 | { | |
37 | ||
38 | } | |
39 | ||
40 | ||
41 | static CharStar backedCharSequence(char[] sequence) | |
42 | { | |
43 |
1
1. backedCharSequence : mutated return of Object value for io/earcam/utilitarian/charstar/CharSequences::backedCharSequence to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CharStar.backedCharSequence(sequence); |
44 | } | |
45 | ||
46 | ||
47 | static CharStar charSequence(char[] sequence) | |
48 | { | |
49 | char[] encapsulated = Arrays.copyOf(sequence, sequence.length); | |
50 |
1
1. charSequence : mutated return of Object value for io/earcam/utilitarian/charstar/CharSequences::charSequence to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CharStar.charSequence(encapsulated); |
51 | } | |
52 | ||
53 | ||
54 | static CharStar charSequence(CharSequence sequence) | |
55 | { | |
56 |
1
1. charSequence : mutated return of Object value for io/earcam/utilitarian/charstar/CharSequences::charSequence to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CharStar.charSequence(toArray(sequence)); |
57 | } | |
58 | ||
59 | ||
60 | public static boolean startsWith(CharSequence text, CharSequence prefix) | |
61 | { | |
62 |
2
1. startsWith : changed conditional boundary → KILLED 2. startsWith : negated conditional → KILLED |
if(prefix.length() > text.length()) { |
63 |
1
1. startsWith : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return false; |
64 | } | |
65 |
2
1. startsWith : changed conditional boundary → KILLED 2. startsWith : negated conditional → KILLED |
for(int i = 0; i < prefix.length(); i++) { |
66 |
1
1. startsWith : negated conditional → KILLED |
if(text.charAt(i) != prefix.charAt(i)) { |
67 |
1
1. startsWith : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return false; |
68 | } | |
69 | } | |
70 |
1
1. startsWith : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return true; |
71 | } | |
72 | ||
73 | ||
74 | public static boolean endsWith(CharSequence text, CharSequence suffix) | |
75 | { | |
76 |
2
1. endsWith : changed conditional boundary → KILLED 2. endsWith : negated conditional → KILLED |
if(suffix.length() > text.length()) { |
77 |
1
1. endsWith : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return false; |
78 | } | |
79 |
6
1. endsWith : changed conditional boundary → KILLED 2. endsWith : Changed increment from -1 to 1 → KILLED 3. endsWith : Changed increment from -1 to 1 → KILLED 4. endsWith : Replaced integer subtraction with addition → KILLED 5. endsWith : Replaced integer subtraction with addition → KILLED 6. endsWith : negated conditional → KILLED |
for(int i = text.length() - 1, j = suffix.length() - 1; j >= 0; i--, j--) { |
80 |
1
1. endsWith : negated conditional → KILLED |
if(text.charAt(i) != suffix.charAt(j)) { |
81 |
1
1. endsWith : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return false; |
82 | } | |
83 | } | |
84 |
1
1. endsWith : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return true; |
85 | } | |
86 | ||
87 | ||
88 | public static boolean same(@Nullable CharSequence a, @Nullable CharSequence b) | |
89 | { | |
90 |
2
1. same : negated conditional → KILLED 2. same : negated conditional → KILLED |
if(a == null || b == null) { |
91 |
2
1. same : negated conditional → KILLED 2. same : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return a == b; |
92 | } | |
93 |
1
1. same : negated conditional → KILLED |
if(a.length() != b.length()) { |
94 |
1
1. same : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return false; |
95 | } | |
96 |
2
1. same : changed conditional boundary → KILLED 2. same : negated conditional → KILLED |
for(int i = 0; i < a.length(); i++) { |
97 |
1
1. same : negated conditional → KILLED |
if(a.charAt(i) != b.charAt(i)) { |
98 |
1
1. same : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return false; |
99 | } | |
100 | } | |
101 |
1
1. same : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return true; |
102 | } | |
103 | ||
104 | ||
105 | public static int hashCode(CharSequence sequence) | |
106 | { | |
107 | int hash = 0; | |
108 |
3
1. hashCode : changed conditional boundary → KILLED 2. hashCode : Changed increment from 1 to -1 → KILLED 3. hashCode : negated conditional → KILLED |
for(int i = 0; i < sequence.length(); i++) { |
109 |
2
1. hashCode : Replaced integer multiplication with division → KILLED 2. hashCode : Replaced integer addition with subtraction → KILLED |
hash = 31 * hash + sequence.charAt(i); |
110 | } | |
111 |
1
1. hashCode : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return hash; |
112 | } | |
113 | ||
114 | ||
115 | public static CharSequence trim(CharSequence padded) | |
116 | { | |
117 | int s = 0; | |
118 | int e = padded.length(); | |
119 |
1
1. trim : negated conditional → KILLED |
while(Character.isWhitespace(padded.charAt(s))) { |
120 |
1
1. trim : Changed increment from 1 to -1 → KILLED |
s += 1; |
121 | } | |
122 |
2
1. trim : Replaced integer subtraction with addition → KILLED 2. trim : negated conditional → KILLED |
while(Character.isWhitespace(padded.charAt(e - 1))) { |
123 |
1
1. trim : Changed increment from -1 to 1 → KILLED |
e -= 1; |
124 | } | |
125 |
1
1. trim : mutated return of Object value for io/earcam/utilitarian/charstar/CharSequences::trim to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return padded.subSequence(s, e); |
126 | } | |
127 | ||
128 | ||
129 | public static @Nullable CharSequence replace(@Nullable CharSequence sequence, char find, char replace) | |
130 | { | |
131 |
1
1. replace : negated conditional → KILLED |
if(sequence == null) { |
132 |
1
1. replace : mutated return of Object value for io/earcam/utilitarian/charstar/CharSequences::replace to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return null; |
133 | } | |
134 | char[] chars = new char[sequence.length()]; | |
135 |
2
1. replace : changed conditional boundary → KILLED 2. replace : negated conditional → KILLED |
for(int i = 0; i < chars.length; i++) { |
136 | chars[i] = sequence.charAt(i); | |
137 |
1
1. replace : negated conditional → KILLED |
if(chars[i] == find) { |
138 | chars[i] = replace; | |
139 | } | |
140 | } | |
141 |
1
1. replace : mutated return of Object value for io/earcam/utilitarian/charstar/CharSequences::replace to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CharStar.backedCharSequence(chars); |
142 | } | |
143 | ||
144 | ||
145 | /** | |
146 | * Returns {@code true} if the argument {@code text} is {@code null} or {@link CharSequence#length()} {@code == 0} | |
147 | * | |
148 | * @param text the CharSequence to test | |
149 | * @return {@code true} IFF <i>empty</i> | |
150 | */ | |
151 | public static boolean isEmpty(@Nullable CharSequence text) | |
152 | { | |
153 |
3
1. isEmpty : negated conditional → KILLED 2. isEmpty : negated conditional → KILLED 3. isEmpty : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return text == null || text.length() == 0; |
154 | } | |
155 | ||
156 | ||
157 | /** | |
158 | * Equivalent to {@link String#lastIndexOf(int) | |
159 | * | |
160 | * @param text the CharSequence to test | |
161 | * @param character the {@code char} to find | |
162 | * @return the index of first occurrence of argument {@code character} | |
163 | */ | |
164 | public static int lastIndexOf(CharSequence text, char character) | |
165 | { | |
166 |
4
1. lastIndexOf : changed conditional boundary → KILLED 2. lastIndexOf : Changed increment from -1 to 1 → KILLED 3. lastIndexOf : Replaced integer subtraction with addition → KILLED 4. lastIndexOf : negated conditional → KILLED |
for(int i = text.length() - 1; i >= 0; i--) { |
167 |
1
1. lastIndexOf : negated conditional → KILLED |
if(text.charAt(i) == character) { |
168 |
1
1. lastIndexOf : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return i; |
169 | } | |
170 | } | |
171 |
1
1. lastIndexOf : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return -1; |
172 | } | |
173 | ||
174 | ||
175 | public static int indexOf(CharSequence text, char character) | |
176 | { | |
177 |
1
1. indexOf : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return indexOf(text, character, 0); |
178 | } | |
179 | ||
180 | ||
181 | public static int indexOf(CharSequence text, char character, int start) | |
182 | { | |
183 |
2
1. indexOf : changed conditional boundary → KILLED 2. indexOf : negated conditional → KILLED |
for(int i = start; i < text.length(); i++) { |
184 |
1
1. indexOf : negated conditional → KILLED |
if(text.charAt(i) == character) { |
185 |
1
1. indexOf : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return i; |
186 | } | |
187 | } | |
188 |
1
1. indexOf : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return -1; |
189 | } | |
190 | ||
191 | ||
192 | public static byte[] toBytes(CharSequence utf8Sequence) | |
193 | { | |
194 |
1
1. toBytes : mutated return of Object value for io/earcam/utilitarian/charstar/CharSequences::toBytes to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return toBytes(utf8Sequence, UTF_8); |
195 | } | |
196 | ||
197 | ||
198 | public static byte[] toBytes(CharSequence sequence, Charset charset) | |
199 | { | |
200 | ByteBuffer buffer = charset.encode(CharBuffer.wrap(sequence)); | |
201 | byte[] bytes = new byte[buffer.remaining()]; | |
202 | buffer.get(bytes); | |
203 |
1
1. toBytes : mutated return of Object value for io/earcam/utilitarian/charstar/CharSequences::toBytes to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return bytes; |
204 | } | |
205 | ||
206 | ||
207 | public static Comparator<CharSequence> comparator() | |
208 | { | |
209 |
1
1. comparator : mutated return of Object value for io/earcam/utilitarian/charstar/CharSequences::comparator to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return CharSequences::compare; |
210 | } | |
211 | ||
212 | ||
213 | public static int compare(CharSequence a, CharSequence b) | |
214 | { | |
215 | int max = Math.min(a.length(), b.length()); | |
216 | int i = 0; | |
217 |
2
1. compare : changed conditional boundary → KILLED 2. compare : negated conditional → KILLED |
while(i < max) { |
218 | char c1 = a.charAt(i); | |
219 | char c2 = b.charAt(i); | |
220 |
1
1. compare : negated conditional → KILLED |
if(c1 != c2) { |
221 |
2
1. compare : Replaced integer subtraction with addition → KILLED 2. compare : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return c1 - c2; |
222 | } | |
223 |
1
1. compare : Changed increment from 1 to -1 → KILLED |
i++; |
224 | } | |
225 |
2
1. compare : Replaced integer subtraction with addition → KILLED 2. compare : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return a.length() - b.length(); |
226 | } | |
227 | ||
228 | ||
229 | public static char[] toArray(CharSequence sequence) | |
230 | { | |
231 | char[] array = new char[sequence.length()]; | |
232 |
2
1. toArray : changed conditional boundary → KILLED 2. toArray : negated conditional → KILLED |
for(int i = 0; i < array.length; i++) { |
233 | array[i] = sequence.charAt(i); | |
234 | } | |
235 |
1
1. toArray : mutated return of Object value for io/earcam/utilitarian/charstar/CharSequences::toArray to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return array; |
236 | } | |
237 | } | |
Mutations | ||
43 |
1.1 |
|
50 |
1.1 |
|
56 |
1.1 |
|
62 |
1.1 2.2 |
|
63 |
1.1 |
|
65 |
1.1 2.2 |
|
66 |
1.1 |
|
67 |
1.1 |
|
70 |
1.1 |
|
76 |
1.1 2.2 |
|
77 |
1.1 |
|
79 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
80 |
1.1 |
|
81 |
1.1 |
|
84 |
1.1 |
|
90 |
1.1 2.2 |
|
91 |
1.1 2.2 |
|
93 |
1.1 |
|
94 |
1.1 |
|
96 |
1.1 2.2 |
|
97 |
1.1 |
|
98 |
1.1 |
|
101 |
1.1 |
|
108 |
1.1 2.2 3.3 |
|
109 |
1.1 2.2 |
|
111 |
1.1 |
|
119 |
1.1 |
|
120 |
1.1 |
|
122 |
1.1 2.2 |
|
123 |
1.1 |
|
125 |
1.1 |
|
131 |
1.1 |
|
132 |
1.1 |
|
135 |
1.1 2.2 |
|
137 |
1.1 |
|
141 |
1.1 |
|
153 |
1.1 2.2 3.3 |
|
166 |
1.1 2.2 3.3 4.4 |
|
167 |
1.1 |
|
168 |
1.1 |
|
171 |
1.1 |
|
177 |
1.1 |
|
183 |
1.1 2.2 |
|
184 |
1.1 |
|
185 |
1.1 |
|
188 |
1.1 |
|
194 |
1.1 |
|
203 |
1.1 |
|
209 |
1.1 |
|
217 |
1.1 2.2 |
|
220 |
1.1 |
|
221 |
1.1 2.2 |
|
223 |
1.1 |
|
225 |
1.1 2.2 |
|
232 |
1.1 2.2 |
|
235 |
1.1 |