StringBuf.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C)2005-2017 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. Its cross-platform implementation uses String concatenation internally, but
  26. StringBuf may be optimized for different targets.
  27. Unlike String, an instance of StringBuf is not immutable in the sense that
  28. it can be passed as argument to functions which modify it by appending more
  29. values. However, the internal buffer cannot be modified.
  30. **/
  31. import lua.Table;
  32. class StringBuf {
  33. var b:Table<Int,String>;
  34. /*
  35. The length of `this` StringBuf in characters.
  36. **/
  37. public var length(get,null) : Int;
  38. /**
  39. Creates a new StringBuf instance.
  40. This may involve initialization of the internal buffer.
  41. **/
  42. public inline function new() {
  43. b = Table.create();
  44. this.length = 0;
  45. }
  46. inline function get_length() : Int {
  47. return length;
  48. }
  49. /**
  50. Appends the representation of `x` to `this` StringBuf.
  51. The exact representation of `x` may vary per platform. To get more
  52. consistent behavior, this function should be called with
  53. Std.string(x).
  54. If `x` is null, the String "null" is appended.
  55. **/
  56. public inline function add<T>( x : T ) : Void {
  57. var str = Std.string(x);
  58. Table.insert(b, str);
  59. length += str.length;
  60. }
  61. /**
  62. Appends the character identified by `c` to `this` StringBuf.
  63. If `c` is negative or has another invalid value, the result is
  64. unspecified.
  65. **/
  66. public inline function addChar( c : Int ) : Void {
  67. Table.insert(b, String.fromCharCode(c));
  68. length += 1;
  69. }
  70. /**
  71. Appends a substring of `s` to `this` StringBuf.
  72. This function expects `pos` and `len` to describe a valid substring of
  73. `s`, or else the result is unspecified. To get more robust behavior,
  74. `this.add(s.substr(pos,len))` can be used instead.
  75. If `s` or `pos` are null, the result is unspecified.
  76. If `len` is omitted or null, the substring ranges from `pos` to the end
  77. of `s`.
  78. **/
  79. public inline function addSub( s : String, pos : Int, ?len : Int) : Void {
  80. var part = len == null ? s.substr(pos) : s.substr(pos, len);
  81. Table.insert(b, part);
  82. length += part.length;
  83. }
  84. /**
  85. Returns the content of `this` StringBuf as String.
  86. The buffer is not emptied by this operation.
  87. **/
  88. public inline function toString() : String {
  89. return Table.concat(b);
  90. }
  91. }