Enable nullable reference types

Closes #36
This commit is contained in:
Patrik Svensson
2020-08-11 17:15:58 +02:00
committed by Patrik Svensson
parent a273f74758
commit 5d132220ba
25 changed files with 98 additions and 72 deletions

View File

@ -45,7 +45,7 @@ namespace Spectre.Console.Internal
return ColorPalette.EightBit[number];
}
public static string GetName(int number)
public static string? GetName(int number)
{
_nameLookup.TryGetValue(number, out var name);
return name;

View File

@ -17,14 +17,9 @@ namespace Spectre.Console.Internal
public static string NormalizeLineEndings(this string text, bool native = false)
{
if (text == null)
{
return null;
}
var normalized = text?.Replace("\r\n", "\n")
?.Replace("\r", string.Empty);
text ??= string.Empty;
var normalized = text?.Replace("\r\n", "\n")?.Replace("\r", string.Empty) ?? string.Empty;
if (native && !_alreadyNormalized)
{
normalized = normalized.Replace("\n", Environment.NewLine);
@ -35,7 +30,8 @@ namespace Spectre.Console.Internal
public static string[] SplitLines(this string text)
{
return text.NormalizeLineEndings().Split(new[] { '\n' }, StringSplitOptions.None);
var result = text?.NormalizeLineEndings()?.Split(new[] { '\n' }, StringSplitOptions.None);
return result ?? Array.Empty<string>();
}
}
}

View File

@ -6,7 +6,7 @@ namespace Spectre.Console.Internal
{
internal static class MarkupParser
{
public static Text Parse(string text, Style style = null)
public static Text Parse(string text, Style? style = null)
{
style ??= Style.Plain;
@ -18,6 +18,10 @@ namespace Spectre.Console.Internal
while (tokenizer.MoveNext())
{
var token = tokenizer.Current;
if (token == null)
{
break;
}
if (token.Kind == MarkupTokenKind.Open)
{

View File

@ -7,7 +7,7 @@ namespace Spectre.Console.Internal
{
private readonly StringBuffer _reader;
public MarkupToken Current { get; private set; }
public MarkupToken? Current { get; private set; }
public MarkupTokenizer(string text)
{

View File

@ -1,10 +1,12 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
namespace Spectre.Console.Internal
{
internal sealed class StringBuffer : IDisposable
{
[SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "False positive")]
private readonly StringReader _reader;
private readonly int _length;

View File

@ -14,16 +14,23 @@ namespace Spectre.Console.Internal
throw new InvalidOperationException(error);
}
if (style == null)
{
// This should not happen, but we need to please the compiler
// which cannot know that style isn't null here.
throw new InvalidOperationException("Could not parse style.");
}
return style;
}
public static bool TryParse(string text, out Style style)
public static bool TryParse(string text, out Style? style)
{
style = Parse(text, out var error);
return error == null;
}
private static Style Parse(string text, out string error)
private static Style? Parse(string text, out string? error)
{
var effectiveDecoration = (Decoration?)null;
var effectiveForeground = (Color?)null;
@ -113,11 +120,11 @@ namespace Spectre.Console.Internal
}
[SuppressMessage("Design", "CA1031:Do not catch general exception types")]
private static Color? ParseHexColor(string hex, out string error)
private static Color? ParseHexColor(string hex, out string? error)
{
error = null;
hex = hex ?? string.Empty;
hex ??= string.Empty;
hex = hex.Replace("#", string.Empty).Trim();
try
@ -151,11 +158,12 @@ namespace Spectre.Console.Internal
}
[SuppressMessage("Design", "CA1031:Do not catch general exception types")]
private static Color? ParseRgbColor(string rgb, out string error)
private static Color? ParseRgbColor(string rgb, out string? error)
{
try
{
error = null;
var normalized = rgb ?? string.Empty;
if (normalized.Length >= 3)
{

View File

@ -40,7 +40,7 @@ namespace Spectre.Console.Internal
return result;
}
public static List<int> Distribute(int total, List<int> ratios, List<int> minimums = null)
public static List<int> Distribute(int total, List<int> ratios, List<int>? minimums = null)
{
if (minimums != null)
{