1 //
2 // Copyright (c) 2010-2020 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 
8 using System;
9 
10 namespace Antmicro.Renode.Utilities
11 {
12     public enum Rank
13     {
14         Units = 1,
15         Tens = 10
16     }
17 
18     public static class IntegerRankExtensions
19     {
ReadRank(this int value, Rank rank)20         public static int ReadRank(this int value, Rank rank)
21         {
22             switch(rank)
23             {
24                 case Rank.Tens:
25                     return (value / 10) % 10;
26                 case Rank.Units:
27                     return value % 10;
28                 default:
29                     throw new ArgumentException($"Unsupported rank: {rank}");
30             }
31         }
32 
33         // Returns 'current' with updated tens or units
WithUpdatedRank(this int current, int value, Rank rank)34         public static int WithUpdatedRank(this int current, int value, Rank rank)
35         {
36             if(value < 0 || value > 9)
37             {
38                 throw new ArgumentException($"Expected a single-digit value, but got: {value}");
39             }
40 
41             switch(rank)
42             {
43                 case Rank.Tens:
44                     return current + 10 * (value - current.ReadRank(Rank.Tens));
45                 case Rank.Units:
46                     return current + (value - current.ReadRank(Rank.Units));
47                 default:
48                     throw new ArgumentException($"Unsupported rank: {rank}");
49             }
50         }
51     }
52 }
53