MCPcopy Create free account
hub / github.com/ddnet/ddnet / IsValidEditorTooltip

Function IsValidEditorTooltip

src/test/editor_test.cpp:7–76  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5bool is_letter(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
6
7bool IsValidEditorTooltip(const char *pTooltip, char *pErrorMsg, int ErrorMsgSize)
8{
9 pErrorMsg[0] = '\0';
10 char aHotkey[512];
11 aHotkey[0] = '\0';
12
13 if(pTooltip[0] == '[')
14 {
15 str_copy(aHotkey, pTooltip + 1);
16 const char *pHotkeyEnd = str_find(aHotkey, "]");
17 if(!pHotkeyEnd)
18 {
19 str_copy(pErrorMsg, "tooltip missing closing square bracket", ErrorMsgSize);
20 return false;
21 }
22 aHotkey[pHotkeyEnd - aHotkey] = '\0';
23
24 for(int i = 0; aHotkey[i]; i++)
25 {
26 bool ExpectLowerCase = true;
27 if(i == 0)
28 {
29 ExpectLowerCase = false;
30 }
31 else if(!is_letter(aHotkey[i - 1]))
32 {
33 // the first character of a word should be uppercase
34 ExpectLowerCase = false;
35 }
36
37 bool IsLower = aHotkey[i] >= 'a' && aHotkey[i] <= 'z';
38 bool IsUpper = aHotkey[i] >= 'A' && aHotkey[i] <= 'Z';
39
40 if(ExpectLowerCase && IsUpper)
41 {
42 str_format(pErrorMsg, ErrorMsgSize, "expected character '%c' at index %d to be lower case", aHotkey[i], i + 1);
43 return false;
44 }
45 if(!ExpectLowerCase && IsLower)
46 {
47 str_format(pErrorMsg, ErrorMsgSize, "expected character '%c' at index %d to be upper case", aHotkey[i], i + 1);
48 return false;
49 }
50 }
51 }
52
53 const char *pParenthesis = str_find(pTooltip, "(");
54 if(pParenthesis)
55 {
56 const char *pHotkey = str_find_nocase(pParenthesis, "ctrl");
57 if(!pHotkey)
58 pHotkey = str_find_nocase(pParenthesis, "shift");
59 if(!pHotkey)
60 pHotkey = str_find_nocase(pParenthesis, "home");
61
62 if(pHotkey)
63 {
64 int Offset = pHotkey - pTooltip;

Callers 2

AssertTooltipFunction · 0.85
TESTFunction · 0.85

Calls 6

str_findFunction · 0.85
is_letterFunction · 0.85
str_formatFunction · 0.85
str_find_nocaseFunction · 0.85
str_endswithFunction · 0.85
str_copyFunction · 0.50

Tested by

no test coverage detected