view dmd/String.d @ 114:e28b18c23469

added a module dmd.common for commonly used stuff it currently holds code for consistency checking of predefined versions also added a VisualD project file
author Trass3r
date Wed, 01 Sep 2010 18:21:58 +0200
parents 2e2a5c3f943a
children e3afd1303184
line wrap: on
line source

module dmd.String;

import dmd.common;
import dmd.Array;

import core.stdc.string : strlen;
import std.string : cmp;

import std.stdio : writefln;

class String
{
    string str;

    this(string str)
	{
		this.str = str;
	}

    static hash_t calcHash(const(char)* str, size_t len)
	{
		hash_t hash = 0;

		for (;;)
		{
			switch (len)
			{
				case 0:
					return hash;

				case 1:
					hash *= 37;
					hash += *cast(ubyte*)str;
					return hash;

				case 2:
					hash *= 37;
					hash += *cast(ushort*)str;
					return hash;

				case 3:
					hash *= 37;
					hash += (*(cast(ushort*)str) << 8) +
						(cast(ubyte*)str)[2];
					return hash;

				default:
					hash *= 37;
					hash += *cast(uint*)str;
					str += 4;
					len -= 4;
					break;
			}
		}
	}
	
    static hash_t calcHash(string str)
	{
		return calcHash(str.ptr, str.length);
	}
	
    hash_t hashCode()
	{
		return calcHash(str.ptr, str.length);
	}
    
	uint len()
	{
		return str.length;
	}

    bool equals(Object obj)
	{
		return str == (cast(String)obj).str;
	}

    override int opCmp(Object obj)
	{
		return cmp(str, (cast(String)obj).str);
	}
	
    string toChars()
	{
		return str;
	}

    void print()
	{
		writefln("String '%s'", str);
	}
}