Utf8.hx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (C)2005-2019 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. using cpp.NativeString;
  24. @:coreApi
  25. @:deprecated('haxe.Utf8 is deprecated. Use UnicodeString instead.')
  26. class Utf8 {
  27. var __s:Array<Int>;
  28. public function new(?size:Int):Void {
  29. __s = new Array<Int>();
  30. if (size != null && size > 0)
  31. cpp.NativeArray.reserve(__s, size);
  32. }
  33. public function addChar(c:Int):Void {
  34. __s.push(c);
  35. }
  36. public function toString():String {
  37. return untyped __global__.__hxcpp_char_array_to_utf8_string(__s);
  38. }
  39. // Incoming string is array of bytes containing possibly invalid utf8 chars
  40. // Result is the same string with the bytes expanded into utf8 sequences
  41. public static function encode(s:String):String {
  42. return untyped __global__.__hxcpp_char_bytes_to_utf8_string(s);
  43. }
  44. // Incoming string is array of bytes representing valid utf8 chars
  45. // Result is a string containing the compressed bytes
  46. public static function decode(s:String):String {
  47. return untyped __global__.__hxcpp_utf8_string_to_char_bytes(s);
  48. }
  49. public #if !cppia inline #end static function iter(s:String, chars:Int->Void):Void {
  50. var src = s.c_str();
  51. var end = src.add(s.length);
  52. while (src.lt(end))
  53. chars(src.ptr.utf8DecodeAdvance());
  54. }
  55. public static function charCodeAt(s:String, index:Int):Int {
  56. return s.utf8CharCodeAt(index);
  57. }
  58. public static function validate(s:String):Bool {
  59. return s.utf8IsValid();
  60. }
  61. public static function length(s:String):Int {
  62. return s.utf8Length();
  63. }
  64. public static function compare(a:String, b:String):Int {
  65. return a.compare(b);
  66. }
  67. public static function sub(s:String, pos:Int, len:Int):String {
  68. return s.utf8Sub(pos, len);
  69. }
  70. }