-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
java: Improve text trimming performance (#85)
Precompiled the Pattern used in the utility methods. Improves performance. `StringUtils.trim()` is more than 3 times faster. Fixes: #84
- Loading branch information
Showing
3 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
java/src/test/java/io/cucumber/gherkin/StringUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.cucumber.gherkin; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class StringUtilsTest { | ||
private static final String WHITESPACE = "\u00A0 \t"; | ||
private static final String CUCUMBER = "🥒"; | ||
|
||
@Test | ||
void testRtrim() { | ||
assertEquals(WHITESPACE + CUCUMBER, StringUtils.rtrim(WHITESPACE + CUCUMBER + WHITESPACE)); | ||
} | ||
|
||
@Test | ||
void testRtrim_multiline() { | ||
assertEquals("\n" + WHITESPACE + "\n" + WHITESPACE + CUCUMBER, | ||
StringUtils.rtrim("\n" + WHITESPACE + "\n" + WHITESPACE + CUCUMBER + WHITESPACE + "\n" + WHITESPACE + "\n")); | ||
} | ||
|
||
@Test | ||
void testRtrimKeepNewlines() { | ||
assertEquals(WHITESPACE + CUCUMBER + "\n", StringUtils.rtrimKeepNewLines(WHITESPACE + CUCUMBER + "\n" + WHITESPACE)); | ||
} | ||
|
||
@Test | ||
void testLtrim() { | ||
assertEquals(CUCUMBER + WHITESPACE, StringUtils.ltrim(WHITESPACE + CUCUMBER + WHITESPACE)); | ||
} | ||
|
||
@Test | ||
void testLtrim_multiline() { | ||
assertEquals(CUCUMBER + WHITESPACE + "\n" + WHITESPACE + "\n", | ||
StringUtils.ltrim("\n" + WHITESPACE + "\n" + WHITESPACE + CUCUMBER + WHITESPACE + "\n" + WHITESPACE + "\n")); | ||
} | ||
|
||
@Test | ||
void testLtrimKeepNewlines() { | ||
assertEquals("\n" + CUCUMBER + WHITESPACE, StringUtils.ltrimKeepNewLines(WHITESPACE + "\n" + CUCUMBER + WHITESPACE)); | ||
} | ||
|
||
@Test | ||
void testTrim() { | ||
assertEquals(CUCUMBER, StringUtils.trim(WHITESPACE + CUCUMBER + WHITESPACE)); | ||
} | ||
|
||
@Test | ||
void testTrim_multiline() { | ||
assertEquals(CUCUMBER, | ||
StringUtils.trim("\n" + WHITESPACE + "\n" + WHITESPACE + CUCUMBER + WHITESPACE + "\n" + WHITESPACE + "\n")); | ||
} | ||
|
||
@Test | ||
void testTrim_empty() { | ||
assertEquals("", | ||
StringUtils.trim("")); | ||
} | ||
} |