Skip to content

Instantly share code, notes, and snippets.

@UplinkCoder
Created August 22, 2016 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UplinkCoder/2a8fd659eef9f3a2fdf8032f700bde80 to your computer and use it in GitHub Desktop.
Save UplinkCoder/2a8fd659eef9f3a2fdf8032f700bde80 to your computer and use it in GitHub Desktop.
fixwhitespace - fixes what dmd complains about
/**
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(DMDSRC _checkwhitespace.d)
*/
import std.stdio : writefln;
import std.file;
import std.string;
import std.range;
import std.regex;
import std.algorithm;
import std.path;
int main(string[] args)
{
bool error;
uint checks;
auto r = regex(r" +\n");
foreach(a; args[1..$])
{
try
{
uint line = 1;
ptrdiff_t pos;
auto str = a.readText();
char[] wFile;
ptrdiff_t lastpos;
wFile.reserve(str.length);
while(str.length) {
if ((pos = str.indexOf("\r\n")) >= 0)
{
if (lastpos == pos) break;
lastpos = pos;
line += str[0..pos].count('\n');
writefln("Info - file '%s' did contain windows line endings at line %d", a, line);
error = true;
wFile ~= str[0 .. pos];
wFile ~= "\n";
str = str[pos+2 .. $];
continue;
}
if (a.extension() != ".mak" && (pos = str.indexOf('\t')) >= 0)
{
if (lastpos == pos) break;
lastpos = pos;
line += str[0..pos].count('\n');
writefln("Info - file '%s' did contain tabs at line %d", a, line);
error = true;
wFile ~= str[0 .. pos] ~ " ";
str = str[pos+1 .. $];
while(str[0] == '\t') {wFile ~= " "; str = str[1 .. $];}
continue;
}
auto m = str.matchFirst(r);
if (!m.empty)
{
pos = m.front.ptr - str.ptr; // assume the match is a slice of the string
if (lastpos == pos) break;
lastpos = pos;
line += str[0..pos].count('\n');
writefln("Info - file '%s' did contain trailing whitespace at line %d", a, line);
wFile ~= str[0 .. pos];
str = str[pos + m.front.length-1 .. $];
error = true;
continue;
}
if (checks++ == 3) break;
}
wFile ~= str[0 .. $];
str = str[$ .. $];
a.write(wFile);
}
catch(Exception e)
{
writefln("Exception - file '%s': %s", a, e.msg);
}
}
return error ? 1 : 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment