keysym == XKB_KEY_KP_Enter || keysym == XKB_KEY_KP_Equal)
return keysym & 0x7f;
- /* also check for directly encoded 24-bit UCS characters */
- if ((keysym & 0xff000000) == 0x01000000)
- return keysym & 0x00ffffff;
+ /* also check for directly encoded Unicode codepoints */
+ /*
+ * In theory, this is supposed to start from 0x100100, such that the ASCII
+ * range, which is already covered by 0x00-0xff, can't be encoded in two
+ * ways. However, changing this after a couple of decades probably won't
+ * go well, so it stays as it is.
+ */
+ if (0x01000000 <= keysym && keysym <= 0x0110ffff)
+ return keysym - 0x01000000;
/* search main table */
return bin_search(keysymtab, ARRAY_SIZE(keysymtab) - 1, keysym);
length = 3;
head = 0xe0;
}
- else if (unichar <= 0x1fffff) {
+ else if (unichar <= 0x10ffff) {
length = 4;
head = 0xf0;
}
- else if (unichar <= 0x3ffffff) {
- length = 5;
- head = 0xf8;
- }
else {
- length = 6;
- head = 0xfc;
+ buffer[0] = '\0';
+ return 0;
}
for (count = length - 1, shift = 0; count > 0; count--, shift += 6)
fprintf(stderr, "Received keysym %#x -> %s (%u bytes)\n\n", keysym, s,
(unsigned) strlen(s));
+ assert(expected != NULL);
return streq(s, expected);
}
assert(test_utf8(XKB_KEY_KP_Multiply, "*"));
assert(test_utf8(XKB_KEY_KP_Subtract, "-"));
+ assert(test_utf8(0x10005d0, "א"));
+ assert(test_utf8(0x110ffff, "\xf4\x8f\xbf\xbf"));
+ assert(test_utf8(0x1110000, NULL) == 0);
+
assert(xkb_keysym_is_lower(XKB_KEY_a));
assert(xkb_keysym_is_lower(XKB_KEY_Greek_lambda));
assert(xkb_keysym_is_lower(xkb_keysym_from_name("U03b1", 0))); /* GREEK SMALL LETTER ALPHA */
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
+#include <string.h>
#include "utf8.h"
+#include "utils.h"
#define VALID(lit) assert(is_valid_utf8(lit, sizeof(lit)-1))
#define INVALID(lit) assert(!is_valid_utf8(lit, sizeof(lit)-1))
/* INVALID("\xEF\xBF\xBF"); */
}
+static void
+check_utf32_to_utf8(uint32_t unichar, int expected_length, const char *expected) {
+ char buffer[7];
+ int length;
+
+ length = utf32_to_utf8(unichar, buffer);
+
+ assert(length == expected_length);
+ assert(streq(buffer, expected));
+}
+
+static void
+test_utf32_to_utf8(void)
+{
+ check_utf32_to_utf8(0x0, 2, "");
+ check_utf32_to_utf8(0x40, 2, "\x40");
+ check_utf32_to_utf8(0xA1, 3, "\xc2\xa1");
+ check_utf32_to_utf8(0x2701, 4, "\xe2\x9c\x81");
+ check_utf32_to_utf8(0x1f004, 5, "\xf0\x9f\x80\x84");
+ check_utf32_to_utf8(0x110000, 0, "");
+ check_utf32_to_utf8(0xffffffff, 0, "");
+}
+
int
main(void)
{
test_is_valid_utf8();
+ test_utf32_to_utf8();
return 0;
}