/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of xAPI4Unity. */ namespace xAPI4Unity.Editor.Types { /// /// Represents a resolved range type with a minimum and maximum value. /// Extends to provide additional range information. /// public sealed class ResolvedRange : ResolvedType { /// /// Indicates that this type is a range. /// public override bool IsRange => true; /// /// The underlying type of the range (e.g., int for int[0,10]). /// public ResolvedType ElementType { get; } /// /// The raw minimum value as a string (e.g., "0" in int[0,10]). /// public string RawMin { get; } /// /// The raw maximum value as a string (e.g., "10" in int[0,10]). /// public string RawMax { get; } /// /// Indicates whether the minimum value is inclusive. /// public bool MinInclusive { get; } /// /// Indicates whether the maximum value is inclusive. /// public bool MaxInclusive { get; } /// /// Constructs a new instance of a resolved range with the specified details. /// /// The type of the range (e.g., int, double). /// The raw minimum value as a string. /// The raw maximum value as a string. /// True if the minimum value is inclusive; otherwise, false. /// True if the maximum value is inclusive; otherwise, false. /// The full range definition as a string. public ResolvedRange(ResolvedType elementType, string rawMin, string rawMax, bool minInclusive, bool maxInclusive, string definition) : base(elementType.Type, definition) { ElementType = elementType; RawMin = rawMin; RawMax = rawMax; MinInclusive = minInclusive; MaxInclusive = maxInclusive; } /// /// Gets the syntax representation of the type (e.g., "int" for int[0,10]). /// /// The type syntax as a string. public override string GetTypeSyntax() => ElementType.GetTypeSyntax(); /// /// Attempts to parse a range definition from the input string in the form of type[min,max]. /// /// The input string to parse. /// Extracted type name. /// Minimum range value. /// Maximum range value. /// Indicates if the minimum value is inclusive. /// Indicates if the maximum value is inclusive. /// True if parsing was successful, otherwise false. public static bool TryParseRange(string input, out string type, out string min, out string max, out bool minInclusive, out bool maxInclusive) { type = min = max = null; minInclusive = maxInclusive = false; if (string.IsNullOrWhiteSpace(input)) return false; var open = input.IndexOfAny(new[] { '[', '(' }); var close = input.LastIndexOfAny(new[] { ']', ')' }); if (open <= 0 || close <= open) return false; // Extract the type name before the range brackets. type = input.Substring(0, open).Trim(); var rangeContent = input.Substring(open + 1, close - open - 1); var parts = rangeContent.Split(','); if (parts.Length != 2) return false; min = parts[0].Trim(); max = parts[1].Trim(); minInclusive = input[open] == '['; maxInclusive = input[close] == ']'; return true; } } }