StringBuf.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. /**
  26. A String buffer is an efficient way to build a big string by
  27. appending small elements together.
  28. **/
  29. class StringBuf {
  30. /**
  31. Creates a new string buffer.
  32. **/
  33. public function new() {
  34. #if neko
  35. b = __make();
  36. #else
  37. b = "";
  38. #end
  39. }
  40. /**
  41. Adds the representation of any value to the string buffer.
  42. **/
  43. public inline function add( ?x : Dynamic ) {
  44. #if neko
  45. __add(b,x);
  46. #else
  47. b += x;
  48. #end
  49. }
  50. /**
  51. Adds a part of a string to the string buffer.
  52. **/
  53. public inline function addSub( s : String, pos : Int, ?len : Int ) {
  54. #if neko
  55. __add_sub(b,untyped s.__s,pos,len == null ? s.length - pos : len);
  56. #elseif flash9
  57. if( len == null )
  58. b += s.substr(pos);
  59. else
  60. b += s.substr(pos,len);
  61. #else
  62. b += s.substr(pos,len);
  63. #end
  64. }
  65. /**
  66. Adds a character to the string buffer.
  67. **/
  68. public inline function addChar( c : Int ) untyped {
  69. #if neko
  70. __add_char(b,c);
  71. #elseif (flash9 || js || php)
  72. b += String.fromCharCode(c);
  73. #elseif flash
  74. b += String["fromCharCode"](c);
  75. #end
  76. }
  77. /**
  78. Returns the content of the string buffer.
  79. The buffer is not emptied by this operation.
  80. **/
  81. public inline function toString() : String {
  82. #if neko
  83. return new String(__string(b));
  84. #else
  85. return b;
  86. #end
  87. }
  88. private var b : #if neko Dynamic #else String #end;
  89. #if neko
  90. static var __make : Dynamic = neko.Lib.load("std","buffer_new",0);
  91. static var __add : Dynamic = neko.Lib.load("std","buffer_add",2);
  92. static var __add_char : Dynamic = neko.Lib.load("std","buffer_add_char",2);
  93. static var __add_sub : Dynamic = neko.Lib.load("std","buffer_add_sub",4);
  94. static var __string : Dynamic = neko.Lib.load("std","buffer_string",1);
  95. #end
  96. }