@@ -2,6 +2,8 @@ namespace Vezel.Novadrop.Data;
2
2
3
3
public static class DataCenterExtensions
4
4
{
5
+ private static readonly CultureInfo _culture = CultureInfo . InvariantCulture ;
6
+
5
7
public static DataCenterNode Child ( this DataCenterNode node )
6
8
{
7
9
Check . Null ( node ) ;
@@ -192,16 +194,20 @@ public static IEnumerable<DataCenterNode> DescendantsAt(this DataCenterNode node
192
194
return node . DescendantsAt ( path . ToArray ( ) ) ;
193
195
}
194
196
195
- public static int ToInt32 ( this DataCenterValue value )
197
+ public static int ToInt32 ( this DataCenterValue value , int radix = 10 )
196
198
{
199
+ Check . Range ( radix is 2 or 10 or 16 , radix ) ;
200
+
197
201
return value . TypeCode switch
198
202
{
199
203
DataCenterTypeCode . Int32 => value . UnsafeAsInt32 ,
200
204
DataCenterTypeCode . Single => ( int ) value . UnsafeAsSingle ,
201
- DataCenterTypeCode . String => value . UnsafeAsString switch
205
+ DataCenterTypeCode . String => radix switch
202
206
{
203
- var s when int . TryParse ( s , NumberStyles . Integer , CultureInfo . InvariantCulture , out var i ) => i ,
204
- var s => int . Parse ( s , NumberStyles . HexNumber , CultureInfo . InvariantCulture ) ,
207
+ 2 => int . Parse ( value . UnsafeAsString , NumberStyles . BinaryNumber , _culture ) ,
208
+ 10 => int . Parse ( value . UnsafeAsString , NumberStyles . Integer , _culture ) ,
209
+ 16 => int . Parse ( value . UnsafeAsString , NumberStyles . HexNumber , _culture ) ,
210
+ _ => throw new UnreachableException ( ) ,
205
211
} ,
206
212
DataCenterTypeCode . Boolean => value . UnsafeAsInt32 , // Normalized internally; reinterpret as 0 or 1.
207
213
_ => 0 ,
@@ -214,8 +220,7 @@ public static float ToSingle(this DataCenterValue value)
214
220
{
215
221
DataCenterTypeCode . Int32 => value . UnsafeAsInt32 ,
216
222
DataCenterTypeCode . Single => value . UnsafeAsSingle ,
217
- DataCenterTypeCode . String =>
218
- float . Parse ( value . UnsafeAsString , NumberStyles . Float , CultureInfo . InvariantCulture ) ,
223
+ DataCenterTypeCode . String => float . Parse ( value . UnsafeAsString , NumberStyles . Float , _culture ) ,
219
224
DataCenterTypeCode . Boolean => value . UnsafeAsInt32 , // Normalized internally; reinterpret as 0 or 1.
220
225
_ => 0.0f ,
221
226
} ;
0 commit comments