Utf8.hx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /**
  24. Since not all platforms guarantee that `String` always uses UTF-8 encoding, you
  25. can use this cross-platform API to perform operations on such strings.
  26. **/
  27. @:deprecated('haxe.Utf8 is deprecated. Use UnicodeString instead.')
  28. class Utf8 {
  29. var __b:String;
  30. /**
  31. Allocate a new Utf8 buffer using an optional bytes size.
  32. **/
  33. public function new(?size:Int) {
  34. __b = "";
  35. }
  36. /**
  37. Add the given UTF8 character code to the buffer.
  38. **/
  39. public inline function addChar(c:Int):Void {
  40. __b += String.fromCharCode(c);
  41. }
  42. /**
  43. Returns the buffer converted to a String.
  44. **/
  45. public inline function toString():String {
  46. return __b;
  47. }
  48. /**
  49. Call the `chars` function for each UTF8 char of the string.
  50. **/
  51. public static function iter(s:String, chars:Int->Void) {
  52. for (i in 0...s.length)
  53. chars(s.charCodeAt(i));
  54. }
  55. /**
  56. Encode the input ISO string into the corresponding UTF8 one.
  57. **/
  58. public static function encode(s:String):String {
  59. throw new haxe.exceptions.NotImplementedException();
  60. }
  61. /**
  62. Decode an UTF8 string back to an ISO string.
  63. Throw an exception if a given UTF8 character is not supported by the decoder.
  64. **/
  65. public static function decode(s:String):String {
  66. throw new haxe.exceptions.NotImplementedException();
  67. }
  68. /**
  69. Similar to `String.charCodeAt` but uses the UTF8 character position.
  70. **/
  71. public static inline function charCodeAt(s:String, index:Int):Int {
  72. return s.charCodeAt(index);
  73. }
  74. /**
  75. Tells if the String is correctly encoded as UTF8.
  76. **/
  77. public static inline function validate(s:String):Bool {
  78. return true;
  79. }
  80. /**
  81. Returns the number of UTF8 chars of the String.
  82. **/
  83. #if js
  84. extern
  85. #end
  86. public static inline function length(s:String):Int {
  87. return s.length;
  88. }
  89. /**
  90. Compare two UTF8 strings, character by character.
  91. **/
  92. public static function compare(a:String, b:String):Int {
  93. return a > b ? 1 : (a == b ? 0 : -1);
  94. }
  95. /**
  96. This is similar to `String.substr` but the `pos` and `len` parts are considering UTF8 characters.
  97. **/
  98. public static inline function sub(s:String, pos:Int, len:Int):String {
  99. return s.substr(pos, len);
  100. }
  101. }