ProjectArcade

Форк
0
156 строк · 5.3 Кб
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5

6
namespace EmulatorLauncher.Common
7
{
8
    public static class StringExtensions
9
    {
10
        public static string ExtractString(this string html, string start, string end)
11
        {
12
            int idx1 = html.IndexOf(start);
13
            if (idx1 < 0)
14
                return "";
15

16
            int idx2 = html.IndexOf(end, idx1 + start.Length);
17
            if (idx2 > idx1)
18
                return html.Substring(idx1 + start.Length, idx2 - idx1 - start.Length);
19

20
            return "";
21
        }
22

23
        public static string[] ExtractStrings(this string cSearchExpression, string cBeginDelim, string cEndDelim, bool keepDelims = false, bool firstOnly = false, StringComparison comparison = StringComparison.Ordinal)
24
        {
25
            List<string> ret = new List<string>();
26

27
            if (string.IsNullOrEmpty(cSearchExpression))
28
                return ret.ToArray();
29

30
            if (string.IsNullOrEmpty(cBeginDelim))
31
                return ret.ToArray();
32

33
            if (string.IsNullOrEmpty(cEndDelim))
34
                return ret.ToArray();
35

36
            int startpos = cSearchExpression.IndexOf(cBeginDelim, comparison);
37
            while (startpos >= 0 && startpos < cSearchExpression.Length && startpos + cBeginDelim.Length <= cSearchExpression.Length)
38
            {
39
                int endpos = cSearchExpression.IndexOf(cEndDelim, startpos + cBeginDelim.Length, comparison);
40
                if (endpos > startpos)
41
                {
42
                    if (keepDelims)
43
                        ret.Add((cSearchExpression.Substring(startpos, cBeginDelim.Length) + cSearchExpression.Substring(startpos + cBeginDelim.Length, endpos - startpos - cBeginDelim.Length) + cEndDelim).Trim());
44
                    else
45
                        ret.Add(cSearchExpression.Substring(startpos + cBeginDelim.Length, endpos - startpos - cBeginDelim.Length).Trim());
46

47
                    if (firstOnly)
48
                        break;
49

50
                    startpos = cSearchExpression.IndexOf(cBeginDelim, endpos + cEndDelim.Length, comparison);
51
                }
52
                else
53
                    startpos = cSearchExpression.IndexOf(cBeginDelim, startpos + cBeginDelim.Length, comparison);
54
            }
55

56
            return ret.ToArray();
57
        }
58

59
        public static int ToInteger(this string value)
60
        {
61
            int ret = 0;
62
            int.TryParse(value, out ret);
63
            return ret;
64
        }
65

66
        public static float ToFloat(this string value)
67
        {
68
            float ret = 0;
69
            float.TryParse(value,System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out ret);
70
            return ret;
71
        }
72

73
        public static float ToRatio(this string value)
74
        {
75
            if (string.IsNullOrEmpty(value))
76
                return 0;
77

78
            string[] parts = value.Split(new char[] { ':', '/' }, StringSplitOptions.RemoveEmptyEntries);
79
            if (parts.Length != 2)
80
                return 0;
81

82
            float numerator = parts[0].ToFloat();
83
            float denominator = parts[1].ToFloat();
84

85
            if (denominator == 0)
86
                return 0;
87

88
            return numerator / denominator;
89
        }
90

91
        public static string JoinArguments(this IEnumerable<string> args)
92
        {
93
            return string.Join(" ", args.Select(a => a.Contains(" ") ? "\"" + a + "\"" : a).ToArray());
94
        }
95

96
        public static string FormatVersionString(string version)
97
        {
98
            var numbers = version.Split(new char[] { '.', '-' }, StringSplitOptions.RemoveEmptyEntries).ToList();
99
            while (numbers.Count < 4)
100
                numbers.Add("0");
101

102
            return string.Join(".", numbers.Take(4).ToArray());
103
        }
104

105
        public static string[] SplitCommandLine(this string commandLine)
106
        {
107
            char[] parmChars = commandLine.ToCharArray();
108

109
            bool inQuote = false;
110
            for (int index = 0; index < parmChars.Length; index++)
111
            {
112
                if (parmChars[index] == '"')
113
                    inQuote = !inQuote;
114
                if (!inQuote && parmChars[index] == ' ')
115
                    parmChars[index] = '\n';
116
            }
117

118
            return (new string(parmChars))
119
                .Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
120
                .Select(s => s.Replace("\"", ""))
121
                .ToArray();
122
        }
123

124
        public static string AsIndexedRomName(this string name)
125
        {
126
            if (string.IsNullOrEmpty(name))
127
                return "";
128

129
            if (name.Contains("\\") || name.Contains("/"))
130
                name = System.IO.Path.GetFileNameWithoutExtension(name);
131

132
            StringBuilder ret = new StringBuilder(name.Length);
133

134
            bool inpar = false;
135
            bool inblock = false;
136

137
            foreach (var c in name.ToLowerInvariant())
138
            {
139
                if (c == '(')
140
                    inpar = true;
141
                else if (c == ')')
142
                    inpar = false;
143
                else if (c == '[')
144
                    inblock = true;
145
                else if (c == ']')
146
                    inblock = false;
147
                else if (!inpar && !inblock && (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
148
                    ret.Append(c);
149
            }
150

151
            return ret.ToString().Trim();
152
        }
153

154

155
    }
156
}
157

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.