Skip to content

Commit

Permalink
Make a few variable declarations const or immutable. (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
jondegenhardt committed Feb 25, 2019
1 parent 828352b commit d43d882
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 61 deletions.
16 changes: 8 additions & 8 deletions common/src/tsv_utils/common/numerics.d
Expand Up @@ -73,7 +73,7 @@ if (isFloatingPoint!T || isIntegral!T)
* - This handles integer values stored in floating point types.
* - Values like NaN and infinity also handled.
*/
auto str = format("%.*f", floatPrecision, num);
immutable str = format("%.*f", floatPrecision, num);
size_t trimToLength = str.length;

if (floatPrecision != 0 && str.length > floatPrecision + 1)
Expand Down Expand Up @@ -615,9 +615,9 @@ body
}
else if (method == QuantileInterpolation.R2)
{
double h1 = data.length * prob + 0.5;
size_t lo = ((h1 - 0.5).ceil.to!long - 1).max(0);
size_t hi = ((h1 + 0.5).to!size_t - 1).min(data.length - 1);
immutable double h1 = data.length * prob + 0.5;
immutable size_t lo = ((h1 - 0.5).ceil.to!long - 1).max(0);
immutable size_t hi = ((h1 + 0.5).to!size_t - 1).min(data.length - 1);
q = (data[lo].to!double + data[hi].to!double) / 2.0;
}
else if (method == QuantileInterpolation.R3)
Expand All @@ -628,7 +628,7 @@ body
* - DMD will sometimes choose the incorrect 0.5 rounding if the calculation
* is done as a single step. The separate calculation of 'h1' avoids this.
*/
double h1 = data.length * prob;
immutable double h1 = data.length * prob;
q = data[h1.lrint.max(1) - 1].to!double;
}
else if ((method == QuantileInterpolation.R4) ||
Expand All @@ -654,12 +654,12 @@ body
}

double h1IntegerPart;
double h1FractionPart = modf(h1, &h1IntegerPart);
size_t lo = (h1IntegerPart - 1.0).to!long.max(0).min(data.length - 1);
immutable double h1FractionPart = modf(h1, &h1IntegerPart);
immutable size_t lo = (h1IntegerPart - 1.0).to!long.max(0).min(data.length - 1);
q = data[lo];
if (h1FractionPart > 0.0)
{
size_t hi = h1IntegerPart.to!long.min(data.length - 1);
immutable size_t hi = h1IntegerPart.to!long.min(data.length - 1);
q += h1FractionPart * (data[hi].to!double - data[lo].to!double);
}
}
Expand Down
4 changes: 2 additions & 2 deletions common/src/tsv_utils/common/utils.d
Expand Up @@ -1575,11 +1575,11 @@ void throwIfWindowsNewlineOnUnix
{
static if (nlWasRemoved)
{
bool hasWindowsLineEnding = line.length != 0 && line[$ - 1] == '\r';
immutable bool hasWindowsLineEnding = line.length != 0 && line[$ - 1] == '\r';
}
else
{
bool hasWindowsLineEnding =
immutable bool hasWindowsLineEnding =
line.length > 1 &&
line[$ - 2] == '\r' &&
line[$ - 1] == '\n';
Expand Down
6 changes: 3 additions & 3 deletions csv2tsv/src/tsv_utils/csv2tsv.d
Expand Up @@ -17,7 +17,7 @@ import std.range;
import std.traits : Unqual;
import std.typecons : Nullable, tuple;

auto helpText = q"EOS
immutable helpText = q"EOS
Synopsis: csv2tsv [options] [file...]
csv2tsv converts comma-separated text (CSV) to tab-separated format (TSV). Records
Expand All @@ -27,7 +27,7 @@ Use '--help-verbose' for details the CSV formats accepted.
Options:
EOS";

auto helpTextVerbose = q"EOS
immutable helpTextVerbose = q"EOS
Synopsis: csv2tsv [options] [file...]
csv2tsv converts CSV (comma-separated) text to TSV (tab-separated) format. Records
Expand Down Expand Up @@ -177,7 +177,7 @@ else
}

Csv2tsvOptions cmdopt;
auto r = cmdopt.processArgs(cmdArgs);
const r = cmdopt.processArgs(cmdArgs);
if (!r[0]) return r[1];
version(LDC_Profile)
{
Expand Down
44 changes: 22 additions & 22 deletions tsv-filter/src/tsv_utils/tsv-filter.d
Expand Up @@ -41,7 +41,7 @@ int main(string[] cmdArgs)
}

TsvFilterOptions cmdopt;
auto r = cmdopt.processArgs(cmdArgs);
const r = cmdopt.processArgs(cmdArgs);
if (!r[0]) return r[1];
version(LDC_Profile)
{
Expand All @@ -57,7 +57,7 @@ int main(string[] cmdArgs)
return 0;
}

auto helpText = q"EOS
immutable helpText = q"EOS
Synopsis: tsv-filter [options] [file...]
Filter tab-delimited files for matching lines via comparison tests against
Expand Down Expand Up @@ -110,7 +110,7 @@ Operators:
EOS";

auto helpTextVerbose = q"EOS
immutable helpTextVerbose = q"EOS
Synopsis: tsv-filter [options] [file...]
Filter lines of tab-delimited files via comparison tests against fields. Multiple
Expand Down Expand Up @@ -148,7 +148,7 @@ Details:
Options:
EOS";

auto helpTextOptions = q"EOS
immutable helpTextOptions = q"EOS
Synopsis: tsv-filter [options] [file...]
Options:
Expand Down Expand Up @@ -348,15 +348,15 @@ void fieldUnaryOptionHandler(
format("Invalid option: '--%s %s'. Zero is not a valid field index.", option, optionVal));
}

