view dwt/dwthelper/utils.d @ 15:1bea9f0c6f63

FontData, Font
author Frank Benoit <benoit@tionex.de>
date Fri, 25 Jan 2008 19:16:45 +0100
parents 5406a8f6526d
children 5f2e72114476
line wrap: on
line source

/**
 * Authors: Frank Benoit <keinfarbton@googlemail.com>
 */
module dwt.dwthelper.utils;

public import dwt.dwthelper.System;
public import Math = tango.math.Math;

import tango.io.Stdout;
import tango.stdc.stringz;
import tango.text.Util;
import tango.text.Unicode;
import tango.text.convert.Utf;
import tango.core.Exception;
import tango.stdc.stdlib : exit;

void implMissing( char[] file, uint line ){
    Stderr.formatln( "implementation missing in file {} line {}", file, line );
    Stderr.formatln( "exiting ..." );
    exit(1);
}

abstract class ArrayWrapper{
}

class ArrayWrapperT(T) : ArrayWrapper {
    public T[] array;
    public this( T[] data ){
        array = data;
    }
}

alias ArrayWrapperT!(byte)    ArrayWrapperByte;
alias ArrayWrapperT!(int)     ArrayWrapperInt;
alias ArrayWrapperT!(Object)  ArrayWrapperObject;
alias ArrayWrapperT!(char)    ArrayWrapperString;
alias ArrayWrapperT!(char[])  ArrayWrapperString2;

dchar getFirstCodepoint( char[] str ){
    foreach( dchar d; str ){
        return d;
    }
}
dchar CharacterToLower( dchar c ){
    dchar[] r = tango.text.Unicode.toLower( [c] );
    return r[0];
}
dchar CharacterToUpper( dchar c ){
    dchar[] r = tango.text.Unicode.toUpper( [c] );
    return r[0];
}

public int indexOf( char[] str, char searched ){
    int res = tango.text.Util.locate( str, searched );
    if( res is str.length ) res = -1;
    return res;
}

public int indexOf( char[] str, char searched, int startpos ){
    int res = tango.text.Util.locate( str, searched, startpos );
    if( res is str.length ) res = -1;
    return res;
}

public int indexOf(char[] str, char[] ch, int start){
    int res = tango.text.Util.locatePattern( str, ch, start );
    if( res is str.length ) res = -1;
    return res;
}

public char[] substring( char[] str, int start ){
    return str[ start .. $ ].dup;
}

public char[] substring( char[] str, int start, int end ){
    return str[ start .. end ].dup;
}

public wchar[] substring( wchar[] str, int start ){
    return str[ start .. $ ].dup;
}

public wchar[] substring( wchar[] str, int start, int end ){
    return str[ start .. end ].dup;
}

public char charAt( char[] str, int pos ){
    return str[ pos ];
}

static char[] toHex(uint value, bool prefix = true, int radix = 8){
    return tango.text.convert.Integer.toString( 
            value, 
            radix is 10 ? tango.text.convert.Integer.Style.Signed :
            radix is  8 ? tango.text.convert.Integer.Style.Octal  :
            radix is 16 ? tango.text.convert.Integer.Style.Hex    :
                          tango.text.convert.Integer.Style.Signed,
            prefix ? tango.text.convert.Integer.Flags.Prefix : tango.text.convert.Integer.Flags.None
            );
}

class NumberFormatException : IllegalArgumentException {
    this( char[] e ){
        super(e);
    }
    this( TracedException e ){
        super(e.toString);
    }
}