| | 1 | | using System; |
| | 2 | | using System.Globalization; |
| | 3 | |
|
| | 4 | | namespace LockCheck.Linux; |
| | 5 | |
|
| | 6 | | internal readonly struct InodeInfo |
| | 7 | | { |
| | 8 | | public static bool TryParse(ReadOnlySpan<char> field, out InodeInfo value) |
| | 9 | | { |
| | 10 | | #if NET9_0_OR_GREATER |
| | 11 | | // This implementation *look* more complex and thus slower, but is actually faster: |
| | 12 | | // |
| | 13 | | // | Method | Mean | Error | StdDev | Ratio | RatioSD | Code Size | Allocated | Alloc Ratio | |
| | 14 | | // |------- |---------:|---------:|---------:|------:|--------:|----------:|----------:|------------:| |
| | 15 | | // | Old | 71.97 ns | 1.462 ns | 1.900 ns | 1.00 | 0.04 | 1,604 B | - | NA | |
| | 16 | | // | NET9.0 | 51.40 ns | 0.800 ns | 0.668 ns | 0.71 | 0.02 | 1,355 B | - | NA | |
| | 17 | |
|
| 2 | 18 | | int num = 0; |
| 2 | 19 | | int major = 0; |
| 2 | 20 | | int minor = 0; |
| 2 | 21 | | long number = 0; |
| | 22 | |
|
| 2 | 23 | | foreach (var range in field.Split(':')) |
| | 24 | | { |
| 2 | 25 | | var fieldContent = field[range].Trim(); |
| | 26 | |
|
| | 27 | | switch (num) |
| | 28 | | { |
| | 29 | | case 0: |
| 2 | 30 | | if (!int.TryParse(fieldContent, NumberStyles.HexNumber, null, out major)) |
| | 31 | | { |
| 0 | 32 | | goto fail; |
| | 33 | | } |
| | 34 | | break; |
| | 35 | | case 1: |
| 2 | 36 | | if (!int.TryParse(fieldContent, NumberStyles.HexNumber, null, out minor)) |
| | 37 | | { |
| 0 | 38 | | goto fail; |
| | 39 | | } |
| | 40 | | break; |
| | 41 | | case 2: |
| 2 | 42 | | if (!long.TryParse(fieldContent, NumberStyles.Integer, null, out number)) |
| | 43 | | { |
| | 44 | | goto fail; |
| | 45 | | } |
| | 46 | | break; |
| | 47 | | } |
| | 48 | |
|
| 2 | 49 | | if (++num > 2) |
| | 50 | | { |
| | 51 | | // Ignore additional fields |
| | 52 | | break; |
| | 53 | | } |
| | 54 | | } |
| | 55 | |
|
| 2 | 56 | | if (num < 2) |
| | 57 | | { |
| | 58 | | // Not enough fields |
| | 59 | | goto fail; |
| | 60 | | } |
| | 61 | |
|
| 2 | 62 | | value = new InodeInfo(major, minor, number); |
| 2 | 63 | | return true; |
| | 64 | | fail: |
| 0 | 65 | | value = default; |
| 0 | 66 | | return false; |
| | 67 | | #else |
| | 68 | | int count = field.Count(':') + 1; |
| | 69 | | Span<Range> ranges = count < 128 ? stackalloc Range[count] : new Range[count]; |
| | 70 | | int num = MemoryExtensions.Split(field, ranges, ':', StringSplitOptions.TrimEntries); |
| | 71 | | if (num < 3 || |
| | 72 | | !int.TryParse(field[ranges[0]], NumberStyles.HexNumber, null, out int major) || |
| | 73 | | !int.TryParse(field[ranges[1]], NumberStyles.HexNumber, null, out int minor) || |
| | 74 | | !long.TryParse(field[ranges[2]], NumberStyles.Integer, null, out long number)) |
| | 75 | | { |
| | 76 | | value = default; |
| | 77 | | return false; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | value = new InodeInfo(major, minor, number); |
| | 81 | | return true; |
| | 82 | | #endif |
| | 83 | | } |
| | 84 | |
|
| | 85 | | private InodeInfo(int majorDeviceId, int minorDeviceId, long iNodeNumber) |
| | 86 | | { |
| 2 | 87 | | MajorDeviceId = majorDeviceId; |
| 2 | 88 | | MinorDeviceId = minorDeviceId; |
| 2 | 89 | | INodeNumber = iNodeNumber; |
| 2 | 90 | | } |
| | 91 | |
|
| 0 | 92 | | public readonly int MajorDeviceId { get; } |
| 0 | 93 | | public readonly int MinorDeviceId { get; } |
| 2 | 94 | | public readonly long INodeNumber { get; } |
| | 95 | | } |