Skip to content

Commit 91e7dc6

Browse files
committed
Add fallback encoding to read all text; Add upgrade guide
1 parent 6129d50 commit 91e7dc6

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

MagicFileEncoding/FileEncoding.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ public static string ReadAllText(string filename)
4040
/// </summary>
4141
/// <param name="filename">The file to read text</param>
4242
/// <param name="targetEncoding">The target encoding to transform to the return value</param>
43+
/// <param name="fallbackEncoding">Fallback encoding for the input</param>
4344
/// <returns>Returns the text</returns>
44-
public static string ReadAllText(string filename, Encoding targetEncoding)
45+
public static string ReadAllText(string filename, Encoding targetEncoding, Encoding fallbackEncoding = null)
4546
=> targetEncoding
46-
.GetString(AutomaticTransformBytes(filename, targetEncoding))
47+
.GetString(AutomaticTransformBytes(filename, targetEncoding, fallbackEncoding))
4748
.Trim(new[]{'\uFEFF'});
4849

4950
/// <summary>
@@ -59,10 +60,12 @@ public static void WriteAllText(string path, string text, Encoding targetEncodin
5960
/// Automatic transform bytes
6061
/// </summary>
6162
/// <param name="filename">The file to analyze</param>
62-
/// <param name="targetEncoding"></param>
63+
/// <param name="targetEncoding">The output target encoding</param>
64+
/// <param name="fallbackEncoding">Fallback encoding for the input</param>
6365
/// <returns>Transformed byte array</returns>
64-
private static byte[] AutomaticTransformBytes(string filename, Encoding targetEncoding)
65-
=> Encoding.Convert(GetAcceptableEncoding(filename), targetEncoding,
66+
private static byte[] AutomaticTransformBytes(string filename, Encoding targetEncoding,
67+
Encoding fallbackEncoding = null)
68+
=> Encoding.Convert(GetAcceptableEncoding(filename,fallbackEncoding), targetEncoding,
6669
File.ReadAllBytes(filename));
6770

6871
/// <summary>

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ It is strongly recommended to write unit tests for your use case to ensure the l
1313

1414
#### Read a text file
1515
```csharp
16+
var text = FileEncoding.ReadAllText(filePath);
17+
// or
1618
var text = FileEncoding.ReadAllText(filePath, Encoding.Unicode);
1719
```
1820
#### Write a text file
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Upgrade Guide v1.0.0 to v1.1.1
2+
3+
#### Read a text file
4+
5+
```csharp
6+
// v1.0.0
7+
var text = new FileEncoding().AutomaticReadAllText(filename);
8+
```
9+
changes to:
10+
```csharp
11+
// v1.1.1
12+
var text = FileEncoding.ReadAllText(filePath);
13+
// or
14+
var text = FileEncoding.ReadAllText(filePath, Encoding.Unicode);
15+
```
16+
#### Write a text file
17+
```csharp
18+
// v1.0.0
19+
new FileEncoding().WriteAllText(tmpFile.Path, text, Encoding.UTF8);
20+
```
21+
changes to:
22+
```csharp
23+
// v1.1.1
24+
FileEncoding.WriteAllText(tmpFile.Path, text, Encoding.UTF8);
25+
```
26+
### Just detect suitable encoding
27+
```csharp
28+
// v1.0.0
29+
var encoding = new FileEncoding().GetAcceptableEncoding(filename);
30+
```
31+
changes to:
32+
```csharp
33+
// v1.1.1
34+
var encoding = FileEncoding.GetAcceptableEncoding(filename);
35+
```
36+
#### Change fallback (default) encoding
37+
```csharp
38+
// v1.0.0
39+
var fe = new FileEncoding();
40+
fe.FallbackEncoding = Encoding.UTF8;
41+
var text = fe.AutomaticReadAllText(filename);
42+
```
43+
changes to:
44+
```csharp
45+
// v1.1.1
46+
var text = FileEncoding.ReadAllText(filePath, Encoding.Unicode, Encoding.UTF8);
47+
```

0 commit comments

Comments
 (0)