Skip to content

Commit

Permalink
Add ReadOnlySpan API to netcore1.3 build
Browse files Browse the repository at this point in the history
  • Loading branch information
lechu445 committed Jan 13, 2023
1 parent b7a3471 commit 496907d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 69 deletions.
2 changes: 1 addition & 1 deletion XmlTools.LightXmlWriter.Tests/LightXmlWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void WriteElementString_Null_Value()
var sb = new StringBuilder();
using (var subject = new LightXmlWriter(new StringWriter(sb)))
{
subject.WriteElementString("Person", null);
subject.WriteElementString("Person", (string)null);
}
Assert.Equal("<Person/>", sb.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void WriteEndElement()
sb.Clear();

writer.WriteStartElement("root");
writer.WriteValue(null);
writer.WriteValue((string)null);
writer.WriteEndElement("root");
Assert.Equal("<root></root>", sb.ToString());
sb.Clear();
Expand Down Expand Up @@ -146,7 +146,7 @@ public void WriteEndElement_With_Prefix()
sb.Clear();

writer.WriteStartElement("root");
writer.WriteValue(null);
writer.WriteValue((string)null);
writer.WriteEndElement("prefix", "root");
Assert.Equal("<root></prefix:root>", sb.ToString());
sb.Clear();
Expand Down
2 changes: 0 additions & 2 deletions XmlTools.LightXmlWriter/LightXmlWriter.Elements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ public void WriteElementString(string name, DateTime value, string? format)
this.writingElement = false;
}

