Utf8.hx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package haxe;
  2. @:core_api
  3. class Utf8
  4. {
  5. var __s:Array<Int>;
  6. public function new( ?size : Null<Int> ) : Void {
  7. __s = new Array<Int>();
  8. if (size!=null && size>0)
  9. __s[size-1] = 0;
  10. }
  11. public function addChar( c : Int ) : Void {
  12. __s.push(c);
  13. }
  14. public function toString() : String {
  15. return untyped __global__.__hxcpp_char_array_to_utf8_string(__s);
  16. }
  17. // Incoming string is array of bytes containing possibly invalid utf8 chars
  18. // Result is the same string with the bytes expanded into utf8 sequences
  19. public static function encode( s : String ) : String {
  20. return untyped __global__.__hxcpp_char_bytes_to_utf8_string(s);
  21. }
  22. // Incoming string is array of bytes representing valid utf8 chars
  23. // Result is a string containing the compressed bytes
  24. public static function decode( s : String ) : String {
  25. return untyped __global__.__hxcpp_utf8_string_to_char_bytes(s);
  26. }
  27. public static function iter( s : String, chars : Int -> Void ) : Void {
  28. var array:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(s);
  29. for(a in array)
  30. chars(a);
  31. }
  32. public static function charCodeAt( s : String, index : Int ) : Int {
  33. var array:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(s);
  34. return array[index];
  35. }
  36. public static function validate( s : String ) : Bool {
  37. try {
  38. untyped __global__.__hxcpp_utf8_string_to_char_array(s);
  39. return true;
  40. } catch(e:Dynamic) { }
  41. return false;
  42. }
  43. public static function length( s : String ) : Int {
  44. var array:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(s);
  45. return array.length;
  46. }
  47. public static function compare( a : String, b : String ) : Int {
  48. var a_:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(a);
  49. var b_:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(b);
  50. var min = a_.length < b_.length ? a_.length : b_.length;
  51. for(i in 0...min)
  52. {
  53. if (a_[i] < b_[i]) return -1;
  54. if (a_[i] > b_[i]) return 1;
  55. }
  56. return a_.length==b_.length ? 0 : a_.length<b_.length ? -1 : 1;
  57. }
  58. public static function sub( s : String, pos : Int, len : Int ) : String {
  59. var array:Array<Int> = untyped __global__.__hxcpp_utf8_string_to_char_array(s);
  60. var sub = array.slice(pos,len);
  61. return untyped __global__.__hxcpp_char_array_to_utf8_string(sub);
  62. }
  63. }