Skip to content

Commit

Permalink
In TexPredefinedFormulaSettingsParser, methods GetSymbolMappings
Browse files Browse the repository at this point in the history
…and `GetDelimiterMappings` were refactored and they now share the same mechanism.
  • Loading branch information
Lehonti Ramos committed Sep 27, 2023
1 parent 837e6da commit 0918493
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
using XamlMath.Data;
using XamlMath.Utils;
Expand All @@ -11,23 +12,6 @@ internal sealed class TexPredefinedFormulaSettingsParser
{
private static readonly string resourceName = TexUtilities.ResourcesDataDirectory + "TexFormulaSettings.xml";

static TexPredefinedFormulaSettingsParser()
{
}

private static void AddToMap(IEnumerable<XElement> mapList, string[] table)
{
foreach (var map in mapList)
{
var character = map.AttributeValue("char");
var symbol = map.AttributeValue("symbol");
Debug.Assert(character != null);
Debug.Assert(symbol != null);
Debug.Assert(character.Length == 1);
table[character[0]] = symbol;
}
}

private readonly XElement rootElement;

public TexPredefinedFormulaSettingsParser()

Check warning on line 17 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / main.linux

Non-nullable field 'rootElement' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 17 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / main.macos

Non-nullable field 'rootElement' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 17 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / nuget-push

Non-nullable field 'rootElement' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 17 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / main.windows

Non-nullable field 'rootElement' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 17 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / main.linux

Non-nullable field 'rootElement' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 17 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / main.macos

Non-nullable field 'rootElement' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 17 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / nuget-push

Non-nullable field 'rootElement' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 17 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / main.windows

Non-nullable field 'rootElement' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
Expand All @@ -37,24 +21,37 @@ public TexPredefinedFormulaSettingsParser()
this.rootElement = doc.Root;

Check warning on line 21 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / main.linux

Possible null reference assignment.

Check warning on line 21 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / main.macos

Possible null reference assignment.

Check warning on line 21 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / nuget-push

Possible null reference assignment.

Check warning on line 21 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / main.windows

Possible null reference assignment.

Check warning on line 21 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / main.linux

Possible null reference assignment.

Check warning on line 21 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / main.macos

Possible null reference assignment.

Check warning on line 21 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / nuget-push

Possible null reference assignment.

Check warning on line 21 in src/XamlMath.Shared/TexPredefinedFormulaSettingsParser.cs

View workflow job for this annotation

GitHub Actions / main.windows

Possible null reference assignment.
}

public IReadOnlyList<string> GetSymbolMappings()
private readonly record struct CharMappingPair(char Key, string Value);

private static CharMappingPair ExtractCharMappingPair(XElement mapTag)
{
var mappings = new string[TexFontInfo.charCodesCount];
var charToSymbol = rootElement.Element("CharacterToSymbolMappings");
if (charToSymbol != null)
AddToMap(charToSymbol.Elements("Map"), mappings);
return mappings;
var character = mapTag.AttributeValue("char");
var symbol = mapTag.AttributeValue("symbol");
Debug.Assert(character != null);
Debug.Assert(symbol != null);
Debug.Assert(character.Length == 1);
return new(character[0], symbol);
}

public IReadOnlyList<string> GetDelimiterMappings()
private IReadOnlyList<string> GetMappingsFromElement(XName elementName)
{
var mappings = new string[TexFontInfo.charCodesCount];
var charToDelimiter = rootElement.Element("CharacterToDelimiterMappings");
if (charToDelimiter != null)
AddToMap(charToDelimiter.Elements("Map"), mappings);
var charToSymbol = rootElement.Element(elementName);
if (charToSymbol != null)
{
var additionsToMap = charToSymbol.Elements("Map").Select(ExtractCharMappingPair);
foreach (var addition in additionsToMap)
{
mappings[addition.Key] = addition.Value;
}
}
return mappings;
}

public IReadOnlyList<string> GetSymbolMappings() => GetMappingsFromElement("CharacterToSymbolMappings");

public IReadOnlyList<string> GetDelimiterMappings() => GetMappingsFromElement("CharacterToDelimiterMappings");

public IEnumerable<string> GetTextStyles()
{
var textStyles = rootElement.Element("TextStyles");
Expand Down

0 comments on commit 0918493

Please sign in to comment.