@@ -19,6 +19,78 @@ public abstract class Watch
1919 IEquatable < Cheat > ,
2020 IComparable < Watch >
2121 {
22+ private const string ERR_MSG_INVALID_WIDTH = "can only parse numeric strings for 1-, 2-, or 4-octet watches" ;
23+
24+ public static string FormatValue ( uint value , WatchSize width , WatchDisplayType dispType )
25+ => width switch
26+ {
27+ WatchSize . Byte => dispType switch
28+ {
29+ WatchDisplayType . Signed => unchecked ( ( sbyte ) value ) . ToString ( ) ,
30+ WatchDisplayType . Unsigned => value . ToString ( ) ,
31+ WatchDisplayType . Hex => $ "{ value : X2} ",
32+ WatchDisplayType . Binary => Convert . ToString ( unchecked ( ( byte ) value ) , toBase : 2 ) . PadLeft ( 8 , '0' ) . Insert ( 4 , " " ) ,
33+ _ => value . ToString ( ) , //TODO throw instead?
34+ } ,
35+ WatchSize . Word => dispType switch
36+ {
37+ WatchDisplayType . Signed => unchecked ( ( short ) value ) . ToString ( ) ,
38+ WatchDisplayType . Unsigned => value . ToString ( ) ,
39+ WatchDisplayType . Hex => $ "{ value : X4} ",
40+ WatchDisplayType . Binary => Convert . ToString ( unchecked ( ( ushort ) value ) , toBase : 2 ) . PadLeft ( 16 , '0' )
41+ . Insert ( 8 , " " ) . Insert ( 4 , " " ) . Insert ( 14 , " " ) ,
42+ WatchDisplayType . FixedPoint_12_4 => $ "{ unchecked ( ( short ) value ) / 16.0 : F4} ",
43+ _ => value . ToString ( ) , //TODO throw instead?
44+ } ,
45+ WatchSize . DWord => dispType switch
46+ {
47+ WatchDisplayType . Signed => unchecked ( ( int ) value ) . ToString ( ) ,
48+ WatchDisplayType . Unsigned => value . ToString ( ) ,
49+ WatchDisplayType . Hex => $ "{ value : X8} ",
50+ WatchDisplayType . Binary => Convert . ToString ( value , toBase : 2 ) . PadLeft ( 32 , '0' )
51+ . Insert ( 28 , " " ) . Insert ( 24 , " " ) . Insert ( 20 , " " ) . Insert ( 16 , " " ) . Insert ( 12 , " " ) . Insert ( 8 , " " ) . Insert ( 4 , " " ) ,
52+ WatchDisplayType . FixedPoint_20_12 => $ "{ unchecked ( ( int ) value ) / 4096.0 : 0.######} ",
53+ WatchDisplayType . FixedPoint_16_16 => $ "{ unchecked ( ( int ) value ) / 65536.0 : 0.######} ",
54+ WatchDisplayType . Float => NumberExtensions . ReinterpretAsF32 ( value ) . ToString ( NumberFormatInfo . InvariantInfo ) ,
55+ _ => value . ToString ( ) , //TODO throw instead?
56+ } ,
57+ _ => throw new ArgumentOutOfRangeException ( paramName : nameof ( width ) , width , message : ERR_MSG_INVALID_WIDTH ) ,
58+ } ;
59+
60+ public static uint ParseValue ( string value , WatchSize width , WatchDisplayType dispType )
61+ => width switch
62+ {
63+ WatchSize . Byte => dispType switch
64+ {
65+ WatchDisplayType . Signed => unchecked ( ( byte ) sbyte . Parse ( value ) ) ,
66+ WatchDisplayType . Unsigned => byte . Parse ( value ) ,
67+ WatchDisplayType . Hex => byte . Parse ( value , NumberStyles . HexNumber ) ,
68+ WatchDisplayType . Binary => Convert . ToByte ( value , fromBase : 2 ) ,
69+ _ => 0 , //TODO throw instead?
70+ } ,
71+ WatchSize . Word => dispType switch
72+ {
73+ WatchDisplayType . Signed => unchecked ( ( ushort ) short . Parse ( value ) ) ,
74+ WatchDisplayType . Unsigned => ushort . Parse ( value ) ,
75+ WatchDisplayType . Hex => ushort . Parse ( value , NumberStyles . HexNumber ) ,
76+ WatchDisplayType . Binary => Convert . ToUInt16 ( value , fromBase : 2 ) ,
77+ WatchDisplayType . FixedPoint_12_4 => unchecked ( ( ushort ) ( 16.0 * double . Parse ( value , NumberFormatInfo . InvariantInfo ) ) ) ,
78+ _ => 0 , //TODO throw instead?
79+ } ,
80+ WatchSize . DWord => dispType switch
81+ {
82+ WatchDisplayType . Signed => unchecked ( ( uint ) int . Parse ( value ) ) ,
83+ WatchDisplayType . Unsigned => uint . Parse ( value ) ,
84+ WatchDisplayType . Hex => uint . Parse ( value , NumberStyles . HexNumber ) ,
85+ WatchDisplayType . Binary => Convert . ToUInt32 ( value , fromBase : 2 ) ,
86+ WatchDisplayType . FixedPoint_20_12 => unchecked ( ( uint ) ( 4096.0 * double . Parse ( value , NumberFormatInfo . InvariantInfo ) ) ) ,
87+ WatchDisplayType . FixedPoint_16_16 => unchecked ( ( uint ) ( 65536.0 * double . Parse ( value , NumberFormatInfo . InvariantInfo ) ) ) ,
88+ WatchDisplayType . Float => NumberExtensions . ReinterpretAsUInt32 ( float . Parse ( value , NumberFormatInfo . InvariantInfo ) ) ,
89+ _ => 0 , //TODO throw instead?
90+ } ,
91+ _ => throw new ArgumentOutOfRangeException ( paramName : nameof ( width ) , width , message : ERR_MSG_INVALID_WIDTH ) ,
92+ } ;
93+
2294 private MemoryDomain _domain ;
2395 private WatchDisplayType _type ;
2496
0 commit comments