/bin/std/neko/Utf8.hx

http://github.com/Yoomee/clippy · Haxe · 109 lines · 69 code · 16 blank · 24 comment · 9 complexity · 6719c3f9008528237f21778d9224357a MD5 · raw file

  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package neko;
  26. class Utf8 {
  27. var __b : Void;
  28. public function new( ?size : Int ) {
  29. __b = utf8_buf_alloc(if( size == null ) 1 else size);
  30. }
  31. public function addChar( c : Int ) {
  32. utf8_buf_add(__b,c);
  33. }
  34. public function toString() {
  35. return new String(utf8_buf_content(__b));
  36. }
  37. public static function encode( s : String ) : String {
  38. s = untyped s.__s;
  39. var sl = untyped __dollar__ssize(s);
  40. var buf = utf8_buf_alloc( sl );
  41. var i = 0;
  42. while( i < sl ) {
  43. utf8_buf_add(buf,untyped __dollar__sget(s,i));
  44. i += 1;
  45. }
  46. return new String( utf8_buf_content(buf) );
  47. }
  48. public static function decode( s : String ) : String {
  49. s = untyped s.__s;
  50. var sl = untyped __dollar__ssize(s);
  51. var ret = untyped __dollar__smake(sl);
  52. var i = 0;
  53. utf8_iter(s,function(c) {
  54. if( c == 8364 ) // euro symbol
  55. c = 164;
  56. else if( c == 0xFEFF ) // BOM
  57. return;
  58. else if( c > 255 )
  59. throw "Utf8::decode invalid character ("+c+")";
  60. untyped __dollar__sset(ret,i,c);
  61. i += 1;
  62. });
  63. return new String( untyped __dollar__ssub(ret,0,i) );
  64. }
  65. public static function iter( s : String, chars : Int -> Void ) {
  66. utf8_iter(untyped s.__s,chars);
  67. }
  68. public static function charCodeAt( s : String, index : Int ) : Int {
  69. return utf8_get(untyped s.__s,index);
  70. }
  71. public static function validate( s : String ) : Bool {
  72. return utf8_validate(untyped s.__s);
  73. }
  74. public static function length( s : String ) : Int {
  75. return utf8_length(untyped s.__s);
  76. }
  77. public static function compare( a : String, b : String ) : Int {
  78. return utf8_compare(untyped a.__s,untyped b.__s);
  79. }
  80. public static function sub( s : String, pos : Int, len : Int ) : String {
  81. return new String(utf8_sub(untyped s.__s,pos,len));
  82. }
  83. static var utf8_buf_alloc = Lib.load("std","utf8_buf_alloc",1);
  84. static var utf8_buf_add = Lib.load("std","utf8_buf_add",2);
  85. static var utf8_buf_content = Lib.load("std","utf8_buf_content",1);
  86. static var utf8_buf_length = Lib.load("std","utf8_buf_length",1);
  87. static var utf8_iter = Lib.load("std","utf8_iter",2);
  88. static var utf8_get = Lib.load("std","utf8_get",2);
  89. static var utf8_validate = Lib.load("std","utf8_validate",1);
  90. static var utf8_length = Lib.load("std","utf8_length",1);
  91. static var utf8_compare = Lib.load("std","utf8_compare",2);
  92. static var utf8_sub = Lib.load("std","utf8_sub",3);
  93. }