Skip to content

Commit 45ec85f

Browse files
authored
Optimize isCloudEventsHeader check (#445)
Instead of using: ```java key.substring(0, CE_PREFIX.length()).toLowerCase().startsWith(CE_PREFIX); ``` we can just use: ```java key.regionMatches(true /* ignoreCase */, 0, CE_PREFIX, 0, CE_PREFIX.length()); ``` Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com>
1 parent 1d87fb7 commit 45ec85f

7 files changed

Lines changed: 84 additions & 6 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2018-Present The CloudEvents Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package io.cloudevents.core.impl;
19+
20+
import javax.annotation.Nonnull;
21+
22+
final public class StringUtils {
23+
24+
private StringUtils() {
25+
// Prevent construction.
26+
}
27+
28+
public static boolean startsWithIgnoreCase(@Nonnull final String s, @Nonnull final String prefix) {
29+
return s.regionMatches(true /* ignoreCase */, 0, prefix, 0, prefix.length());
30+
}
31+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2018-Present The CloudEvents Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package io.cloudevents.core.impl;
19+
20+
import org.assertj.core.api.Assertions;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.Arguments;
23+
import org.junit.jupiter.params.provider.MethodSource;
24+
25+
import java.util.stream.Stream;
26+
27+
public class StringUtilsTest {
28+
29+
@ParameterizedTest
30+
@MethodSource("startsWithIgnoreCaseArgs")
31+
public void startsWithIgnoreCase(final String s, final String prefix, final boolean expected) {
32+
Assertions.assertThat(StringUtils.startsWithIgnoreCase(s, prefix)).isEqualTo(expected);
33+
}
34+
35+
private static Stream<Arguments> startsWithIgnoreCaseArgs() {
36+
return Stream.of(
37+
Arguments.of("s", "s", true),
38+
Arguments.of("sa", "S", true),
39+
Arguments.of("saS", "As", false),
40+
Arguments.of("sasso", "SASO", false),
41+
Arguments.of("sasso", "SaSsO", true)
42+
);
43+
}
44+
}

http/basic/src/main/java/io/cloudevents/http/impl/HttpMessageReader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.cloudevents.CloudEventData;
2020
import io.cloudevents.SpecVersion;
2121
import io.cloudevents.core.data.BytesCloudEventData;
22+
import io.cloudevents.core.impl.StringUtils;
2223
import io.cloudevents.core.message.impl.BaseGenericBinaryMessageReaderImpl;
2324

2425
import java.util.function.BiConsumer;
@@ -47,7 +48,7 @@ protected boolean isContentTypeHeader(String key) {
4748

4849
@Override
4950
protected boolean isCloudEventsHeader(String key) {
50-
return key != null && key.length() > 3 && key.substring(0, CE_PREFIX.length()).toLowerCase().startsWith(CE_PREFIX);
51+
return key != null && key.length() > CE_PREFIX.length() && StringUtils.startsWithIgnoreCase(key, CE_PREFIX);
5152
}
5253

5354
@Override

http/restful-ws/src/main/java/io/cloudevents/http/restful/ws/impl/BinaryRestfulWSMessageReaderImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import io.cloudevents.SpecVersion;
2121
import io.cloudevents.core.data.BytesCloudEventData;
22+
import io.cloudevents.core.impl.StringUtils;
2223
import io.cloudevents.core.message.impl.BaseGenericBinaryMessageReaderImpl;
2324

2425
import javax.ws.rs.core.HttpHeaders;
@@ -46,7 +47,7 @@ protected boolean isContentTypeHeader(String key) {
4647

4748
@Override
4849
protected boolean isCloudEventsHeader(String key) {
49-
return key.length() > 3 && key.substring(0, CE_PREFIX.length()).toLowerCase().startsWith(CE_PREFIX);
50+
return key.length() > CE_PREFIX.length() && StringUtils.startsWithIgnoreCase(key, CE_PREFIX);
5051
}
5152

5253
@Override

http/vertx/src/main/java/io/cloudevents/http/vertx/impl/BinaryVertxMessageReaderImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import io.cloudevents.SpecVersion;
2121
import io.cloudevents.core.data.BytesCloudEventData;
22+
import io.cloudevents.core.impl.StringUtils;
2223
import io.cloudevents.core.message.impl.BaseGenericBinaryMessageReaderImpl;
2324
import io.vertx.core.MultiMap;
2425
import io.vertx.core.buffer.Buffer;
@@ -45,7 +46,7 @@ protected boolean isContentTypeHeader(String key) {
4546

4647
@Override
4748
protected boolean isCloudEventsHeader(String key) {
48-
return key.length() > 3 && key.substring(0, CloudEventsHeaders.CE_PREFIX.length()).toLowerCase().startsWith(CloudEventsHeaders.CE_PREFIX);
49+
return key.length() > CloudEventsHeaders.CE_PREFIX.length() && StringUtils.startsWithIgnoreCase(key, CloudEventsHeaders.CE_PREFIX);
4950
}
5051

5152
@Override

kafka/src/main/java/io/cloudevents/kafka/impl/KafkaBinaryMessageReaderImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected boolean isContentTypeHeader(String key) {
4444

4545
@Override
4646
protected boolean isCloudEventsHeader(String key) {
47-
return key.length() > 3 && key.substring(0, KafkaHeaders.CE_PREFIX.length()).startsWith(KafkaHeaders.CE_PREFIX);
47+
return key.length() > KafkaHeaders.CE_PREFIX.length() && key.startsWith(KafkaHeaders.CE_PREFIX);
4848
}
4949

5050
@Override

spring/src/main/java/io/cloudevents/spring/messaging/MessageBinaryMessageReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import io.cloudevents.SpecVersion;
2222
import io.cloudevents.core.data.BytesCloudEventData;
23+
import io.cloudevents.core.impl.StringUtils;
2324
import io.cloudevents.core.message.impl.BaseGenericBinaryMessageReaderImpl;
2425

2526
import static io.cloudevents.spring.messaging.CloudEventsHeaders.CE_PREFIX;
@@ -51,8 +52,7 @@ protected boolean isContentTypeHeader(String key) {
5152

5253
@Override
5354
protected boolean isCloudEventsHeader(String key) {
54-
return key != null && key.length() > 3
55-
&& key.substring(0, CE_PREFIX.length()).toLowerCase().startsWith(CE_PREFIX);
55+
return key != null && key.length() > CE_PREFIX.length() && StringUtils.startsWithIgnoreCase(key, CE_PREFIX);
5656
}
5757

5858
@Override

0 commit comments

Comments
 (0)