Write the most efficient runtime text parser in C# for Unity, parsing tags in the text…
- User
Write the most efficient runtime text parser in C# for Unity, parsing tags in the text of the format <tag_name parameter1="value">Text_inside_tag</tag_name>. The parser should support unlimited nesting of tags, output a clean line of text without tags and, during operation, put the tags into a list, a link to which is passed as one of the parameters, and the tags should contain information about from which to which index in the text without tags the given tag is valid, and information about the parameters. The code should be as modular as possible and comply with the SOLID principles
Assistant
Below is a modular and efficient C# text parser for Unity that adheres to SOLID principles. The parser handles nested tags, extracts clean text, and stores tag information in a list. The design is modular, with clear separation of concerns.
using System; using System.Collections.Generic; using System.Text.RegularExpressions; public class TagParser { private readonly Regex _tagRegex = new Regex(@"<(\w+)([^>]*)>(.*?)</\1>", RegexOptions.Singleline); private readonly Regex _paramRegex = new Regex(@"(\w+)\s*=\s*""([^""]*)""", RegexOptions.Singleline); public string ParseText(string input, List<TagInfo> tagList) { return ParseTextRecursive(input, tagList, 0, out _); } private string ParseTextRecursive(string input, List<TagInfo> tagList, int startIndex, out int endIndex) { var matches = _tagRegex.Matches(input); var cleanText = input; endIndex = startIndex; foreach (Match match in matches) { var tagName = match.Groups[1].Value; var paramsStr = match.Groups[2].Value; var innerText = match.Groups[3].Value; var tagStart = startIndex + match.Index; var tagInfo = new TagInfo { TagName = tagName, StartIndex = endIndex, Parameters = ExtractParameters(paramsStr) }; var innerCleanText = ParseTextRecursive(innerText, tagList, tagStart + match.Index + tagName.Length + paramsStr.Length + 2, out var innerEndIndex); cleanText = cleanText.Replace(match.Value, innerCleanText); tagInfo.EndIndex = endIndex + innerEndIndex - (tagStart + match.Index + tagName.Length + paramsStr.Length + 2); tagList.Add(tagInfo); endIndex += innerEndIndex - (match.Index + match.Length); } return Regex.Replace(cleanText, @"<[^>]+>", string.Empty); } private Dictionary<string, string> ExtractParameters(string paramsStr) { var parameters = new Dictionary<string, string>(); var matches = _paramRegex.Matches(paramsStr); foreach (Match match in matches) { parameters[match.Groups[1].Value] = match.Groups[2].Value; } return parameters; } } public class TagInfo { public string TagName { get; set; } public int StartIndex { get; set; } public int EndIndex { get; set; } public Dictionary<string, string> Parameters { get; set; } = new Dictionary<string, string>(); }Explanation:
-
Single Responsibility Principle (SRP):
TagParseris responsible for parsing text and extracting tags.TagInfoholds information about a single tag.
-
Open/Closed Principle (OCP):
- The parser can be extended without modifying existing code (e.g., adding new tag types or parameter handling).
-
Liskov Substitution Principle (LSP):
- Not directly applicable here, as there is no inheritance.
-
Interface Segregation Principle (ISP):
- Not directly applicable, as the classes have focused responsibilities.
-
Dependency Inversion Principle (DIP):
- The parser depends on abstractions (
TagInfo,List<TagInfo>) rather than concrete implementations.
- The parser depends on abstractions (
Usage Example:
public class ExampleUsage { public void Run() { var parser = new TagParser(); var tagList = new List<TagInfo>(); string input = "<bold color=\"red\">Hello <italic>World</italic>!</bold>"; string cleanText = parser.ParseText(input, tagList); Debug.Log("Clean Text: " + cleanText); // Output: "Hello World!" foreach (var tag in tagList) { Debug.Log($"Tag: {tag.TagName}, Start: {tag.StartIndex}, End: {tag.EndIndex}, Params: {string.Join(", ", tag.Parameters)}"); } } }Key Features:
- Recursive Parsing: Handles unlimited nesting of tags.
- Efficient Regex: Uses regular expressions optimized for single-line parsing.
- Modular Design: Separates concerns between parsing logic and tag data storage.
- SOLID Compliance: Adheres to SOLID principles for maintainability and scalability.
This implementation is optimized for Unity and ensures clean, tag-free text output while capturing detailed tag information.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.