@@ -1142,69 +1142,39 @@ char_from_hex(int x)
11421142 return Py_hexdigits [x ];
11431143}
11441144
1145+ /* This table maps characters to their hexadecimal values, only
1146+ * works with encodings whose lower half is ASCII (like UTF-8).
1147+ * '0' maps to 0, ..., '9' maps to 9.
1148+ * 'a' and 'A' map to 10, ..., 'f' and 'F' map to 15.
1149+ * All other indices map to -1.
1150+ */
1151+ static const int
1152+ _CHAR_TO_HEX [256 ] = {
1153+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1154+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1155+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1156+ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , -1 , -1 , -1 , -1 , -1 , -1 ,
1157+ -1 , 10 , 11 , 12 , 13 , 14 , 15 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1158+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1159+ -1 , 10 , 11 , 12 , 13 , 14 , 15 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1160+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1161+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1162+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1163+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1164+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1165+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1166+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1167+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1168+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
1169+ };
1170+
1171+ /* Convert a character to its hexadecimal value, or -1 if it's not a
1172+ * valid hexadecimal character, only works with encodings whose lower
1173+ * half is ASCII (like UTF-8).
1174+ */
11451175static int
1146- hex_from_char (char c ) {
1147- int x ;
1148- switch (c ) {
1149- case '0' :
1150- x = 0 ;
1151- break ;
1152- case '1' :
1153- x = 1 ;
1154- break ;
1155- case '2' :
1156- x = 2 ;
1157- break ;
1158- case '3' :
1159- x = 3 ;
1160- break ;
1161- case '4' :
1162- x = 4 ;
1163- break ;
1164- case '5' :
1165- x = 5 ;
1166- break ;
1167- case '6' :
1168- x = 6 ;
1169- break ;
1170- case '7' :
1171- x = 7 ;
1172- break ;
1173- case '8' :
1174- x = 8 ;
1175- break ;
1176- case '9' :
1177- x = 9 ;
1178- break ;
1179- case 'a' :
1180- case 'A' :
1181- x = 10 ;
1182- break ;
1183- case 'b' :
1184- case 'B' :
1185- x = 11 ;
1186- break ;
1187- case 'c' :
1188- case 'C' :
1189- x = 12 ;
1190- break ;
1191- case 'd' :
1192- case 'D' :
1193- x = 13 ;
1194- break ;
1195- case 'e' :
1196- case 'E' :
1197- x = 14 ;
1198- break ;
1199- case 'f' :
1200- case 'F' :
1201- x = 15 ;
1202- break ;
1203- default :
1204- x = -1 ;
1205- break ;
1206- }
1207- return x ;
1176+ hex_from_char (unsigned char c ) {
1177+ return _CHAR_TO_HEX [c ];
12081178}
12091179
12101180/* convert a float to a hexadecimal string */
0 commit comments