size_t zeroBasedIndex = field - 1;
immutable size_t zeroBasedIndex = field - 1;
tests ~= makeFieldUnaryDelegate(fn, zeroBasedIndex);
maxFieldIndex = (zeroBasedIndex > maxFieldIndex) ? zeroBasedIndex : maxFieldIndex;
}

void fieldVsNumberOptionHandler(
ref FieldsPredicate[] tests, ref size_t maxFieldIndex, FieldVsNumberPredicate fn, string option, string optionVal)
{
auto valSplit = findSplit(optionVal, ":");
immutable valSplit = findSplit(optionVal, ":");
if (valSplit[1].length == 0 || valSplit[2].length == 0)
{
throw new Exception(
Expand All @@ -382,15 +382,15 @@ void fieldVsNumberOptionHandler(
throw new Exception(
format("Invalid option: '--%s %s'. Zero is not a valid field index.", option, optionVal));
}
size_t zeroBasedIndex = field - 1;
immutable size_t zeroBasedIndex = field - 1;
tests ~= makeFieldVsNumberDelegate(fn, zeroBasedIndex, value);
maxFieldIndex = (zeroBasedIndex > maxFieldIndex) ? zeroBasedIndex : maxFieldIndex;
}

void fieldVsStringOptionHandler(
ref FieldsPredicate[] tests, ref size_t maxFieldIndex, FieldVsStringPredicate fn, string option, string optionVal)
{
auto valSplit = findSplit(optionVal, ":");
immutable valSplit = findSplit(optionVal, ":");
if (valSplit[1].length == 0 || valSplit[2].length == 0)
{
throw new Exception(
Expand All @@ -416,7 +416,7 @@ void fieldVsStringOptionHandler(
throw new Exception(
format("Invalid option: '--%s %s'. Zero is not a valid field index.", option, optionVal));
}
size_t zeroBasedIndex = field - 1;
immutable size_t zeroBasedIndex = field - 1;
tests ~= makeFieldVsStringDelegate(fn, zeroBasedIndex, value);
maxFieldIndex = (zeroBasedIndex > maxFieldIndex) ? zeroBasedIndex : maxFieldIndex;
}
Expand All @@ -427,7 +427,7 @@ void fieldVsStringOptionHandler(
void fieldVsIStringOptionHandler(
ref FieldsPredicate[] tests, ref size_t maxFieldIndex, FieldVsIStringPredicate fn, string option, string optionVal)
{
auto valSplit = findSplit(optionVal, ":");
immutable valSplit = findSplit(optionVal, ":");
if (valSplit[1].length == 0 || valSplit[2].length == 0)
{
throw new Exception(
Expand All @@ -453,7 +453,7 @@ void fieldVsIStringOptionHandler(
throw new Exception(
format("Invalid option: '--%s %s'. Zero is not a valid field index.", option, optionVal));
}
size_t zeroBasedIndex = field - 1;
immutable size_t zeroBasedIndex = field - 1;
tests ~= makeFieldVsIStringDelegate(fn, zeroBasedIndex, value.to!dstring.toLower);
maxFieldIndex = (zeroBasedIndex > maxFieldIndex) ? zeroBasedIndex : maxFieldIndex;
}
Expand All @@ -462,7 +462,7 @@ void fieldVsRegexOptionHandler(
ref FieldsPredicate[] tests, ref size_t maxFieldIndex, FieldVsRegexPredicate fn, string option, string optionVal,
bool caseSensitive)
{
auto valSplit = findSplit(optionVal, ":");
immutable valSplit = findSplit(optionVal, ":");
if (valSplit[1].length == 0 || valSplit[2].length == 0)
{
throw new Exception(
Expand All @@ -473,7 +473,7 @@ void fieldVsRegexOptionHandler(
Regex!char value;
try
{
auto modifiers = caseSensitive ? "" : "i";
immutable modifiers = caseSensitive ? "" : "i";
field = valSplit[0].to!size_t;
value = regex(valSplit[2], modifiers);
}
Expand All @@ -489,15 +489,15 @@ void fieldVsRegexOptionHandler(
throw new Exception(
format("Invalid option: '--%s %s'. Zero is not a valid field index.", option, optionVal));
}
size_t zeroBasedIndex = field - 1;
immutable size_t zeroBasedIndex = field - 1;
tests ~= makeFieldVsRegexDelegate(fn, zeroBasedIndex, value);
maxFieldIndex = (zeroBasedIndex > maxFieldIndex) ? zeroBasedIndex : maxFieldIndex;
}

void fieldVsFieldOptionHandler(
ref FieldsPredicate[] tests, ref size_t maxFieldIndex, FieldVsFieldPredicate fn, string option, string optionVal)
{
auto valSplit = findSplit(optionVal, ":");
immutable valSplit = findSplit(optionVal, ":");
if (valSplit[1].length == 0 || valSplit[2].length == 0)
{
throw new Exception(
Expand Down Expand Up @@ -530,8 +530,8 @@ void fieldVsFieldOptionHandler(
format("Invalid option: '--%s %s'. Field1 and field2 must be different fields", option, optionVal));
}

size_t zeroBasedIndex1 = field1 - 1;
size_t zeroBasedIndex2 = field2 - 1;
immutable size_t zeroBasedIndex1 = field1 - 1;
immutable size_t zeroBasedIndex2 = field2 - 1;
tests ~= makeFieldVsFieldDelegate(fn, zeroBasedIndex1, zeroBasedIndex2);
maxFieldIndex = max(maxFieldIndex, zeroBasedIndex1, zeroBasedIndex2);
}
Expand All @@ -543,12 +543,12 @@ void fieldFieldNumOptionHandler(
size_t field1;
size_t field2;
double value;
auto valSplit = findSplit(optionVal, ":");
immutable valSplit = findSplit(optionVal, ":");
auto invalidOption = (valSplit[1].length == 0 || valSplit[2].length == 0);

if (!invalidOption)
{
auto valSplit2 = findSplit(valSplit[2], ":");
immutable valSplit2 = findSplit(valSplit[2], ":");
invalidOption = (valSplit2[1].length == 0 || valSplit2[2].length == 0);

if (!invalidOption)
Expand Down Expand Up @@ -583,8 +583,8 @@ void fieldFieldNumOptionHandler(
format("Invalid option: '--%s %s'. Field1 and field2 must be different fields", option, optionVal));
}

size_t zeroBasedIndex1 = field1 - 1;
size_t zeroBasedIndex2 = field2 - 1;
immutable size_t zeroBasedIndex1 = field1 - 1;
immutable size_t zeroBasedIndex2 = field2 - 1;
tests ~= makeFieldFieldNumDelegate(fn, zeroBasedIndex1, zeroBasedIndex2, value);
maxFieldIndex = max(maxFieldIndex, zeroBasedIndex1, zeroBasedIndex2);
}
Expand Down Expand Up @@ -859,7 +859,7 @@ void tsvFilter(in TsvFilterOptions cmdopt, in string[] inputFiles)
if (cmdopt.invert) passed = !passed;
if (passed)
{
bool wasFlushed = bufferedOutput.appendln(line);
const bool wasFlushed = bufferedOutput.appendln(line);
if (wasFlushed) inputLinesWithoutBufferFlush = 0;
else if (inputLinesWithoutBufferFlush > maxInputLinesWithoutBufferFlush)
{
Expand Down

0 comments on commit d43d882

Please sign in to comment.