forked from jamietre/CsQuery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlEncoding.cs
More file actions
85 lines (77 loc) · 2.27 KB
/
Copy pathHtmlEncoding.cs
File metadata and controls
85 lines (77 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CsQuery.HtmlParser
{
/// <summary>
/// Simplify access to character set encodings for this system.
/// </summary>
public static class HtmlEncoding
{
private static Dictionary<string, EncodingInfo> _Encodings;
/// <summary>
/// A dictionary of all encodings available on this system
/// </summary>
private static Dictionary<string, EncodingInfo> Encodings
{
get
{
if (_Encodings == null)
{
_Encodings = new Dictionary<string, EncodingInfo>(StringComparer.CurrentCultureIgnoreCase);
foreach (var encoding in Encoding.GetEncodings())
{
_Encodings[encoding.Name] = encoding;
}
}
return _Encodings;
}
}
/// <summary>
/// Try to get a character set encoding from its web name.
/// </summary>
///
/// <param name="encodingName">
/// Name of the encoding.
/// </param>
/// <param name="encoding">
/// [out] The encoding.
/// </param>
///
/// <returns>
/// true if it succeeds, false if it fails.
/// </returns>
public static bool TryGetEncoding(string encodingName, out Encoding encoding)
{
EncodingInfo info;
if (Encodings.TryGetValue(encodingName, out info)) {
encoding = info.GetEncoding();
return true;
} else {
encoding =null;
return false;
}
}
/// <summary>
/// Gets an encoding.
/// </summary>
///
/// <param name="encodingName">
/// Name of the encoding.
/// </param>
///
/// <returns>
/// The encoding.
/// </returns>
public static Encoding GetEncoding(string encodingName)
{
Encoding encoding;
if (TryGetEncoding(encodingName, out encoding)) {
return encoding;
} else {
return null;
}
}
}
}