#StringBuilder() implementation (using buffers)

1 messages · Page 1 of 1 (latest)

zealous forum
#

...we had a little chat about string concatenation, and this inspired me to do a little StringBuilder implementation, as you might know it from .net/C#

It uses buffers, and as we have to take care to buffer_delete them, when we are done, I thought, the most convenient way is, that the class destroys the buffer after you request the contents via .toString()

I don't want to create a repository for a single function, so I thought, I'd share it here directly. In my library, I have unit tests for it. It works in the form posted below.

Methods are chainable, so you can do:

var postal_address = new StringBuilder()
  .append_line(recipient_name)
  .append(street).append_word(number).append_line()
  .append(zip).append_word(city)
  .toString();

and you get it all without string concatenation.

The lines above will result in something like

firstname lastname
some street 135
12345 star city

...is just wanted to show the append/append_line/append_word functions.
Hope, someone has use for it -- I have included it in the next raptor release
Have fun! Make Games!
Gris

zealous forum
#
function StringBuilder(_initial_size = 64) constructor {
    
    _buffer = buffer_create(_initial_size, buffer_grow, 1);
    
    /// @func append(_val)
    static append = function(_val) {
        buffer_write(_buffer, buffer_text, string(_val));
        return self;
    }
    
    /// @func append_line(_val = "")
    /// @desc    Appends the specified value to the string and adds a newline \n character.
    static append_line = function(_val = "") {
        buffer_write(_buffer, buffer_text, string_concat(_val, "\n"));
        return self;
    }
    
    /// @func append_word(_val)
    /// @desc    Appends a blank character and then the specified value to the string.
    ///            Convenience function to avoid .append(" ").append(something) chains
    static append_word = function(_val) {
        buffer_write(_buffer, buffer_text, string_concat(" ", _val));
        return self;
    }

    /// @func length()
    static length = function() {
        return string_length(toString(false));
    }
    
    /// @func clear()
    /// @desc Clears (deletes) the buffer and creates a new one with the initial size
    static clear = function() {
        buffer_delete(_buffer);
        _buffer = buffer_create(_initial_size, buffer_grow, 1);
    }
    
    /// @func clean_up()
    static clean_up = function() {
        if (_buffer == undefined) return;
        buffer_delete(_buffer);
        _buffer = undefined;
    }
    
    /// @func toString(_delete_buffer = true)
    /// @desc    Returns the contents as string and deletes the buffer to avoid memory leak.
    toString = function(_delete_buffer = true) {
        buffer_seek(_buffer, buffer_seek_start, 0);
        var rv = buffer_read(_buffer, buffer_string);
        if (!_delete_buffer)
            buffer_seek(_buffer, buffer_seek_end, 0);
        else {
            buffer_delete(_buffer);
            _buffer = undefined;
        }
        
        return rv;
    }
    
}