ProjectArcade
156 строк · 5.3 Кб
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5
6namespace EmulatorLauncher.Common7{
8public static class StringExtensions9{10public static string ExtractString(this string html, string start, string end)11{12int idx1 = html.IndexOf(start);13if (idx1 < 0)14return "";15
16int idx2 = html.IndexOf(end, idx1 + start.Length);17if (idx2 > idx1)18return html.Substring(idx1 + start.Length, idx2 - idx1 - start.Length);19
20return "";21}22
23public static string[] ExtractStrings(this string cSearchExpression, string cBeginDelim, string cEndDelim, bool keepDelims = false, bool firstOnly = false, StringComparison comparison = StringComparison.Ordinal)24{25List<string> ret = new List<string>();26
27if (string.IsNullOrEmpty(cSearchExpression))28return ret.ToArray();29
30if (string.IsNullOrEmpty(cBeginDelim))31return ret.ToArray();32
33if (string.IsNullOrEmpty(cEndDelim))34return ret.ToArray();35
36int startpos = cSearchExpression.IndexOf(cBeginDelim, comparison);37while (startpos >= 0 && startpos < cSearchExpression.Length && startpos + cBeginDelim.Length <= cSearchExpression.Length)38{39int endpos = cSearchExpression.IndexOf(cEndDelim, startpos + cBeginDelim.Length, comparison);40if (endpos > startpos)41{42if (keepDelims)43ret.Add((cSearchExpression.Substring(startpos, cBeginDelim.Length) + cSearchExpression.Substring(startpos + cBeginDelim.Length, endpos - startpos - cBeginDelim.Length) + cEndDelim).Trim());44else45ret.Add(cSearchExpression.Substring(startpos + cBeginDelim.Length, endpos - startpos - cBeginDelim.Length).Trim());46
47if (firstOnly)48break;49
50startpos = cSearchExpression.IndexOf(cBeginDelim, endpos + cEndDelim.Length, comparison);51}52else53startpos = cSearchExpression.IndexOf(cBeginDelim, startpos + cBeginDelim.Length, comparison);54}55
56return ret.ToArray();57}58
59public static int ToInteger(this string value)60{61int ret = 0;62int.TryParse(value, out ret);63return ret;64}65
66public static float ToFloat(this string value)67{68float ret = 0;69float.TryParse(value,System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out ret);70return ret;71}72
73public static float ToRatio(this string value)74{75if (string.IsNullOrEmpty(value))76return 0;77
78string[] parts = value.Split(new char[] { ':', '/' }, StringSplitOptions.RemoveEmptyEntries);79if (parts.Length != 2)80return 0;81
82float numerator = parts[0].ToFloat();83float denominator = parts[1].ToFloat();84
85if (denominator == 0)86return 0;87
88return numerator / denominator;89}90
91public static string JoinArguments(this IEnumerable<string> args)92{93return string.Join(" ", args.Select(a => a.Contains(" ") ? "\"" + a + "\"" : a).ToArray());94}95
96public static string FormatVersionString(string version)97{98var numbers = version.Split(new char[] { '.', '-' }, StringSplitOptions.RemoveEmptyEntries).ToList();99while (numbers.Count < 4)100numbers.Add("0");101
102return string.Join(".", numbers.Take(4).ToArray());103}104
105public static string[] SplitCommandLine(this string commandLine)106{107char[] parmChars = commandLine.ToCharArray();108
109bool inQuote = false;110for (int index = 0; index < parmChars.Length; index++)111{112if (parmChars[index] == '"')113inQuote = !inQuote;114if (!inQuote && parmChars[index] == ' ')115parmChars[index] = '\n';116}117
118return (new string(parmChars))119.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)120.Select(s => s.Replace("\"", ""))121.ToArray();122}123
124public static string AsIndexedRomName(this string name)125{126if (string.IsNullOrEmpty(name))127return "";128
129if (name.Contains("\\") || name.Contains("/"))130name = System.IO.Path.GetFileNameWithoutExtension(name);131
132StringBuilder ret = new StringBuilder(name.Length);133
134bool inpar = false;135bool inblock = false;136
137foreach (var c in name.ToLowerInvariant())138{139if (c == '(')140inpar = true;141else if (c == ')')142inpar = false;143else if (c == '[')144inblock = true;145else if (c == ']')146inblock = false;147else if (!inpar && !inblock && (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))148ret.Append(c);149}150
151return ret.ToString().Trim();152}153
154
155}156}
157