From 958d8057fc33cc2b229d7a1502cfde40569cb138 Mon Sep 17 00:00:00 2001 From: Ryusei Yamaguchi Date: Wed, 2 Sep 2015 03:31:00 +0900 Subject: [PATCH] replace new CodePoint with CodePoint.fromInt [breaking-change] --- README.md | 2 +- test/TestCodePoint.hx | 4 ++-- test/TestUtf32.hx | 2 +- unifill/CodePoint.hx | 13 +++++++++---- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index dc0bb81..bfaf244 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ class Main { trace("日本語".uLength()); // ==> 3 trace("русский".uCharAt(5)); // ==> и trace("🍺".uCodePointAt(0).toInt()); // ==> 127866 - trace(new CodePoint(0x1F37B)); // ==> 🍻 + trace(CodePoint.fromInt(0x1F37B)); // ==> 🍻 for (c in "♠♡♢♣".uIterator()) { trace(c); trace(c + 4); diff --git a/test/TestCodePoint.hx b/test/TestCodePoint.hx index 84fc035..50962e0 100644 --- a/test/TestCodePoint.hx +++ b/test/TestCodePoint.hx @@ -18,8 +18,8 @@ class TestCodePoint extends haxe.unit.TestCase { } public function test_CodePoint_cons_and_snoc() { - assertEquals("𩸽あëa", new CodePoint(0x29E3D) + "あëa"); - assertEquals("あëa𩸽", "あëa" + new CodePoint(0x29E3D)); + assertEquals("𩸽あëa", CodePoint.fromInt(0x29E3D) + "あëa"); + assertEquals("あëa𩸽", "あëa" + CodePoint.fromInt(0x29E3D)); } } diff --git a/test/TestUtf32.hx b/test/TestUtf32.hx index fe17bd9..75a6115 100644 --- a/test/TestUtf32.hx +++ b/test/TestUtf32.hx @@ -24,7 +24,7 @@ class TestUtf32 extends haxe.unit.TestCase { } public function test_cons_and_snoc() { - var c = new CodePoint(0x29E3D); + var c = CodePoint.fromInt(0x29E3D); assertTrue(Utf32.fromString("𩸽あëa") == c + Utf32.fromString("あëa")); assertTrue(Utf32.fromString("あëa𩸽") == Utf32.fromString("あëa") + c); } diff --git a/unifill/CodePoint.hx b/unifill/CodePoint.hx index 2282d52..a654b14 100644 --- a/unifill/CodePoint.hx +++ b/unifill/CodePoint.hx @@ -2,6 +2,14 @@ package unifill; abstract CodePoint(Int) { + @:from + public static inline function fromInt(code : Int) : CodePoint { + if (!Unicode.isScalar(code)) { + throw Exception.InvalidCodePoint(code); + } + return new CodePoint(code); + } + @:op(A + B) public static inline function cons(a : CodePoint, b : String) : String return a.toString() + b; @@ -29,10 +37,7 @@ abstract CodePoint(Int) { @:op(A > B) public static function gt(a : CodePoint, b : CodePoint) : Bool; @:op(A >= B) public static function gte(a : CodePoint, b : CodePoint) : Bool; - public inline function new(code : Int) : Void { - if (!Unicode.isScalar(code)) { - throw Exception.InvalidCodePoint(code); - } + inline function new(code : Int) : Void { this = code; }