| 5 | bool is_letter(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } |
| 6 | |
| 7 | bool 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; |
no test coverage detected