view dwt/dwthelper/utils.d @ 78:b00303c762ac

successfully linked
author Frank Benoit <benoit@tionex.de>
date Tue, 05 Feb 2008 20:28:19 +0100
parents 66203354c9d3
children 67d24430822a
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{
}
abstract class ValueWrapper{
}

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

class ValueWrapperT(T) : ValueWrapper {
    public T value;
    public this( T data ){
        value = data;
    }
}

alias ValueWrapperT!(bool)    ValueWrapperBool;
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];
}
bool CharacterIsWhitespace( dchar c ){
    return tango.text.Unicode.isWhitespace( c );
}
bool CharacterIsDigit( dchar c ){
    return tango.text.Unicode.isDigit( c );
}

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){
    return indexOf( str, ch, 0 );
}

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 ];
}

public void getChars( char[] src, int srcBegin, int srcEnd, char[] dst, int dstBegin){
    dst[ dstBegin .. dstBegin + srcEnd - srcBegin ] = src[ srcBegin .. srcEnd ];
}

public wchar[] toCharArray( char[] str ){
    return toString16( str );
}

public bool endsWith( char[] src, char[] pattern ){
    if( src.length < pattern.length ){
        return false;
    }
    return src[ $-pattern.length .. $ ] == pattern;
}

public char[] toLowerCase( char[] src ){
    return tango.text.Unicode.toLower( src );
}

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( Exception e ){
        super(e.toString);
    }
}

struct GCStats {
    size_t poolsize;        // total size of pool
    size_t usedsize;        // bytes allocated
    size_t freeblocks;      // number of blocks marked FREE
    size_t freelistsize;    // total of memory on free lists
    size_t pageblocks;      // number of blocks marked PAGE
}
extern(C) GCStats gc_stats();
size_t RuntimeTotalMemory(){
    GCStats s = gc_stats();
    return s.poolsize;
}

}