Merge pull request #8 from qznc/master

Bunch of improvements
This commit is contained in:
Brian Schott 2015-01-16 21:28:56 +00:00
commit 9e7d57af84
4 changed files with 56 additions and 30 deletions

View File

@ -197,6 +197,28 @@ private:
newline();
break;
}
else if (current.type == tok!",")
{
// compute length until next , or ;
int length_of_next_chunk = INVALID_TOKEN_LENGTH;
for (size_t i=index+1; i<tokens.length; i++)
{
if (tokens[i].type == tok!"," || tokens[i].type == tok!";")
break;
const len = tokenLength(i);
assert (len >= 0);
length_of_next_chunk += len;
}
assert (length_of_next_chunk > 0);
writeToken();
if (currentLineLength+1+length_of_next_chunk >= config.columnSoftLimit)
{
pushIndent();
newline();
}
else
write(" ");
}
else
formatStep();
}
@ -215,7 +237,7 @@ private:
currentLineLength += currentTokenLength() + 1;
writeToken();
write(" ");
writeParens();
writeParens(false);
if (current.type != tok!"{" && current.type != tok!";")
{
pushIndent();
@ -286,7 +308,7 @@ private:
}
goto binary;
case tok!"(":
writeParens();
writeParens(true);
break;
case tok!":":
if (!assumeSorted(astInformation.ternaryColonLocations)
@ -403,7 +425,8 @@ private:
else if (current.type == tok!"identifier")
{
writeToken();
if (current.type == tok!"identifier" || isKeyword(current.type))
if (current.type == tok!"identifier" || isKeyword(current.type)
|| current.type == tok!"@")
write(" ");
}
else
@ -488,7 +511,7 @@ private:
popIndent();
}
void writeParens()
void writeParens(bool space_afterwards)
in
{
assert (current.type == tok!"(", str(current.type));
@ -518,7 +541,8 @@ private:
&& isKeyword(tokens[index + 1].type)))
{
writeToken();
write(" ");
if (space_afterwards)
write(" ");
}
else
writeToken();
@ -542,7 +566,7 @@ private:
immutable l = indentLevel;
writeToken(); // switch
write(" ");
writeParens();
writeParens(true);
if (current.type != tok!"{")
return;
if (config.braceStyle == BraceStyle.otbs)
@ -597,33 +621,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
{

View File

@ -10,5 +10,5 @@ void main()
++lines;
sumLength += line.length;
}
writeln("Average line length: ", lines ? sumLength / lines:0);
writeln("Average line length: ", lines ? sumLength / lines : 0);
}

View File

@ -4,8 +4,7 @@ void main()
{
immutable interval = tuple(1, 100);
writefln("Guess my target number that is between "
~ "%d and %d (inclusive).\n",
interval[]);
~ "%d and %d (inclusive).\n", interval[]);
immutable target = uniform!"[]"(interval[]);
foreach (immutable i; sequence!q{n})
{
@ -31,6 +30,6 @@ void main()
writeln(" Well guessed.");
break;
}
writeln(answer < target ? " Too low.":" Too high.");
writeln(answer < target ? " Too low." : " Too high.");
}
}

View File

@ -1,7 +1,7 @@
import std.stdio, std.math, std.range, std.algorithm, std.numeric, std.traits,
std.typecons;
double hero(in uint a, in uint b, in uint c) pure nothrow @safe@nogc
double hero(in uint a, in uint b, in uint c) pure nothrow @safe @nogc
{
immutable s = (a + b + c) / 2.0;
immutable a2 = s * (s - a) * (s - b) * (s - c);
@ -39,9 +39,9 @@ void main() /*@safe*/
writefln("%3s %8d %3dx%dx%d", t[].hero, t[].only.sum, t[]);
}
writefln("Primitive Heronian triangles with sides up to %d: %d", maxSide,
h.length);
"\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:".writeln;
writefln("Primitive Heronian triangles with sides up to %d: %d", maxSide, h.length);
"\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:"
.writeln;
showTriangles(h.take(10));
"\nAll with area 210 subject to the previous ordering:".writeln;
showTriangles(h.filter!(t => t[].hero == 210));