language_helper/language_helper.d [ Modules ]

[ Top ] [ Modules ]

NAME

    language_helper.d

FUNCTION

    This file contains many high-level functions that should be useful in 
    most D programs. It uses the Tango library. In many cases it can be imported
    instead of having to import many of the common files from Tango.

language_helper/after [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a substring after the separator.
    Returns "" if there are no separators.

INPUTS

    value         - the string to examine.
    separator     - the string at the back.

SOURCE

public string after(string value, string separator) {
    size_t i = index(value, separator);

    if(i == value.length)
        return "";

    size_t start = i + separator.length;

    return value[start .. length];
}

language_helper/after_last [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a substring after the last separator.
    Returns "" if there are no separators.

INPUTS

    value         - the string to examine.
    separator     - the string at the back.

SOURCE

public string after_last(string value, string separator) {
    size_t i = rindex(value, separator);

    if(i == value.length)
        return "";

    size_t start = i + separator.length;

    return value[start .. length];
}

language_helper/Array [ Classes ]

[ Top ] [ Classes ]

FUNCTION

    Template functions that are useful for arrays.

language_helper/Array.contains [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    Return true if the item is in the array.

INPUTS

    array   - the array.
    item    - the item to look for.

SOURCE

    bool contains(ref T[] array, ref T item) {
        // Return true if the item is in it
        foreach(T entry; array)
            if(entry == item)
                return true;

        // Return false if not found
        return false;
    }

language_helper/Array.pop [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    Remove the item at the index and return it.

INPUTS

    array   - the array.
    i       - the index of the item to pop.

SOURCE

    T pop(ref T[] array, size_t i) {
        // Get the item
        T item = array[i];

        // Remove the item
        remove(array, i);

        return item;
    }

language_helper/Array.pop_item [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    Remove the item and return it.

INPUTS

    array   - the array.
    item    - the item to pop.

SOURCE

    T pop_item(ref T[] array, ref T item) {
        for(size_t i=0; i<array.length; i++)
            if(array[i] == item)
                return pop(array, i);

        throw new Exception("No item to pop.");
    }

language_helper/Array.remove [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    Remove the item at the index.

INPUTS

    array   - the array.
    i       - the index to remove.

SOURCE

    void remove(ref T[] array, size_t i) {
        // Get the length
        size_t len = array.length;

        // If we are not removing from the end, move 
        // the last element to the location of the removed.
        if(i != len - 1)
            array[i] = array[len - 1];

        // Decrease the length by one
        array = array[0 .. len - 1];
    }

language_helper/Array.remove_item [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    Remove the item.

INPUTS

    array   - the array.
    item    - the item to remove.

SOURCE

    void remove_item(ref T[] array, T item) {
        // Find the index of the item
        for(size_t i=0; i<array.length; i++) {
            if(array[i] == item) {
                remove(array, i);
                return;
            }
        }
    }

language_helper/AutoStringArray [ Classes ]

[ Top ] [ Classes ]

NAME

    AutoStringArray

FUNCTION

    Collects strings by auto converting any type you try to add.
    For performance, it stores them in a buffer as they are added.

EXAMPLE

    auto a = new AutoStringArray();
    a ~= "An int: ";
    a ~= 600;
    a ~= "\n";
    a ~= "A bool: ";
    a ~= true;
    a ~= "\n";
    a ~= "A float: ";
    a ~= 5.5f;
    a ~= "\n";
    Stdout(a.toString());

language_helper/AutoStringArray.opCatAssign( bool ) [ Methods ]

[ Top ] [ Methods ]

INPUTS

    value       - the bool to concatenate to the buffer.

SOURCE

    public void opCatAssign(bool value) { opCatAssign(to_s(value)); }

language_helper/AutoStringArray.opCatAssign( char ) [ Methods ]

[ Top ] [ Methods ]

INPUTS

    value       - the char to concatenate to the buffer.

SOURCE

    public void opCatAssign(char value) { opCatAssign(to_s(value)); }

language_helper/AutoStringArray.opCatAssign( double ) [ Methods ]

[ Top ] [ Methods ]

INPUTS

    value       - the double to concatenate to the buffer.

SOURCE

    public void opCatAssign(double value) { opCatAssign(to_s(value)); }

language_helper/AutoStringArray.opCatAssign( FixedPoint ) [ Methods ]

[ Top ] [ Methods ]

INPUTS

    value       - the FixedPoint to concatenate to the buffer.

SOURCE

    public void opCatAssign(FixedPoint value) { opCatAssign(to_s(value)); }

language_helper/AutoStringArray.opCatAssign( float ) [ Methods ]

[ Top ] [ Methods ]

INPUTS

    value       - the float to concatenate to the buffer.

SOURCE

    public void opCatAssign(float value) { opCatAssign(to_s(value)); }

language_helper/AutoStringArray.opCatAssign( int ) [ Methods ]

[ Top ] [ Methods ]

INPUTS

    value       - the int to concatenate to the buffer.

SOURCE

    public void opCatAssign(int value) { opCatAssign(to_s(value)); }

language_helper/AutoStringArray.opCatAssign( long ) [ Methods ]

[ Top ] [ Methods ]

INPUTS

    value       - the long to concatenate to the buffer.

SOURCE

    public void opCatAssign(long value) { opCatAssign(to_s(value)); }

language_helper/AutoStringArray.opCatAssign( real ) [ Methods ]

[ Top ] [ Methods ]

INPUTS

    value       - the real to concatenate to the buffer.

SOURCE

    public void opCatAssign(real value) { opCatAssign(to_s(value)); }

language_helper/AutoStringArray.opCatAssign( string ) [ Methods ]

[ Top ] [ Methods ]

INPUTS

    value       - the string to concatenate to the buffer.

SOURCE

    public void opCatAssign(string value) {
        size_t value_length = value.length;

        // If the value wont fit in the buffer move to a new one
        if(_i + value_length > BUFFER_SIZE) {
            // Trim the extra space off the buffer
            _buffers[_j] = _buffers[_j][0 .. _i];

            // Use the regular buffer size. But if the value is too big, use its size.
            size_t new_size;
            if(value_length > BUFFER_SIZE)
                new_size = value_length;
            else
                new_size = BUFFER_SIZE;

            // Create a new buffer
            _buffers ~= new char[new_size];
            _j++;
            _i = 0;
        }

        // Copy the value into the buffer
        _buffers[_j][_i .. _i+value_length] = value[0 .. value_length];
        _i+= value_length;
    }

language_helper/AutoStringArray.opCatAssign( uint ) [ Methods ]

[ Top ] [ Methods ]

INPUTS

    value       - the uint to concatenate to the buffer.

SOURCE

    public void opCatAssign(uint value) { opCatAssign(to_s(value)); }

language_helper/AutoStringArray.opCatAssign( ulong ) [ Methods ]

[ Top ] [ Methods ]

INPUTS

    value       - the ulong to concatenate to the buffer.

SOURCE

    public void opCatAssign(ulong value) { opCatAssign(to_s(value)); }

language_helper/AutoStringArray.this [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    A constructor.

INPUTS

    buffer    - an existing string buffer. Or null if you want 
                it to create its own buffer.

SOURCE

    public this(string buffer = null) { 
        if(buffer)
            _buffers ~= buffer;
        else
            _buffers ~= new char[BUFFER_SIZE];
    }

language_helper/AutoStringArray.toString [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    Returns everything added to the AutoStringArray as a string.

SOURCE

    public string toString() {
        string retval;
        if(_buffers.length > 1) {
            retval = join(_buffers[0 .. length-1], "");
        }
        retval ~= _buffers[length-1][0 .. _i];

        return retval;
    }

language_helper/before [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a substring before the separator.
    Returns the value if there are no separators.

INPUTS

    value         - the string to examine.
    separator     - the string at the front.

SOURCE

public string before(string value, string separator) {
    size_t i = index(value, separator);

    if(i == value.length)
        return value;

    return value[0 .. i];
}

language_helper/between [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns the substring between the before and after.

INPUTS

    value      - the string to examine.
    before     - the string at the front.
    after      - the string at the back.

SOURCE

public string between(string value, string before, string after) {
    return split(split(value, before)[1], after)[0];
}

language_helper/BUFFER_SIZE [ Definitions ]

[ Top ] [ Definitions ]

FUNCTION

    The default size of of any string buffer.

SOURCE

public const size_t BUFFER_SIZE = 1024 * 10;

language_helper/capitalize [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a capitalized string.

SOURCE

public string capitalize(string value) {
    if(value.length == 0) return value;

    string first = value[0 .. 1].dup;
    toUpper(first);
    return first ~ value[1 .. length];
}

language_helper/contains [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns true if the match is in the value.

INPUTS

    value   - the string to look in.
    match   - the part of the string to find.

SOURCE

public bool contains(string value, string match) {
    return tango.text.Util.containsPattern!(char)(value, match);
}

unittest {
    describe("language_helper#contains", 
        it("Should not find blanks", function() {
            assert(!contains("", ""));
        }),
        it("Should find single matches in the front", function() {
            assert(contains("abc", "c"));
        }),
        it("Should not find matches that are not there", function() {
            assert(!contains("abc", "z"));
        }),
        it("Should not find blank matches", function() {
            assert(!contains("abc", ""));
        }),
        it("Should not find anythin in a blank value", function() {
            assert(!contains("", "abc"));
        })
    );
}

language_helper/count [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns the number of instances of match in the value.

INPUTS

    value   - the string to look in.
    match   - the part of the string to find.

NOTES

    The tango count function is broken. This:
    tango.text.Util.count!(char)("method", "%")
    returns 1 instead of 0. So we create our own.

SOURCE

public size_t count(string value, string match) {
    // Just return 0 if the length is 0
    if(match.length == 0)
        return 0;

    size_t retval = 0;
    size_t i = 0;
    while(i < value.length && i + match.length <= value.length) {
        if(value[i .. i+match.length] == match)
            retval++;
        i++;
    }

    return retval;
}

unittest {
    describe("language_helper#count", 
        it("Should not find blanks", function() {
            assert(count("", "") == 0);
        }),
        it("Should not find blank matches", function() {
            assert(count("method", "") == 0);
        }),
        it("Should not find similar but different strings", function() {
            assert(count("abc", "abcdef") == 0);
        }),
        it("Should find single matches in the front", function() {
            assert(count("method", "m") == 1);
        }),
        it("Should find single matches in the back", function() {
            assert(count("method", "d") == 1);
        }),
        it("Should find single matches in the middle", function() {
            assert(count("methhod", "hh") == 1);
        }),
        it("Should find multiple matches", function() {
            assert(count("hhmethhod", "hh") == 2);
        })
    );
}

language_helper/Dictionary [ Classes ]

[ Top ] [ Classes ]

NAME

    Dictionary

FUNCTION

    A dictionary that has key value pairs. It is designed to have keys
    that are strings or ints, with a value of Dictionary.

EXAMPLE

    auto d = new Dictionary();
    d["company"].value = "Can Opener Inc.";
    d["employees"]["bob"].value = "So Awesome";
    d["employees"]["tim"].value = "Just Okay";

language_helper/Dictionary.has_key( string ) [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    Returns true if the associative array uses the string key.

INPUTS

    key       - the associative array key as a string.

SOURCE

    public bool has_key(string key) {
        return(this.named_items != null && (key in this.named_items) != null);
    }

language_helper/Dictionary.opIndex( size_t ) [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    Returns the value associated with a number key.

INPUTS

    key       - the associative array key as a size_t.

SOURCE

    public Dictionary opIndex(size_t i) {
        if((i in this.array_items) == null)
            this.array_items[i] = new Dictionary();
        return this.array_items[i];
    }

language_helper/Dictionary.opIndex( string ) [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    Returns the value associated with a string key.

INPUTS

    key       - the associative array key as a string.

SOURCE

    public Dictionary opIndex(string key) {
        // Initialize the value if it does not exist.
        if((key in this.named_items) == null)
            this.named_items[key] = new Dictionary();
        return this.named_items[key];
    }

language_helper/ends_with [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns true if the value ends with the match.

INPUTS

    value      - the string to examine.
    match      - the string to look for at the end.

SOURCE

public bool ends_with(string value, string match) {
    if(value is null || match is null)
        return false;

    if(value.length < match.length)
        return false;

    return value[length-match.length .. length] == match;
}

unittest {
    describe("language_helper#ends_with", 
        it("Should return false on null arguemnts", function() {
            assert(!ends_with(null, null));
        }),
        it("Should return true on blank match", function() {
            assert(ends_with("abc", ""));
        }),
        it("Should return true if the end matches", function() {
            assert(ends_with("abc", "c"));
        }),
        it("Should return false if the end does not match", function() {
            assert(!ends_with("abc", "b"));
        })
    );
}

language_helper/FixedPoint [ Classes ]

[ Top ] [ Classes ]

NAME

    FixedPoint

FUNCTION

    A class for using fixed point numbers.

EXAMPLE

    FixedPoint f = new FixedPoint(11, 3, 10, 2);

language_helper/FixedPoint.max_precision_width [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    The max precision width is the number of digits before the decimal.

SOURCE

    public uint max_precision_width() { return _max_precision_width; }

language_helper/FixedPoint.max_scale [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    The max scale is the largest number that fits in the max scale width.

SOURCE

    public ulong max_scale() {
        return pow(10, _max_scale_width) - 1;
    }

language_helper/FixedPoint.max_scale_width [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    The max scale width is the number of digits after the decimal.

SOURCE

    public uint max_scale_width() { return _max_scale_width; }

language_helper/FixedPoint.opAddAssign( double ) [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    This fixed point += a double.

INPUTS

    a       - the double to add.

SOURCE

    public void opAddAssign(double a) {
        this += to_FixedPoint(a);
    }

language_helper/FixedPoint.opAddAssign( FixedPoint ) [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    This fixed point += another fixed point.

INPUTS

    a       - the FixedPoint to add.

SOURCE

    public void opAddAssign(FixedPoint a) {
        // Get the new precision and scale
        long new_precision = _precision + a._precision;
        ulong new_scale;
        if(a._precision >= 0) {
            new_scale = _scale + a._scale;
        } else {
            if(a._scale > _scale) {
                new_precision -= 1;
                new_scale = (1000000000000000000 + _scale) - a._scale;
            } else {
                new_scale = _scale - a._scale;
            }
        }

        // Perform the rounding
        if(new_scale > 999999999999999999) {
            ulong more_scale = new_scale - 1000000000000000000;
            new_precision += (new_scale / 1000000000000000000);
            new_scale = more_scale;
        }

        // Make sure the new_precision does not overflow
        if(to_s(new_precision).length > _max_precision_width) {
            string[] buffer;
            for(size_t i=0; i<_max_precision_width; i++)
                buffer ~= "9";
            new_precision = to_long(join(buffer, ""));
        }
/*
        // Make sure the new_scale does not overflow
        if(to_s(new_scale).length > _max_scale_width) {
            string[] buffer;
            for(size_t i=0; i<_max_scale_width; i++)
                buffer ~= "9";
            new_scale = to_ulong(join(buffer, ""));
        }
        Stdout("\nscale overflow").newline.flush;
        Stdout.format("new_scale: {}", new_scale).newline.flush;
        Stdout.format("new_precision: {}", new_precision).newline.flush;
*/
        // Save the result
        _precision = new_precision;
        _scale = new_scale;
    }

language_helper/FixedPoint.opAddAssign( int ) [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    This fixed point += an int.

INPUTS

    a       - the int to add.

SOURCE

    public void opAddAssign(int a) {
        _precision += a;
    }

language_helper/FixedPoint.opEquals( double ) [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    This fixed point == an double.

INPUTS

    a       - the double to compare.

SOURCE

    public bool opEquals(double a) {
        return this.toDouble() == a;
    }

language_helper/FixedPoint.opEquals( long ) [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    This fixed point == an int.

INPUTS

    a       - the long to compare.

SOURCE

    public bool opEquals(long a) {
        return this.toLong() == a;
    }

language_helper/FixedPoint.opSubAssign [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    This fixed point -= another fixed point.

INPUTS

    a       - the FixedPoint to subtract.

SOURCE

    public void opSubAssign(FixedPoint a){
        // Negative the number so we can add it
        auto other = new FixedPoint(-a.precision, a.scale, a.max_precision_width, a.max_scale_width);
        this += other;
    }

language_helper/FixedPoint.precision [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    The precision is the number before the decimal.

SOURCE

    public long precision() { return _precision; }

language_helper/FixedPoint.scale [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    The scale is the number after the decimal.

SOURCE

    public ulong scale() { return _scale; }

language_helper/FixedPoint.this [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    A constructor.

INPUTS

    precision           - the number before the decimal.
    scale               - the number after the decimal.
    max_precision_width - the width of digits before the decimal.
    max_scale_width     - the width of digits after the decimal.

SOURCE

    public this(long precision, ulong scale, uint max_precision_width, uint max_scale_width) {
        uint max_width = 18;

        // Make sure the max_precision_width is not too big
        if(max_precision_width > max_width) {
            throw new Exception("The max_precision_width of '" ~ 
                to_s(max_precision_width) ~ "' is bigger than '" ~ 
                to_s(max_width) ~ "' the max width.");
        }

        // Make sure the max_scale_width is not too big
        if(max_scale_width > max_width) {
            throw new Exception("The max_scale_width of '" ~ 
                to_s(max_scale_width) ~ "' is bigger than '" ~ 
                to_s(max_width) ~ "' the max width.");
        }

        //// Convert the scale to its full format
        //scale = to_ulong(ljust(to_s(scale), 18, "0"));

        // Make sure the max_precision_width is not zero
        if(max_precision_width == 0) {
            throw new Exception("The max_precision_width cannot be zero.");
        }

        // Make sure the max_scale_width is not zero
        if(max_scale_width == 0) {
            throw new Exception("The max_scale_width cannot be zero.");
        }

        // Make sure the value will fit in the max_precision_width
        if(to_s(precision).length > max_precision_width) {
            throw new Exception("The value '" ~ to_s(precision) ~ 
            "' will not fit in the max_precision_width '" ~ to_s(max_precision_width) ~ "'.");
        }

        /// Make sure the value will fit in the max_scale_width
        //if(to_s(scale).length > max_scale_width) {
        //  throw new Exception("The value '" ~ to_s(scale) ~ 
        //  "' will not fit in the max_scale_width '" ~ to_s(max_scale_width) ~ "'.");
        //}

        _precision = precision;
        _scale = scale;
        _max_precision_width = max_precision_width;
        _max_scale_width = max_scale_width;
    }

language_helper/FixedPoint.toDouble [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    The number converted to a double.

SOURCE

    public double toDouble() {
        return to_double(to_s(this));
    }

language_helper/FixedPoint.toLong [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    The number converted to a long.

SOURCE

    public long toLong() {
        return cast(long) this.toDouble();
    }

language_helper/FixedPoint.toString [ Methods ]

[ Top ] [ Methods ]

FUNCTION

    The number converted to a string.

SOURCE

    public string toString() {
        string retval = to_s(_precision) ~ "." ~ stripr(rjust(to_s(_scale), 18, "0"), "0");
        if(ends_with(retval, "."))
            retval ~= "0";
        return retval;
    }

language_helper/index [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns the index of the first match in the value.
    Scans from left to right.
    Returns the length of the value when no match is found.

INPUTS

    value   - the string to look in.
    match   - the part of the string to find.
    start   - the index to start at. The default is zero.

SOURCE

public size_t index(string value, string match, size_t start=0) {
    return tango.text.Util.index!(char)(value, match, start);
}

language_helper/join [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns the values joined together with the separator between them.

INPUTS

    values      - the strings to join.
    separator   - the part of the string to remove.

SOURCE

public string join(string[] values, string separator) {
    return tango.text.Util.join(values, separator);
}

language_helper/json_to_dict( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Converts a json string into a Dictionary.

SOURCE

public void json_to_dict(ref Dictionary dict, string json_in_a_string) {
    auto json = new Json!(char);
    json.parse(json_in_a_string);
    json_to_dict(dict, json.value());
}

language_helper/json_to_dict( value ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Converts a json value into a Dictionary.

SOURCE

public void json_to_dict(ref Dictionary dict, Json!(char).Value value) {
    switch(value.type) {
        case Json!(char).Type.Null:
            dict.value = to_s("null");
            break;
        case Json!(char).Type.String:
            dict.value = to_s(value.toString());
            break;
        case Json!(char).Type.RawString:
            dict.value = to_s(value.toString());
            break;
        case Json!(char).Type.True:
            dict.value = to_s(value.toBool());
            break;
        case Json!(char).Type.False:
            dict.value = to_s(value.toBool());
            break;
        case Json!(char).Type.Number:
            dict.value = to_s(value.toNumber());
            break;
        case Json!(char).Type.Object:
            foreach(string sub_key, Json!(char).Value sub_value ; value.toObject.attributes()) {
                Dictionary d = dict[sub_key];
                json_to_dict(d, sub_value);
            }
            break;
        case Json!(char).Type.Array:
            foreach(Json!(char).Value sub_value ; value.toArray()) {
                size_t i = dict.array_items.length;
                Dictionary d = dict[i];
                json_to_dict(d, sub_value);
            }
            break;
        default:
            throw new Exception("Unknown json type.");
    }
}

language_helper/ljust [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns the value justified to the left.

INPUTS

    value         - the value to pad.
    width         - the width on the returned string.
    pad_char      - the character to pad the string with. The default is "".

SOURCE

public string ljust(string value, uint width, string pad_char=" ") {
    int len = value.length;
    string retval = new char[width];
    tango.text.Util.repeat(pad_char, width, retval);
    retval[0 .. len] = value;
    return retval;
}

language_helper/pair [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Splits the string in half, and returns the two strings in an array.
    Returns true if the separator was found, or false if not.

INPUTS

    value       - the string to split.
    separator   - the string that is between the two returned strings.

SOURCE

public bool pair(string value, string separator, ref string[] pair) {
    size_t i = index(value, separator);
    if(i == value.length)
        return false;

    pair[0] = value[0 .. i];
    pair[1] = value[i+separator.length .. length];

    return true;
}

unittest {
    describe("language_helper#pair", 
        it("Should split the string in two parts", function() {
            string[] _pair = new string[2];
            assert(pair("abc", "b", _pair));
            assert(_pair == ["a", "c"]);
        }),
        it("Should not split when the separator is not found", function() {
            string[] _pair = new string[2];
            assert(!pair("abc", "z", _pair));
            assert(_pair == ["", ""]);
        })
    );
}

language_helper/pow 1 [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns x to the power of n.

INPUTS

    x     - the number.
    n     - the exponent.

RESULT

    n^x

EXAMPLE

    double result = pow(1.5d, 7);

SOURCE

public double pow(double x, int n) {
    return tango.math.Math.pow(cast(real) x, n);
}

language_helper/pow 2 [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns x to the power of n.

INPUTS

    x     - the number.
    n     - the exponent.

RESULT

    n^x

EXAMPLE

    int result = pow(2, 32);

SOURCE

public int pow(int x, int n) {
    return cast(int) tango.math.Math.pow(cast(real) x, n);
}

language_helper/pow 3 [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns x to the power of n.

INPUTS

    x     - the number.
    n     - the exponent.

RESULT

    n^x

EXAMPLE

    int result = pow(2, 32);

SOURCE

public int pow(int x, uint n) {
    return cast(int) tango.math.Math.pow(cast(real) x, n);
}

language_helper/rindex [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns the index of the first match in the value. 
    Scans from right to left.
    Returns the length of the value when no match is found.

INPUTS

    value   - the string to look in.
    match   - the part of the string to find.

SOURCE

public size_t rindex(string value, string match) {
    return tango.text.Util.rindex!(char)(value, match);
}

language_helper/rjust [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns the value justified to the right.

INPUTS

    value         - the value to pad.
    width         - the width on the returned string.
    pad_char      - the character to pad the string with. The default is "".

SOURCE

public string rjust(string value, uint width, string pad_char=" ") {
    int len = width - value.length;
    string retval = new char[width];
    tango.text.Util.repeat(pad_char, width, retval);
    retval[len .. length] = value;
    return retval;
}

language_helper/split [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns the value split with the separator

INPUTS

    value       - the string to split.
    separator   - the string that splits the value.

SOURCE

public string[] split(string value, string separator) {
    string[] retval = new string[count(value, separator)+1];
    size_t start = 0;
    size_t value_length = value.length;
    size_t separator_length = separator.length;
    size_t i, j;

    while(true) {
        // Get the location of the next split
        i = index(value, separator, start);

        // If there are no more splits, add the last string
        if(i == value_length) {
            retval[j] = value[start .. value_length];
            break;
        }

        // Add the next string
        retval[j] = value[start .. i];
        start = i + separator_length;
        j++;
    }

    return retval;
}

unittest {
    describe("language_helper#split", 
        it("Should return the value if the separator is blank", function() {
            assert(split("abc", "") == ["abc"]);
        }),
        it("Should return the value if the separator is not found", function() {
            assert(split("abc", "z") == ["abc"]);
        }),
        it("Should split the middle", function() {
            assert(split("abc", "b") == ["a", "c"]);
        }),
        it("Should split the tail", function() {
            assert(split("abc", "a") == ["", "bc"]);
        }),
        it("Should split the head", function() {
            assert(split("abc", "c") == ["ab", ""]);
        }),
        it("Should split multiple strings when multiple separators are found", function() {
            assert(split("abababababa", "b") == ["a", "a", "a", "a", "a", "a"]);
        })
    );
}

language_helper/split_lines [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns the value split with "\r\n"

INPUTS

    value   - the string to split.

SOURCE

public string[] split_lines(string value) {
    return split(value, "\r\n");
}

language_helper/starts_with [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns true if the value starts with the match.

INPUTS

    value      - the string to examine.
    match      - the string to look for at the start.

SOURCE

public bool starts_with(string value, string match) {
    if(value is null || match is null)
        return false;

    if(value.length < match.length)
        return false;

    return value[0 .. match.length] == match;
}

unittest {
    describe("language_helper#starts_with", 
        it("Should return false on null arguemnts", function() {
            assert(!starts_with(null, null));
        }),
        it("Should return true on blank match", function() {
            assert(starts_with("abc", ""));
        }),
        it("Should return true if the start matches", function() {
            assert(starts_with("abc", "a"));
        }),
        it("Should return false if the start does not match", function() {
            assert(!starts_with("abc", "b"));
        })
    );
}

language_helper/string [ Classes ]

[ Top ] [ Classes ]

FUNCTION

    An alias to the D type char[]

EXAMPLE

    string name = "bobrick";

SOURCE

public alias char[] string;

language_helper/strip [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns the value with the match removed from the ends.

INPUTS

    value   - the string to split.
    match   - the part of the string to remove.

SOURCE

public string strip(string value, string match) {
    value = stripr(value, match);
    value = stripl(value, match);

    return value;
}

language_helper/stripl [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns the value with the match removed from the left.

INPUTS

    value   - the string to split.
    match   - the part of the string to remove.

SOURCE

//FIXME: Reaname to lstrip
public string stripl(string value, string match) {
    if(value == null || value.length == 0)
        return value;

    if(match == null || match.length == 0)
        return value;

    while(starts_with(value, match)) {
        value = value[match.length .. length];
    }

    return value;
}

unittest {
    describe("language_helper#strip", 
        it("Should return null on null arguemnts", function() {
            assert(strip(null, null) == null);
        }),
        it("Should return the value if the match is not found", function() {
            assert(strip("abc", "") == "abc");
        }),
        it("Should strip matches from the sides", function() {
            assert(strip("  abc  ", " ") == "abc");
        }),
        it("Should ignore other white space on space strip", function() {
            assert(strip(" abc\t ", " ") == "abc\t");
        })
    );
}

language_helper/stripr [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns the value with the match removed from the right.

INPUTS

    value   - the string to split.
    match   - the part of the string to remove.

SOURCE

//FIXME: Reaname to rstrip
public string stripr(string value, string match) {
    if(value == null || value.length == 0)
        return value;

    if(match == null || match.length == 0)
        return value;

    while(ends_with(value, match)) {
        size_t start = value.length - match.length;
        value = value[0 .. start];
    }

    return value;
}

language_helper/substitute [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Substitute a part of a string.

INPUTS

    value   - the string to look in.
    before  - the part of the string to replace.
    after   - the string that will replace the matches.

SOURCE

public string substitute(string value, string before, string after) {
    return tango.text.Util.substitute(value, before, after);
}

language_helper/to_bool( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a bool converted to a string.

SOURCE

public bool to_bool(string value) {
    return value=="true" || value=="1";
}

language_helper/to_double( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a double converted to a string.

SOURCE

public double to_double(string value) {
    return tango.text.convert.Float.parse(value);
}

language_helper/to_FixedPoint( double ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a double converted to a FixedPoint.

SOURCE

public FixedPoint to_FixedPoint(double value) {
    return to_FixedPoint(to_s(value));
}

language_helper/to_FixedPoint( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a string converted to a FixedPoint.

SOURCE

public FixedPoint to_FixedPoint(string value) {
    long precision = 0;
    ulong scale = 0;
    string[] pair = split(value, ".");
    precision = to_long(pair[0]);
    if(pair.length == 2) {
        scale =  to_ulong(stripl(ljust(pair[1], 18, "0"), "0"));
    }
    return new FixedPoint(precision, scale, 18, 18);
}
/*
unittest {
    describe("language_helper#to_FixedPoint( string )", 
        it("Should ...", function() {
            auto z = to_FixedPoint("1.1");
            assert(to_s(z) == "1.1");
        }),
        it("Should ...", function() {
            assert(to_s(to_FixedPoint("1.1")) == "1.1");
        })
    );
}
*/

language_helper/to_float( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a float converted to a string.

SOURCE

public float to_float(string value) {
    return tango.text.convert.Float.toFloat(value);
}

language_helper/to_int( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns an int converted to a string.

SOURCE

public int to_int(string value) {
    return tango.text.convert.Integer.toInt(value);
}

language_helper/to_long( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a long converted to a string.

SOURCE

public long to_long(string value) {
    return tango.text.convert.Integer.toLong(value);
}

language_helper/to_real( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a real converted to a string.

SOURCE

public real to_real(string value) {
    return tango.text.convert.Float.parse(value);
}

language_helper/to_s( bool ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a bool converted to a string.

SOURCE

public string to_s(bool value) {
    return value ? "true" : "false";
}

language_helper/to_s( char ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a char converted to a string.

SOURCE

public string to_s(char value) {
    string new_value;
    new_value ~= value;
    return new_value;
}

language_helper/to_s( double ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a double converted to a string.

SOURCE

public string to_s(double value) {
    string retval = tango.text.convert.Float.toString(value);
    if(count(retval, ".") == 0)
        retval ~= ".0";
    return retval;
}

language_helper/to_s( FixedPoint ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a FixedPoint converted to a string.

SOURCE

public string to_s(FixedPoint value) {
    if(value)
        return value.toString();
    else
        return "0.0";
}

language_helper/to_s( float ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a float converted to a string.

EXAMPLE

    to_s(6.5); "6.5"
    to_s(6.0); "6.0"

SOURCE

public string to_s(float value) {
    string retval = tango.text.convert.Float.toString(value);
    if(count(retval, ".") == 0)
        retval ~= ".0";
    return retval;
}

unittest {
    describe("language_helper#to_s", 
        it("Should include the point", function() {
            assert(to_s(6.7f) == "6.7", to_s(6.7f) ~ "!= 6.7");
            assert(to_s(6.0f) == "6.0");
        })
    );
}

language_helper/to_s( int ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns an int converted to a string.

SOURCE

public string to_s(int value) {
    return tango.text.convert.Integer.toString(value);
}

language_helper/to_s( long ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a long converted to a string.

SOURCE

public string to_s(long value) {
    return tango.text.convert.Integer.toString(value);
}

language_helper/to_s( real ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a real converted to a string.

SOURCE

public string to_s(real value) {
    string retval = tango.text.convert.Float.toString(value);
    if(count(retval, ".") == 0)
        retval ~= ".0";
    return retval;
}

language_helper/to_s( short ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a short converted to a string.

SOURCE

public string to_s(short value) {
    return tango.text.convert.Integer.toString(value);
}

language_helper/to_s( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a string converted to a string.

SOURCE

public string to_s(string value) {
    return value.dup;
}

language_helper/to_s( uint ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns an uint converted to a string.

SOURCE

public string to_s(uint value) {
    return tango.text.convert.Integer.toString(value);
}

language_helper/to_s( ulong ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns an ulong converted to a string.

SOURCE

public string to_s(ulong value) {
    char[66] tmp = void;
    return tango.text.convert.Integer.format(tmp, cast(long)value, "u").dup;
}

language_helper/to_s( ushort ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns an ushort converted to a string.

SOURCE

public string to_s(ushort value) {
    return tango.text.convert.Integer.toString(value);
}

language_helper/to_short( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns a short converted to a string.

SOURCE

public short to_short(string value) {
    return cast(short) tango.text.convert.Integer.convert(value);
}

language_helper/to_uint( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns an uint converted to a string.

SOURCE

public uint to_uint(string value) {
    return cast(uint) tango.text.convert.Integer.convert(value);
}

language_helper/to_ulong( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns an ulong converted to a string.

SOURCE

public ulong to_ulong(string value) {
    return tango.text.convert.Integer.convert(value);
}

language_helper/to_ushort( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns an ushort converted to a string.

SOURCE

public ushort to_ushort(string value) {
    return cast(ushort) tango.text.convert.Integer.convert(value);
}

language_helper/trim [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Returns the value with white space removed from the ends.

INPUTS

    value       - the string to trim.

SOURCE

public string trim(string value) {
    return tango.text.Util.trim(value);
}

language_helper/xml_to_dict( node ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Converts an xml node into a Dictionary.

SOURCE

public void xml_to_dict(ref Dictionary dict, Document!(char).Node node) {
    switch(node.type) {
        case XmlNodeType.Data:
            dict.value = to_s(node.value);
        case XmlNodeType.Attribute:
        case XmlNodeType.CData:
        case XmlNodeType.Comment:
        case XmlNodeType.PI:
        case XmlNodeType.Doctype:
            Dictionary d = dict[node.name];
            foreach(child ; node.children) {
                xml_to_dict(d, child);
            }
            break;
        case XmlNodeType.Document:
            // use dict as the root element
            foreach(child ; node.children) {
                xml_to_dict(dict, child);
            }
            break;
        case XmlNodeType.Element:
            Dictionary d = null;
            // Array
            foreach(attribute ; node.attributes) {
                if(attribute.name == "type" && attribute.value == "array") {
                    size_t i = dict.array_items.length;
                    d = dict[node.name][i];
                }
            }

            // Object
            if(d is null)
                d = dict[node.name];
            foreach(child ; node.children) {
                xml_to_dict(d, child);
            }
            break;
        default:
            throw new Exception("Unknown xml type.");
    }
}

language_helper/xml_to_dict( string ) [ Functions ]

[ Top ] [ Functions ]

FUNCTION

    Converts an xml string into a Dictionary.

SOURCE

public void xml_to_dict(ref Dictionary dict, string xml_in_a_string) {
    auto doc = new Document!(char);
    doc.parse(xml_in_a_string);
    xml_to_dict(dict, doc.tree);
}