commit
9e7d57af84
71
src/dfmt.d
71
src/dfmt.d
|
@ -197,6 +197,28 @@ private:
|
||||||
newline();
|
newline();
|
||||||
break;
|
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
|
else
|
||||||
formatStep();
|
formatStep();
|
||||||
}
|
}
|
||||||
|
@ -215,7 +237,7 @@ private:
|
||||||
currentLineLength += currentTokenLength() + 1;
|
currentLineLength += currentTokenLength() + 1;
|
||||||
writeToken();
|
writeToken();
|
||||||
write(" ");
|
write(" ");
|
||||||
writeParens();
|
writeParens(false);
|
||||||
if (current.type != tok!"{" && current.type != tok!";")
|
if (current.type != tok!"{" && current.type != tok!";")
|
||||||
{
|
{
|
||||||
pushIndent();
|
pushIndent();
|
||||||
|
@ -286,7 +308,7 @@ private:
|
||||||
}
|
}
|
||||||
goto binary;
|
goto binary;
|
||||||
case tok!"(":
|
case tok!"(":
|
||||||
writeParens();
|
writeParens(true);
|
||||||
break;
|
break;
|
||||||
case tok!":":
|
case tok!":":
|
||||||
if (!assumeSorted(astInformation.ternaryColonLocations)
|
if (!assumeSorted(astInformation.ternaryColonLocations)
|
||||||
|
@ -403,7 +425,8 @@ private:
|
||||||
else if (current.type == tok!"identifier")
|
else if (current.type == tok!"identifier")
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
if (current.type == tok!"identifier" || isKeyword(current.type))
|
if (current.type == tok!"identifier" || isKeyword(current.type)
|
||||||
|
|| current.type == tok!"@")
|
||||||
write(" ");
|
write(" ");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -488,7 +511,7 @@ private:
|
||||||
popIndent();
|
popIndent();
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeParens()
|
void writeParens(bool space_afterwards)
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert (current.type == tok!"(", str(current.type));
|
assert (current.type == tok!"(", str(current.type));
|
||||||
|
@ -518,7 +541,8 @@ private:
|
||||||
&& isKeyword(tokens[index + 1].type)))
|
&& isKeyword(tokens[index + 1].type)))
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
write(" ");
|
if (space_afterwards)
|
||||||
|
write(" ");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
writeToken();
|
writeToken();
|
||||||
|
@ -542,7 +566,7 @@ private:
|
||||||
immutable l = indentLevel;
|
immutable l = indentLevel;
|
||||||
writeToken(); // switch
|
writeToken(); // switch
|
||||||
write(" ");
|
write(" ");
|
||||||
writeParens();
|
writeParens(true);
|
||||||
if (current.type != tok!"{")
|
if (current.type != tok!"{")
|
||||||
return;
|
return;
|
||||||
if (config.braceStyle == BraceStyle.otbs)
|
if (config.braceStyle == BraceStyle.otbs)
|
||||||
|
@ -597,33 +621,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
|
||||||
{
|
{
|
||||||
|
|
|
@ -10,5 +10,5 @@ void main()
|
||||||
++lines;
|
++lines;
|
||||||
sumLength += line.length;
|
sumLength += line.length;
|
||||||
}
|
}
|
||||||
writeln("Average line length: ", lines ? sumLength / lines:0);
|
writeln("Average line length: ", lines ? sumLength / lines : 0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,7 @@ void main()
|
||||||
{
|
{
|
||||||
immutable interval = tuple(1, 100);
|
immutable interval = tuple(1, 100);
|
||||||
writefln("Guess my target number that is between "
|
writefln("Guess my target number that is between "
|
||||||
~ "%d and %d (inclusive).\n",
|
~ "%d and %d (inclusive).\n", interval[]);
|
||||||
interval[]);
|
|
||||||
immutable target = uniform!"[]"(interval[]);
|
immutable target = uniform!"[]"(interval[]);
|
||||||
foreach (immutable i; sequence!q{n})
|
foreach (immutable i; sequence!q{n})
|
||||||
{
|
{
|
||||||
|
@ -31,6 +30,6 @@ void main()
|
||||||
writeln(" Well guessed.");
|
writeln(" Well guessed.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
writeln(answer < target ? " Too low.":" Too high.");
|
writeln(answer < target ? " Too low." : " Too high.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import std.stdio, std.math, std.range, std.algorithm, std.numeric, std.traits,
|
import std.stdio, std.math, std.range, std.algorithm, std.numeric, std.traits,
|
||||||
std.typecons;
|
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 s = (a + b + c) / 2.0;
|
||||||
immutable a2 = s * (s - a) * (s - b) * (s - c);
|
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("%3s %8d %3dx%dx%d", t[].hero, t[].only.sum, t[]);
|
||||||
}
|
}
|
||||||
|
|
||||||
writefln("Primitive Heronian triangles with sides up to %d: %d", maxSide,
|
writefln("Primitive Heronian triangles with sides up to %d: %d", maxSide, h.length);
|
||||||
h.length);
|
"\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:"
|
||||||
"\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:".writeln;
|
.writeln;
|
||||||
showTriangles(h.take(10));
|
showTriangles(h.take(10));
|
||||||
"\nAll with area 210 subject to the previous ordering:".writeln;
|
"\nAll with area 210 subject to the previous ordering:".writeln;
|
||||||
showTriangles(h.filter!(t => t[].hero == 210));
|
showTriangles(h.filter!(t => t[].hero == 210));
|
||||||
|
|
Loading…
Reference in New Issue