StringBuf.hx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. import cpp.NativeString;
  23. import cpp.Pointer;
  24. @:coreApi
  25. class StringBuf {
  26. private var b:Null<Array<String>> = null;
  27. public var length(get, never):Int;
  28. var charBuf:Null<Array<cpp.Char>> = null;
  29. public function new():Void {}
  30. private function drainCharBuf():String {
  31. final buffer = this.charBuf;
  32. final length = buffer.length;
  33. buffer.push(0);
  34. final bufferPtr = Pointer.arrayElem(buffer, 0);
  35. final bufferString = NativeString.fromGcPointer(bufferPtr, length);
  36. this.charBuf = null;
  37. return bufferString;
  38. }
  39. private function flush():Void {
  40. final charBufAsString = drainCharBuf();
  41. if (b == null)
  42. b = [charBufAsString];
  43. else
  44. b.push(charBufAsString);
  45. }
  46. function get_length():Int {
  47. var len = 0;
  48. if (charBuf != null)
  49. len = charBuf.length;
  50. if (b != null)
  51. for (s in b)
  52. len += s == null ? 4 : s.length;
  53. return len;
  54. }
  55. public inline function add<T>(x:T):Void {
  56. if (charBuf != null)
  57. flush();
  58. if (b == null)
  59. b = [Std.string(x)];
  60. else
  61. b.push(Std.string(x));
  62. }
  63. public #if !cppia inline #end function addSub(s:String, pos:Int, ?len:Int):Void {
  64. if (charBuf != null)
  65. flush();
  66. if (b == null)
  67. b = [s.substr(pos, len)];
  68. else
  69. b.push(s.substr(pos, len));
  70. }
  71. public #if !cppia inline #end function addChar(c:Int):Void {
  72. #if hxcpp_smart_strings
  73. if (c >= 127)
  74. add(String.fromCharCode(c));
  75. else
  76. #end
  77. {
  78. if (charBuf == null)
  79. charBuf = new Array<cpp.Char>();
  80. charBuf.push(c);
  81. }
  82. }
  83. public function clear():Void {
  84. this.charBuf?.resize(0);
  85. this.b?.resize(0);
  86. }
  87. public function toString():String {
  88. if (charBuf != null)
  89. flush();
  90. if (b == null || b.length == 0)
  91. return "";
  92. if (b.length == 1)
  93. return b[0];
  94. return b.join("");
  95. }
  96. }