Skip to content

Commit 6b88af7

Browse files
rstm-sf304NotModified
authored andcommitted
Add support for .NET Core 3.0 (#67)
* Add support .NET Core 3.0 * Update UTF-unknown.csproj * Fix problem with unicode We write crutches because it's "best practice"... * Move to static initializer * Update UTF-unknown.csproj * Update image to Visual Studio 2019 for appveyor * Add netcoreapp3.0 to tests * Move RegisterProvider to CharsetDetector * refactor: less code
1 parent a277507 commit 6b88af7

7 files changed

Lines changed: 40 additions & 34 deletions

File tree

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ environment:
44
version: 2.0.{build}
55

66
clone_folder: c:\utfUnknown
7-
image: Visual Studio 2017
7+
image: Visual Studio 2019
88
configuration: Release
99
platform: Any CPU
1010
nuget:

example/ConsoleExample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp1.1</TargetFramework>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>

example/DetectFile.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
// Udetect.cs created with MonoDevelop
2-
//
3-
// Author:
4-
// Rudi Pettazzi <rudi.pettazzi@gmail.com>
5-
// J. Verdurmen
6-
//
7-
81
using System;
9-
using System.Text;
2+
using System.IO;
103
using UtfUnknown;
114

125
namespace ConsoleExample
@@ -17,26 +10,26 @@ public class DetectFile
1710
/// Command line example: detects the encoding of the given file.
1811
/// </summary>
1912
/// <param name="args">a filename</param>
20-
public static void Main(String[] args)
13+
public static void Main(string[] args)
2114
{
2215
if (args.Length == 0)
2316
{
24-
Console.WriteLine("Usage: udetect <filename>");
17+
Console.WriteLine("Usage: ConsoleExample <filename>");
2518
return;
2619
}
2720

28-
string filename = args[0];
29-
30-
var result = CharsetDetector.DetectFromFile(filename);
31-
32-
if (result.Detected != null)
21+
var filename = args[0];
22+
if (!File.Exists(filename))
3323
{
34-
Console.WriteLine("Charset: {0}, confidence: {1}", result.Detected.EncodingName, result.Detected.Confidence);
35-
}
36-
else
37-
{
38-
Console.WriteLine("Detection failed.");
24+
Console.WriteLine($"File not found: {filename}");
25+
return;
3926
}
27+
28+
var result = CharsetDetector.DetectFromFile(filename);
29+
var message = result.Detected != null
30+
? $"Detected encoding {result.Detected.Encoding.WebName} with confidence {result.Detected.Confidence}."
31+
: $"Detection failed: {filename}";
32+
Console.WriteLine(message);
4033
}
4134
}
4235
}

src/CharsetDetector.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ namespace UtfUnknown
7575
/// </summary>
7676
public class CharsetDetector
7777
{
78+
#if NETCOREAPP3_0
79+
/// <summary>
80+
/// Adds the encodings of the EncodingProvider object to the common language runtime
81+
/// </summary>
82+
static CharsetDetector()
83+
{
84+
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
85+
}
86+
#endif // NETCOREAPP3_0
87+
7888
internal InputState InputState;
7989

8090
/// <summary>

src/UTF-unknown.csproj

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard1.0;netstandard1.3;net40</TargetFrameworks>
4+
<TargetFrameworks>netstandard1.0;netstandard1.3;net40;netcoreapp3.0</TargetFrameworks>
55
</PropertyGroup>
6+
67
<PropertyGroup>
78
<AssemblyName>UtfUnknown</AssemblyName>
89
<PackageId>UTF.Unknown</PackageId>
910
<Version>2.0.0</Version> <!-- patched in AppVeyor.yml -->
10-
1111
</PropertyGroup>
12+
1213
<PropertyGroup>
1314
<OutputType>Library</OutputType>
1415
</PropertyGroup>
16+
1517
<PropertyGroup>
1618
<Authors>Julian Verdurmen, Rudi Pettazzi, Shy Shalom</Authors>
1719
<NeutralLanguage>en-US</NeutralLanguage>
@@ -21,9 +23,9 @@
2123
This package is based on Ude and since version 2 also on uchardet, which are ports of the Mozilla Universal Charset Detector.
2224

2325
Features:
24-
- Detects 28 charsets
25-
- Easy to use API
26-
- .NET standard 1.0 + 2.0 support
26+
- Easy to use API
27+
- .NET standard 1.0 + 2.0 support
28+
- .NET Core 3.0 support
2729
- Strong named
2830
- XML documentation included
2931

@@ -33,18 +35,17 @@ Features:
3335
- Added some docs
3436
- Improve error handling
3537
- Improved unit tests
36-
37-
3838
</Description>
39-
<Copyright></Copyright>
39+
40+
<Copyright/>
4041

4142
</PropertyGroup>
43+
4244
<PropertyGroup>
4345

4446
<PackageTags>charset;detection;unicode;ascii;netstandard</PackageTags>
4547
<PackageReleaseNotes>
4648
- Added support for CP949 (@HelloWorld017)
47-
4849
</PackageReleaseNotes>
4950
<PackageIconUrl>https://raw.githubusercontent.com/CharsetDetector/UTF-unknown/master/logo.png</PackageIconUrl>
5051
<PackageProjectUrl>https://github.com/CharsetDetector/UTF-unknown</PackageProjectUrl>
@@ -56,6 +57,7 @@ Features:
5657
<SignAssembly>True</SignAssembly>
5758
<AssemblyOriginatorKeyFile>UtfUnknown.snk</AssemblyOriginatorKeyFile>
5859
</PropertyGroup>
60+
5961
<PropertyGroup>
6062
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\UtfUnknown.xml</DocumentationFile>
6163
<NoWarn>1701;1702;1705,1570,1591</NoWarn>

tests/CharsetDetectorTestBatch.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ private void TestFile(string expectedCharset, string file)
108108
{
109109
var result = CharsetDetector.DetectFromFile(file);
110110
var detected = result.Detected;
111-
111+
#if !NETCOREAPP3_0
112+
// because System.NotSupportedException in .net core 3.0 for Encoding
112113
_logWriter.WriteLine(string.Format("- {0} ({1}) -> {2}", file, expectedCharset, JsonConvert.SerializeObject(result)));
113-
114+
#endif // !NETCOREAPP3_0
114115
StringAssert.AreEqualIgnoringCase(expectedCharset, detected.EncodingName,
115116
$"Charset detection failed for {file}. Expected: {expectedCharset}, detected: {detected.EncodingName} ({detected.Confidence * 100.0f:0.00############}% confidence)");
116117
Assert.NotNull(detected.Encoding);

tests/UTF-unknown.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net452</TargetFrameworks>
4+
<TargetFrameworks>net452;netcoreapp3.0</TargetFrameworks>
55
<RootNamespace>UtfUnknown.Tests</RootNamespace>
66
<AssemblyName>UtfUnknown.Tests</AssemblyName>
77
</PropertyGroup>

0 commit comments

Comments
 (0)