Skip to content

Commit 3746a2e

Browse files
committed
refine StringUtils.wrap
1 parent e233ab6 commit 3746a2e

File tree

2 files changed

+285
-23
lines changed

2 files changed

+285
-23
lines changed

src/main/java/org/apache/commons/lang3/StringUtils.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9538,7 +9538,7 @@ public static String wrap(final String str, final String wrapWith) {
95389538
return str;
95399539
}
95409540

9541-
return wrapWith.concat(str).concat(wrapWith);
9541+
return wrapWith + str + wrapWith;
95429542
}
95439543

95449544
/**
@@ -9574,19 +9574,19 @@ public static String wrapIfMissing(final String str, final char wrapWith) {
95749574
}
95759575
final boolean wrapStart = str.charAt(0) != wrapWith;
95769576
final boolean wrapEnd = str.charAt(str.length() - 1) != wrapWith;
9577-
if (!wrapStart && !wrapEnd) {
9578-
return str;
9579-
}
9580-
9581-
final StringBuilder builder = new StringBuilder(str.length() + 2);
95829577
if (wrapStart) {
9583-
builder.append(wrapWith);
9584-
}
9585-
builder.append(str);
9586-
if (wrapEnd) {
9587-
builder.append(wrapWith);
9578+
if (wrapEnd) {
9579+
return wrapWith + str + wrapWith;
9580+
} else {
9581+
return wrapWith + str;
9582+
}
9583+
} else {
9584+
if (wrapEnd) {
9585+
return str + wrapWith;
9586+
} else {
9587+
return str;
9588+
}
95889589
}
9589-
return builder.toString();
95909590
}
95919591

95929592
/**
@@ -9627,19 +9627,19 @@ public static String wrapIfMissing(final String str, final String wrapWith) {
96279627

96289628
final boolean wrapStart = !str.startsWith(wrapWith);
96299629
final boolean wrapEnd = !str.endsWith(wrapWith);
9630-
if (!wrapStart && !wrapEnd) {
9631-
return str;
9632-
}
9633-
9634-
final StringBuilder builder = new StringBuilder(str.length() + wrapWith.length() + wrapWith.length());
96359630
if (wrapStart) {
9636-
builder.append(wrapWith);
9637-
}
9638-
builder.append(str);
9639-
if (wrapEnd) {
9640-
builder.append(wrapWith);
9631+
if (wrapEnd) {
9632+
return wrapWith + str + wrapWith;
9633+
} else {
9634+
return wrapWith + str;
9635+
}
9636+
} else {
9637+
if (wrapEnd) {
9638+
return str + wrapWith;
9639+
} else {
9640+
return str;
9641+
}
96419642
}
9642-
return builder.toString();
96439643
}
96449644

96459645
/**
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.lang3;
18+
19+
import org.openjdk.jmh.annotations.Benchmark;
20+
import org.openjdk.jmh.annotations.BenchmarkMode;
21+
import org.openjdk.jmh.annotations.Fork;
22+
import org.openjdk.jmh.annotations.Measurement;
23+
import org.openjdk.jmh.annotations.Mode;
24+
import org.openjdk.jmh.annotations.OutputTimeUnit;
25+
import org.openjdk.jmh.annotations.Scope;
26+
import org.openjdk.jmh.annotations.State;
27+
import org.openjdk.jmh.annotations.Warmup;
28+
import org.openjdk.jmh.infra.Blackhole;
29+
30+
import java.util.concurrent.TimeUnit;
31+
32+
import static org.apache.commons.lang3.StringUtils.isEmpty;
33+
34+
/**
35+
* Test to show whether using BitSet for removeAll() methods is faster than using HashSet.
36+
*/
37+
@BenchmarkMode(Mode.AverageTime)
38+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
39+
@State(Scope.Thread)
40+
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
41+
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
42+
@Fork(value = 1, jvmArgs = {"-server", "-Xms512M", "-Xmx512M"})
43+
public class StringUtilsWrapTest {
44+
static String[] strings = buildStrings();
45+
46+
private static String[] buildStrings() {
47+
String[] res = new String[128 * 128];
48+
for (int i = 0; i < 128; i++) {
49+
for (int j = 0; j < 128; j++) {
50+
StringBuilder stringBuilder = new StringBuilder();
51+
stringBuilder.append((char) i);
52+
stringBuilder.append((char) j);
53+
res[i * 128 + j] = stringBuilder.toString();
54+
}
55+
}
56+
return res;
57+
}
58+
59+
@Benchmark
60+
public String test00Old() {
61+
return wrapOld("a", "b");
62+
}
63+
64+
@Benchmark
65+
public String test00New() {
66+
return wrapNew("a", "b");
67+
}
68+
69+
@Benchmark
70+
public String test01Old() {
71+
return wrapOld("aaaaaaaaaa", "b");
72+
}
73+
74+
@Benchmark
75+
public String test01New() {
76+
return wrapNew("aaaaaaaaaa", "b");
77+
}
78+
79+
@Benchmark
80+
public String test10Old() {
81+
return wrapIfMissingOld("aaaaaaaaaa", "b");
82+
}
83+
84+
@Benchmark
85+
public String test10New() {
86+
return wrapIfMissingNew("aaaaaaaaaa", "b");
87+
}
88+
89+
@Benchmark
90+
public String test11Old() {
91+
return wrapIfMissingOld("ab", "b");
92+
}
93+
94+
@Benchmark
95+
public String test11New() {
96+
return wrapIfMissingNew("ab", "b");
97+
}
98+
99+
@Benchmark
100+
public String test12ld() {
101+
return wrapIfMissingOld("bb", "b");
102+
}
103+
104+
@Benchmark
105+
public String test12New() {
106+
return wrapIfMissingNew("bb", "b");
107+
}
108+
109+
@Benchmark
110+
public String test20Old() {
111+
return wrapIfMissingOld("aaaaaaaaaa", 'b');
112+
}
113+
114+
@Benchmark
115+
public String test20New() {
116+
return wrapIfMissingNew("aaaaaaaaaa", 'b');
117+
}
118+
119+
@Benchmark
120+
public String test21Old() {
121+
return wrapIfMissingOld("ab", 'b');
122+
}
123+
124+
@Benchmark
125+
public String test21New() {
126+
return wrapIfMissingNew("ab", 'b');
127+
}
128+
129+
@Benchmark
130+
public String test22Old() {
131+
return wrapIfMissingOld("bb", 'b');
132+
}
133+
134+
@Benchmark
135+
public String test22New() {
136+
return wrapIfMissingNew("bb", 'b');
137+
}
138+
139+
@Benchmark
140+
public void testsOld(Blackhole blackhole) {
141+
for (int i = 0; i < 128; i++) {
142+
for (String au : strings) {
143+
blackhole.consume(wrapIfMissingOld(au, (char) i));
144+
}
145+
}
146+
}
147+
148+
@Benchmark
149+
public void testsNew(Blackhole blackhole) {
150+
for (int i = 0; i < 128; i++) {
151+
for (String au : strings) {
152+
blackhole.consume(wrapIfMissingNew(au, (char) i));
153+
}
154+
}
155+
}
156+
157+
//-----
158+
159+
public static String wrapOld(final String str, final String wrapWith) {
160+
161+
if (isEmpty(str) || isEmpty(wrapWith)) {
162+
return str;
163+
}
164+
165+
return wrapWith.concat(str).concat(wrapWith);
166+
}
167+
168+
public static String wrapNew(final String str, final String wrapWith) {
169+
170+
if (isEmpty(str) || isEmpty(wrapWith)) {
171+
return str;
172+
}
173+
174+
return wrapWith + str + wrapWith;
175+
}
176+
177+
public static String wrapIfMissingOld(final String str, final char wrapWith) {
178+
if (isEmpty(str) || wrapWith == CharUtils.NUL) {
179+
return str;
180+
}
181+
final boolean wrapStart = str.charAt(0) != wrapWith;
182+
final boolean wrapEnd = str.charAt(str.length() - 1) != wrapWith;
183+
if (!wrapStart && !wrapEnd) {
184+
return str;
185+
}
186+
187+
final StringBuilder builder = new StringBuilder(str.length() + 2);
188+
if (wrapStart) {
189+
builder.append(wrapWith);
190+
}
191+
builder.append(str);
192+
if (wrapEnd) {
193+
builder.append(wrapWith);
194+
}
195+
return builder.toString();
196+
}
197+
198+
public static String wrapIfMissingNew(final String str, final char wrapWith) {
199+
if (isEmpty(str) || wrapWith == CharUtils.NUL) {
200+
return str;
201+
}
202+
final boolean wrapStart = str.charAt(0) != wrapWith;
203+
final boolean wrapEnd = str.charAt(str.length() - 1) != wrapWith;
204+
if (wrapStart) {
205+
if (wrapEnd) {
206+
return wrapWith + str + wrapWith;
207+
} else {
208+
return wrapWith + str;
209+
}
210+
} else {
211+
if (wrapEnd) {
212+
return str + wrapWith;
213+
} else {
214+
return str;
215+
}
216+
}
217+
}
218+
219+
public static String wrapIfMissingOld(final String str, final String wrapWith) {
220+
if (isEmpty(str) || isEmpty(wrapWith)) {
221+
return str;
222+
}
223+
224+
final boolean wrapStart = !str.startsWith(wrapWith);
225+
final boolean wrapEnd = !str.endsWith(wrapWith);
226+
if (!wrapStart && !wrapEnd) {
227+
return str;
228+
}
229+
230+
final StringBuilder builder = new StringBuilder(str.length() + wrapWith.length() + wrapWith.length());
231+
if (wrapStart) {
232+
builder.append(wrapWith);
233+
}
234+
builder.append(str);
235+
if (wrapEnd) {
236+
builder.append(wrapWith);
237+
}
238+
return builder.toString();
239+
}
240+
241+
public static String wrapIfMissingNew(final String str, final String wrapWith) {
242+
if (isEmpty(str) || isEmpty(wrapWith)) {
243+
return str;
244+
}
245+
246+
final boolean wrapStart = !str.startsWith(wrapWith);
247+
final boolean wrapEnd = !str.endsWith(wrapWith);
248+
if (wrapStart) {
249+
if (wrapEnd) {
250+
return wrapWith + str + wrapWith;
251+
} else {
252+
return wrapWith + str;
253+
}
254+
} else {
255+
if (wrapEnd) {
256+
return str + wrapWith;
257+
} else {
258+
return str;
259+
}
260+
}
261+
}
262+
}

0 commit comments

Comments
 (0)