Subdomain Posts
ActionScript | 2 days ago
ActionScript | 2 days ago
ActionScript | 2 days ago
ActionScript | 2 days ago
ActionScript | 6 days ago
ActionScript 3 | 6 days ago
ActionScript 3 | 6 days ago
ActionScript | 7 days ago
ActionScript | 7 days ago
ActionScript | 10 days ago
Recent Posts
None | 12 sec ago
None | 54 sec ago
Bash | 1 min ago
None | 1 min ago
None | 1 min ago
Python | 1 min ago
None | 1 min ago
C | 1 min ago
Lua | 1 min ago
None | 2 min ago
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Anonymous on the 6th of Feb 2010 09:03:03 PM Download | Raw | Embed | Report
  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. #if flash9
  27. import flash.utils.ByteArray;
  28. #end
  29.  
  30. /**
  31.         A String buffer is an efficient way to build a big string by
  32.         appending small elements together.
  33. **/
  34. class StringBuf {
  35.        
  36.         /**
  37.                 Creates a new string buffer.
  38.         **/
  39.         public function new() {
  40.                 #if neko
  41.                         b = __make();
  42.                 #elseif js
  43.                         b = new Array();
  44.                 #elseif flash9
  45.                         b = new ByteArray();
  46.                 #else
  47.                         b = "";
  48.                 #end
  49.         }
  50.  
  51.         /**
  52.                 Adds the representation of any value to the string buffer.
  53.         **/
  54.         public inline function add( ?x : Dynamic ) {
  55.                 #if neko
  56.                         __add(b,x);
  57.                 #elseif js
  58.                         b[b.length] = x;
  59.                 #elseif flash9
  60.                         b.writeUTFBytes(Std.string(x));
  61.                 #else
  62.                         b += x;
  63.                 #end
  64.         }
  65.  
  66.         /**
  67.                 Adds a part of a string to the string buffer.
  68.         **/
  69.         public inline function addSub( s : String, pos : Int, ?len : Int ) {
  70.                 #if neko
  71.                         __add_sub(b, untyped s.__s, pos, len == null ? s.length - pos : len);
  72.                 #elseif flash9
  73.                         if( len == null )
  74.                                 add(s.substr(pos));
  75.                         else
  76.                                 add(s.substr(pos, len));
  77.                 #elseif js
  78.                         b[b.length] = s.substr(pos,len);
  79.                 #else
  80.                         b += s.substr(pos,len);
  81.                 #end
  82.         }
  83.  
  84.         /**
  85.                 Adds a character to the string buffer.
  86.         **/
  87.         public inline function addChar( c : Int ) untyped {
  88.                 #if neko
  89.                         __add_char(b,c);
  90.                 #elseif js
  91.                         b[b.length] = String.fromCharCode(c);
  92.                 #elseif (flash && !flash9)
  93.                         add(String["fromCharCode"](c));
  94.                 #else
  95.                         add(String.fromCharCode(c));
  96.                 #end
  97.         }
  98.  
  99.         /**
  100.                 Returns the content of the string buffer.
  101.                 The buffer is not emptied by this operation.
  102.         **/
  103.         public inline function toString() : String {
  104.                 #if neko
  105.                         return new String(__string(b));
  106.                 #elseif js
  107.                         return b.join("");
  108.                 #elseif flash9         
  109.                         b.position = 0;
  110.                         var result = b.readUTFBytes(b.length);
  111.                         b.position = b.length;
  112.                         return result;
  113.                 #else
  114.                         return b;
  115.                 #end
  116.         }
  117.  
  118.         private var b :
  119.         #if neko
  120.                 Dynamic
  121.         #elseif js
  122.                 Array<Dynamic>
  123.         #elseif flash9
  124.                 ByteArray
  125.         #else
  126.                 String
  127.         #end;
  128.  
  129. #if neko
  130.         static var __make : Dynamic = neko.Lib.load("std","buffer_new",0);
  131.         static var __add : Dynamic = neko.Lib.load("std","buffer_add",2);
  132.         static var __add_char : Dynamic = neko.Lib.load("std","buffer_add_char",2);
  133.         static var __add_sub : Dynamic = neko.Lib.load("std","buffer_add_sub",4);
  134.         static var __string : Dynamic = neko.Lib.load("std","buffer_string",1);
  135. #end
  136.  
  137. }
Submit a correction or amendment below. [ previous version ] | [ difference ] | Make A New Post
To highlight particular lines, prefix each line with @@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: