summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ArgumentMergingPreProcessor.java
blob: ce818b79fc59aa4c9b3c32d2ba91cf28e78e27d6 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package io.dico.dicore.command.parameter;

public class ArgumentMergingPreProcessor implements IArgumentPreProcessor {
    private final String tokens;
    private final char escapeChar;

    public ArgumentMergingPreProcessor(String tokens, char escapeChar) {
        if ((tokens.length() & 1) != 0 || tokens.isEmpty()) throw new IllegalArgumentException();
        this.tokens = tokens;
        this.escapeChar = escapeChar;
    }

    @Override
    public ArgumentBuffer process(ArgumentBuffer buffer, int count) {
        Parser parser = new Parser(buffer.getArray().clone(), buffer.getCursor(), count);
        String[] array = parser.doProcess();
        ArgumentBuffer result = new ArgumentBuffer(array);
        parser.updateBuffer(result);
        return result;
    }

    private class Parser {
        private final String[] args;
        private final int start;
        private final int count;

        private int foundSectionCount;
        private int currentIndex;
        private int sectionStart;
        private char closingToken;
        private int sectionEnd;
        private int removeCount;

        Parser(String[] args, int start, int count) {
            this.start = start;
            this.args = args;
            this.count = count;
        }

        private void reset() {
            foundSectionCount = 0;
            currentIndex = start;
            sectionStart = -1;
            closingToken = 0;
            sectionEnd = -1;
            removeCount = 0;
        }

        private boolean findNextSectionStart() {
            if (count >= 0 && foundSectionCount >= count) return false;

            while (currentIndex < args.length) {
                String arg = args[currentIndex];
                if (arg == null) {
                    throw new IllegalArgumentException();
                }

                if (arg.isEmpty()) {
                    ++currentIndex;
                    continue;
                }

                int openingTokenIndex = tokens.indexOf(arg.charAt(0));
                if (openingTokenIndex == -1 || (openingTokenIndex & 1) != 0) {
                    ++currentIndex;
                    continue;
                }

                // found
                closingToken = tokens.charAt(openingTokenIndex | 1);
                sectionStart = currentIndex;
                return true;
            }

            return false;
        }

        private boolean findNextSectionEnd() {
            while (currentIndex < args.length) {
                String arg = args[currentIndex];
                if (arg == null) {
                    throw new IllegalArgumentException();
                }

                if (arg.isEmpty()
                    || arg.charAt(arg.length() - 1) != closingToken
                    || (sectionStart == currentIndex && arg.length() == 1)) {
                    ++currentIndex;
                    continue;
                }

                if (escapeChar != 0
                    && arg.length() > 1
                    && arg.charAt(arg.length() - 2) == escapeChar) {
                    // escaped
                    ++currentIndex;
                    continue;
                }

                // found
                closingToken = 0;
                sectionEnd = currentIndex;
                ++currentIndex;
                return true;
            }

            return false;
        }

        private void processFoundSection() {
            if (sectionStart == sectionEnd) {
                String arg = args[sectionStart];
                args[sectionStart] = arg.substring(1, arg.length() - 1);
                return;
            }

            removeCount += sectionEnd - sectionStart;

            StringBuilder sb = new StringBuilder();
            sb.append(args[sectionStart].substring(1));

            for (int i = sectionStart + 1; i < sectionEnd; i++) {
                sb.append(' ');
                sb.append(args[i]);
                args[i] = null;
            }
            sb.append(' ');
            sb.append(args[sectionEnd].substring(0, args[sectionEnd].length() - 1));
            args[sectionEnd] = null;

            args[sectionStart] = sb.toString();

            sectionStart = -1;
            sectionEnd = -1;

            ++foundSectionCount;
        }

        String[] doProcess() {
            reset();

            while (findNextSectionStart()) {
                if (findNextSectionEnd()) {
                    processFoundSection();
                } else {
                    currentIndex = sectionStart + 1;
                }
            }

            if (removeCount == 0) {
                return args;
            }

            String[] result = new String[args.length - removeCount];
            int i = 0;
            for (String arg : args) {
                if (arg != null) {
                    result[i++] = arg;
                }
            }

            return result;
        }

        void updateBuffer(ArgumentBuffer buffer) {
            if (count < 0) {
                buffer.setCursor(start);
            } else {
                buffer.setCursor(currentIndex);
            }
        }

    }

}