Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Stromberg committed Mar 28, 2018
2 parents 747c455 + fc92cd0 commit 9ad7596
Show file tree
Hide file tree
Showing 41 changed files with 639 additions and 580 deletions.
6 changes: 3 additions & 3 deletions CommandLine/Builders/ConsoleAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public IConsoleAppErrors ShowErrors()

private void PrintErrors()
{
foreach (var error in _data.Errors)
foreach (string error in _data.Errors)
{
Console.Write("- ");
Console.ForegroundColor = ConsoleColor.Red;
Expand Down Expand Up @@ -174,8 +174,8 @@ private void ShowPerformanceData(Benchmark benchmark)
{
if (_data.DisableOutput) return;

var peakMemoryUsageBytes = MemoryUtilities.GetPeakMemoryUsage();
var wallTimeSpan = benchmark.GetElapsedTime();
long peakMemoryUsageBytes = MemoryUtilities.GetPeakMemoryUsage();
var wallTimeSpan = benchmark.GetElapsedTime();

Console.WriteLine();
if (peakMemoryUsageBytes > 0) Console.WriteLine("Peak memory usage: {0}", MemoryUtilities.ToHumanReadable(peakMemoryUsageBytes));
Expand Down
2 changes: 1 addition & 1 deletion CommandLine/Builders/IConsoleAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface IConsoleAppBuilder

public interface IConsoleAppValidator
{
IConsoleAppValidator DisableOutput(bool condition);
IConsoleAppValidator DisableOutput(bool condition = true);
IConsoleAppBanner ShowBanner(string authors);
IConsoleAppBanner SkipBanner();
IConsoleAppBuilderData Data { get; }
Expand Down
8 changes: 4 additions & 4 deletions CommandLine/Builders/TopLevelAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public ITopLevelAppHelpMenu ShowHelpMenu(string description)
private static void DisplayCommands(Dictionary<string, TopLevelOption> ops)
{
const string label = "COMMAND: ";
string filler = new string(' ', label.Length);
var filler = new string(' ', label.Length);

int commandColumnLen = GetMaxCommandLen(ops.Keys) + 3;
bool useLabel = true;
var useLabel = true;

foreach (var op in ops)
{
Expand All @@ -96,7 +96,7 @@ private static void DisplayCommands(Dictionary<string, TopLevelOption> ops)
}
else Console.Write(filler);

string commandFiller = new string(' ', commandColumnLen - op.Key.Length);
var commandFiller = new string(' ', commandColumnLen - op.Key.Length);
Console.WriteLine(op.Key + commandFiller + op.Value.Description);
}
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public ITopLevelAppErrors ShowErrors()

private void PrintErrors()
{
foreach (var error in _data.Errors)
foreach (string error in _data.Errors)
{
Console.Write("- ");
Console.ForegroundColor = ConsoleColor.Red;
Expand Down
7 changes: 3 additions & 4 deletions CommandLine/Builders/ValidationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static IConsoleAppValidator CheckNonZero(this IConsoleAppValidator valida
public static IConsoleAppValidator CheckEachFilenameExists(this IConsoleAppValidator validator,
IEnumerable<string> filePaths, string description, string commandLineOption, bool isRequired = true)
{
foreach (var filePath in filePaths)
foreach (string filePath in filePaths)
{
validator.CheckInputFilenameExists(filePath, description, commandLineOption, isRequired);
}
Expand All @@ -51,8 +51,7 @@ public static IConsoleAppValidator CheckInputFilenameExists(this IConsoleAppVali
$"The {description} file was not specified. Please use the {commandLineOption} parameter.",
ExitCodes.MissingCommandLineOption);
}
else if (!isRequired || ignoreValue != null && filePath == ignoreValue) { }
else if (!File.Exists(filePath))
else if (isRequired && (ignoreValue == null || filePath != ignoreValue) && !File.Exists(filePath))
{
validator.Data.AddError($"The {description} file ({filePath}) does not exist.", ExitCodes.FileNotFound);
}
Expand Down Expand Up @@ -106,7 +105,7 @@ public static IConsoleAppValidator CheckEachDirectoryContainsFiles(this IConsole
{
if (validator.SkipValidation) return validator;

foreach (var directoryPath in directories)
foreach (string directoryPath in directories)
{
var files = Directory.Exists(directoryPath) ? Directory.GetFiles(directoryPath, searchPattern) : null;
if (files != null && files.Length != 0) continue;
Expand Down
83 changes: 44 additions & 39 deletions CommandLine/NDesk.Options/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ protected static T Parse<T>(string value, OptionContext c)

private OptionValueType ParsePrototype()
{
char type = '\0';
List<string> seps = new List<string>();
for (int i = 0; i < Names.Length; ++i)
var type = '\0';
var seps = new List<string>();
for (var i = 0; i < Names.Length; ++i)
{
string name = Names[i];
if (name.Length == 0)
Expand All @@ -202,12 +202,19 @@ private OptionValueType ParsePrototype()
string.Format("Cannot provide key/value separators for Options taking {0} value(s).", MaxValueCount),
nameof(MaxValueCount));
if (MaxValueCount <= 1) return type == '=' ? OptionValueType.Required : OptionValueType.Optional;
if (seps.Count == 0)
ValueSeparators = new[] { ":", "=" };
else if (seps.Count == 1 && seps[0].Length == 0)
ValueSeparators = null;
else
ValueSeparators = seps.ToArray();

switch (seps.Count)
{
case 0:
ValueSeparators = new[] { ":", "=" };
break;
case 1 when seps[0].Length == 0:
ValueSeparators = null;
break;
default:
ValueSeparators = seps.ToArray();
break;
}

return type == '=' ? OptionValueType.Required : OptionValueType.Optional;
}
Expand Down Expand Up @@ -293,11 +300,11 @@ private void AddImpl(Option option)
{
if (option == null)
throw new ArgumentNullException(nameof(option));
List<string> added = new List<string>(option.Names.Length);
var added = new List<string>(option.Names.Length);
try
{
// KeyedCollection.InsertItem/SetItem handle the 0th name.
for (int i = 1; i < option.Names.Length; ++i)
for (var i = 1; i < option.Names.Length; ++i)
{
Dictionary.Add(option.Names[i], option);
added.Add(option.Names[i]);
Expand Down Expand Up @@ -371,8 +378,8 @@ public List<string> Parse(IEnumerable<string> arguments)
{
OptionContext c = CreateOptionContext();
c.OptionIndex = -1;
bool process = true;
List<string> unprocessed = new List<string>();
var process = true;
var unprocessed = new List<string>();
Option def = Contains("<>") ? this["<>"] : null;
foreach (string argument in arguments)
{
Expand Down Expand Up @@ -439,7 +446,7 @@ private bool Parse(string argument, OptionContext c)
return true;
}

if (!GetOptionParts(argument, out var f, out var n, out var s, out var v))
if (!GetOptionParts(argument, out string f, out string n, out string s, out string v))
return false;

if (!Contains(n)) return ParseBool(argument, n, c) || ParseBundledValue(f, n + s + v, c);
Expand Down Expand Up @@ -499,7 +506,7 @@ private bool ParseBundledValue(string f, string n, OptionContext c)
{
if (f != "-")
return false;
for (int i = 0; i < n.Length; ++i)
for (var i = 0; i < n.Length; ++i)
{
string opt = f + n[i];
string rn = n[i].ToString();
Expand Down Expand Up @@ -545,7 +552,7 @@ public void WriteOptionDescriptions(TextWriter o)
{
foreach (Option p in this)
{
int written = 0;
var written = 0;
if (!WriteOptionPrototype(o, p, ref written))
continue;

Expand All @@ -557,8 +564,8 @@ public void WriteOptionDescriptions(TextWriter o)
o.Write(new string(' ', OptionWidth));
}

bool indent = false;
string prefix = new string(' ', OptionWidth + 2);
var indent = false;
var prefix = new string(' ', OptionWidth + 2);
foreach (string line in GetLines(GetDescription(p.Description)))
{
if (indent)
Expand All @@ -571,7 +578,7 @@ public void WriteOptionDescriptions(TextWriter o)

private static bool WriteOptionPrototype(TextWriter o, Option p, ref int written)
{
string[] names = p.Names;
var names = p.Names;

int i = GetNextOptionIndex(names, 0);
if (i == names.Length)
Expand All @@ -596,26 +603,24 @@ private static bool WriteOptionPrototype(TextWriter o, Option p, ref int written
Write(o, ref written, names[i]);
}

if (p.OptionValueType == OptionValueType.Optional ||
p.OptionValueType == OptionValueType.Required)
if (p.OptionValueType != OptionValueType.Optional && p.OptionValueType != OptionValueType.Required) return true;

Write(o, ref written, " ");
if (p.OptionValueType == OptionValueType.Optional)
{
Write(o, ref written, " ");
if (p.OptionValueType == OptionValueType.Optional)
{
Write(o, ref written, "[");
}
Write(o, ref written, "<" + GetArgumentName(0, p.MaxValueCount, p.Description) + '>');
string sep = p.ValueSeparators != null && p.ValueSeparators.Length > 0
? p.ValueSeparators[0]
: " ";
for (int c = 1; c < p.MaxValueCount; ++c)
{
Write(o, ref written, sep + GetArgumentName(c, p.MaxValueCount, p.Description));
}
if (p.OptionValueType == OptionValueType.Optional)
{
Write(o, ref written, "]");
}
Write(o, ref written, "[");
}
Write(o, ref written, "<" + GetArgumentName(0, p.MaxValueCount, p.Description) + '>');
string sep = p.ValueSeparators != null && p.ValueSeparators.Length > 0
? p.ValueSeparators[0]
: " ";
for (var c = 1; c < p.MaxValueCount; ++c)
{
Write(o, ref written, sep + GetArgumentName(c, p.MaxValueCount, p.Description));
}
if (p.OptionValueType == OptionValueType.Optional)
{
Write(o, ref written, "]");
}
return true;
}
Expand Down Expand Up @@ -663,7 +668,7 @@ private static string GetDescription(string description)
return string.Empty;
StringBuilder sb = StringBuilderCache.Acquire(description.Length);
int start = -1;
for (int i = 0; i < description.Length; ++i)
for (var i = 0; i < description.Length; ++i)
{
switch (description[i])
{
Expand Down
6 changes: 3 additions & 3 deletions CommandLine/Utilities/MemoryUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ public static string ToHumanReadable(long numBytes)
{
if (numBytes > NumBytesInGB)
{
var gigaBytes = numBytes / (double)NumBytesInGB;
double gigaBytes = numBytes / (double)NumBytesInGB;
return $"{gigaBytes:0.000} GB";
}

if (numBytes > NumBytesInMB)
{
var megaBytes = numBytes / (double)NumBytesInMB;
double megaBytes = numBytes / (double)NumBytesInMB;
return $"{megaBytes:0.0} MB";
}

// ReSharper disable once InvertIf
if (numBytes > NumBytesInKB)
{
var kiloBytes = numBytes / (double)NumBytesInKB;
double kiloBytes = numBytes / (double)NumBytesInKB;
return $"{kiloBytes:0.0} KB";
}

Expand Down
6 changes: 3 additions & 3 deletions Compression/DataStructures/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public int Read(Stream stream)

if (_header.IsEmpty) return -1;

var numBytesRead = _header.NumCompressedBytes == -1
int numBytesRead = _header.NumCompressedBytes == -1
? ReadUncompressedBlock(stream)
: ReadCompressedBlock(stream);

Expand All @@ -137,7 +137,7 @@ public int Read(Stream stream)

private int ReadCompressedBlock(Stream stream)
{
var numBytesRead = stream.Read(_compressedBlock, 0, _header.NumCompressedBytes);
int numBytesRead = stream.Read(_compressedBlock, 0, _header.NumCompressedBytes);
if (numBytesRead != _header.NumCompressedBytes)
{
throw new IOException($"Expected {_header.NumCompressedBytes} bytes from the block, but received only {numBytesRead} bytes.");
Expand All @@ -154,7 +154,7 @@ private int ReadCompressedBlock(Stream stream)

private int ReadUncompressedBlock(Stream stream)
{
var numBytesRead = stream.Read(_uncompressedBlock, 0, _header.NumUncompressedBytes);
int numBytesRead = stream.Read(_uncompressedBlock, 0, _header.NumUncompressedBytes);
if (numBytesRead != _header.NumUncompressedBytes)
{
throw new IOException($"Expected {_header.NumUncompressedBytes} bytes from the uncompressed block, but received only {numBytesRead} bytes.");
Expand Down
6 changes: 3 additions & 3 deletions Compression/FileHandling/BgzipTextReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public string ReadLine()
FillBuffer();

if (_bufferLength == 0) return null;
var startIndex = _bufferIndex;
int startIndex = _bufferIndex;

while (_buffer[_bufferIndex++] != _newLineChar)
{
Expand All @@ -60,13 +60,13 @@ public string ReadLine()
? Encoding.UTF8.GetString(SubArray(_buffer, startIndex, _bufferIndex - startIndex - 1))
: Encoding.UTF8.GetString(SubArray(_buffer, startIndex, _bufferIndex - startIndex)));

var result = StringBuilderCache.GetStringAndRelease(sb);
string result = StringBuilderCache.GetStringAndRelease(sb);
return result.Length == 0 ? null : result;
}

private static T[] SubArray<T>(T[] data, int index, int length)
{
T[] result = new T[length];
var result = new T[length];
Array.Copy(data, index, result, 0, length);
return result;
}
Expand Down
30 changes: 11 additions & 19 deletions Compression/FileHandling/BgzipTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ public sealed class BgzipTextWriter : StreamWriter, IDisposable

public long Position => _bgzipStream.Position + _bufferIndex;

public BgzipTextWriter(string path) : this(new BlockGZipStream(FileUtilities.GetCreateStream(path),
CompressionMode.Compress))
{}


public BgzipTextWriter(string path) : this(new BlockGZipStream(FileUtilities.GetCreateStream(path), CompressionMode.Compress))
private BgzipTextWriter(BlockGZipStream bgzipStream) : base(Console.OpenStandardError())
{
}
// the stream writer needs to have a stream but we cannot provide it with
private BgzipTextWriter(BlockGZipStream bgzipStream):base(Console.OpenStandardError())
{
_buffer = new byte[BufferSize];//4kb blocks
_buffer = new byte[BufferSize];
_bgzipStream = bgzipStream;
}

Expand All @@ -34,20 +32,14 @@ public override void Flush()
_bufferIndex = 0;
}

public override void WriteLine()
{
Write("\n");
}
public override void WriteLine() => Write("\n");

public override void WriteLine(string s)
{
Write(s+"\n");
}
public override void WriteLine(string value) => Write(value + "\n");

public override void Write(string line)
public override void Write(string value)
{
if (string.IsNullOrEmpty(line)) return;
var lineBytes = Encoding.UTF8.GetBytes(line);
if (string.IsNullOrEmpty(value)) return;
var lineBytes = Encoding.UTF8.GetBytes(value);

if (lineBytes.Length <= BufferSize - _bufferIndex)
{
Expand All @@ -58,7 +50,7 @@ public override void Write(string line)
{
// fill up the buffer
Array.Copy(lineBytes, 0, _buffer, _bufferIndex, BufferSize - _bufferIndex);
var lineIndex = BufferSize - _bufferIndex;
int lineIndex = BufferSize - _bufferIndex;

// write it out to the stream
_bgzipStream.Write(_buffer, 0, BufferSize);
Expand Down
Loading

0 comments on commit 9ad7596

Please sign in to comment.