From 8f32ac525f34aa32cd6dab4d13d0cfe84bf30879 Mon Sep 17 00:00:00 2001 From: Paul LeoNerd Evans Date: Tue, 6 Sep 2011 22:49:05 +0100 Subject: Canonicalise (a local copy of) the key structures given to termkey_keycmp() before comparing them --- t/13cmpkey.c | 14 +++++++++++++- termkey.c | 30 ++++++++++++++++++------------ termkey_keycmp.3 | 5 +++-- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/t/13cmpkey.c b/t/13cmpkey.c index 90ff9e1..62902cc 100644 --- a/t/13cmpkey.c +++ b/t/13cmpkey.c @@ -6,7 +6,7 @@ int main(int argc, char *argv[]) TermKey *tk; TermKeyKey key1, key2; - plan_tests(10); + plan_tests(12); tk = termkey_new(0, TERMKEY_FLAG_NOTERMIOS); @@ -44,6 +44,18 @@ int main(int argc, char *argv[]) ok(termkey_keycmp(tk, &key1, &key2) < 0, "cmpkey orders KEYSYM after UNICODE"); ok(termkey_keycmp(tk, &key2, &key1) > 0, "cmpkey orders UNICODE before KEYSYM"); + key1.type = TERMKEY_TYPE_KEYSYM; + key1.code.sym = TERMKEY_SYM_SPACE; + key1.modifiers = 0; + key2.type = TERMKEY_TYPE_UNICODE; + key2.code.codepoint = ' '; + key2.modifiers = 0; + + is_int(termkey_keycmp(tk, &key1, &key2), 0, "cmpkey considers KEYSYM/SPACE and UNICODE/SP identical"); + + termkey_set_canonflags(tk, termkey_get_canonflags(tk) | TERMKEY_CANON_SPACESYMBOL); + is_int(termkey_keycmp(tk, &key1, &key2), 0, "cmpkey considers KEYSYM/SPACE and UNICODE/SP identical under SPACESYMBOL"); + termkey_destroy(tk); return exit_status(); diff --git a/termkey.c b/termkey.c index 9376916..a93e00f 100644 --- a/termkey.c +++ b/termkey.c @@ -1244,28 +1244,34 @@ char *termkey_strpkey(TermKey *tk, const char *str, TermKeyKey *key, TermKeyForm return (char *)str; } -int termkey_keycmp(TermKey *tk, const TermKeyKey *key1, const TermKeyKey *key2) +int termkey_keycmp(TermKey *tk, const TermKeyKey *key1p, const TermKeyKey *key2p) { - if(key1->type != key2->type) - return key1->type - key2->type; + /* Copy the key structs since we'll be modifying them */ + TermKeyKey key1 = *key1p, key2 = *key2p; - switch(key1->type) { + termkey_canonicalise(tk, &key1); + termkey_canonicalise(tk, &key2); + + if(key1.type != key2.type) + return key1.type - key2.type; + + switch(key1.type) { case TERMKEY_TYPE_UNICODE: - if(key1->code.codepoint != key2->code.codepoint) - return key1->code.codepoint - key2->code.codepoint; + if(key1.code.codepoint != key2.code.codepoint) + return key1.code.codepoint - key2.code.codepoint; case TERMKEY_TYPE_KEYSYM: - if(key1->code.sym != key2->code.sym) - return key1->code.sym - key2->code.sym; + if(key1.code.sym != key2.code.sym) + return key1.code.sym - key2.code.sym; case TERMKEY_TYPE_FUNCTION: - if(key1->code.number != key2->code.number) - return key1->code.number - key2->code.number; + if(key1.code.number != key2.code.number) + return key1.code.number - key2.code.number; case TERMKEY_TYPE_MOUSE: { - int cmp = strncmp(key1->code.mouse, key2->code.mouse, 4); + int cmp = strncmp(key1.code.mouse, key2.code.mouse, 4); if(cmp != 0) return cmp; } } - return key1->modifiers - key2->modifiers; + return key1.modifiers - key2.modifiers; } diff --git a/termkey_keycmp.3 b/termkey_keycmp.3 index d2a0c9b..3eed66c 100644 --- a/termkey_keycmp.3 +++ b/termkey_keycmp.3 @@ -11,7 +11,7 @@ termkey_keycmp \- compare two key events .sp Link with \fI-ltermkey\fP. .SH DESCRIPTION -\fBtermkey_keycmp\fP() compares two key structures and applies a total ordering, returning a value that is positive, zero, or negative, to indicate if the given structures are increasing, identical, or decreasing. +\fBtermkey_keycmp\fP() compares two key structures and applies a total ordering, returning a value that is positive, zero, or negative, to indicate if the given structures are increasing, identical, or decreasing. Before comparison, copies of both referenced structures are taken, and canonicalised according to the rules for \fBtermkey_canonicalise\fP(3). .PP Two structures of differing type are ordered \fBTERMKEY_TYPE_UNICODE\fP, \fBTERMKEY_TYPE_KEYSYM\fP, \fBTERMKEY_TYPE_FUNCTION\fP, \fBTERMKEY_TYPE_MOUSE\fP. Unicode structures are ordered by codepoint, keysym structures are ordered by keysym number, function structures are ordered by function key number, and mouse structures are ordered opaquely by an unspecified but consistent ordering. Within these values, keys different in modifier bits are ordered by the modifiers. .SH "RETURN VALUE" @@ -21,4 +21,5 @@ This function does not perform any canonicalisation of the key structures, and p .SH "SEE ALSO" .BR termkey_new (3), .BR termkey_getkey (3), -.BR termkey_strpkey (3) +.BR termkey_strpkey (3), +.BR termkey_canonicalise (3) -- cgit v1.2.3-70-g09d2