Projekt

Obecné

Profil

« Předchozí | Další » 

Revize e6a01bd8

Přidáno uživatelem Pultak před asi 2 roky(ů)

re #9712 Added documentation of the newly implemented methods + refactoring

Zobrazit rozdíly:

ld_client/LDClient/detection/DebuggerInfoParser.cs
1 1
using System.Text.RegularExpressions;
2 2

  
3
namespace LDClient.detection {
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 {
4 10

  
5 11
    /// <summary>
6
    /// This class parses the .txt file generated from the debugger.
7
    /// Its primary interest is to find two serial numbers (head + body). 
12
    /// Number of serial numbers expected to be in the .txt file (number of matches - regex).
8 13
    /// </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;
14
    private const int ExpectedNumberOfMatches = 2;
15 15
        
16
        /// <summary>
17
        /// Regular expression used to find the serial numbers.
18
        /// </summary>
19
        private static readonly Regex SerialNumberRegex = new("(?<=Serial Number: )(.*)");
16
    /// <summary>
17
    /// Regular expression used to find the serial numbers.
18
    /// </summary>
19
    private static readonly Regex SerialNumberRegex = new("(?<=Serial Number: )(.*)");
20 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);
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 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());
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})");
39 35
        }
36
            
37
        // Return the two serial numbers (head and body).
38
        return (matches[1].ToString().Trim(), matches[0].ToString().Trim());
40 39
    }
41 40
}

Také k dispozici: Unified diff