Skip to content

Commit

Permalink
Reorder functions
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Sep 5, 2011
1 parent 3ff3c74 commit 925375d
Showing 1 changed file with 75 additions and 75 deletions.
150 changes: 75 additions & 75 deletions std/format.d
Expand Up @@ -2582,64 +2582,6 @@ private void skipData(Range, Char)(ref Range input, ref FormatSpec!Char spec)
}
}

/**
Reads an array (except for string types) and returns it.
*/
T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
if (isArray!T && !isSomeString!T)
{
auto app = appender!T();
for (;;)
{
auto e = parse!(ElementType!(T))(input);
app.put(e);
if (!std.string.startsWith(input, spec.nested)) break; // done
input = input[spec.nested.length .. $];
if (input.empty) break; // the trailing is terminator, not
// separator
}
return app.data;
}

/**
Reads a string and returns it.
*/
T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
if (isInputRange!Range && isSomeString!T)
{
auto app = appender!T();
if (spec.trailing.empty)
{
for (; !input.empty; input.popFront())
{
app.put(input.front);
}
}
else
{
for (; !input.empty && input.front != spec.trailing.front;
input.popFront())
{
app.put(input.front);
}
}
auto result = app.data;
return result;
}

unittest
{
string s1, s2;
char[] line = "hello, world".dup;
formattedRead(line, "%s", &s1);
assert(s1 == "hello, world", s1);

line = "hello, world;yah".dup;
formattedRead(line, "%s;%s", &s1, &s2);
assert(s1 == "hello, world", s1);
assert(s2 == "yah", s2);
}

private template acceptedSpecs(T)
{
static if (isIntegral!T) enum acceptedSpecs = "sdu";// + "coxX" (todo)
Expand All @@ -2662,23 +2604,6 @@ if (isIntegral!T && isInputRange!Range)
assert(0, "Parsing spec '"~spec.spec~"' not implemented.");
}

/**
* Reads one character.
*/
T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
if (isSomeChar!T && isInputRange!Range)
{
enforce(std.algorithm.find("cdosuxX", spec.spec).length,
text("Wrong character type specifier: `", spec.spec, "'"));
if (spec.spec == 's')
{
auto result = to!T(input.front);
input.popFront();
return result;
}
assert(0, "Parsing spec '"~spec.spec~"' not implemented.");
}

/**
Reads a floating-point value and returns it.
*/
Expand Down Expand Up @@ -2755,6 +2680,81 @@ unittest
assert(t[0] == 1 && t[1] == 2.125);
}

/**
* Reads one character.
*/
T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
if (isSomeChar!T && isInputRange!Range)
{
enforce(std.algorithm.find("cdosuxX", spec.spec).length,
text("Wrong character type specifier: `", spec.spec, "'"));
if (spec.spec == 's')
{
auto result = to!T(input.front);
input.popFront();
return result;
}
assert(0, "Parsing spec '"~spec.spec~"' not implemented.");
}

/**
Reads an array (except for string types) and returns it.
*/
T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
if (isArray!T && !isSomeString!T)
{
auto app = appender!T();
for (;;)
{
auto e = parse!(ElementType!(T))(input);
app.put(e);
if (!std.string.startsWith(input, spec.nested)) break; // done
input = input[spec.nested.length .. $];
if (input.empty) break; // the trailing is terminator, not
// separator
}
return app.data;
}

/**
Reads a string and returns it.
*/
T unformatValue(T, Range, Char)(ref Range input, ref FormatSpec!Char spec)
if (isInputRange!Range && isSomeString!T)
{
auto app = appender!T();
if (spec.trailing.empty)
{
for (; !input.empty; input.popFront())
{
app.put(input.front);
}
}
else
{
for (; !input.empty && input.front != spec.trailing.front;
input.popFront())
{
app.put(input.front);
}
}
auto result = app.data;
return result;
}

unittest
{
string s1, s2;
char[] line = "hello, world".dup;
formattedRead(line, "%s", &s1);
assert(s1 == "hello, world", s1);

line = "hello, world;yah".dup;
formattedRead(line, "%s;%s", &s1, &s2);
assert(s1 == "hello, world", s1);
assert(s2 == "yah", s2);
}

// Legacy implementation

enum Mangle : char
Expand Down

0 comments on commit 925375d

Please sign in to comment.