| 1 |
#Region "Microsoft.VisualBasic::208fbfa9b3a38cd9b3df433f5066f6db, Microsoft.VisualBasic.Core\Scripting\Runtime\CType\Casting.vb"
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#End Region
|
| 47 |
|
| 48 |
Imports System.Drawing
|
| 49 |
Imports System.IO
|
| 50 |
Imports System.Runtime.CompilerServices
|
| 51 |
Imports System.Text
|
| 52 |
Imports System.Text.RegularExpressions
|
| 53 |
Imports Microsoft.VisualBasic.ApplicationServices.Debugging.Logging
|
| 54 |
Imports Microsoft.VisualBasic.CommandLine.Reflection
|
| 55 |
Imports Microsoft.VisualBasic.ComponentModel
|
| 56 |
Imports Microsoft.VisualBasic.ComponentModel.Ranges.Model
|
| 57 |
Imports Microsoft.VisualBasic.Imaging
|
| 58 |
Imports Microsoft.VisualBasic.Text
|
| 59 |
Imports Microsoft.VisualBasic.ValueTypes
|
| 60 |
|
| 61 |
Namespace Scripting.Runtime
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
Public Module Casting
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
<Extension>
|
| 76 |
Public Function TryParse(Of T As Structure)(expression As Match, Optional [default] As T = Nothing) As T
|
| 77 |
Dim result As T = Nothing
|
| 78 |
|
| 79 |
If [Enum].TryParse(Of T)(expression.Value, result) Then
|
| 80 |
Return result
|
| 81 |
Else
|
| 82 |
Return [default]
|
| 83 |
End If
|
| 84 |
End Function
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 92 |
<Extension>
|
| 93 |
Public Function ScriptValue(size As Size) As String
|
| 94 |
Return $"{size.Width},{size.Height}"
|
| 95 |
End Function
|
| 96 |
|
| 97 |
<Extension>
|
| 98 |
Public Iterator Function [As](Of T)(source As IEnumerable) As IEnumerable(Of T)
|
| 99 |
Dim l As New List(Of Object)
|
| 100 |
|
| 101 |
For Each x In source
|
| 102 |
l.Add(x)
|
| 103 |
|
| 104 |
If l.Count > 1 Then
|
| 105 |
Exit For
|
| 106 |
End If
|
| 107 |
Next
|
| 108 |
|
| 109 |
If l.Count = 1 AndAlso Not l.First.GetType Is GetType(T) Then
|
| 110 |
|
| 111 |
Dim x = l.First
|
| 112 |
|
| 113 |
|
| 114 |
With DirectCast(x, IEnumerator)
|
| 115 |
Do While .MoveNext
|
| 116 |
Yield DirectCast(.Current, T)
|
| 117 |
Loop
|
| 118 |
End With
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
Else
|
| 125 |
For Each o As Object In source
|
| 126 |
Yield DirectCast(o, T)
|
| 127 |
Next
|
| 128 |
End If
|
| 129 |
End Function
|
| 130 |
|
| 131 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 132 |
<Extension>
|
| 133 |
Public Function NumericRangeParser(exp As String) As DoubleRange
|
| 134 |
Return CType(exp, DoubleRange)
|
| 135 |
End Function
|
| 136 |
|
| 137 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 138 |
<Extension>
|
| 139 |
Public Function [As](Of T As {IComparable(Of T), Structure})(x As Double) As T
|
| 140 |
Return CType(CObj(x), T)
|
| 141 |
End Function
|
| 142 |
|
| 143 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 144 |
<Extension>
|
| 145 |
Public Function Expression(size As Size) As String
|
| 146 |
With size
|
| 147 |
Return $"{ .Width},{ .Height}"
|
| 148 |
End With
|
| 149 |
End Function
|
| 150 |
|
| 151 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 152 |
<Extension>
|
| 153 |
Public Function Expression(size As SizeF) As String
|
| 154 |
With size
|
| 155 |
Return $"{ .Width},{ .Height}"
|
| 156 |
End With
|
| 157 |
End Function
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
Public Function PointParser(pt$) As Point
|
| 165 |
Dim x, y As Double
|
| 166 |
Call Ranges.Parser(pt, x, y)
|
| 167 |
Return New Point(x, y)
|
| 168 |
End Function
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
Public Function FloatPointParser(pt$) As PointF
|
| 176 |
Dim x, y As Double
|
| 177 |
Call Ranges.Parser(pt, x, y)
|
| 178 |
Return New PointF(x, y)
|
| 179 |
End Function
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 187 |
<Extension> Public Function SizeParser(pt$) As Size
|
| 188 |
Return pt.FloatSizeParser.ToSize
|
| 189 |
End Function
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
<Extension>
|
| 197 |
Public Function FloatSizeParser(pt$) As SizeF
|
| 198 |
If pt.StringEmpty Then
|
| 199 |
Return Nothing
|
| 200 |
Else
|
| 201 |
Dim x, y As Double
|
| 202 |
Call Ranges.Parser(pt, x, y)
|
| 203 |
Return New SizeF(x, y)
|
| 204 |
End If
|
| 205 |
End Function
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 218 |
<Extension>
|
| 219 |
Public Function AsBaseType(Of TIn As Class, T)(obj As TIn) As T
|
| 220 |
If obj Is Nothing Then
|
| 221 |
Return Nothing
|
| 222 |
Else
|
| 223 |
Return DirectCast(CObj(obj), T)
|
| 224 |
End If
|
| 225 |
End Function
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
<Extension> Public Function [As](Of T, TOut)(list As IEnumerable(Of T)) As TOut()
|
| 235 |
If list Is Nothing Then
|
| 236 |
Return {}
|
| 237 |
Else
|
| 238 |
Return list _
|
| 239 |
.Select(Function(x) CType(CObj(x), TOut)) _
|
| 240 |
.ToArray
|
| 241 |
End If
|
| 242 |
End Function
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
Public Const RegexpDouble$ = "-?\d+(\.\d+)?"
|
| 248 |
Public Const ScientificNotation$ = RegexpDouble & "[Ee][+-]\d+"
|
| 249 |
Public Const RegexpFloat$ = RegexpDouble & "([Ee][+-]\d+)?"
|
| 250 |
Public Const RegexInteger$ = "[-]?\d+"
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 260 |
<ExportAPI("Double.Match")>
|
| 261 |
<Extension> Public Function RegexParseDouble(s As String) As Double
|
| 262 |
Return Val(s.Match(RegexpFloat))
|
| 263 |
End Function
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
<Extension>
|
| 277 |
Public Function ParseNumeric(s As String) As Double
|
| 278 |
s = Strings.Trim(s)
|
| 279 |
|
| 280 |
If String.IsNullOrEmpty(s) Then
|
| 281 |
Return 0R
|
| 282 |
ElseIf String.Equals(s, "NaN", StringComparison.Ordinal) OrElse
|
| 283 |
String.Equals(s, "NA", StringComparison.Ordinal) Then
|
| 284 |
|
| 285 |
|
| 286 |
Return Double.NaN
|
| 287 |
Else
|
| 288 |
s = s.Replace(",", "")
|
| 289 |
End If
|
| 290 |
|
| 291 |
If s.Last = "%"c Then
|
| 292 |
Return Conversion.Val(Mid(s, 1, s.Length - 1)) / 100
|
| 293 |
ElseIf InStr(s, "/") > 0 Then
|
| 294 |
Dim t$() = s.Split("/"c)
|
| 295 |
Return Val(t(0)) / Val(t(1))
|
| 296 |
ElseIf InStr(s, "e", CompareMethod.Text) > 0 Then
|
| 297 |
Dim t = s.ToLower.Split("e"c)
|
| 298 |
Return Val(t(0)) * (10 ^ Val(t(1)))
|
| 299 |
Else
|
| 300 |
Return Conversion.Val(s)
|
| 301 |
End If
|
| 302 |
End Function
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 311 |
Public Function CastChar(obj As String) As Char
|
| 312 |
Return If(String.IsNullOrEmpty(obj), ASCII.NUL, obj.First)
|
| 313 |
End Function
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 322 |
Public Function CastInteger(obj As String) As Integer
|
| 323 |
Return CInt(ParseNumeric(obj))
|
| 324 |
End Function
|
| 325 |
|
| 326 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 327 |
Public Function CastLong(obj As String) As Long
|
| 328 |
Return CLng(ParseNumeric(obj))
|
| 329 |
End Function
|
| 330 |
|
| 331 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 332 |
Public Function CastCharArray(obj As String) As Char()
|
| 333 |
Return obj.ToArray
|
| 334 |
End Function
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 342 |
Public Function CastDate(obj As String) As DateTime
|
| 343 |
If obj.StringEmpty OrElse obj = "0000-00-00 00:00:00" OrElse obj.ToUpper = "NULL" OrElse obj.ToUpper = "NA" Then
|
| 344 |
Return New Date
|
| 345 |
ElseIf obj.IsPattern("\d+") Then
|
| 346 |
|
| 347 |
Return CLng(Val(obj)).FromUnixTimeStamp
|
| 348 |
Else
|
| 349 |
Return DateTime.Parse(obj)
|
| 350 |
End If
|
| 351 |
End Function
|
| 352 |
|
| 353 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 354 |
Public Function CastStringBuilder(obj As String) As StringBuilder
|
| 355 |
Return New StringBuilder(obj)
|
| 356 |
End Function
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 365 |
Public Function CastCommandLine(obj As String) As CommandLine.CommandLine
|
| 366 |
Return CommandLine.TryParse(obj)
|
| 367 |
End Function
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 376 |
Public Function CastImage(path As String) As Image
|
| 377 |
Return LoadImage(path)
|
| 378 |
End Function
|
| 379 |
|
| 380 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 381 |
Public Function CastFileInfo(path As String) As FileInfo
|
| 382 |
Return FileIO.FileSystem.GetFileInfo(path)
|
| 383 |
End Function
|
| 384 |
|
| 385 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 386 |
Public Function CastGDIPlusDeviceHandle(path As String) As Graphics2D
|
| 387 |
Return CanvasCreateFromImageFile(path)
|
| 388 |
End Function
|
| 389 |
|
| 390 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 391 |
Public Function CastFont(face As String) As Font
|
| 392 |
Return New Font(face, 10)
|
| 393 |
End Function
|
| 394 |
|
| 395 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 396 |
Public Function CastIPEndPoint(addr As String) As System.Net.IPEndPoint
|
| 397 |
Return New Net.IPEndPoint(addr).GetIPEndPoint
|
| 398 |
End Function
|
| 399 |
|
| 400 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 401 |
Public Function CastLogFile(path As String) As LogFile
|
| 402 |
Return New LogFile(path)
|
| 403 |
End Function
|
| 404 |
|
| 405 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 406 |
Public Function CastProcess(exe As String) As Process
|
| 407 |
Return Process.Start(exe)
|
| 408 |
End Function
|
| 409 |
|
| 410 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 411 |
Public Function CastSingle(n As String) As Single
|
| 412 |
Return CSng(ParseNumeric(n))
|
| 413 |
End Function
|
| 414 |
|
| 415 |
Public Function CastRegexOptions(name As String) As RegexOptions
|
| 416 |
If String.Equals(name, RegexExtensions.NameOf.Compiled, StringComparison.OrdinalIgnoreCase) Then
|
| 417 |
Return RegexOptions.Compiled
|
| 418 |
ElseIf String.Equals(name, RegexExtensions.NameOf.CultureInvariant, StringComparison.OrdinalIgnoreCase) Then
|
| 419 |
Return RegexOptions.CultureInvariant
|
| 420 |
ElseIf String.Equals(name, RegexExtensions.NameOf.ECMAScript, StringComparison.OrdinalIgnoreCase) Then
|
| 421 |
Return RegexOptions.ECMAScript
|
| 422 |
ElseIf String.Equals(name, RegexExtensions.NameOf.ExplicitCapture, StringComparison.OrdinalIgnoreCase) Then
|
| 423 |
Return RegexOptions.ExplicitCapture
|
| 424 |
ElseIf String.Equals(name, RegexExtensions.NameOf.IgnoreCase, StringComparison.OrdinalIgnoreCase) Then
|
| 425 |
Return RegexOptions.IgnoreCase
|
| 426 |
ElseIf String.Equals(name, RegexExtensions.NameOf.IgnorePatternWhitespace, StringComparison.OrdinalIgnoreCase) Then
|
| 427 |
Return RegexOptions.IgnorePatternWhitespace
|
| 428 |
ElseIf String.Equals(name, RegexExtensions.NameOf.Multiline, StringComparison.OrdinalIgnoreCase) Then
|
| 429 |
Return RegexOptions.Multiline
|
| 430 |
ElseIf String.Equals(name, RegexExtensions.NameOf.RightToLeft, StringComparison.OrdinalIgnoreCase) Then
|
| 431 |
Return RegexOptions.RightToLeft
|
| 432 |
ElseIf String.Equals(name, RegexExtensions.NameOf.Singleline, StringComparison.OrdinalIgnoreCase) Then
|
| 433 |
Return RegexOptions.Singleline
|
| 434 |
Else
|
| 435 |
Return RegexOptions.None
|
| 436 |
End If
|
| 437 |
End Function
|
| 438 |
End Module
|
| 439 |
End Namespace
|