Allow XyzEncoder.Default to be settable.
This commit is contained in:
parent
ae456401a8
commit
332900b175
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Microsoft.Framework.WebEncoders
|
namespace Microsoft.Framework.WebEncoders
|
||||||
|
|
@ -57,20 +58,33 @@ namespace Microsoft.Framework.WebEncoders
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The default <see cref="HtmlEncoder"/>, which uses <see cref="UnicodeRanges.BasicLatin"/> as its allow list.
|
/// A default instance of <see cref="HtmlEncoder"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This normally corresponds to <see cref="UnicodeRanges.BasicLatin"/>. However, this property is
|
||||||
|
/// settable so that a developer can change the default implementation application-wide.
|
||||||
|
/// </remarks>
|
||||||
public static HtmlEncoder Default
|
public static HtmlEncoder Default
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
HtmlEncoder defaultEncoder = Volatile.Read(ref _defaultEncoder);
|
return Volatile.Read(ref _defaultEncoder) ?? CreateDefaultEncoderSlow();
|
||||||
if (defaultEncoder == null)
|
|
||||||
{
|
|
||||||
defaultEncoder = new HtmlEncoder();
|
|
||||||
Volatile.Write(ref _defaultEncoder, defaultEncoder);
|
|
||||||
}
|
|
||||||
return defaultEncoder;
|
|
||||||
}
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(value));
|
||||||
|
}
|
||||||
|
Volatile.Write(ref _defaultEncoder, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.NoInlining)] // the JITter can attempt to inline the caller itself without worrying about us
|
||||||
|
private static HtmlEncoder CreateDefaultEncoderSlow()
|
||||||
|
{
|
||||||
|
var onDemandEncoder = new HtmlEncoder();
|
||||||
|
return Interlocked.CompareExchange(ref _defaultEncoder, onDemandEncoder, null) ?? onDemandEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Microsoft.Framework.WebEncoders
|
namespace Microsoft.Framework.WebEncoders
|
||||||
|
|
@ -57,20 +58,33 @@ namespace Microsoft.Framework.WebEncoders
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The default <see cref="JavaScriptStringEncoder"/>, which uses <see cref="UnicodeRanges.BasicLatin"/> as its allow list.
|
/// A default instance of <see cref="JavaScriptStringEncoder"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This normally corresponds to <see cref="UnicodeRanges.BasicLatin"/>. However, this property is
|
||||||
|
/// settable so that a developer can change the default implementation application-wide.
|
||||||
|
/// </remarks>
|
||||||
public static JavaScriptStringEncoder Default
|
public static JavaScriptStringEncoder Default
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
JavaScriptStringEncoder defaultEncoder = Volatile.Read(ref _defaultEncoder);
|
return Volatile.Read(ref _defaultEncoder) ?? CreateDefaultEncoderSlow();
|
||||||
if (defaultEncoder == null)
|
|
||||||
{
|
|
||||||
defaultEncoder = new JavaScriptStringEncoder();
|
|
||||||
Volatile.Write(ref _defaultEncoder, defaultEncoder);
|
|
||||||
}
|
|
||||||
return defaultEncoder;
|
|
||||||
}
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(value));
|
||||||
|
}
|
||||||
|
Volatile.Write(ref _defaultEncoder, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.NoInlining)] // the JITter can attempt to inline the caller itself without worrying about us
|
||||||
|
private static JavaScriptStringEncoder CreateDefaultEncoderSlow()
|
||||||
|
{
|
||||||
|
var onDemandEncoder = new JavaScriptStringEncoder();
|
||||||
|
return Interlocked.CompareExchange(ref _defaultEncoder, onDemandEncoder, null) ?? onDemandEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Microsoft.Framework.WebEncoders
|
namespace Microsoft.Framework.WebEncoders
|
||||||
|
|
@ -57,20 +58,33 @@ namespace Microsoft.Framework.WebEncoders
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The default <see cref="UrlEncoder"/> which uses <see cref="UnicodeRanges.BasicLatin"/> as its allow list.
|
/// A default instance of <see cref="UrlEncoder"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This normally corresponds to <see cref="UnicodeRanges.BasicLatin"/>. However, this property is
|
||||||
|
/// settable so that a developer can change the default implementation application-wide.
|
||||||
|
/// </remarks>
|
||||||
public static UrlEncoder Default
|
public static UrlEncoder Default
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
UrlEncoder defaultEncoder = Volatile.Read(ref _defaultEncoder);
|
return Volatile.Read(ref _defaultEncoder) ?? CreateDefaultEncoderSlow();
|
||||||
if (defaultEncoder == null)
|
|
||||||
{
|
|
||||||
defaultEncoder = new UrlEncoder();
|
|
||||||
Volatile.Write(ref _defaultEncoder, defaultEncoder);
|
|
||||||
}
|
|
||||||
return defaultEncoder;
|
|
||||||
}
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(value));
|
||||||
|
}
|
||||||
|
Volatile.Write(ref _defaultEncoder, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.NoInlining)] // the JITter can attempt to inline the caller itself without worrying about us
|
||||||
|
private static UrlEncoder CreateDefaultEncoderSlow()
|
||||||
|
{
|
||||||
|
var onDemandEncoder = new UrlEncoder();
|
||||||
|
return Interlocked.CompareExchange(ref _defaultEncoder, onDemandEncoder, null) ?? onDemandEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue