1 #Region "Microsoft.VisualBasic::8d08da1a3e9f42a5b1c12805fe56e39d, Microsoft.VisualBasic.Core\CommandLine\Parsers\DictionaryParser.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 DictionaryParser
35     
36     '         FunctionTryParse
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports Microsoft.VisualBasic.Emit.Marshal
44 Imports Microsoft.VisualBasic.Text
45 Imports Microsoft.VisualBasic.Language
46
47 Namespace CommandLine.Parsers
48
49     Public NotInheritable Class DictionaryParser
50
51         ''' <summary>
52         ''' 键值对之间使用分号分隔
53         ''' </summary>
54         ''' <param name="str$"></param>
55         ''' <returns></returns>
56         Public Shared Function TryParse(str$) As Dictionary(Of StringString)
57             Dim chars As New Pointer(Of Char)(str$)
58             Dim tmp As New List(Of Char)
59             Dim out As New Dictionary(Of StringString)
60             Dim t As New List(Of String)
61             Dim markOpen As Boolean = False
62             Dim left As Char
63
64             Do While Not chars.EndRead
65                 Dim c As Char = +chars
66
67                 tmp += c
68
69                 If c = ASCII.Mark Then
70                     If Not markOpen Then
71                         If left = "="Then
72                             markOpen = True
73                         End If
74                     Else
75                         If chars.Current = ";"Then
76                             markOpen = False
77                             t += New String(tmp)
78                             tmp.Clear()
79                             chars += 1
80                         End If
81                     End If
82                 ElseIf c = ";"Then
83                     If Not markOpen Then
84                         tmp.RemoveLast
85                         t += New String(tmp)
86                         tmp.Clear()
87                     End If
88                 End If
89
90                 left = c
91             Loop
92
93             If tmp.Count > 0 Then
94                 t += New String(tmp)
95             End If
96
97             For Each var$ In t
98                 Dim value = var.GetTagValue("="c)
99                 out(value.Name) =
100                     value.Value.GetString("'")
101             Next
102
103             Return out
104         End Function
105     End Class
106 End Namespace