Utf8.hx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package haxe;
  23. @:coreApi
  24. class Utf8 {
  25. var __b : String;
  26. public function new( ?size : Int ) : Void {
  27. __b = '';
  28. }
  29. public function addChar( c : Int ) : Void {
  30. __b += uchr(c);
  31. }
  32. public function toString() : String {
  33. return __b;
  34. }
  35. public static function encode( s : String ) : String {
  36. return untyped __call__("utf8_encode", s);
  37. }
  38. public static function decode( s : String ) : String {
  39. return untyped __call__("utf8_decode", s);
  40. }
  41. public static function iter(s : String, chars : Int -> Void ) : Void {
  42. var len = length(s);
  43. for(i in 0...len)
  44. chars(charCodeAt(s, i));
  45. }
  46. public static function charCodeAt( s : String, index : Int ) : Int {
  47. return uord(sub(s, index, 1));
  48. }
  49. static function uchr(i : Int) : String {
  50. return untyped __php__("mb_convert_encoding(pack('N',$i), 'UTF-8', 'UCS-4BE')");
  51. }
  52. static function uord(s : String) : Int untyped {
  53. var c : Array<Int> = untyped __php__("unpack('N', mb_convert_encoding($s, 'UCS-4BE', 'UTF-8'))");
  54. return c[1];
  55. }
  56. public static function validate( s : String ) : Bool {
  57. return untyped __call__("mb_check_encoding", s, enc);
  58. }
  59. public static function length( s : String ) : Int {
  60. return untyped __call__("mb_strlen", s, enc);
  61. }
  62. public static function compare( a : String, b : String ) : Int {
  63. return untyped __call__("strcmp", a, b);
  64. }
  65. public static function sub( s : String, pos : Int, len : Int ) : String {
  66. return untyped __call__("mb_substr", s, pos, len, enc);
  67. }
  68. private static inline var enc = "UTF-8";
  69. }