@@ -1148,55 +1148,95 @@ Handle<Value> MakeCallback(Isolate* isolate,
1148
1148
}
1149
1149
1150
1150
1151
- enum encoding ParseEncoding (Isolate* isolate,
1152
- Handle <Value> encoding_v,
1153
- enum encoding _default) {
1154
- HandleScope scope (isolate);
1155
-
1156
- if (!encoding_v->IsString ())
1157
- return _default;
1158
-
1159
- node::Utf8Value encoding (isolate, encoding_v);
1151
+ enum encoding ParseEncoding (const char * encoding,
1152
+ enum encoding default_encoding) {
1153
+ switch (encoding[0 ]) {
1154
+ case ' u' :
1155
+ // utf8, utf16le
1156
+ if (encoding[1 ] == ' t' && encoding[2 ] == ' f' ) {
1157
+ // Skip `-`
1158
+ encoding += encoding[3 ] == ' -' ? 4 : 3 ;
1159
+ if (encoding[0 ] == ' 8' && encoding[1 ] == ' \0 ' )
1160
+ return UTF8;
1161
+ if (strncmp (encoding, " 16le" , 4 ) == 0 )
1162
+ return UCS2;
1163
+
1164
+ // ucs2
1165
+ } else if (encoding[1 ] == ' c' && encoding[2 ] == ' s' ) {
1166
+ encoding += encoding[3 ] == ' -' ? 4 : 3 ;
1167
+ if (encoding[0 ] == ' 2' && encoding[1 ] == ' \0 ' )
1168
+ return UCS2;
1169
+ }
1170
+ break ;
1171
+ case ' b' :
1172
+ // binary
1173
+ if (encoding[1 ] == ' i' ) {
1174
+ if (strncmp (encoding + 2 , " nary" , 4 ) == 0 )
1175
+ return BINARY;
1176
+
1177
+ // buffer
1178
+ } else if (encoding[1 ] == ' u' ) {
1179
+ if (strncmp (encoding + 2 , " ffer" , 4 ) == 0 )
1180
+ return BUFFER;
1181
+ }
1182
+ break ;
1183
+ case ' \0 ' :
1184
+ return default_encoding;
1185
+ default :
1186
+ break ;
1187
+ }
1160
1188
1161
- if (strcasecmp (* encoding, " utf8" ) == 0 ) {
1189
+ if (strcasecmp (encoding, " utf8" ) == 0 ) {
1162
1190
return UTF8;
1163
- } else if (strcasecmp (* encoding, " utf-8" ) == 0 ) {
1191
+ } else if (strcasecmp (encoding, " utf-8" ) == 0 ) {
1164
1192
return UTF8;
1165
- } else if (strcasecmp (* encoding, " ascii" ) == 0 ) {
1193
+ } else if (strcasecmp (encoding, " ascii" ) == 0 ) {
1166
1194
return ASCII;
1167
- } else if (strcasecmp (* encoding, " base64" ) == 0 ) {
1195
+ } else if (strcasecmp (encoding, " base64" ) == 0 ) {
1168
1196
return BASE64;
1169
- } else if (strcasecmp (* encoding, " ucs2" ) == 0 ) {
1197
+ } else if (strcasecmp (encoding, " ucs2" ) == 0 ) {
1170
1198
return UCS2;
1171
- } else if (strcasecmp (* encoding, " ucs-2" ) == 0 ) {
1199
+ } else if (strcasecmp (encoding, " ucs-2" ) == 0 ) {
1172
1200
return UCS2;
1173
- } else if (strcasecmp (* encoding, " utf16le" ) == 0 ) {
1201
+ } else if (strcasecmp (encoding, " utf16le" ) == 0 ) {
1174
1202
return UCS2;
1175
- } else if (strcasecmp (* encoding, " utf-16le" ) == 0 ) {
1203
+ } else if (strcasecmp (encoding, " utf-16le" ) == 0 ) {
1176
1204
return UCS2;
1177
- } else if (strcasecmp (* encoding, " binary" ) == 0 ) {
1205
+ } else if (strcasecmp (encoding, " binary" ) == 0 ) {
1178
1206
return BINARY;
1179
- } else if (strcasecmp (* encoding, " buffer" ) == 0 ) {
1207
+ } else if (strcasecmp (encoding, " buffer" ) == 0 ) {
1180
1208
return BUFFER;
1181
- } else if (strcasecmp (* encoding, " hex" ) == 0 ) {
1209
+ } else if (strcasecmp (encoding, " hex" ) == 0 ) {
1182
1210
return HEX;
1183
- } else if (strcasecmp (* encoding, " raw" ) == 0 ) {
1211
+ } else if (strcasecmp (encoding, " raw" ) == 0 ) {
1184
1212
if (!no_deprecation) {
1185
1213
fprintf (stderr, " 'raw' (array of integers) has been removed. "
1186
1214
" Use 'binary'.\n " );
1187
1215
}
1188
1216
return BINARY;
1189
- } else if (strcasecmp (* encoding, " raws" ) == 0 ) {
1217
+ } else if (strcasecmp (encoding, " raws" ) == 0 ) {
1190
1218
if (!no_deprecation) {
1191
1219
fprintf (stderr, " 'raws' encoding has been renamed to 'binary'. "
1192
1220
" Please update your code.\n " );
1193
1221
}
1194
1222
return BINARY;
1195
1223
} else {
1196
- return _default ;
1224
+ return default_encoding ;
1197
1225
}
1198
1226
}
1199
1227
1228
+
1229
+ enum encoding ParseEncoding (Isolate* isolate,
1230
+ Handle <Value> encoding_v,
1231
+ enum encoding default_encoding) {
1232
+ if (!encoding_v->IsString ())
1233
+ return default_encoding;
1234
+
1235
+ node::Utf8Value encoding (isolate, encoding_v);
1236
+
1237
+ return ParseEncoding (*encoding, default_encoding);
1238
+ }
1239
+
1200
1240
Local<Value> Encode (Isolate* isolate,
1201
1241
const char * buf,
1202
1242
size_t len,
0 commit comments