Skip to content

Commit 0cda7fa

Browse files
committed
+split, Milestone 1
1 parent 73eea4c commit 0cda7fa

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=1.0.0-M0
1+
version=1.0.0-M1

src/main/java/ix/Ix.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ public static <T, R> Ix<R> forloop(T seed, Pred<? super T> condition,
182182
Func1<? super T, ? extends R> selector) {
183183
return new IxForloop<T, R>(seed, condition, selector, next);
184184
}
185+
186+
public static Ix<String> split(String string, String by) {
187+
return new IxSplit(string, by);
188+
}
185189

186190
//---------------------------------------------------------------------------------------
187191
// Instance operators

src/main/java/ix/IxSplit.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ix;
2+
3+
import java.util.Iterator;
4+
5+
final class IxSplit extends Ix<String> {
6+
7+
final String string;
8+
9+
final String by;
10+
11+
public IxSplit(String string, String by) {
12+
this.string = string;
13+
this.by = by;
14+
}
15+
16+
@Override
17+
public Iterator<String> iterator() {
18+
return new SplitIterator(string, by);
19+
}
20+
21+
static final class SplitIterator extends IxBaseIterator<String> {
22+
final String string;
23+
24+
final String by;
25+
26+
int index;
27+
28+
public SplitIterator(String string, String by) {
29+
this.string = string;
30+
this.by = by;
31+
}
32+
33+
@Override
34+
protected boolean moveNext() {
35+
int i = index;
36+
int j = string.indexOf(by, i);
37+
38+
if (j < 0) {
39+
value = string.substring(i);
40+
hasValue = true;
41+
done = true;
42+
return true;
43+
}
44+
45+
hasValue = true;
46+
index = j + by.length();
47+
value = string.substring(i, j);
48+
return true;
49+
}
50+
}
51+
}

src/test/java/ix/SplitTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2011-2016 David Karnok
3+
*
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+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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+
package ix;
18+
19+
import org.junit.Test;
20+
21+
public class SplitTest {
22+
23+
@Test
24+
public void normal() {
25+
Ix<String> source = Ix.split("a|b|c|d", "|");
26+
27+
IxTestHelper.assertValues(source, "a", "b", "c", "d");
28+
29+
IxTestHelper.assertNoRemove(source);
30+
}
31+
32+
@Test
33+
public void normal2() {
34+
Ix<String> source = Ix.split("a|b|c|", "|");
35+
36+
IxTestHelper.assertValues(source, "a", "b", "c", "");
37+
38+
IxTestHelper.assertNoRemove(source);
39+
}
40+
41+
@Test
42+
public void normal3() {
43+
Ix<String> source = Ix.split("a1|b2|c3", "|");
44+
45+
IxTestHelper.assertValues(source, "a1", "b2", "c3");
46+
47+
IxTestHelper.assertNoRemove(source);
48+
}
49+
50+
@Test
51+
public void normal4() {
52+
Ix<String> source = Ix.split("a1|<b2|c3|<d4", "|<");
53+
54+
IxTestHelper.assertValues(source, "a1", "b2|c3", "d4");
55+
56+
IxTestHelper.assertNoRemove(source);
57+
}
58+
59+
@Test
60+
public void empty() {
61+
Ix<String> source = Ix.split("", "|");
62+
63+
IxTestHelper.assertValues(source, "");
64+
65+
IxTestHelper.assertNoRemove(source);
66+
}
67+
}

0 commit comments

Comments
 (0)