Utf8.hx 3.1 KB

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