Написана система взаимодействия со строками
Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
94
RegexHandler.cs
Normal file
94
RegexHandler.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SaveWizard_rewritten
|
||||
{
|
||||
static class RegexHandler
|
||||
{
|
||||
private static List<string> lines;
|
||||
|
||||
public static void FillLines(List<string> _lines)
|
||||
{
|
||||
lines = _lines;
|
||||
}
|
||||
|
||||
public static string GetLine(int index)
|
||||
{
|
||||
return lines[index];
|
||||
}
|
||||
|
||||
public static List<string> 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<int> SearchAllLines(string term)
|
||||
{
|
||||
List<int> matches = new List<int>();
|
||||
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<string> GetArrayItems(int line)
|
||||
{
|
||||
List<string> items = new List<string>();
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user