Andrei Alexandrescu 2011-01-09 21:44:03 +00:00
parent 2c8d824173
commit 6462d38a07
2 changed files with 94 additions and 3 deletions

View file

@ -3225,6 +3225,35 @@ unittest {
assert(skipOver(s1, "Hell") && s1 == "o world"); assert(skipOver(s1, "Hell") && s1 == "o world");
} }
/* (Not yet documented.)
Consume all elements from $(D r) that are equal to one of the elements
$(D es).
*/
void skipAll(alias pred = "a == b", R, Es...)(ref R r, Es es)
//if (is(typeof(binaryFun!pred(r1.front, es[0]))))
{
loop:
for (; !r.empty; r.popFront())
{
foreach (i, E; Es)
{
if (binaryFun!pred(r.front, es[i]))
{
continue loop;
}
}
break;
}
}
unittest
{
//scope(success) writeln("unittest @", __FILE__, ":", __LINE__, " done.");
auto s1 = "Hello world";
skipAll(s1, 'H', 'e');
assert(s1 == "llo world");
}
/** /**
The reciprocal of $(D startsWith). The reciprocal of $(D startsWith).

View file

@ -734,7 +734,8 @@ private T parseString(T)(const(char)[] v)
Array-to-array conversion (except when target is a string type) Array-to-array conversion (except when target is a string type)
converts each element in turn by using $(D to). converts each element in turn by using $(D to).
*/ */
T toImpl(T, S)(S src) if (isDynamicArray!(S) && isArray!(T) && !isSomeString!(T) T toImpl(T, S)(S src) if (isDynamicArray!(S) && isArray!(T)
&& !isSomeString!(S) && !isSomeString!(T)
&& !implicitlyConverts!(S, T)) && !implicitlyConverts!(S, T))
{ {
alias typeof(T.init[0]) E; alias typeof(T.init[0]) E;
@ -1269,10 +1270,10 @@ unittest
Target parse(Target, Source)(ref Source p) Target parse(Target, Source)(ref Source p)
if (isInputRange!Source && /*!isSomeString!Source && */isFloatingPoint!Target) if (isInputRange!Source && /*!isSomeString!Source && */isFloatingPoint!Target)
{ {
static immutable real negtab[] = static immutable real negtab[14] =
[ 1e-4096L,1e-2048L,1e-1024L,1e-512L,1e-256L,1e-128L,1e-64L,1e-32L, [ 1e-4096L,1e-2048L,1e-1024L,1e-512L,1e-256L,1e-128L,1e-64L,1e-32L,
1e-16L,1e-8L,1e-4L,1e-2L,1e-1L,1.0L ]; 1e-16L,1e-8L,1e-4L,1e-2L,1e-1L,1.0L ];
static immutable real postab[] = static immutable real postab[13] =
[ 1e+4096L,1e+2048L,1e+1024L,1e+512L,1e+256L,1e+128L,1e+64L,1e+32L, [ 1e+4096L,1e+2048L,1e+1024L,1e+512L,1e+256L,1e+128L,1e+64L,1e+32L,
1e+16L,1e+8L,1e+4L,1e+2L,1e+1L ]; 1e+16L,1e+8L,1e+4L,1e+2L,1e+1L ];
// static immutable string infinity = "infinity"; // static immutable string infinity = "infinity";
@ -1684,6 +1685,67 @@ if (isSomeString!Source && is(Target == typedef))
static assert(0); static assert(0);
} }
private void skipWS(R)(ref R r)
{
skipAll(r, ' ', '\n', '\t', '\r');
}
/**
* Parses an array from a string given the left bracket (default $(D
* '[')), right bracket (default $(D ']')), and element seprator (by
* default $(D ',')).
*/
Target parse(Target, Source)(ref Source s, dchar lbracket = '[', dchar rbracket = ']', dchar comma = ',')
if (isSomeString!Source && isDynamicArray!Target && !isSomeString!Target)
{
Target result;
skipWS(s);
if (s.front != lbracket) return result;
s.popFront();
skipWS(s);
if (s.front == rbracket)
{
s.popFront();
return result;
}
for (;; s.popFront(), skipWS(s))
{
result ~= parse!(ElementType!Target)(s);
skipWS(s);
if (s.front != comma) break;
}
if (s.front == rbracket)
{
s.popFront();
}
return result;
}
unittest
{
int[] a = [1, 2, 3, 4, 5];
auto s = to!string(a);
assert(to!(int[])(s) == a);
}
unittest
{
int[][] a = [ [1, 2] , [3], [4, 5] ];
auto s = to!string(a);
//assert(to!(int[][])(s) == a);
}
unittest
{
int[][][] ia = [ [[1,2],[3,4],[5]] , [[6],[],[7,8,9]] , [[]] ];
char[] s = to!(char[])(ia);
int[][][] ia2;
ia2 = to!(typeof(ia2))(s);
assert( ia == ia2);
}
// Customizable integral parse // Customizable integral parse
// private N parseIntegral(S, N)(ref S s) // private N parseIntegral(S, N)(ref S s)