StringBuf.hx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. @:coreApi class StringBuf {
  23. var b : hl.Bytes;
  24. var size : Int;
  25. var pos : Int;
  26. public var length(get,never) : Int;
  27. public function new() : Void {
  28. pos = 0;
  29. size = 8; // ensure 4 bytes expand for addChar()
  30. b = new hl.Bytes(size);
  31. }
  32. inline function get_length() : Int {
  33. return pos >> 1;
  34. }
  35. inline function __expand( need : Int ) : Void {
  36. var nsize = (size * 3) >> 1;
  37. if( need > nsize ) nsize = need;
  38. var b2 = new hl.Bytes(nsize);
  39. b2.blit(0, b, 0, pos);
  40. b = b2;
  41. size = nsize;
  42. }
  43. inline function __add( bytes : hl.Bytes, spos : Int, ssize : Int ) : Void {
  44. if( pos + ssize > size ) __expand(pos + ssize);
  45. b.blit(pos, bytes, spos, ssize);
  46. pos += ssize;
  47. }
  48. public function add<T>( x : T ) : Void {
  49. var slen = 0;
  50. var sbytes = hl.Bytes.fromValue(x, new hl.Ref(slen));
  51. __add(sbytes, 0, slen<<1);
  52. }
  53. public function addSub( s : String, pos : Int, ?len : Int ) : Void @:privateAccess {
  54. if( pos < 0 ) pos = 0;
  55. if( pos >= s.length ) return;
  56. var slen : Int;
  57. if( len == null ) slen = s.length - pos else {
  58. slen = len;
  59. if( pos + slen > s.length ) slen = s.length - pos;
  60. if( slen <= 0 ) return;
  61. }
  62. __add(s.bytes, pos << 1, slen << 1);
  63. }
  64. public function addChar( c : Int ) : Void {
  65. if( c >= 0 && c < 0x10000 ) {
  66. if( c >= 0xD800 && c <= 0xDFFF ) throw "Invalid unicode char " + c;
  67. if( pos + 2 > size ) __expand(0);
  68. b.setUI16(pos, c);
  69. pos += 2;
  70. } else if( c < 0x110000 ) {
  71. if( pos + 4 > size ) __expand(0);
  72. c -= 0x10000;
  73. b.setUI16(pos, (c >> 10) + 0xD800);
  74. b.setUI16(pos + 2, (c & 1023) + 0xDC00);
  75. pos += 4;
  76. } else
  77. throw "Invalid unicode char " + c;
  78. }
  79. public function toString() : String {
  80. if( pos+2 > size ) __expand(0);
  81. b.setUI16(pos,0);
  82. return @:privateAccess String.__alloc__(b, pos>>1);
  83. }
  84. }