Stop relying on NUL string termination in the InputMap.

* I don't anticipate caring about this, but in principle, there's nothing
   wrong with a NUL character in the input map, so fix this while the code
   is fresh in my mind.
This commit is contained in:
Ryan Prichard 2015-12-02 00:50:13 -06:00
parent 3fc4b88b39
commit cf4a99e63b
3 changed files with 16 additions and 21 deletions

View File

@ -22,6 +22,7 @@
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "InputMap.h"
@ -184,6 +185,7 @@ static void addSimpleEntries(InputMap &inputMap) {
for (size_t i = 0; i < DIM(simpleEncodings); ++i) {
inputMap.set(simpleEncodings[i].encoding,
strlen(simpleEncodings[i].encoding),
simpleEncodings[i].key);
}
}
@ -194,10 +196,11 @@ struct ExpandContext {
char *buffer;
};
static inline void setEncoding(const ExpandContext &ctx, int extraKeyState) {
static inline void setEncoding(const ExpandContext &ctx, char *end,
int extraKeyState) {
InputMap::Key k = ctx.e.key;
k.keyState |= extraKeyState;
ctx.inputMap.set(ctx.buffer, k);
ctx.inputMap.set(ctx.buffer, end - ctx.buffer, k);
}
static inline int keyStateForMod(int mod) {
@ -213,26 +216,22 @@ static void expandNumericEncodingSuffix(const ExpandContext &ctx, char *p,
{
char *q = p;
*q++ = '~';
*q++ = '\0';
setEncoding(ctx, extraKeyState);
setEncoding(ctx, q, extraKeyState);
}
if (ctx.e.modifiers & kSuffixShift) {
char *q = p;
*q++ = '$';
*q++ = '\0';
setEncoding(ctx, extraKeyState | SHIFT_PRESSED);
setEncoding(ctx, q, extraKeyState | SHIFT_PRESSED);
}
if (ctx.e.modifiers & kSuffixCtrl) {
char *q = p;
*q++ = '^';
*q++ = '\0';
setEncoding(ctx, extraKeyState | LEFT_CTRL_PRESSED);
setEncoding(ctx, q, extraKeyState | LEFT_CTRL_PRESSED);
}
if (ctx.e.modifiers & (kSuffixCtrl | kSuffixShift)) {
char *q = p;
*q++ = '@';
*q++ = '\0';
setEncoding(ctx, extraKeyState | SHIFT_PRESSED | LEFT_CTRL_PRESSED);
setEncoding(ctx, q, extraKeyState | SHIFT_PRESSED | LEFT_CTRL_PRESSED);
}
}
@ -248,8 +247,7 @@ static inline void expandEncodingAfterAltPrefix(
expandNumericEncodingSuffix(ctx, q, extraKeyState);
} else {
*q++ = ctx.e.id;
*q++ = '\0';
setEncoding(ctx, extraKeyState);
setEncoding(ctx, q, extraKeyState);
}
}
if (ctx.e.modifiers & kBareMod) {
@ -258,8 +256,7 @@ static inline void expandEncodingAfterAltPrefix(
char *q = p;
*q++ = '0' + mod;
*q++ = ctx.e.id;
*q++ = '\0';
setEncoding(ctx, extraKeyState | keyStateForMod(mod));
setEncoding(ctx, q, extraKeyState | keyStateForMod(mod));
}
}
if (ctx.e.modifiers & kSemiMod) {
@ -276,8 +273,7 @@ static inline void expandEncodingAfterAltPrefix(
*q++ = ';';
*q++ = '0' + mod;
*q++ = ctx.e.id;
*q++ = '\0';
setEncoding(ctx, extraKeyState | keyStateForMod(mod));
setEncoding(ctx, q, extraKeyState | keyStateForMod(mod));
}
}
}

View File

@ -148,12 +148,11 @@ InputMap::~InputMap() {
delete [] m_children;
}
void InputMap::set(const char *encoding, const Key &key) {
unsigned char ch = encoding[0];
if (ch == '\0') {
void InputMap::set(const char *encoding, int encodingLen, const Key &key) {
if (encodingLen == 0) {
setKey(key);
} else {
getOrCreateChild(ch)->set(encoding + 1, key);
getOrCreateChild(encoding[0])->set(encoding + 1, encodingLen - 1, key);
}
}

View File

@ -39,7 +39,7 @@ public:
InputMap();
~InputMap();
void set(const char *encoding, const Key &key);
void set(const char *encoding, int encodingLen, const Key &key);
void setKey(const Key &key);
const Key *getKey() const { return m_key; }
bool hasChildren() const { return m_children != NULL; }