Utf8.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. {
  26. var __s:Array<Int>;
  27. public function new( ?size : Null<Int> ) : Void {
  28. __s = new Array<Int>();
  29. if (size!=null && size>0)
  30. __s[size-1] = 0;
  31. }
  32. public function addChar( c : Int ) : Void {
  33. __s.push(c);
  34. }
  35. public function toString() : String {
  36. return untyped __global__.__hxcpp_char_array_to_utf8_string(__s);
  37. }
  38. // Incoming string is array of bytes containing possibly invalid utf8 chars
  39. // Result is the same string with the bytes expanded into utf8 sequences
  40. public static function encode( s : String ) : String {
  41. return untyped __global__.__hxcpp_char_bytes_to_utf8_string(s);
  42. }
  43. // Incoming string is array of bytes representing valid utf8 chars
  44. // Result is a string containing the compressed bytes
  45. public static function decode( s : String ) : String {
  46. return untyped __global__.__hxcpp_utf8_string_to_char_bytes(s);
  47. }
  48. public static function iter( s : String, chars : Int -> Void ) : Void {
  49. var array:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(s);
  50. for(a in array)
  51. chars(a);
  52. }
  53. public static function charCodeAt( s : String, index : Int ) : Int {
  54. var array:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(s);
  55. return array[index];
  56. }
  57. public static function validate( s : String ) : Bool {
  58. try {
  59. untyped __global__.__hxcpp_utf8_string_to_char_array(s);
  60. return true;
  61. } catch(e:Dynamic) { }
  62. return false;
  63. }
  64. public static function length( s : String ) : Int {
  65. var array:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(s);
  66. return array.length;
  67. }
  68. public static function compare( a : String, b : String ) : Int {
  69. var a_:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(a);
  70. var b_:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(b);
  71. var min = a_.length < b_.length ? a_.length : b_.length;
  72. for(i in 0...min)
  73. {
  74. if (a_[i] < b_[i]) return -1;
  75. if (a_[i] > b_[i]) return 1;
  76. }
  77. return a_.length==b_.length ? 0 : a_.length<b_.length ? -1 : 1;
  78. }
  79. public static function sub( s : String, pos : Int, len : Int ) : String {
  80. var array:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(s);
  81. var last = len < 0 ? array.length : pos+len;
  82. if (last>array.length) last = array.length;
  83. var sub = array.slice(pos,last);
  84. return untyped __global__.__hxcpp_char_array_to_utf8_string(sub);
  85. }
  86. }