demangle tool

Michel Fortin michel.fortin at michelf.com
Fri Apr 10 18:56:18 PDT 2009


On 2009-04-10 17:25:30 -0400, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

> Curiously, I didn't find a need for them in std.algorithm so far. I 
> only defined sameHead, and that's only to help a little improvement in 
> the bringToFront algorithm.

I've written a few parsers where I repeat this pattern a lot:

	string read(alias consumer)(ref string input)
	{
		string value = input;
		consumer(input);
		return value.before(input);
	}

And I like very much how it reads: "text before input".

All this done on D1. Perhaps it'd need to be revamped a little for D2. 
Here's my implementation if you're interested.

	T[] after(T)(T[] r, T[] s)
	in {
		assert(s.ptr >= r.ptr && s.ptr <= r.ptr + r.length);
	}
	body {
		char* begin = s.ptr + s.length;
		char* end = r.ptr + r.length;
		return begin[0..end-begin];
	}
	
	T[] before(T)(T[] r, T[] s)
	in {
		assert(s.ptr >= r.ptr && s.ptr <= r.ptr + r.length);
	}
	body {
		char* begin = r.ptr;
		char* end = s.ptr;
		return begin[0..end-begin];
	}

	unittest
	{
		string a = "abcdef";
		string b = a[1..3]; // bc
		assert(a.after(b) == "def");
		assert(a.before(b) == "a");
		
		string c = a[3..3]; // empty string
		assert(a.after(c) == "def");
		assert(a.before(c) == "abc");
		
		string d = a[0..$]; // same string
		assert(a.after(d) == "");
		assert(a.before(d) == "");
	}


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list