2
0

StringBuf.hx 2.5 KB

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