-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 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
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]); | ||
} | ||
|
||
} |
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,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; | ||
} | ||
|
||
} |