Skip to content

Commit

Permalink
Description for TriFuncSplitPreserve.java added + bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
tjann committed Nov 2, 2024
1 parent 979663d commit 78683cb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/main/java/org/cactoos/func/TriFuncSplitPreserve.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@
import org.cactoos.list.ListOf;

/**
* Used for avoiding static method calls.
* A String splitter preserving all tokens.
* Unlike regular Split, stores empty "" tokens
* created by adjacent regex separators.
*
* <p>
* Examples:
* 1) text - " hello there", regex - " "
* result: ["", "hello", "there", ""]
* 2) text - "aaa", regex - "a"
* result: ["", "", "", ""]
* </p>
*
* @since 0.0
*/
Expand All @@ -46,14 +56,14 @@ public Collection<String> apply(
int start = 0;
int pos = str.indexOf(regex);
while (pos >= start) {
if (lmt > 0 && ret.size() == lmt) {
if (lmt > 0 && ret.size() == lmt || regex.isEmpty()) {
break;
}
ret.add(str.substring(start, pos));
start = pos + regex.length();
pos = str.indexOf(regex, start);
}
if (lmt <= 0 || ret.size() < lmt) {
if (lmt <= 0 || ret.size() < lmt || regex.isEmpty()) {
if (start < str.length()) {
ret.add(str.substring(start));
} else if (start == str.length()) {
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/cactoos/text/SplitPreserveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
final class SplitPreserveTest {
@Test
void checkingSplit() {
String txt = " ";
String txt = "aaa";
final String msg = "Adjacent separators must create an empty element";
ArrayList<Text> array = new ArrayList<>(4);
array.add(new TextOf(""));
Expand All @@ -54,7 +54,7 @@ void checkingSplit() {
this.getLength(
new Split(
new TextOf(txt),
new TextOf(" ")
new TextOf("a")
).iterator()
),
IsNot.not(
Expand Down Expand Up @@ -103,7 +103,7 @@ int getLength(final Iterator<Text> iter) {

@Test
void checkingSplitPreserveTokens() {
String txt = " ";
String txt = "aaa";
final String msg = "Adjacent separators must create an empty element";
ArrayList<Text> array = new ArrayList<>(4);
array.add(new TextOf(""));
Expand All @@ -115,7 +115,7 @@ void checkingSplitPreserveTokens() {
this.getLength(
new SplitPreserveAllTokens(
new TextOf(txt),
new TextOf(" ")
new TextOf("a")
).iterator()
),
Matchers.equalTo(
Expand Down

0 comments on commit 78683cb

Please sign in to comment.