using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace SaveWizard_rewritten { static class RegexHandler { private static List lines; public static void FillLines(List _lines) { lines = _lines; } public static string GetLine(int index) { return lines[index]; } public static List GetAllLines() { return lines; } public static int SearchLine(string term, int start = 0, string cancel = "this_string_must_not_exist") { if (lines[start].Contains(term)) return start; start += 1; while (start <= (lines.Count-1)) { if (lines[start].Contains(term)) return start; if (lines[start].Contains(cancel)) return 0; start += 1; } return 0; } public static int SearchLineInUnit(string term, string unit) { return SearchLine(term, start: SearchLine(" : " + unit + " {"), cancel: "}"); } public static List SearchAllLines(string term) { List matches = new List(); int line = 0; while (SearchLine(term, start: line + 1) != 0) { line = SearchLine(term, start: line + 1); matches.Add(line); } if (matches.Count == 0) return null; return matches; } public static string GetValue(int line) { return Regex.Match(": (.+)$", lines[line]).Value; } public static void SetValue(int line, string value) { string name = Regex.Match("(.+):", lines[line]).Value; lines[line] = $"{name}: {value}"; } public static int GetArrayLength(int line) { return Convert.ToInt32(Regex.Match(": ([0-9]+)$", lines[line]).Value); } public static List GetArrayItems(int line) { List items = new List(); for (int i = 0; i < GetArrayLength(line); i++) items.Add(Regex.Match(": (.+)$", lines[line + i + 1]).Value); if (items.Count == 0) return null; return items; } public static void AddArrayValue(int line, string value) { string name = Regex.Match("(.+):", lines[line]).Value; int length = GetArrayLength(line); lines[line] = $"{name}: {length + 1}"; lines.Insert(line + length + 1, $"{name}[{length}]: {value}"); } } }