From 2d218f234b1df336d34258e9aeaf30a588044a10 Mon Sep 17 00:00:00 2001 From: Andreas Zwinkau Date: Fri, 16 Jan 2015 21:34:15 +0100 Subject: [PATCH] refactor: extract function 'tokenLength' Seems to fix a bug where the length was incorrectly calculated, since tests/guessnumber.d output changed to something saner. --- src/dfmt.d | 35 +++++++++++++++++++---------------- tests/guessnumber.d.ref | 4 ++-- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/dfmt.d b/src/dfmt.d index c2c0f91..c8c47c2 100644 --- a/src/dfmt.d +++ b/src/dfmt.d @@ -599,33 +599,36 @@ private: newline(); } - int currentTokenLength() - { - switch (current.type) - { - mixin (generateFixedLengthCases()); - default: return cast(int) current.text.length; - } - } - - int nextTokenLength() + int tokenLength(size_t i) pure @safe @nogc { import std.algorithm : countUntil; - if (index + 1 >= tokens.length) - return INVALID_TOKEN_LENGTH; - auto nextToken = tokens[index + 1]; - switch (nextToken.type) + assert (i+1 <= tokens.length); + switch (tokens[i].type) { case tok!"identifier": case tok!"stringLiteral": case tok!"wstringLiteral": 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()); - 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 in { diff --git a/tests/guessnumber.d.ref b/tests/guessnumber.d.ref index 071f7e4..8304502 100644 --- a/tests/guessnumber.d.ref +++ b/tests/guessnumber.d.ref @@ -3,8 +3,8 @@ import std.stdio, std.random, std.typecons, std.conv, std.string, std.range; void main() { immutable interval = tuple(1, 100); - writefln("Guess my target number that is between " ~ "%d and %d (inclusive).\n", - interval[]); + writefln("Guess my target number that is between " + ~ "%d and %d (inclusive).\n", interval[]); immutable target = uniform!"[]"(interval[]); foreach (immutable i; sequence!q{n}) {