StringBuf.hx 3.1 KB

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