Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Strings.cs
1using System.Text;
2using System;
3
4namespace Waher.Runtime.IO
5{
9 public static class Strings
10 {
23 public static Encoding GetEncoding(byte[] Data, Encoding DefaultEncoding, out int BomLength)
24 {
25 return GetEncoding(Data, 0, Data.Length, DefaultEncoding, out BomLength);
26 }
27
42 public static Encoding GetEncoding(byte[] Data, int Offset, int Count, Encoding DefaultEncoding, out int BomLength)
43 {
44 BomLength = 0;
45
46 if (Data is null)
47 return DefaultEncoding ?? ISO_8859_1;
48
49 if (Count == 0)
50 return DefaultEncoding ?? ISO_8859_1;
51
52 /************************************************************
53 * See: https://en.wikipedia.org/wiki/Byte_order_mark
54 *
55 * Script to extract known BOMs from the system:
56 *
57 * foreach x in 0..65535 do
58 * if exists((Encoding:=System.Text.Encoding.GetEncoding(x);BOM:=Encoding.GetPreamble())) and BOM.Length>0 then
59 * print([x,Encoding.WebName,Encoding.EncodingName,BOM])
60 *
61 * Script to extract existing encodings in the system:
62 *
63 * foreach x in 0..65535 do
64 * if exists(Encoding:=System.Text.Encoding.GetEncoding(x)) then
65 * print([x,Encoding.WebName,Encoding.EncodingName])
66 ************************************************************/
67
68
69 BomLength = 0;
70
71 if (Count >= 3 && Data[Offset] == 0xef && Data[Offset + 1] == 0xbb && Data[Offset + 2] == 0xbf)
72 {
73 BomLength = 3;
74 return Encoding.UTF8;
75 }
76 else if (Count >= 2 && Data[Offset] == 0xfe && Data[Offset + 1] == 0xff)
77 {
78 BomLength = 2;
79 return Encoding.BigEndianUnicode;
80 }
81 else if (Count >= 2 && Data[Offset] == 0xff && Data[Offset + 1] == 0xfe)
82 {
83 if (Count >= 4 && Data[Offset + 2] == 0 && Data[Offset + 3] == 0)
84 {
85 BomLength = 4;
86 return Encoding.UTF32;
87 }
88 else
89 {
90 BomLength = 2;
91 return Encoding.Unicode;
92 }
93 }
94 else if (Count >= 4 && Data[Offset] == 0 && Data[Offset + 1] == 0 && Data[Offset + 2] == 0xfe && Data[Offset + 3] == 0xff)
95 {
96 BomLength = 4;
97 return BigEndianUnicode32;
98 }
99 else if (Count >= 4 && Data[Offset] == 0x2b && Data[Offset + 1] == 0x2f && Data[Offset + 2] == 0x76)
100 {
101 if (Data[Offset + 3] == 0x39 ||
102 Data[Offset + 3] == 0x2b ||
103 Data[Offset + 3] == 0x2f)
104 {
105 BomLength = 4;
106 return Encoding.UTF7;
107 }
108 else if (Data[Offset + 3] == 0x38)
109 {
110 if (Count >= 5 && Data[Offset + 4] == 0x2d)
111 BomLength = 5;
112 else
113 BomLength = 4;
114
115 return Encoding.UTF7;
116 }
117 }
118 else if (Count >= 3 && Data[Offset] == 0xf7 && Data[Offset + 1] == 0x64 && Data[Offset + 2] == 0x4c)
119 throw new ArgumentException("UTF-1 encoding not supported.", nameof(Data));
120 else if (Count >= 4 && Data[Offset] == 0xdd && Data[Offset + 1] == 0x73 && Data[Offset + 2] == 0x66 && Data[Offset + 3] == 0x73)
121 throw new ArgumentException("UTF-EBCDIC encoding not supported.", nameof(Data));
122 else if (Count >= 3 && Data[Offset] == 0x0e && Data[Offset + 1] == 0xfe && Data[Offset + 2] == 0xff)
123 throw new ArgumentException("SCSU encoding not supported.", nameof(Data));
124 else if (Count >= 3 && Data[Offset] == 0xfb && Data[Offset + 1] == 0xee && Data[Offset + 2] == 0x28)
125 throw new ArgumentException("BOCU encoding not supported.", nameof(Data));
126 else if (Count >= 4 && Data[Offset] == 0x84 && Data[Offset + 1] == 0x31 && Data[Offset + 2] == 0x95 && Data[Offset + 3] == 0x33)
127 {
128 BomLength = 4;
129 return GB18030;
130 }
131
132 return DefaultEncoding ?? ISO_8859_1;
133 }
134
148 public static string GetString(byte[] Data, int Offset, int Count, Encoding DefaultEncoding)
149 {
150 if (Data is null)
151 return null;
152
153 Encoding Encoding = GetEncoding(Data, Offset, Count, DefaultEncoding, out int BomLength);
154
155 return Encoding.GetString(Data, Offset + BomLength, Count - BomLength);
156 }
157
169 public static string GetString(byte[] Data, Encoding DefaultEncoding)
170 {
171 if (Data is null)
172 return null;
173
174 Encoding Encoding = GetEncoding(Data, DefaultEncoding, out int Offset);
175
176 return Encoding.GetString(Data, Offset, Data.Length - Offset);
177 }
178
182 public static Encoding ISO_8859_1
183 {
184 get
185 {
186 if (iso_8859_1 is null)
187 iso_8859_1 = Encoding.GetEncoding("iso-8859-1");
188
189 return iso_8859_1;
190 }
191 }
192
193 private static Encoding iso_8859_1 = null;
194
198 public static Encoding BigEndianUnicode32
199 {
200 get
201 {
202 if (utf_32be is null)
203 utf_32be = Encoding.GetEncoding("utf-32BE");
204
205 return utf_32be;
206 }
207 }
208
209 private static Encoding utf_32be = null;
210
214 public static Encoding GB18030
215 {
216 get
217 {
218 if (gb18030 is null)
219 gb18030 = Encoding.GetEncoding("GB18030");
220
221 return gb18030;
222 }
223 }
224
225 private static Encoding gb18030 = null;
226
230 public static Encoding Utf8WithBom
231 {
232 get
233 {
234 if (utf8WithBom is null)
235 utf8WithBom = new UTF8Encoding(true);
236
237 return utf8WithBom;
238 }
239 }
240
241 private static Encoding utf8WithBom = null;
242
246 public static Encoding Utf8WithoutBom
247 {
248 get
249 {
250 if (utf8WithoutBom is null)
251 utf8WithoutBom = new UTF8Encoding(false);
252
253 return utf8WithoutBom;
254 }
255 }
256
257 private static Encoding utf8WithoutBom = null;
258
264 public static string RemovePortNumber(this string s)
265 {
266 if (s is null)
267 return null;
268
269 int i;
270
271 if (s.StartsWith("["))
272 {
273 i = s.LastIndexOf("]:");
274 if (i < 0)
275 return s;
276
277 i++;
278 }
279 else
280 {
281 i = s.LastIndexOf(':');
282 if (i < 0)
283 return s;
284 }
285
286 if (int.TryParse(s.Substring(i + 1), out int j) && j >= 0 && j <= 65535)
287 return s.Substring(0, i);
288 else
289 return s;
290 }
291 }
292}
Static class managing binary representations of strings.
Definition: Strings.cs:10
static Encoding Utf8WithoutBom
UTF-8 encoding without Byte Order Mark (BOM)
Definition: Strings.cs:247
static Encoding BigEndianUnicode32
ISO-8859-1 encoding.
Definition: Strings.cs:199
static Encoding ISO_8859_1
ISO-8859-1 encoding.
Definition: Strings.cs:183
static string GetString(byte[] Data, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Definition: Strings.cs:169
static Encoding GetEncoding(byte[] Data, Encoding DefaultEncoding, out int BomLength)
Gets the encoding of a string from a its binary representation, taking any Byte Order Mark (BOM) into...
Definition: Strings.cs:23
static Encoding GetEncoding(byte[] Data, int Offset, int Count, Encoding DefaultEncoding, out int BomLength)
Gets the encoding of a string from a its binary representation, taking any Byte Order Mark (BOM) into...
Definition: Strings.cs:42
static Encoding Utf8WithBom
UTF-8 encoding with Byte Order Mark (BOM)
Definition: Strings.cs:231
static Encoding GB18030
GB18030 encoding (simplified Chinese).
Definition: Strings.cs:215
static string RemovePortNumber(this string s)
Returns the port number at the end of a string, if found.
Definition: Strings.cs:264
static string GetString(byte[] Data, int Offset, int Count, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Definition: Strings.cs:148