view src/impl/hoofbaby/util/meta.d @ 0:3425707ddbf6

Initial import (hopefully this mercurial stuff works...)
author fraserofthenight
date Mon, 06 Jul 2009 08:06:28 -0700
parents
children
line wrap: on
line source

/**
 * Hoofbaby -- http://www.dsource.org/projects/hoofbaby
 * Copyright (C) 2009 Robert Fraser
 * 
 * This program is free software; you can redistribute it andor
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

module hoofbaby.util.meta;

public char[] ctfeItoa(int n)
{
	bool neg = false;
	char[] r;
	if (n < 0)
	{
		n = -n;
		neg = true;
	}
	do
	{
		r = cast(char)('0' + (n % 10)) ~ r;
	} while ((n /= 10) > 0);
	if(neg)
		r = '-' ~ r;
	return r;
}

public bool ctfeStartsWith(char[] haystack, char[] needle)
{
	for(int i = 0; i < needle.length; i++)
	{
		if(i >= haystack.length || haystack[i] != needle[i])
			return false;
	}
	return true;
}

public char[] ctfeReplace(char[] text, char[] search, char[] rep)
{
	char[] r;
	int slen = search.length;
	int tlen = text.length - slen + 1;
	int lendiff = rep.length - slen;
	int i = 0;
	int j = 0;
	for(i = 0; i < tlen; i++)
	{
		if(text[i .. i + slen] == search)
		{
			r ~= text[j .. i] ~ rep;
			i = j = i + slen;
		}
	}
	r ~= text[j .. $];
	return r;
}