#if !NETSTANDARD1_3
public void WriteElementString(string name, ReadOnlySpan<char> value, bool escapeValue = true)
{
WriteStartElement(name);
Expand All @@ -171,7 +170,6 @@ public void WriteElementString(string name, ReadOnlySpan<char> value, bool escap
this.valueWritten = true;
this.writingElement = false;
}
#endif

public void WriteElementString(string? prefix, string name, string? ns, string? value, bool escapeValue = true)
{
Expand Down
51 changes: 6 additions & 45 deletions XmlTools.LightXmlWriter/LightXmlWriter.Escaping.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.CompilerServices;

namespace XmlTools
{
Expand All @@ -11,7 +10,6 @@ public sealed partial class LightXmlWriter
private static readonly char[] EscapeChars = new[] { '<', '>', '\"', '\'', '&' };
private static readonly char[] EscapeCharsForValue = new[] { '<', '>', '\'', '&' };

#if !NETSTANDARD1_3
private void WriteEscaped(ReadOnlySpan<char> str, bool escapeValue)
{
if (str.IsEmpty)
Expand All @@ -31,12 +29,12 @@ private void WriteEscaped(ReadOnlySpan<char> str, bool escapeValue)

if (index == -1)
{
this.writer.Write(str);
WriteSpan(str);
return;
}
else
{
this.writer.Write(str.Slice(0, index));
WriteSpan(str.Slice(0, index));
if (escapeValue)
{
WriteEscapeSequenceForValue(str[index]);
Expand All @@ -50,7 +48,6 @@ private void WriteEscaped(ReadOnlySpan<char> str, bool escapeValue)
}
}
}
#endif

private void WriteEscaped(string? str, bool escapeValue)
{
Expand Down Expand Up @@ -86,18 +83,9 @@ private void WriteEscaped(string? str, bool escapeValue)
else
{
foundAnyEscapeChar = true;
#if NETSTANDARD1_3
if (TryCopyToBuffer(str, newIndex, index - newIndex))
{
this.writer.Write(this.buffer, 0, index - newIndex);
}
else
{
this.writer.Write(str.Substring(newIndex, index - newIndex));
}
#else
this.writer.Write(str.AsSpan(newIndex, index - newIndex));
#endif

WriteSpan(str.AsSpan(newIndex, index - newIndex));

if (escapeValue)
{
WriteEscapeSequenceForValue(str[index]);
Expand All @@ -114,35 +102,8 @@ private void WriteEscaped(string? str, bool escapeValue)

private void WriteEscaped(string str, int newIndex)
{
#if NETSTANDARD1_3
if (TryCopyToBuffer(str, newIndex, str.Length - newIndex))
{
this.writer.Write(this.buffer, 0, str.Length - newIndex);
}
else
{
this.writer.Write(str.Substring(newIndex, str.Length - newIndex));
}
#else
this.writer.Write(str.AsSpan(newIndex, str.Length - newIndex));
#endif
}

#if NETSTANDARD1_3
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool TryCopyToBuffer(string str, int startIndex, int count)
{
if (count > this.buffer.Length)
{
return false;
}
else
{
str.CopyTo(startIndex, this.buffer, 0, count);
return true;
}
WriteSpan(str.AsSpan(newIndex, str.Length - newIndex));
}
#endif

private void WriteEscapeSequence(char c)
{
Expand Down
32 changes: 19 additions & 13 deletions XmlTools.LightXmlWriter/LightXmlWriter.Values.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ public void WriteChars(char[] value, int index, int count, bool escape = true)
{
if (escape)
{
#if NETSTANDARD1_3
WriteEscaped(new string(value, index, count), escapeValue: false);
#else
WriteEscaped(value.AsSpan(index, count), escapeValue: false);
#endif
}
else
{
Expand All @@ -68,11 +64,7 @@ public void WriteChars(char[] value, int index, int count, bool escape = true)

if (escape)
{
#if NETSTANDARD1_3
WriteEscaped(new string(value, index, count), escapeValue: true);
#else
WriteEscaped(value.AsSpan(index, count), escapeValue: true);
#endif
}
else
{
Expand Down Expand Up @@ -257,6 +249,7 @@ void Write(DateTime value, ReadOnlySpan<char> format)
}
}
}
#endif

public void WriteValue(ReadOnlySpan<char> value, bool escape = true)
{
Expand All @@ -275,9 +268,7 @@ public void WriteValue(ReadOnlySpan<char> value, bool escape = true)
WriteXmlValueString(value, escape);
this.valueWritten = true;
}
#endif

#if !NETSTANDARD1_3
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void WriteXmlString(ReadOnlySpan<char> value, bool escape = true)
{
Expand All @@ -287,7 +278,7 @@ private void WriteXmlString(ReadOnlySpan<char> value, bool escape = true)
}
else
{
this.writer.Write(value);
WriteSpan(value);
}
}

Expand All @@ -300,10 +291,9 @@ private void WriteXmlValueString(ReadOnlySpan<char> value, bool escape = true)
}
else
{
this.writer.Write(value);
WriteSpan(value);
}
}
#endif

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void WriteXmlValueString(string? value, bool escape)
Expand Down Expand Up @@ -356,5 +346,21 @@ private void WriteXmlCharValue(char value, bool escape)
this.writer.Write(value);
}
}

private void WriteSpan(ReadOnlySpan<char> span)
{
#if NETSTANDARD1_3
if (span.TryCopyTo(this.buffer))
{
this.writer.Write(buffer, 0, span.Length);
}
else
{
this.writer.Write(span.ToArray());
}
#else
this.writer.Write(span);
#endif
}
}
}
12 changes: 6 additions & 6 deletions XmlTools.LightXmlWriter/XmlTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<DebugType>embedded</DebugType>
<LangVersion>9.0</LangVersion>
<LangVersion>latest</LangVersion>
<PackageTags>xml, XmlWriter, light, fast</PackageTags>
<Version>1.0.2</Version>
<Version>1.1.0</Version>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReleaseNotes>- optimization in some cases
- added SourceLink
<PackageReleaseNotes>- Added ReadOnlySpan API to netstandard1.3 build
</PackageReleaseNotes>
</PropertyGroup>

Expand All @@ -26,14 +25,15 @@

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.3">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 496907d

Please sign in to comment.