diff --git a/test/Main.hx b/test/Main.hx index bd5419b..f579df5 100644 --- a/test/Main.hx +++ b/test/Main.hx @@ -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(); } diff --git a/test/TestUtfIter.hx b/test/TestUtfIter.hx new file mode 100644 index 0000000..da7d34c --- /dev/null +++ b/test/TestUtfIter.hx @@ -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 : S, index : Array) { + var itr = new UtfIter(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]); + } + +} diff --git a/unifill/UtfIter.hx b/unifill/UtfIter.hx new file mode 100644 index 0000000..83ba1a7 --- /dev/null +++ b/unifill/UtfIter.hx @@ -0,0 +1,25 @@ +package unifill; + +class UtfIter { + + 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; + } + +}