Skip to content

Extend the characters allowed for selectors #454

@Mirchev98

Description

@Mirchev98

Hello! Using the latest version of SmartFormat, I encountered an issue, where my template cannot be processed when it contains Cyrillic letters.

The code:

using SmartFormat;
namespace TestSmartFormat
{
	public class AttributeData
	{
		public string DisplayText { get; set; }
	}

	public class Document
	{
		public Dictionary<string, AttributeData> ArchiveAttributes { get; set; }
	}

	public class UnifiedDocumentData
	{
		public Document Document { get; set; }
	}

	class Program
	{
		static void Main(string[] args)
		{
			// Initialize sample data with Cyrillic keys
			var data = new UnifiedDocumentData
			{
				Document = new Document
				{
					ArchiveAttributes = new Dictionary<string, AttributeData>
					{
						{ "София", new AttributeData { DisplayText = "София" } },
						{ "Пловдив", new AttributeData { DisplayText = "Пловдив" } }
					}
				}
			};

			// Define templates
			string template1 = "{ArchiveAttributes[София].DisplayText}";
			string template2 = "{ArchiveAttributes[Пловдив].DisplayText}";

			var formatter = Smart.CreateDefaultSmartFormat();

			try
			{
				// Perform formatting
				string result1 = formatter.Format(template1, data.Document);
				string result2 = formatter.Format(template2, data.Document);

				// Display results
				Console.WriteLine($"Template 1 Result: {result1}"); // Expected: София
				Console.WriteLine($"Template 2 Result: {result2}"); // Expected: Пловдив
			}
			catch (Exception ex)
			{
				Console.WriteLine($"Unexpected Error: {ex.Message}");
			}
		}
	}
}

The error:
Unexpected Error: The format string has 5 issues:
'0x421': Invalid character in the selector, '0x43E': Invalid character in the selector, '0x444': Invalid character in the selector, '0x438': Invalid character in the selector, '0x44F': Invalid character in the selector
In: "{ArchiveAttributes[София].DisplayText}"
At: -------------------^^^^^

However, when I change the following part to contain only English, it works perfectly well:

var data = new UnifiedDocumentData
			{
				Document = new Document
				{
					ArchiveAttributes = new Dictionary<string, AttributeData>
					{
						{ "Sofia", new AttributeData { DisplayText = "София" } },
						{ "Plovdiv", new AttributeData { DisplayText = "Пловдив" } }
					}
				}
			};

			// Define templates
			string template1 = "{ArchiveAttributes[Sofia].DisplayText}";
			string template2 = "{ArchiveAttributes[Plovdiv].DisplayText}";

I am unsure if I am missing something, or if it is genuinely a bug.

Thanks in advance for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions