This repository has been archived by the owner on Apr 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandSplitter.cs
executable file
·96 lines (78 loc) · 2.85 KB
/
CommandSplitter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bili_live_dm_console {
public static class CommandSplitter {
public static CommandSplitterResult SplitCommand(string command) {
var result = new CommandSplitterResult();
//==========================check
if (command == "" || command == string.Empty) {
result.CommandBugDescription = "No command";
return result;
}
int quotation = 0;
foreach (var item in command) {
if (item == '"') quotation++;
}
if (quotation % 2 != 0) {
result.CommandBugDescription = "Lost quotation";
return result;
}
//==========================split
//make sure splitter can work. add a space
command += " ";
int quotationCount = 0;
string cacheItem = string.Empty;
foreach (var item in command) {
if (item == '"') quotationCount++; //ignore quotation
else if (item == ' ') {
//push data
if (quotationCount % 2 == 0) {
result.Add(cacheItem);
//clear string
cacheItem = string.Empty;
} else cacheItem += item; //save space
} else cacheItem += item; //save word
}
result.IsFine = true;
return result;
}
}
public class CommandSplitterResult : IEnumerable {
public CommandSplitterResult() {
IsFine = false;
CommandBugDescription = string.Empty;
parameter = new List<string>();
}
public bool IsFine { get; set; }
public string CommandBugDescription { get; set; }
List<string> parameter;
public int Count { get { return parameter.Count; } }
/// <summary>
/// indexer
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public string this[int index] {
get {
return index >= Count ? string.Empty : parameter[index];
}
}
/// <summary>
/// iterator
/// </summary>
/// <returns></returns>
public IEnumerator GetEnumerator() {
foreach (var item in parameter) {
yield return item;
}
}
public void Add(string newParameter) => parameter.Add(newParameter);
public bool Contain(string searchedParameter) {
return parameter.Contains(searchedParameter);
}
}
}