StringBuf.hx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /**
  23. A String buffer is an efficient way to build a big string by appending small
  24. elements together.
  25. Unlike String, an instance of StringBuf is not immutable in the sense that
  26. it can be passed as argument to functions which modify it by appending more
  27. values.
  28. **/
  29. class StringBuf {
  30. var b:String;
  31. /**
  32. The length of `this` StringBuf in characters.
  33. **/
  34. public var length(get, never):Int;
  35. /**
  36. Creates a new StringBuf instance.
  37. This may involve initialization of the internal buffer.
  38. **/
  39. public inline function new() {
  40. b = "";
  41. }
  42. inline function get_length():Int {
  43. return b.length;
  44. }
  45. /**
  46. Appends the representation of `x` to `this` StringBuf.
  47. The exact representation of `x` may vary per platform. To get more
  48. consistent behavior, this function should be called with
  49. Std.string(x).
  50. If `x` is null, the String "null" is appended.
  51. **/
  52. public inline function add<T>(x:T):Void {
  53. b += x;
  54. }
  55. /**
  56. Appends the character identified by `c` to `this` StringBuf.
  57. If `c` is negative or has another invalid value, the result is
  58. unspecified.
  59. **/
  60. public inline function addChar(c:Int):Void {
  61. b += String.fromCharCode(c);
  62. }
  63. /**
  64. Appends a substring of `s` to `this` StringBuf.
  65. This function expects `pos` and `len` to describe a valid substring of
  66. `s`, or else the result is unspecified. To get more robust behavior,
  67. `this.add(s.substr(pos,len))` can be used instead.
  68. If `s` or `pos` are null, the result is unspecified.
  69. If `len` is omitted or null, the substring ranges from `pos` to the end
  70. of `s`.
  71. **/
  72. public inline function addSub(s:String, pos:Int, ?len:Int):Void {
  73. b += (len == null ? s.substr(pos) : s.substr(pos, len));
  74. }
  75. /**
  76. Returns the content of `this` StringBuf as String.
  77. The buffer is not emptied by this operation.
  78. **/
  79. public inline function toString():String {
  80. return b;
  81. }
  82. }