StringBuf.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. @: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)
  38. nsize = need;
  39. var b2 = new hl.Bytes(nsize);
  40. b2.blit(0, b, 0, pos);
  41. b = b2;
  42. size = nsize;
  43. }
  44. inline function __add(bytes:hl.Bytes, spos:Int, ssize:Int):Void {
  45. if (pos + ssize > size)
  46. __expand(pos + ssize);
  47. b.blit(pos, bytes, spos, ssize);
  48. pos += ssize;
  49. }
  50. public function add<T>(x:T):Void {
  51. var slen = 0;
  52. var str = Std.downcast((x:Dynamic),String);
  53. if( str != null ) {
  54. __add(@:privateAccess str.bytes, 0, str.length<<1);
  55. return;
  56. }
  57. var sbytes = hl.Bytes.fromValue(x, new hl.Ref(slen));
  58. __add(sbytes, 0, slen << 1);
  59. }
  60. public function addSub(s:String, pos:Int, ?len:Int):Void@:privateAccess {
  61. if (pos < 0)
  62. pos = 0;
  63. if (pos >= s.length)
  64. return;
  65. var slen:Int;
  66. if (len == null)
  67. slen = s.length - pos
  68. else {
  69. slen = len;
  70. if (pos + slen > s.length)
  71. slen = s.length - pos;
  72. if (slen <= 0)
  73. return;
  74. }
  75. __add(s.bytes, pos << 1, slen << 1);
  76. }
  77. public function addChar(c:Int):Void {
  78. if (c >= 0 && c < 0x10000) {
  79. if (c >= 0xD800 && c <= 0xDFFF)
  80. throw "Invalid unicode char " + c;
  81. if (pos + 2 > size)
  82. __expand(0);
  83. b.setUI16(pos, c);
  84. pos += 2;
  85. } else if (c < 0x110000) {
  86. if (pos + 4 > size)
  87. __expand(0);
  88. c -= 0x10000;
  89. b.setUI16(pos, (c >> 10) + 0xD800);
  90. b.setUI16(pos + 2, (c & 1023) + 0xDC00);
  91. pos += 4;
  92. } else
  93. throw "Invalid unicode char " + c;
  94. }
  95. public function toString():String {
  96. if (pos + 2 > size)
  97. __expand(0);
  98. b.setUI16(pos, 0);
  99. return @:privateAccess String.__alloc__(b, pos >> 1);
  100. }
  101. }