| 1 | #Region "Microsoft.VisualBasic::8fd356624247730ac0b79e2c04911f69, Microsoft.VisualBasic.Core\Scripting\Runtime\CType\NumberConversionRoutines.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 NumberConversionRoutines |
| 35 | ' |
| 36 | ' Function: CDblSafe, (+2 Overloads) CIntSafe, (+2 Overloads) CShortSafe, CStrInternal, CStrSafe |
| 37 | ' IsNumber |
| 38 | ' |
| 39 | ' |
| 40 | ' /********************************************************************************/ |
| 41 | |
| 42 | #End Region |
| 43 | |
| 44 | Namespace Scripting.Runtime |
| 45 | |
| 46 | ''' <summary> |
| 47 | ''' 这个模块之中包含有安全的将字符串解析为不同的数值类型的方法函数 |
| 48 | ''' </summary> |
| 49 | Public Module NumberConversionRoutines |
| 50 | |
| 51 | Public Function CDblSafe(strWork As String) As Double |
| 52 | Dim dblValue As Double = 0 |
| 53 | |
| 54 | If Double.TryParse(strWork, dblValue) Then |
| 55 | Return dblValue |
| 56 | Else |
| 57 | Return 0 |
| 58 | End If |
| 59 | End Function |
| 60 | |
| 61 | Public Function CShortSafe(dblWork As Double) As Int16 |
| 62 | If dblWork <= 32767 And dblWork >= -32767 Then |
| 63 | Return CShort(dblWork) |
| 64 | Else |
| 65 | If dblWork < 0 Then |
| 66 | Return -32767 |
| 67 | Else |
| 68 | Return 32767 |
| 69 | End If |
| 70 | End If |
| 71 | End Function |
| 72 | |
| 73 | Public Function CShortSafe(strWork As String) As Int16 |
| 74 | Dim dblValue As Double = 0 |
| 75 | |
| 76 | If Double.TryParse(strWork, dblValue) Then |
| 77 | Return CShortSafe(dblValue) |
| 78 | ElseIf strWork.ToLower() = "true" Then |
| 79 | Return -1 |
| 80 | Else |
| 81 | Return 0 |
| 82 | End If |
| 83 | End Function |
| 84 | |
| 85 | Public Function CIntSafe(dblWork As Double) As Int32 |
| 86 | If dblWork <= Integer.MaxValue And dblWork >= Integer.MinValue Then |
| 87 | Return CInt(dblWork) |
| 88 | Else |
| 89 | If dblWork < 0 Then |
| 90 | Return Integer.MinValue |
| 91 | Else |
| 92 | Return Integer.MaxValue |
| 93 | End If |
| 94 | End If |
| 95 | End Function |
| 96 | |
| 97 | Public Function CIntSafe(strWork As String) As Int32 |
| 98 | Dim dblValue As Double = 0 |
| 99 | |
| 100 | If Double.TryParse(strWork, dblValue) Then |
| 101 | Return CIntSafe(dblValue) |
| 102 | ElseIf strWork.ToLower() = "true" Then |
| 103 | Return -1 |
| 104 | Else |
| 105 | Return 0 |
| 106 | End If |
| 107 | End Function |
| 108 | |
| 109 | ReadOnly cstrCache As New Dictionary(Of Type, INarrowingOperator(Of Object, String)) |
| 110 | |
| 111 | ''' <summary> |
| 112 | ''' 安全的将目标对象转换为字符串值 |
| 113 | ''' </summary> |
| 114 | ''' <param name="obj"></param> |
| 115 | ''' <returns></returns> |
| 116 | Public Function CStrSafe(obj As Object, Optional default$ = "") As String |
| 117 | If obj Is Nothing Then |
| 118 | Return String.Empty |
| 119 | ElseIf Convert.IsDBNull(obj) Then |
| 120 | Return String.Empty |
| 121 | Else |
| 122 | |
| 123 | Try |
| 124 | Return CStrInternal(obj, [default]) |
| 125 | Catch ex As Exception |
| 126 | Try |
| 127 | ' 调用ToString函数来返回字符串值 |
| 128 | Return obj.ToString |
| 129 | Catch ex2 As Exception |
| 130 | Return [default] |
| 131 | End Try |
| 132 | End Try |
| 133 | |
| 134 | End If |
| 135 | End Function |
| 136 | |
| 137 | Private Function CStrInternal(obj As Object, default$) As String |
| 138 | Dim type As Type = obj.GetType |
| 139 | Dim delg As INarrowingOperator(Of Object, String) |
| 140 | |
| 141 | ' 2018-3-18 假若找不到操作符的话,函数会返回Nothing |
| 142 | ' 同样也需要将这个Nothing添加进入字典之中,否则在运行linq代码的时候 |
| 143 | ' 查找函数会被重复执行,同样会造成性能浪费 |
| 144 | If Not cstrCache.ContainsKey(type) Then |
| 145 | delg = type.GetNarrowingOperator(Of String) |
| 146 | |
| 147 | SyncLock cstrCache |
| 148 | Call cstrCache.Add(type, delg) |
| 149 | End SyncLock |
| 150 | Else |
| 151 | delg = cstrCache(type) |
| 152 | End If |
| 153 | |
| 154 | Try |
| 155 | If delg Is Nothing Then |
| 156 | ' 调用ToString函数来返回字符串值 |
| 157 | Return obj.ToString |
| 158 | Else |
| 159 | Return delg(obj) |
| 160 | End If |
| 161 | Catch ex2 As Exception |
| 162 | Return [default] |
| 163 | End Try |
| 164 | End Function |
| 165 | |
| 166 | Public Function IsNumber(strValue As String) As Boolean |
| 167 | Try |
| 168 | Return Double.TryParse(strValue, 0) |
| 169 | Catch ex As Exception |
| 170 | Return False |
| 171 | End Try |
| 172 | End Function |
| 173 | End Module |
| 174 | End Namespace |