1 #Region "Microsoft.VisualBasic::5b6f4bd1fb367932cc365a51c38f15fc, Microsoft.VisualBasic.Core\Extensions\StringHelpers\Parser.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     Module PrimitiveParser
35     
36     '     Properties: BooleanValues
37     
38     '     Function: Eval, IsNumeric, (+2 Overloads) ParseBoolean, ParseDate, ParseDouble
39     '               ParseInteger, ParseLong, ParseSingle
40     
41     ' /********************************************************************************/
42
43 #End Region
44
45 Imports System.Runtime.CompilerServices
46 Imports Microsoft.VisualBasic.CommandLine.Reflection
47 Imports Microsoft.VisualBasic.Language
48 Imports Microsoft.VisualBasic.Scripting.Runtime
49 Imports Microsoft.VisualBasic.Text
50 Imports r = System.Text.RegularExpressions.Regex
51
52 ''' <summary>
53 ''' Simple type parser extension function for <see cref="String"/>
54 ''' </summary>
55 Public Module PrimitiveParser
56
57     ''' <summary>
58     ''' Evaluate the given string expression as numeric value 
59     ''' </summary>
60     ''' <param name="expression$"></param>
61     ''' <param name="default#"></param>
62     ''' <returns></returns>
63     Public Function Eval(expression$, default#) As Double
64         If expression Is Nothing Then
65             Return [default]
66         Else
67             Return Conversion.Val(expression)
68         End If
69     End Function
70
71     ''' <summary>
72     ''' 用于匹配任意实数的正则表达式
73     ''' </summary>
74     Public Const NumericPattern$ = "[-]?\d*(\.\d+)?([eE][-]?\d*)?"
75
76     ''' <summary>
77     ''' Is this token value string is a number?
78     ''' </summary>
79     ''' <param name="str"></param>
80     ''' <returns></returns>
81     <ExportAPI("IsNumeric"Info:="Is this token value string is a number?")>
82     <Extension> Public Function IsNumeric(str$) As Boolean
83         With str.GetString(ASCII.Quot)
84             Dim s$ = r.Match(.ByRef, NumericPattern).Value
85             Return .ByRef = s
86         End With
87     End Function
88
89     ''' <summary>
90     ''' <see cref="Integer"/> text parser
91     ''' </summary>
92     ''' <param name="s"></param>
93     ''' <returns></returns>
94     ''' 
95     <MethodImpl(MethodImplOptions.AggressiveInlining)>
96     <Extension>
97     Public Function ParseInteger(s As StringAs Integer
98         Return CInt(Val(Trim(s)))
99     End Function
100
101     ''' <summary>
102     ''' <see cref="Long"/> text parser
103     ''' </summary>
104     ''' <param name="s"></param>
105     ''' <returns></returns>
106     ''' 
107     <MethodImpl(MethodImplOptions.AggressiveInlining)>
108     <Extension>
109     Public Function ParseLong(s As StringAs Long
110         Return CLng(Val(Trim(s)))
111     End Function
112
113     ''' <summary>
114     ''' <see cref="Double"/> text parser
115     ''' </summary>
116     ''' <param name="s"></param>
117     ''' <returns></returns>
118     ''' 
119     <MethodImpl(MethodImplOptions.AggressiveInlining)>
120     <Extension>
121     Public Function ParseDouble(s As StringAs Double
122         If s Is Nothing Then
123             Return 0
124         Else
125             Return ParseNumeric(s)
126         End If
127     End Function
128
129     ''' <summary>
130     ''' <see cref="Single"/> text parser
131     ''' </summary>
132     ''' <param name="s"></param>
133     ''' <returns></returns>
134     ''' 
135     <MethodImpl(MethodImplOptions.AggressiveInlining)>
136     <Extension>
137     Public Function ParseSingle(s As StringAs Single
138         Return CSng(Val(Trim(s)))
139     End Function
140
141     ''' <summary>
142     ''' <see cref="Date"/> text parser
143     ''' </summary>
144     ''' <param name="s"></param>
145     ''' <returns></returns>
146     ''' 
147     <MethodImpl(MethodImplOptions.AggressiveInlining)>
148     <Extension>
149     Public Function ParseDate(s As StringAs Date
150         Return Date.Parse(Trim(s))
151     End Function
152
153     ''' <summary>
154     ''' Convert the string value into the boolean value, this is useful to the text format configuration file into data model.
155     ''' </summary>
156     ''' <returns></returns>
157     Public ReadOnly Property BooleanValues As New SortedDictionary(Of StringBoolean) From {
158  _
159             {"t"True}, {"true"True},
160             {"1"True},
161             {"y"True}, {"yes"True}, {"ok"True},
162             {"ok!"True},
163             {"success"True}, {"successful"True}, {"successfully"True}, {"succeeded"True},
164             {"right"True},
165             {"wrong"False},
166             {"failure"False}, {"failures"False},
167             {"exception"False},
168             {"error"False}, {"err"False},
169             {"f"False}, {"false"False},
170             {"0"False},
171             {"n"False}, {"no"False}
172         }
173
174     ''' <summary>
175     ''' Convert the string value into the boolean value, this is useful to the text format configuration file into data model.
176     ''' (请注意,空值字符串为False,如果字符串不存在与单词表之中,则也是False)
177     ''' </summary>
178     ''' <param name="str"></param>
179     ''' <returns></returns>
180     ''' <remarks></remarks>
181     <ExportAPI("Get.Boolean")> <Extension> Public Function ParseBoolean(str$) As Boolean
182         If String.IsNullOrEmpty(str) Then
183             Return False
184         Else
185             str = str.ToLower.Trim
186         End If
187
188         If BooleanValues.ContainsKey(key:=str) Then
189             Return BooleanValues(str)
190         Else
191 #If DEBUG Then
192             Call $"""{str}"" {NameOf([Boolean])} (null_value_definition)  ==> False".__DEBUG_ECHO
193 #End If
194             Return False
195         End If
196     End Function
197
198     <Extension> <ExportAPI("Get.Boolean")> Public Function ParseBoolean(ch As CharAs Boolean
199         If ch = ASCII.NUL Then
200             Return False
201         End If
202
203         Select Case ch
204             Case "y"c, "Y"c, "t"c, "T"c, "1"c
205                 Return True
206             Case "n"c, "N"c, "f"c, "F"c, "0"c
207                 Return False
208         End Select
209
210         Return True
211     End Function
212 End Module