| | | 1 | | /******************************************************************************** |
| | | 2 | | * ParameterDefinition.cs * |
| | | 3 | | * * |
| | | 4 | | * Author: Denes Solti * |
| | | 5 | | ********************************************************************************/ |
| | | 6 | | using System; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using System.Text.RegularExpressions; |
| | | 9 | | |
| | | 10 | | namespace NanoRoute.Internals |
| | | 11 | | { |
| | | 12 | | using Properties; |
| | | 13 | | |
| | | 14 | | internal readonly struct ParameterDefinition |
| | | 15 | | { |
| | | 16 | | private const string |
| | | 17 | | PARAMETER_NAME = $@"\w+\??", |
| | | 18 | | PARAMETER_NAME_PATTERN = $@"\G(?:(?<parameterName>{PARAMETER_NAME}):)?"; |
| | | 19 | | |
| | 1 | 20 | | private static readonly Regex s_parameterName = new(PARAMETER_NAME_PATTERN, RuntimeFeature.IsDynamicCodeSupporte |
| | | 21 | | |
| | | 22 | | public static ParameterDefinition Parse(string pattern, ref int offset) |
| | 1 | 23 | | { |
| | 1 | 24 | | int newOffset = offset; |
| | | 25 | | |
| | 1 | 26 | | if (newOffset < pattern.Length && pattern[newOffset++] == '{' && s_parameterName.Match(pattern, newOffset) i |
| | 1 | 27 | | { |
| | 1 | 28 | | string parameterName = parsed.Groups["parameterName"].Value; |
| | | 29 | | |
| | 1 | 30 | | newOffset += length; |
| | | 31 | | |
| | 1 | 32 | | ParameterDefinition result = new() |
| | 1 | 33 | | { |
| | 1 | 34 | | ValueParser = ValueParserDefinition.Parse(pattern, ref newOffset), |
| | 1 | 35 | | ParameterName = parameterName.Length is 0 ? null : parameterName.TrimEnd('?'), |
| | 1 | 36 | | IsOptional = parameterName.EndsWith("?", StringComparison.Ordinal) |
| | 1 | 37 | | }; |
| | | 38 | | |
| | 1 | 39 | | if (newOffset < pattern.Length && pattern[newOffset] == '}') |
| | 1 | 40 | | { |
| | 1 | 41 | | offset = newOffset; |
| | 1 | 42 | | return result; |
| | | 43 | | } |
| | 1 | 44 | | } |
| | | 45 | | |
| | 1 | 46 | | throw new InvalidOperationException(string.Format(Resources.Culture, Resources.ERR_INVALID_PATTERN, offset)) |
| | 1 | 47 | | } |
| | | 48 | | |
| | | 49 | | public required ValueParserDefinition ValueParser { get; init; } |
| | | 50 | | |
| | | 51 | | public string? ParameterName { get; init; } |
| | | 52 | | |
| | | 53 | | public bool IsOptional { get; init; } |
| | | 54 | | |
| | | 55 | | public int Index { get; init; } |
| | | 56 | | } |
| | | 57 | | } |