Utf8.hx 2.4 KB

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