1 #Region "Microsoft.VisualBasic::c2ca89034398b6487e9edc6607f9bff1, Microsoft.VisualBasic.Core\Text\ASCII.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class ASCII
35     
36     '         Properties: AlphaNumericTable, Nonprintings, Symbols
37     
38     '         FunctionIsASCIIString, ReplaceQuot, TrimNonPrintings
39     '         Class [Byte]
40     
41     '             FunctionGetASCIISymbols
42     
43     
44     
45     
46     ' /********************************************************************************/
47
48 #End Region
49
50 Imports System.Runtime.CompilerServices
51 Imports System.Text
52 Imports Microsoft.VisualBasic.Linq
53 Imports Microsoft.VisualBasic.Language
54
55 Namespace Text
56
57     ''' <summary>
58     ''' ``ASCII`` (``American Standard Code for Information Interchange``,美国信息互换标准代码,``ASCⅡ``) 是基于拉丁字母的一套电脑编码系统。
59     ''' 它主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统,并等同于国际标准``ISO/IEC 646``。
60     ''' ASCII第一次以规范标准的型态发表是在1967年,最后一次更新则是在1986年,至今为止共定义了128个字符,其中33个字符无法显示
61     ''' (这是以现今操作系统为依归,但在DOS模式下可显示出一些诸如笑脸、扑克牌花式等8-bit符号),且这33个字符多数都已是陈废的控制字符,
62     ''' 控制字符的用途主要是用来操控已经处理过的文字,在33个字符之外的是95个可显示的字符,包含用键盘敲下空白键所产生的空白字符也算1个可显示字符
63     ''' (显示为空白)。
64     ''' </summary>
65     ''' <remarks>http://ascii.911cha.com/</remarks>
66     Public Class ASCII
67
68         ''' <summary>
69         ''' 0000 0000 0 00 NUL 空字符(Null)
70         ''' </summary>
71         Public Const NUL As Char = Chr(0)
72         ''' <summary>
73         ''' 0000 0001 1 01 SOH 标题开始
74         ''' </summary>
75         Public Const SOH As Char = Chr(1)
76         ''' <summary>
77         ''' 0000 0010 2 02 STX 本文开始
78         ''' </summary>
79         Public Const STX As Char = Chr(2)
80         ''' <summary>
81         ''' 0000 0011 3 03 ETX 本文结束
82         ''' </summary>
83         Public Const ETX As Char = Chr(3)
84         ''' <summary>
85         ''' 0000 0100 4 04 EOT 传输结束
86         ''' </summary>
87         Public Const EOT As Char = Chr(4)
88         ''' <summary>
89         ''' 0000 0101 5 05 ENQ 请求
90         ''' </summary>
91         Public Const ENQ As Char = Chr(5)
92         ''' <summary>
93         ''' 0000 0110 6 06 ACK 确认回应
94         ''' </summary>
95         Public Const ACK As Char = Chr(6)
96         ''' <summary>
97         ''' 0000 0111 7 07 BEL 响铃
98         ''' </summary>
99         Public Const BEL As Char = Chr(7)
100         ''' <summary>
101         ''' 0000 1000 8 08 BS 退格
102         ''' </summary>
103         Public Const BS As Char = Chr(8)
104         ''' <summary>
105         ''' 0000 1001 9 09 HT 水平定位符号
106         ''' </summary>
107         Public Const HT As Char = Chr(9)
108         ''' <summary>
109         ''' 0000 1010 10 0A LF 换行键
110         ''' </summary>
111         Public Const LF As Char = Chr(10)
112         ''' <summary>
113         ''' 0000 1011 11 0B VT 垂直定位符号
114         ''' </summary>
115         Public Const VT As Char = Chr(11)
116         ''' <summary>
117         ''' 0000 1100 12 0C FF 换页键
118         ''' </summary>
119         Public Const FF As Char = Chr(12)
120         ''' <summary>
121         ''' 0000 1101 13 0D CR 归位键
122         ''' </summary>
123         Public Const CR As Char = Chr(13)
124         ''' <summary>
125         ''' 0000 1110 14 0E SO 取消变换(Shift out)
126         ''' </summary>
127         Public Const SO As Char = Chr(14)
128         ''' <summary>
129         ''' 0000 1111 15 0F SI 启用变换(Shift in)
130         ''' </summary>
131         Public Const SI As Char = Chr(15)
132         ''' <summary>
133         ''' 0001 0000 16 10 DLE 跳出数据通讯
134         ''' </summary>
135         Public Const DLE As Char = Chr(16)
136         ''' <summary>
137         ''' 0001 0001 17 11 DC1 设备控制一(XON 启用软件速度控制)
138         ''' </summary>
139         Public Const DC1 As Char = Chr(17)
140         ''' <summary>
141         ''' 0001 0010 18 12 DC2 设备控制二
142         ''' </summary>
143         Public Const DC2 As Char = Chr(18)
144         ''' <summary>
145         ''' 0001 0011 19 13 DC3 设备控制三(XOFF 停用软件速度控制)
146         ''' </summary>
147         Public Const DC3 As Char = Chr(19)
148         ''' <summary>
149         ''' 0001 0100 20 14 DC4 设备控制四
150         ''' </summary>
151         Public Const DC4 As Char = Chr(20)
152         ''' <summary>
153         ''' 0001 0101 21 15 NAK 确认失败回应
154         ''' </summary>
155         Public Const NAK As Char = Chr(21)
156         ''' <summary>
157         ''' 0001 0110 22 16 SYN 同步用暂停
158         ''' </summary>
159         Public Const SYN As Char = Chr(22)
160         ''' <summary>
161         ''' 0001 0111 23 17 ETB 区块传输结束
162         ''' </summary>
163         Public Const ETB As Char = Chr(23)
164         ''' <summary>
165         ''' 0001 1000 24 18 CAN 取消
166         ''' </summary>
167         Public Const CAN As Char = Chr(24)
168         ''' <summary>
169         ''' 0001 1001 25 19 EM 连接介质中断
170         ''' </summary>
171         Public Const EM As Char = Chr(25)
172         ''' <summary>
173         ''' 0001 1010 26 1A SUB 替换
174         ''' </summary>
175         Public Const [SUB] As Char = Chr(26)
176         ''' <summary>
177         ''' 0001 1011 27 1B ESC 跳出
178         ''' </summary>
179         Public Const ESC As Char = Chr(27)
180         ''' <summary>
181         ''' 0001 1100 28 1C FS 文件分割符
182         ''' </summary>
183         Public Const FS As Char = Chr(28)
184         ''' <summary>
185         ''' 0001 1101 29 1D GS 组群分隔符
186         ''' </summary>
187         Public Const GS As Char = Chr(29)
188         ''' <summary>
189         ''' 0001 1110 30 1E RS 记录分隔符
190         ''' </summary>
191         Public Const RS As Char = Chr(30)
192         ''' <summary>
193         ''' 0001 1111 31 1F US 单元分隔符
194         ''' </summary>
195         Public Const US As Char = Chr(31)
196         ''' <summary>
197         ''' 0111 1111 127 7F DEL 删除
198         ''' </summary>
199         Public Const DEL As Char = Chr(127)
200
201         ''' <summary>
202         ''' 非打印字符也可以是正则表达式的组成部分。下表列出了表示非打印字符的转义序列:
203         ''' 
204         ''' ##### 转义序列
205         ''' 
206         ''' |字符|含义|
207         ''' |---|----|
208         ''' |\cx|匹配 x 指示的控制字符。例如,\cM 匹配 Control-M 或回车符。x 的值必须在 A-Z 或 a-z 之间。如果不是这样,则假定 c 就是“c”字符本身。|
209         ''' |\f |换页符匹配。等效于 \x0c 和 \cL。|
210         ''' |\n |换行符匹配。等效于 \x0a 和 \cJ。|
211         ''' |\r |匹配一个回车符。等效于 \x0d 和 \cM。|
212         ''' |\s |匹配任何空白字符,包括空格、制表符、换页符等。与 [\f\n\r\t\v] 等效。|
213         ''' |\S |匹配任何非空白字符。与 [^ \f\n\r\t\v] 等效。|
214         ''' |\t |制表符匹配。与 \x09 和 \cI 等效。|
215         ''' |\v |垂直制表符匹配。与 \x0b 和 \cK 等效。|
216         ''' </summary>
217         ''' <returns></returns>
218         Public Shared ReadOnly Property Nonprintings As Char() = {
219             ASCII.ACK,
220             ASCII.BEL,
221             ASCII.BS,
222             ASCII.CAN,
223             ASCII.DC1,
224             ASCII.DC2,
225             ASCII.DC3,
226             ASCII.DC4,
227             ASCII.DEL,
228             ASCII.DLE,
229             ASCII.EM,
230             ASCII.ENQ,
231             ASCII.EOT,
232             ASCII.ESC,
233             ASCII.ETB,
234             ASCII.ETX,
235             ASCII.FF,
236             ASCII.FS,
237             ASCII.GS,
238             ASCII.HT,
239             ASCII.NAK,
240             ASCII.NUL,
241             ASCII.RS,
242             ASCII.SI,
243             ASCII.SO,
244             ASCII.SOH,
245             ASCII.STX,
246             ASCII.SUB,
247             ASCII.SYN,
248             ASCII.US,
249             ASCII.VT
250         }
251
252         ''' <summary>
253         ''' <see cref="vbTab"/>
254         ''' </summary>
255         Public Const TAB As Char = CChar(vbTab)
256
257         ''' <summary>
258         ''' 双引号``"``
259         <span class="xml_comment">''' </summary></span>
260         Public Const Quot As Char = Chr(34)
261         Public Shared ReadOnly QuotBegin_ZHCN As Char = Convert.ToChar(8220)
262         Public Shared ReadOnly QuotEnds_ZHCN As Char = Convert.ToChar(8221)
263         Public Const QuotUnknown As Char = "″"c
264
265         Public Const A As Integer = Asc("A"c)
266         Public Const Z As Integer = Asc("Z"c)
267         Public Const al% = Asc("a"c)
268         Public Const zl% = Asc("z"c)
269
270         <span class="xml_comment">''' <summary></span>
271         <span class="xml_comment">''' </span>ASCII code for number ``0``
272         <span class="xml_comment">''' </summary></span>
273         Public Const n0% = Asc("0"c)
274         <span class="xml_comment">''' <summary></span>
275         <span class="xml_comment">''' </span>ASCII code for number ``9``
276         <span class="xml_comment">''' </summary></span>
277         Public Const n9% = Asc("9"c)
278
279         <span class="xml_comment">''' <summary></span>
280         <span class="xml_comment">''' </span>单引号
281         <span class="xml_comment">''' </summary></span>
282         Public Const Mark As Char = "'"c
283
284         Public Shared Function TrimNonPrintings(s$) As String
285             For Each c As Char In Nonprintings
286                 Call s.Trim(c, "")
287             Next
288
289             Return s
290         End Function
291
292         <span class="xml_comment">''' <summary></span>
293         <span class="xml_comment">''' </span>分别替换英文双引号,中文双引号为指定的字符串
294         <span class="xml_comment">''' </summary></span>
295         <span class="xml_comment">''' </span><param name="s"></param>
296         <span class="xml_comment">''' </span><param name="replace"></param>
297         <span class="xml_comment">''' </span><returns></returns>
298         Public Shared Function ReplaceQuot(s As StringOptional replace As String = "'") As String
299             Dim sb As New StringBuilder(s)
300
301             Call sb.Replace(ASCII.Quot, replace)
302             Call sb.Replace(ASCII.QuotBegin_ZHCN, replace)
303             Call sb.Replace(ASCII.QuotEnds_ZHCN, replace)
304             Call sb.Replace(ASCII.QuotUnknown, replace)
305
306             Return sb.ToString
307         End Function
308
309         <MethodImpl(MethodImplOptions.AggressiveInlining)>
310         Public Shared Function IsASCIIString(str As StringAs Boolean
311             Return Not str.Any(Function(c) Asc(c) > 128)
312         End Function
313
314         <span class="xml_comment">''' <summary></span>
315         <span class="xml_comment">''' </span>Symbols without white space.(可以印刷的ASCII符号列表)
316         <span class="xml_comment">''' </summary></span>
317         <span class="xml_comment">''' </span><returns></returns>
318         Public Shared ReadOnly Property Symbols As Char() = ASCII.Byte.GetASCIISymbols().Select(AddressOf Chr).ToArray
319         Public Shared ReadOnly Property AlphaNumericTable As New Dictionary(Of CharInteger)() From {
320             {"0"c, 0}, {"1"c, 1}, {"2"c, 2}, {"3"c, 3}, {"4"c, 4},
321             {"5"c, 5}, {"6"c, 6}, {"7"c, 7}, {"8"c, 8}, {"9"c, 9},
322             {"A"c, 10}, {"B"c, 11}, {"C"c, 12}, {"D"c, 13}, {"E"c, 14}, {"F"c, 15}, {"G"c, 16},
323             {"H"c, 17}, {"I"c, 18}, {"J"c, 19}, {"K"c, 20}, {"L"c, 21}, {"M"c, 22}, {"N"c, 23},
324             {"O"c, 24}, {"P"c, 25}, {"Q"c, 26},
325             {"R"c, 27}, {"S"c, 28}, {"T"c, 29},
326             {"U"c, 30}, {"V"c, 31}, {"W"c, 32},
327             {"X"c, 33}, {"Y"c, 34}, {"Z"c, 35},
328             {" "c, 36},
329             {"$"c, 37},
330             {"%"c, 38},
331             {"*"c, 39},
332             {"+"c, 40},
333             {"-"c, 41},
334             {"."c, 42},
335             {"/"c, 43},
336             {":"c, 44}
337         }
338
339         Public Class [Byte]
340
341             Public Shared Function GetASCIISymbols() As Integer()
342                 Dim code As New List(Of Integer)
343
344                 code += {33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}
345                 code += {58, 59, 60, 61, 62, 63, 64}
346                 code += {91, 92, 93, 94, 95, 96}
347                 code += {123, 124, 125, 126}
348
349                 Return code
350             End Function
351
352             ''' <summary>
353             ''' 0000 0000 0 00 NUL 空字符(Null)
354             ''' </summary>
355             Public Const NUL As Integer = 0
356             ''' <summary>
357             ''' 0000 0001 1 01 SOH 标题开始
358             ''' </summary>
359             Public Const SOH As Integer = 1
360             ''' <summary>
361             ''' 0000 0010 2 02 STX 本文开始
362             ''' </summary>
363             Public Const STX As Integer = 2
364             ''' <summary>
365             ''' 0000 0011 3 03 ETX 本文结束
366             ''' </summary>
367             Public Const ETX As Integer = 3
368             ''' <summary>
369             ''' 0000 0100 4 04 EOT 传输结束
370             ''' </summary>
371             Public Const EOT As Integer = 4
372             ''' <summary>
373             ''' 0000 0101 5 05 ENQ 请求
374             ''' </summary>
375             Public Const ENQ As Integer = 5
376             ''' <summary>
377             ''' 0000 0110 6 06 ACK 确认回应
378             ''' </summary>
379             Public Const ACK As Integer = 6
380             ''' <summary>
381             ''' 0000 0111 7 07 BEL 响铃
382             ''' </summary>
383             Public Const BEL As Integer = 7
384             ''' <summary>
385             ''' 0000 1000 8 08 BS 退格
386             ''' </summary>
387             Public Const BS As Integer = 8
388             ''' <summary>
389             ''' 0000 1001 9 09 HT 水平定位符号
390             ''' </summary>
391             Public Const HT As Integer = 9
392             ''' <summary>
393             ''' 0000 1010 10 0A LF 换行键
394             ''' </summary>
395             Public Const LF As Integer = 10
396             ''' <summary>
397             ''' 0000 1011 11 0B VT 垂直定位符号
398             ''' </summary>
399             Public Const VT As Integer = 11
400             ''' <summary>
401             ''' 0000 1100 12 0C FF 换页键
402             ''' </summary>
403             Public Const FF As Integer = 12
404             ''' <summary>
405             ''' 0000 1101 13 0D CR 归位键
406             ''' </summary>
407             Public Const CR As Integer = 13
408             ''' <summary>
409             ''' 0000 1110 14 0E SO 取消变换(Shift out)
410             ''' </summary>
411             Public Const SO As Integer = 14
412             ''' <summary>
413             ''' 0000 1111 15 0F SI 启用变换(Shift in)
414             ''' </summary>
415             Public Const SI As Integer = 15
416             ''' <summary>
417             ''' 0001 0000 16 10 DLE 跳出数据通讯
418             ''' </summary>
419             Public Const DLE As Integer = 16
420             ''' <summary>
421             ''' 0001 0001 17 11 DC1 设备控制一(XON 启用软件速度控制)
422             ''' </summary>
423             Public Const DC1 As Integer = 17
424             ''' <summary>
425             ''' 0001 0010 18 12 DC2 设备控制二
426             ''' </summary>
427             Public Const DC2 As Integer = 18
428             ''' <summary>
429             ''' 0001 0011 19 13 DC3 设备控制三(XOFF 停用软件速度控制)
430             ''' </summary>
431             Public Const DC3 As Integer = 19
432             ''' <summary>
433             ''' 0001 0100 20 14 DC4 设备控制四
434             ''' </summary>
435             Public Const DC4 As Integer = 20
436             ''' <summary>
437             ''' 0001 0101 21 15 NAK 确认失败回应
438             ''' </summary>
439             Public Const NAK As Integer = 21
440             ''' <summary>
441             ''' 0001 0110 22 16 SYN 同步用暂停
442             ''' </summary>
443             Public Const SYN As Integer = 22
444             ''' <summary>
445             ''' 0001 0111 23 17 ETB 区块传输结束
446             ''' </summary>
447             Public Const ETB As Integer = 23
448             ''' <summary>
449             ''' 0001 1000 24 18 CAN 取消
450             ''' </summary>
451             Public Const CAN As Integer = 24
452             ''' <summary>
453             ''' 0001 1001 25 19 EM 连接介质中断
454             ''' </summary>
455             Public Const EM As Integer = 25
456             ''' <summary>
457             ''' 0001 1010 26 1A SUB 替换
458             ''' </summary>
459             Public Const [SUB] As Integer = 26
460             ''' <summary>
461             ''' 0001 1011 27 1B ESC 跳出
462             ''' </summary>
463             Public Const ESC As Integer = 27
464             ''' <summary>
465             ''' 0001 1100 28 1C FS 文件分割符
466             ''' </summary>
467             Public Const FS As Integer = 28
468             ''' <summary>
469             ''' 0001 1101 29 1D GS 组群分隔符
470             ''' </summary>
471             Public Const GS As Integer = 29
472             ''' <summary>
473             ''' 0001 1110 30 1E RS 记录分隔符
474             ''' </summary>
475             Public Const RS As Integer = 30
476             ''' <summary>
477             ''' 0001 1111 31 1F US 单元分隔符
478             ''' </summary>
479             Public Const US As Integer = 31
480             ''' <summary>
481             ''' 0111 1111 127 7F DEL 删除
482             ''' </summary>
483             Public Const DEL As Integer = 127
484
485             ''' <summary>
486             ''' <see cref="vbTab"/>
487             ''' </summary>
488             Public Const TAB As Integer = Asc(vbTab)
489
490         End Class
491     End Class
492 End Namespace