Skip to content
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
22 changes: 15 additions & 7 deletions src/NJsonSchema/ConversionUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//-----------------------------------------------------------------------

using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
Expand Down Expand Up @@ -93,7 +92,7 @@ private static string ConvertToCamelCase(string input, bool firstCharacterMustBe
private static string DoFullCamelCaseConversion(string input, bool firstCharacterMustBeAlpha, CamelCaseMode mode)
{
var capacity = input.Length + (firstCharacterMustBeAlpha ? 1 : 0);
var buffer = capacity < 2 ? stackalloc char[capacity] : new char[capacity];
var buffer = capacity <= 256 ? stackalloc char[256] : new char[capacity];

var sb = new ValueStringBuilder(buffer);

Expand Down Expand Up @@ -363,20 +362,29 @@ private static void AddPrefixToBeginningOfNonEmptyLines(string input, string tab
}
}

private static readonly char [] _cSharpDocLineBreakChars = ['\r', '\n'];
private static readonly Lazy<Regex> _cSharpDocLineBreakRegex = new(static () => new Regex("^( *)/// ", RegexOptions.Multiline | RegexOptions.Compiled));

/// <summary>Converts all line breaks in a string into '\n' and removes white spaces.</summary>
/// <param name="input">The input.</param>
/// <param name="tabCount">The tab count.</param>
/// <returns>The output.</returns>
public static string ConvertCSharpDocs(string input, int tabCount)
{
input = input?
.Replace("\r", string.Empty)
.Replace("\n", "\n" + string.Join("", Enumerable.Repeat(" ", tabCount)) + "/// ")
?? string.Empty;
input ??= "";

var needsCleanup = input.IndexOfAny(_cSharpDocLineBreakChars) != -1;

if (needsCleanup)
{
input = input
.Replace("\r", string.Empty)
.Replace("\n", "\n" + CreateTabString(tabCount) + "/// ");
}

// TODO: Support more markdown features here
var xml = new XText(input).ToString();
return Regex.Replace(xml, @"^( *)/// ", m => m.Groups[1] + "/// <br/>", RegexOptions.Multiline);
return _cSharpDocLineBreakRegex.Value.Replace(xml, static m => m.Groups[1] + "/// <br/>");
}

private static string CreateTabString(int tabCount)
Expand Down
18 changes: 12 additions & 6 deletions src/NJsonSchema/Visitors/JsonReferenceVisitorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,25 @@ protected virtual void Visit(object obj, string path, string? typeNameHint, ISet

private static void ReplaceOrDelete<T>(ObservableCollection<T> collection, int index, T obj)
{
collection.RemoveAt(index);
if (obj != null)
if (obj is not null)
{
collection.Insert(index, obj);
collection[index] = obj;
}
else
{
collection.RemoveAt(index);
}
}

private static void ReplaceOrDelete(IList collection, int index, object obj)
{
collection.RemoveAt(index);
if (obj != null)
if (obj is not null)
{
collection[index] = obj;
}
else
{
collection.Insert(index, obj);
collection.RemoveAt(index);
}
}
}
Expand Down