Last active
December 6, 2019 01:00
-
-
Save rsms/bf68cf3b37fba6d8d009f9e9c7dd3e83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static unsigned char map[256] = { | |
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ | |
/* 0x00 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // <CTRL> ... | |
/* 0x10 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // <CTRL> ... | |
/* 0x20 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // ! " # $ % & ' ( ) * + , - . / | |
/* 0x30 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0 1 2 3 4 5 6 7 8 9 : ; < = > ? | |
/* 0x40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // @ A B C D E F G H I J K L M N O | |
/* 0x50 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // P Q R S T U V W X Y Z [ \ ] ^ _ | |
/* 0x60 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // ` a b c d e f g h i j k l m n o | |
/* 0x70 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // p q r s t u v w x y z { | } ~ <DEL> | |
/* 0x80 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // <CTRL> ... | |
/* 0x90 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // <CTRL> ... | |
/* 0xA0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // <NBSP> ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ <SOFTHYPEN> ® ¯ | |
/* 0xB0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ | |
/* 0xC0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï | |
/* 0xD0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß | |
/* 0xE0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // à á â ã ä å æ ç è é ê ë ì í î ï | |
/* 0xF0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 // ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static size_t WBufAppendSlug(WBuf* b, const char* pch, size_t len) { | |
WBufReserve(b, len); | |
const char* start = b->ptr; | |
char c = 0, pc = 0; | |
for (size_t i = 0; i < len; i++) { | |
u8 x = (u8)pch[i]; | |
if (x >= 0x80) { | |
// decode UTF8-encoded character as Latin-1 | |
if ((x >> 5) == 0x6 && i+1 < len) { | |
u32 cp = ((x << 6) & 0x7ff) + ((pch[++i]) & 0x3f); | |
x = cp <= 0xFF ? cp : 0; | |
} else { | |
x = 0; | |
} | |
} | |
c = slugMap[x]; | |
if (c != '-' || pc != '-' && pc) { | |
// note: check "pc" to trim leading '-' | |
*(b->ptr++) = c; | |
pc = c; | |
} | |
} | |
if (pc == '-') { | |
// trim trailing '-' | |
b->ptr--; | |
} | |
return b->ptr - start; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment