Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Text.RegularExpressions;
- using System.Collections.Generic;
- using System;
- namespace Broargs
- {
- public static class Parser
- {
- private static string enableArgs = "";
- private static string disableArgs = "";
- private static string otherArgs = "";
- // Собирает корректную строку аргументов
- public static string Parse(string stringForParse)
- {
- // Разносит значения аргументов по заранее подготовленным массивам
- Decompose(stringForParse);
- string result = "";
- if (enableArgs.Length > 0)
- {
- result += "--enable-features=" + enableArgs;
- }
- if (disableArgs.Length > 0)
- {
- result += " --disable-features=" + disableArgs;
- }
- return (result + " " + otherArgs).Trim();
- }
- // Разносит значения аргументов по заранее подготовленным массивам
- private static void Decompose(string stringForParse)
- {
- // Разделяет строку на отдельные состаляющие в массив
- string[] arr = Prepare(stringForParse);
- List<string> enableList = new List<string>();
- List<string> disableList = new List<string>();
- List<string> otherList = new List<string>();
- foreach (string elem in arr)
- {
- if (elem.Contains("--enable-features="))
- {
- int beginIndex = elem.IndexOf("=") + 1;
- enableList.Add(elem.Substring(beginIndex));
- }
- else if (elem.Contains("--disable-features="))
- {
- int beginIndex = elem.IndexOf("=") + 1;
- disableList.Add(elem.Substring(beginIndex));
- }
- else
- {
- otherList.Add(elem);
- }
- }
- enableArgs = string.Join(",", enableList);
- disableArgs = string.Join(",", disableList);
- otherArgs = string.Join(" ", otherList);
- }
- // Разделяет строку на отдельные состаляющие в массив
- private static string[] Prepare(string stringForParse)
- {
- // Полагаю это строчка лишняя
- stringForParse = new Regex(@"\s*--").Replace(stringForParse, " --");
- Regex pattern = new Regex("--\\S*");
- // Разделяем строку на отдельные элементы
- MatchCollection matches = pattern.Matches(stringForParse);
- // Последующий код переводит MatchCollection в обычный Array и возвращает его
- // Походу можно написать что-то типа return matches.ToArray(), но проверять неохота
- List<string> list = new List<string>();
- foreach (Match match in matches)
- {
- list.Add(match.ToString());
- }
- return list.ToArray();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement