-
Notifications
You must be signed in to change notification settings - Fork 639
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed encoding provider loading on .NET Framework (for .NET Standard …
…2.0 target) (#1036) * Lucene.Net.Analysis.Ja.Tools.ConnectionCostsWriter: Added using for Lucene.Net.Support * Lucene.Net.Analysis.Kuromoji + Lucene.Net.Analysis.SmartCn: Added EncodingProviderInitializer classes to ensure we don't load the encoding provider on a .NET Framework runtime when targeting netstandard2.0 (fixes #1025)
- Loading branch information
1 parent
3989c5a
commit 2d4d332
Showing
7 changed files
with
120 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/Lucene.Net.Analysis.Kuromoji/Support/Util/EncodingProviderInitializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading; | ||
|
||
namespace Lucene.Net.Util | ||
{ | ||
/// <summary> | ||
/// Loads the <see cref="System.Text.EncodingProvider"/> for the current runtime for support of | ||
/// EUC-JP encoding. | ||
/// </summary> | ||
internal static class EncodingProviderInitializer | ||
{ | ||
private static int initialized; | ||
|
||
private static bool IsNetFramework => | ||
#if NETSTANDARD2_0 | ||
RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase); | ||
#elif NET40_OR_GREATER | ||
true; | ||
#else | ||
false; | ||
#endif | ||
|
||
[Conditional("FEATURE_ENCODINGPROVIDERS")] | ||
public static void EnsureInitialized() | ||
{ | ||
// Only allow a single thread to call this | ||
if (0 != Interlocked.CompareExchange(ref initialized, 1, 0)) return; | ||
|
||
#if FEATURE_ENCODINGPROVIDERS | ||
if (!IsNetFramework) | ||
{ | ||
Initialize(); | ||
} | ||
#endif | ||
} | ||
|
||
#if FEATURE_ENCODINGPROVIDERS | ||
// NOTE: CodePagesEncodingProvider.Instance loads early, so we need this in a separate method to ensure | ||
// that it isn't executed until after we know which runtime we are on. | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static void Initialize() | ||
{ | ||
// Support for EUC-JP encoding. See: https://docs.microsoft.com/en-us/dotnet/api/system.text.codepagesencodingprovider?view=netcore-2.0 | ||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/Lucene.Net.Analysis.SmartCn/Support/Util/EncodingProviderInitializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading; | ||
|
||
namespace Lucene.Net.Util | ||
{ | ||
/// <summary> | ||
/// Loads the <see cref="System.Text.EncodingProvider"/> for the current runtime for support of | ||
/// GB2312 encoding. | ||
/// </summary> | ||
internal static class EncodingProviderInitializer | ||
{ | ||
private static int initialized; | ||
|
||
private static bool IsNetFramework => | ||
#if NETSTANDARD2_0 | ||
RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase); | ||
#elif NET40_OR_GREATER | ||
true; | ||
#else | ||
false; | ||
#endif | ||
|
||
[Conditional("FEATURE_ENCODINGPROVIDERS")] | ||
public static void EnsureInitialized() | ||
{ | ||
// Only allow a single thread to call this | ||
if (0 != Interlocked.CompareExchange(ref initialized, 1, 0)) return; | ||
|
||
#if FEATURE_ENCODINGPROVIDERS | ||
if (!IsNetFramework) | ||
{ | ||
Initialize(); | ||
} | ||
#endif | ||
} | ||
|
||
#if FEATURE_ENCODINGPROVIDERS | ||
// NOTE: CodePagesEncodingProvider.Instance loads early, so we need this in a separate method to ensure | ||
// that it isn't executed until after we know which runtime we are on. | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static void Initialize() | ||
{ | ||
// Support for GB2312 encoding. See: https://docs.microsoft.com/en-us/dotnet/api/system.text.codepagesencodingprovider?view=netcore-2.0 | ||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters