Skip to content

Commit f4107bc

Browse files
authored
Merge pull request #373 from drewnoakes/spanify-string-value-conversions
Spanify StringValue conversions
2 parents 7954af3 + 818b910 commit f4107bc

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

MetadataExtractor/StringValue.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
22

3+
using System.Buffers.Binary;
4+
35
namespace MetadataExtractor
46
{
57
/// <summary>
@@ -62,7 +64,22 @@ int IConvertible.ToInt32(IFormatProvider? provider)
6264
{
6365
try
6466
{
67+
#if NETSTANDARD1_3 || NETFRAMEWORK
6568
return int.Parse(ToString());
69+
#else
70+
if (Bytes.Length < 100)
71+
{
72+
var encoding = Encoding ?? DefaultEncoding;
73+
int charCount = encoding.GetCharCount(Bytes);
74+
Span<char> chars = stackalloc char[charCount];
75+
encoding.GetChars(Bytes, chars);
76+
return int.Parse(chars);
77+
}
78+
else
79+
{
80+
return int.Parse(ToString());
81+
}
82+
#endif
6683
}
6784
catch (Exception)
6885
{
@@ -86,7 +103,22 @@ uint IConvertible.ToUInt32(IFormatProvider? provider)
86103
{
87104
try
88105
{
106+
#if NETSTANDARD1_3 || NETFRAMEWORK
89107
return uint.Parse(ToString());
108+
#else
109+
if (Bytes.Length < 100)
110+
{
111+
var encoding = Encoding ?? DefaultEncoding;
112+
int charCount = encoding.GetCharCount(Bytes);
113+
Span<char> chars = stackalloc char[charCount];
114+
encoding.GetChars(Bytes, chars);
115+
return uint.Parse(chars);
116+
}
117+
else
118+
{
119+
return uint.Parse(ToString());
120+
}
121+
#endif
90122
}
91123
catch (Exception)
92124
{

0 commit comments

Comments
 (0)