AvaloniaUI/Avalonia · error · FormatException

Could not parse specified Unicode range segment.

Error message

Could not parse specified Unicode range segment.

What it means

Thrown by UnicodeRangeSegment.Parse when the input string s is null or empty. A UnicodeRangeSegment is a single component of a unicode-range (e.g. 'U+20' or 'U+20-7F'); an empty/null segment cannot be parsed and is rejected with a FormatException. This is the entry guard of the segment parser.

Source

Thrown at src/Avalonia.Base/Media/UnicodeRange.cs:147

        /// <returns>
        /// <c>true</c> If given value is inside the range segment, <c>false</c> otherwise.
        /// </returns>
        public bool IsInRange(int value)
        {
            return Start <= value && value <= End;
        }

        /// <summary>
        /// Parses a <see cref="UnicodeRangeSegment"/>.
        /// </summary>
        /// <param name="s">The string to parse.</param>
        /// <returns>The parsed <see cref="UnicodeRangeSegment"/>.</returns>
        /// <exception cref="FormatException"></exception>
        public static UnicodeRangeSegment Parse(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                throw new FormatException("Could not parse specified Unicode range segment.");
            }

            var parts = s.Split('-');

            int start, end;

            switch (parts.Length)
            {
                case 1:
                    {
                        //e.g. U+20, U+3F U+30??
                        var single = s_regex.Match(parts[0]);

                        if (!single.Success)
                        {
                            throw new FormatException("Could not parse specified Unicode range segment.");
                        }

View on GitHub (pinned to 11c5427268)

Solutions

  1. Ensure each comma-separated segment is a valid 'U+XXXX' or 'U+XXXX-YYYY' expression.
  2. Trim and filter out empty segments before parsing: parts.Where(p => !string.IsNullOrWhiteSpace(p)).
  3. Validate the full unicode-range string format before calling Parse.

Example fix

// before
var seg = UnicodeRangeSegment.Parse(""); // throws
// or: UnicodeRange.Parse("U+20-7F,") -> empty trailing segment throws

// after
var seg = UnicodeRangeSegment.Parse("U+20");
Defensive patterns

Strategy: validation

Validate before calling

static UnicodeRangeSegment SafeParseSegment(string s)
    => string.IsNullOrWhiteSpace(s)
        ? throw new ArgumentException("empty segment")
        : UnicodeRangeSegment.Parse(s.Trim());

Try / catch

try { return UnicodeRangeSegment.Parse(s); }
catch (FormatException) { /* skip or default the segment */ }

Prevention

When it happens

Trigger: Calling UnicodeRangeSegment.Parse(null) or UnicodeRangeSegment.Parse(""), or indirectly when UnicodeRange.Parse splits on commas and a segment (after the line-86 path) is empty — e.g. a trailing comma in 'U+20-7F,' producing an empty segment.

Common situations: Malformed unicode-range strings with trailing/leading/double commas; deserialized config with empty segments; user-authored CSS @font-face unicode-range with stray commas.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/6f1724acd48425f0. Report an issue: GitHub.