Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable nullability #522

Merged
merged 1 commit into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Markdig/Helpers/CharNormalizer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

#nullable enable

using System.Collections.Generic;

namespace Markdig.Helpers
Expand All @@ -18,9 +20,9 @@ public static class CharNormalizer
/// </summary>
/// <param name="c">The input char.</param>
/// <returns>The simple ASCII string or null if the char itself cannot be simplified</returns>
public static string ConvertToAscii(char c)
public static string? ConvertToAscii(char c)
{
return CodeToAscii.TryGetValue(c, out string str) ? str : null;
return CodeToAscii.TryGetValue(c, out string? str) ? str : null;
}

static CharNormalizer()
Expand Down
11 changes: 7 additions & 4 deletions src/Markdig/Helpers/HtmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

#nullable enable

using System;
using System.Text;

Expand Down Expand Up @@ -30,7 +32,8 @@ static HtmlHelper()
}
}
}
public static string EscapeUrlCharacter(char c)

public static string? EscapeUrlCharacter(char c)
{
return c < 128 ? EscapeUrlsForAscii[c] : null;
}
Expand Down Expand Up @@ -431,7 +434,7 @@ private static bool TryParseHtmlTagProcessingInstruction(ref StringSlice text, S
/// <param name="text">The string data that will be changed by unescaping any punctuation or symbol characters.</param>
/// <param name="removeBackSlash">if set to <c>true</c> [remove back slash].</param>
/// <returns></returns>
public static string Unescape(string text, bool removeBackSlash = true)
public static string Unescape(string? text, bool removeBackSlash = true)
{
// Credits: code from CommonMark.NET
// Copyright (c) 2014, Kārlis Gaņģis All rights reserved.
Expand All @@ -446,9 +449,9 @@ public static string Unescape(string text, bool removeBackSlash = true)
int lastPos = 0;
char c;
char[] search = removeBackSlash ? SearchBackAndAmp : SearchAmp;
StringBuilder sb = null;
StringBuilder? sb = null;

while ((searchPos = text.IndexOfAny(search, searchPos)) != -1)
while ((searchPos = text!.IndexOfAny(search, searchPos)) != -1)
{
sb ??= StringBuilderCache.Local();
c = text[searchPos];
Expand Down
54 changes: 28 additions & 26 deletions src/Markdig/Helpers/LinkHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System;
#nullable enable

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text;
using Markdig.Syntax;
Expand All @@ -14,7 +16,7 @@ namespace Markdig.Helpers
/// </summary>
public static class LinkHelper
{
public static bool TryParseAutolink(StringSlice text, out string link, out bool isEmail)
public static bool TryParseAutolink(StringSlice text, [NotNullWhen(true)] out string? link, out bool isEmail)
{
return TryParseAutolink(ref text, out link, out isEmail);
}
Expand Down Expand Up @@ -118,7 +120,7 @@ private static bool IsReservedPunctuation(char c)
return c == '_' || c == '-' || c == '.';
}

public static bool TryParseAutolink(ref StringSlice text, out string link, out bool isEmail)
public static bool TryParseAutolink(ref StringSlice text, [NotNullWhen(true)] out string? link, out bool isEmail)
{
link = null;
isEmail = false;
Expand Down Expand Up @@ -322,22 +324,22 @@ public static bool TryParseAutolink(ref StringSlice text, out string link, out b
return false;
}

public static bool TryParseInlineLink(StringSlice text, out string link, out string title)
public static bool TryParseInlineLink(StringSlice text, out string? link, out string? title)
{
return TryParseInlineLink(ref text, out link, out title, out _, out _);
}

public static bool TryParseInlineLink(StringSlice text, out string link, out string title, out SourceSpan linkSpan, out SourceSpan titleSpan)
public static bool TryParseInlineLink(StringSlice text, out string? link, out string? title, out SourceSpan linkSpan, out SourceSpan titleSpan)
{
return TryParseInlineLink(ref text, out link, out title, out linkSpan, out titleSpan);
}

public static bool TryParseInlineLink(ref StringSlice text, out string link, out string title)
public static bool TryParseInlineLink(ref StringSlice text, out string? link, out string? title)
{
return TryParseInlineLink(ref text, out link, out title, out SourceSpan linkSpan, out SourceSpan titleSpan);
}

public static bool TryParseInlineLink(ref StringSlice text, out string link, out string title, out SourceSpan linkSpan, out SourceSpan titleSpan)
public static bool TryParseInlineLink(ref StringSlice text, out string? link, out string? title, out SourceSpan linkSpan, out SourceSpan titleSpan)
{
// 1. An inline link consists of a link text followed immediately by a left parenthesis (,
// 2. optional whitespace, TODO: specs: is it whitespace or multiple whitespaces?
Expand Down Expand Up @@ -417,9 +419,9 @@ public static bool TryParseInlineLink(ref StringSlice text, out string link, out

public static bool TryParseInlineLinkTrivia(
ref StringSlice text,
out string link,
out string? link,
out SourceSpan unescapedLink,
out string title,
out string? title,
out SourceSpan unescapedTitle,
out char titleEnclosingCharacter,
out SourceSpan linkSpan,
Expand Down Expand Up @@ -521,12 +523,12 @@ public static bool TryParseInlineLinkTrivia(
return isValid;
}

public static bool TryParseTitle<T>(T text, out string title) where T : ICharIterator
public static bool TryParseTitle<T>(T text, out string? title) where T : ICharIterator
{
return TryParseTitle(ref text, out title, out _);
}

public static bool TryParseTitle<T>(ref T text, out string title, out char enclosingCharacter) where T : ICharIterator
public static bool TryParseTitle<T>(ref T text, out string? title, out char enclosingCharacter) where T : ICharIterator
{
bool isValid = false;
var buffer = StringBuilderCache.Local();
Expand Down Expand Up @@ -620,7 +622,7 @@ public static bool TryParseTitle<T>(ref T text, out string title, out char enclo
return isValid;
}

public static bool TryParseTitleTrivia<T>(ref T text, out string title, out char enclosingCharacter) where T : ICharIterator
public static bool TryParseTitleTrivia<T>(ref T text, out string? title, out char enclosingCharacter) where T : ICharIterator
{
bool isValid = false;
var buffer = StringBuilderCache.Local();
Expand Down Expand Up @@ -714,12 +716,12 @@ public static bool TryParseTitleTrivia<T>(ref T text, out string title, out char
return isValid;
}

public static bool TryParseUrl<T>(T text, out string link) where T : ICharIterator
public static bool TryParseUrl<T>(T text, out string? link) where T : ICharIterator
{
return TryParseUrl(ref text, out link, out _);
}

public static bool TryParseUrl<T>(ref T text, out string link, out bool hasPointyBrackets, bool isAutoLink = false) where T : ICharIterator
public static bool TryParseUrl<T>(ref T text, out string? link, out bool hasPointyBrackets, bool isAutoLink = false) where T : ICharIterator
{
bool isValid = false;
hasPointyBrackets = false;
Expand Down Expand Up @@ -859,7 +861,7 @@ public static bool TryParseUrl<T>(ref T text, out string link, out bool hasPoint
return isValid;
}

public static bool TryParseUrlTrivia<T>(ref T text, out string link, out bool hasPointyBrackets, bool isAutoLink = false) where T : ICharIterator
public static bool TryParseUrlTrivia<T>(ref T text, out string? link, out bool hasPointyBrackets, bool isAutoLink = false) where T : ICharIterator
{
bool isValid = false;
hasPointyBrackets = false;
Expand Down Expand Up @@ -1069,7 +1071,7 @@ public static bool IsValidDomain(string link, int prefixLength)
segmentCount - lastUnderscoreSegment >= 2; // No underscores are present in the last two segments of the domain
}

public static bool TryParseLinkReferenceDefinition<T>(ref T text, out string label, out string url, out string title, out SourceSpan labelSpan, out SourceSpan urlSpan, out SourceSpan titleSpan) where T : ICharIterator
public static bool TryParseLinkReferenceDefinition<T>(ref T text, out string? label, out string? url, out string? title, out SourceSpan labelSpan, out SourceSpan urlSpan, out SourceSpan titleSpan) where T : ICharIterator
{
url = null;
title = null;
Expand Down Expand Up @@ -1158,14 +1160,14 @@ public static bool TryParseLinkReferenceDefinition<T>(ref T text, out string lab
public static bool TryParseLinkReferenceDefinitionTrivia<T>(
ref T text,
out SourceSpan triviaBeforeLabel,
out string label,
out string? label,
out SourceSpan labelWithTrivia,
out SourceSpan triviaBeforeUrl, // can contain newline
out string url,
out string? url,
out SourceSpan unescapedUrl,
out bool urlHasPointyBrackets,
out SourceSpan triviaBeforeTitle, // can contain newline
out string title, // can contain non-consecutive newlines
out string? title, // can contain non-consecutive newlines
out SourceSpan unescapedTitle,
out char titleEnclosingCharacter,
out NewLine newLine,
Expand Down Expand Up @@ -1303,32 +1305,32 @@ public static bool TryParseLinkReferenceDefinitionTrivia<T>(
return true;
}

public static bool TryParseLabel<T>(T lines, out string label) where T : ICharIterator
public static bool TryParseLabel<T>(T lines, out string? label) where T : ICharIterator
{
return TryParseLabel(ref lines, false, out label, out SourceSpan labelSpan);
}

public static bool TryParseLabel<T>(T lines, out string label, out SourceSpan labelSpan) where T : ICharIterator
public static bool TryParseLabel<T>(T lines, out string? label, out SourceSpan labelSpan) where T : ICharIterator
{
return TryParseLabel(ref lines, false, out label, out labelSpan);
}

public static bool TryParseLabel<T>(ref T lines, out string label) where T : ICharIterator
public static bool TryParseLabel<T>(ref T lines, out string? label) where T : ICharIterator
{
return TryParseLabel(ref lines, false, out label, out SourceSpan labelSpan);
}

public static bool TryParseLabel<T>(ref T lines, out string label, out SourceSpan labelSpan) where T : ICharIterator
public static bool TryParseLabel<T>(ref T lines, out string? label, out SourceSpan labelSpan) where T : ICharIterator
{
return TryParseLabel(ref lines, false, out label, out labelSpan);
}

public static bool TryParseLabelTrivia<T>(ref T lines, out string label, out SourceSpan labelSpan) where T : ICharIterator
public static bool TryParseLabelTrivia<T>(ref T lines, out string? label, out SourceSpan labelSpan) where T : ICharIterator
{
return TryParseLabelTrivia(ref lines, false, out label, out labelSpan);
}

public static bool TryParseLabel<T>(ref T lines, bool allowEmpty, out string label, out SourceSpan labelSpan) where T : ICharIterator
public static bool TryParseLabel<T>(ref T lines, bool allowEmpty, out string? label, out SourceSpan labelSpan) where T : ICharIterator
{
label = null;
char c = lines.CurrentChar;
Expand Down Expand Up @@ -1443,7 +1445,7 @@ public static bool TryParseLabel<T>(ref T lines, bool allowEmpty, out string lab
return isValid;
}

public static bool TryParseLabelTrivia<T>(ref T lines, bool allowEmpty, out string label, out SourceSpan labelSpan) where T : ICharIterator
public static bool TryParseLabelTrivia<T>(ref T lines, bool allowEmpty, out string? label, out SourceSpan labelSpan) where T : ICharIterator
{
label = null;
char c = lines.CurrentChar;
Expand Down
7 changes: 5 additions & 2 deletions src/Markdig/Helpers/StringLineGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

#nullable enable

using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -30,6 +32,7 @@ public StringLineGroup(int capacity)
Lines = _pool.Rent(capacity);
Count = 0;
}

internal StringLineGroup(int capacity, bool willRelease)
{
if (capacity <= 0) ThrowHelper.ArgumentOutOfRangeException(nameof(capacity));
Expand Down Expand Up @@ -116,7 +119,7 @@ public readonly override string ToString()
/// </summary>
/// <param name="lineOffsets">The position of the `\n` line offsets from the beginning of the returned slice.</param>
/// <returns>A single slice concatenating the lines of this instance</returns>
public readonly StringSlice ToSlice(List<LineOffset> lineOffsets = null)
public readonly StringSlice ToSlice(List<LineOffset>? lineOffsets = null)
{
// Optimization case for a single line.
if (Count == 1)
Expand Down Expand Up @@ -201,7 +204,7 @@ internal void Release()
{
Array.Clear(Lines, 0, Count);
_pool.Return(Lines);
Lines = null;
Lines = null!;
Count = -1;
}

Expand Down
32 changes: 32 additions & 0 deletions src/Markdig/Helpers/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,52 @@ namespace Markdig.Helpers
[ExcludeFromCodeCoverage]
internal static class ThrowHelper
{
[DoesNotReturn]
public static void ArgumentNullException(string paramName) => throw new ArgumentNullException(paramName);

[DoesNotReturn]
public static void ArgumentNullException_item() => throw new ArgumentNullException("item");

[DoesNotReturn]
public static void ArgumentNullException_text() => throw new ArgumentNullException("text");

[DoesNotReturn]
public static void ArgumentNullException_label() => throw new ArgumentNullException("label");

[DoesNotReturn]
public static void ArgumentNullException_key() => throw new ArgumentNullException("key");

[DoesNotReturn]
public static void ArgumentNullException_name() => throw new ArgumentNullException("name");

[DoesNotReturn]
public static void ArgumentNullException_markdown() => throw new ArgumentNullException("markdown");

[DoesNotReturn]
public static void ArgumentNullException_writer() => throw new ArgumentNullException("writer");

[DoesNotReturn]
public static void ArgumentNullException_leafBlock() => throw new ArgumentNullException("leafBlock");

[DoesNotReturn]
public static void ArgumentNullException_markdownObject() => throw new ArgumentNullException("markdownObject");

[DoesNotReturn]
public static void ArgumentException(string message) => throw new ArgumentException(message);

[DoesNotReturn]
public static void ArgumentException(string message, string paramName) => throw new ArgumentException(message, paramName);

[DoesNotReturn]
public static void ArgumentOutOfRangeException(string paramName) => throw new ArgumentOutOfRangeException(paramName);

[DoesNotReturn]
public static void ArgumentOutOfRangeException(string message, string paramName) => throw new ArgumentOutOfRangeException(message, paramName);

[DoesNotReturn]
public static void ArgumentOutOfRangeException_index() => throw new ArgumentOutOfRangeException("index");

[DoesNotReturn]
public static void InvalidOperationException(string message) => throw new InvalidOperationException(message);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -57,21 +85,25 @@ public static void CheckDepthLimit(int depth, bool useLargeLimit = false)
static void DepthLimitExceeded() => throw new ArgumentException("Markdown elements in the input are too deeply nested - depth limit exceeded. Input is most likely not sensible or is a very large table.");
}

[DoesNotReturn]
public static void ThrowArgumentNullException(ExceptionArgument argument)
{
throw new ArgumentNullException(argument.ToString());
}

[DoesNotReturn]
public static void ThrowArgumentException(ExceptionArgument argument, ExceptionReason reason)
{
throw new ArgumentException(argument.ToString(), GetExceptionReason(reason));
}

[DoesNotReturn]
public static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionReason reason)
{
throw new ArgumentOutOfRangeException(argument.ToString(), GetExceptionReason(reason));
}

[DoesNotReturn]
public static void ThrowIndexOutOfRangeException()
{
throw new IndexOutOfRangeException();
Expand Down
Loading