2using System.Collections.Generic;
3using System.Globalization;
23 private readonly Dictionary<string, string> _segments =
25 StringComparer.OrdinalIgnoreCase);
36 if (
string.IsNullOrWhiteSpace(
39 throw new ArgumentException(
40 "A mathematical domain-key category is required.",
59 public IReadOnlyDictionary<string, string>
Segments =>
81 return _segments.ContainsKey(
101 string normalizedName =
105 if (_segments.TryGetValue(
112 throw new KeyNotFoundException(
113 $
"Domain-key segment '{normalizedName}' is required.");
134 NumberStyles.Integer,
135 CultureInfo.InvariantCulture,
136 out
int parsedValue))
138 throw new FormatException(
139 $
"Domain-key segment '{name}' value '{value}' " +
140 "is not a valid 32-bit integer.");
163 return _segments.TryGetValue(
194 NumberStyles.Integer,
195 CultureInfo.InvariantCulture,
218 if (
string.IsNullOrWhiteSpace(
221 throw new ArgumentException(
222 "A mathematical domain key is required.",
230 StringSplitOptions.TrimEntries);
232 if (parts.Length == 0 ||
233 string.IsNullOrWhiteSpace(
236 throw new FormatException(
237 "A mathematical domain key must start with a " +
253 StringComparison.Ordinal);
255 if (separatorIndex <= 0 ||
259 throw new FormatException(
260 $
"Malformed mathematical domain-key segment " +
261 $
"'{part}'. Expected 'name=value'.");
265 part[..separatorIndex].Trim();
268 part[(separatorIndex + 1)..].Trim();
270 if (name.Length == 0 ||
273 throw new FormatException(
274 $
"Malformed mathematical domain-key segment " +
278 if (!result._segments.TryAdd(
282 throw new FormatException(
283 $
"Mathematical domain-key segment '{name}' " +
284 "appears more than once.");
342 if (_segments.Count == 0)
355 $
"{segment.Key}={segment.Value}")));
358 private static string NormalizeName(
361 if (
string.IsNullOrWhiteSpace(
364 throw new ArgumentException(
365 "A domain-key segment name is required.",
bool Contains(string name)
Determines whether a segment exists.
MathematicalDomainKey(string category)
Initializes a mathematical domain key.
int SegmentCount
Gets the number of parsed segments.
override string ToString()
Returns the canonical textual representation.
IReadOnlyDictionary< string, string > Segments
Gets the parsed name-value segments.
static bool TryParse(string domainKey, out MathematicalDomainKey? result)
Attempts to parse a canonical mathematical domain key.
string GetRequiredString(string name)
Gets a required text segment.
int GetRequiredInt32(string name)
Gets a required integer segment.
static MathematicalDomainKey Parse(string domainKey)
Parses a canonical mathematical domain key.
bool TryGetInt32(string name, out int value)
Attempts to get an integer segment.
bool TryGetString(string name, out string? value)
Attempts to get a text segment.
string Category
Gets the domain-key category.