Skip to content

Commit

Permalink
add class UtfIter
Browse files Browse the repository at this point in the history
  • Loading branch information
mandel59 committed May 30, 2014
1 parent f7fb9f9 commit e81dd67
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Main {
r.add(new TestUtf32());
r.add(new TestInternalEncoding());
r.add(new TestInternalEncodingIter());
r.add(new TestUtfIter());
r.add(new TestCodePoint());
r.run();
}
Expand Down
34 changes: 34 additions & 0 deletions test/TestUtfIter.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package test;

import unifill.UtfIter;
import unifill.Utf;
import unifill.Utf8;
import unifill.Utf16;
import unifill.Utf32;

class TestUtfIter extends haxe.unit.TestCase {

function utfIter<S : Utf>(s : S, index : Array<Int>) {
var itr = new UtfIter<S>(s, 0, s.length);
var i = 0;
while (i < index.length - 1) {
assertTrue(itr.hasNext());
assertEquals(index[i++], itr.next());
}
assertFalse(itr.hasNext());
assertEquals(index[i], itr.index);
}

public function test_UtfIter_Utf8() {
utfIter(Utf8.fromString("𩸽あëa"), [0, 4, 7, 9, 10]);
}

public function test_UtfIter_Utf16() {
utfIter(Utf16.fromString("𩸽あëa"), [0, 2, 3, 4, 5]);
}

public function test_UtfIter_Utf32() {
utfIter(Utf32.fromString("𩸽あëa"), [0, 1, 2, 3, 4]);
}

}
25 changes: 25 additions & 0 deletions unifill/UtfIter.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package unifill;

class UtfIter<S : Utf> {

public var string : S;
public var index : Int;
public var endIndex : Int;

public inline function new(s : S, beginIndex : Int, endIndex : Int) {
string = s;
this.index = beginIndex;
this.endIndex = endIndex;
}

public inline function hasNext() : Bool {
return index < endIndex;
}

public inline function next() : Int {
var i = index;
index += string.codePointWidthAt(index);
return i;
}

}

0 comments on commit e81dd67

Please sign in to comment.