refactor: extract function 'tokenLength'

Seems to fix a bug where the length was incorrectly calculated,
since tests/guessnumber.d output changed to something saner.
This commit is contained in:
Andreas Zwinkau 2015-01-16 21:34:15 +01:00
parent 34f01d22c4
commit 2d218f234b
2 changed files with 21 additions and 18 deletions

View File

@ -599,33 +599,36 @@ private:
newline(); newline();
} }
int currentTokenLength() int tokenLength(size_t i) pure @safe @nogc
{
switch (current.type)
{
mixin (generateFixedLengthCases());
default: return cast(int) current.text.length;
}
}
int nextTokenLength()
{ {
import std.algorithm : countUntil; import std.algorithm : countUntil;
if (index + 1 >= tokens.length) assert (i+1 <= tokens.length);
return INVALID_TOKEN_LENGTH; switch (tokens[i].type)
auto nextToken = tokens[index + 1];
switch (nextToken.type)
{ {
case tok!"identifier": case tok!"identifier":
case tok!"stringLiteral": case tok!"stringLiteral":
case tok!"wstringLiteral": case tok!"wstringLiteral":
case tok!"dstringLiteral": case tok!"dstringLiteral":
return cast(int) nextToken.text.countUntil('\n'); auto c = cast(int) tokens[i].text.countUntil('\n');
if (c == -1)
return cast(int) tokens[i].text.length;
mixin (generateFixedLengthCases()); mixin (generateFixedLengthCases());
default: return -1; default: return INVALID_TOKEN_LENGTH;
} }
} }
int currentTokenLength() pure @safe @nogc
{
return tokenLength(index);
}
int nextTokenLength() pure @safe @nogc
{
if (index + 1 >= tokens.length)
return INVALID_TOKEN_LENGTH;
return tokenLength(index + 1);
}
ref current() const @property ref current() const @property
in in
{ {

View File

@ -3,8 +3,8 @@ import std.stdio, std.random, std.typecons, std.conv, std.string, std.range;
void main() void main()
{ {
immutable interval = tuple(1, 100); immutable interval = tuple(1, 100);
writefln("Guess my target number that is between " ~ "%d and %d (inclusive).\n", writefln("Guess my target number that is between "
interval[]); ~ "%d and %d (inclusive).\n", interval[]);
immutable target = uniform!"[]"(interval[]); immutable target = uniform!"[]"(interval[]);
foreach (immutable i; sequence!q{n}) foreach (immutable i; sequence!q{n})
{ {