Projekt

Obecné

Profil

Stáhnout (1.77 KB) Statistiky
| Větev: | Tag: | Revize:
1
using System.Text.RegularExpressions;
2

    
3
namespace LDClient.detection; 
4

    
5
/// <summary>
6
/// This class parses the .txt file generated from the debugger.
7
/// Its primary interest is to find two serial numbers (head + body). 
8
/// </summary>
9
public static class DebuggerInfoParser {
10

    
11
    /// <summary>
12
    /// Number of serial numbers expected to be in the .txt file (number of matches - regex).
13
    /// </summary>
14
    private const int ExpectedNumberOfMatches = 2;
15
        
16
    /// <summary>
17
    /// Regular expression used to find the serial numbers.
18
    /// </summary>
19
    private static readonly Regex SerialNumberRegex = new("(?<=Serial Number: )(.*)");
20
        
21
    /// <summary>
22
    /// Takes the content of a .txt file and tries to find the two serial numbers (head and body).
23
    /// If it succeed, it will return the two numbers.
24
    /// </summary>
25
    /// <param name="dataTxt">the content of a .txt file (generated from the debugger)</param>
26
    /// <returns>two serial numbers (head and body) of the debugger</returns>
27
    /// <exception cref="ArgumentException">throws an exception if it fails to find the serial numbers</exception>
28
    public static (string headSerialNumber, string bodySerialNumber) Parse(string dataTxt) {
29
        // Find all matches in the content of the file that satisfy the regular expression.
30
        var matches = SerialNumberRegex.Matches(dataTxt);
31

    
32
        // Make sure an exact number of matches has been found.
33
        if (matches.Count != ExpectedNumberOfMatches) {
34
            throw new ArgumentException($"Expected {ExpectedNumberOfMatches} matches to be found in the text (actually found: {matches.Count})");
35
        }
36
            
37
        // Return the two serial numbers (head and body).
38
        return (matches[1].ToString().Trim(), matches[0].ToString().Trim());
39
    }
40
}
(2-2/10)