toolbox_helpers.cpp
2.01 KB
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
#include "toolbox_helpers.h"
#include <ion/charset.h>
#include <apps/i18n.h>
#include <string.h>
#include <assert.h>
namespace Shared {
namespace ToolboxHelpers {
int CursorIndexInCommandText(const char * text) {
size_t textLength = strlen(text);
for (size_t i = 0; i < textLength; i++) {
if (text[i] == '(' || text[i] == '\'') {
return i + 1;
}
if (text[i] == ']') {
return i;
}
}
return textLength;
}
void TextToInsertForCommandMessage(I18n::Message message, char * buffer, int bufferSize, bool replaceArgsWithEmptyChar) {
TextToInsertForCommandText(I18n::translate(message), buffer, bufferSize, replaceArgsWithEmptyChar);
}
void TextToInsertForCommandText(const char * command, char * buffer, int bufferSize, bool replaceArgsWithEmptyChar) {
int currentNewTextIndex = 0;
int numberOfOpenParentheses = 0;
int numberOfOpenBrackets = 0;
bool insideQuote = false;
bool argumentAlreadyReplaced = false;
size_t commandLength = strlen(command);
for (size_t i = 0; i < commandLength; i++) {
if (command[i] == ')') {
numberOfOpenParentheses--;
}
if (command[i] == ']') {
numberOfOpenBrackets--;
}
if (((numberOfOpenParentheses == 0 && numberOfOpenBrackets == 0)
|| command[i] == ','
|| (numberOfOpenBrackets > 0 && (command[i] == ',' || command[i] == '[' || command[i] == ']')))
&& (!insideQuote || command[i] == '\'')) {
assert(currentNewTextIndex < bufferSize);
if (argumentAlreadyReplaced) {
argumentAlreadyReplaced = false;
}
buffer[currentNewTextIndex++] = command[i];
} else {
if (replaceArgsWithEmptyChar && !argumentAlreadyReplaced) {
buffer[currentNewTextIndex++] = Ion::Charset::Empty;
argumentAlreadyReplaced = true;
}
}
if (command[i] == '(') {
numberOfOpenParentheses++;
}
if (command[i] == '[') {
numberOfOpenBrackets++;
}
if (command[i] == '\'') {
insideQuote = !insideQuote;
}
}
buffer[currentNewTextIndex] = 0;
}
}
}