Skip to content

Commit 4e8cddd

Browse files
committed
src: use StringBytes for DecodeWrite/DecodeBytes/Encode
Bonus: this makes node::Encode actually work properly with base64, ucs2, hex, etc.
1 parent 69dac92 commit 4e8cddd

1 file changed

Lines changed: 7 additions & 90 deletions

File tree

src/node.cc

Lines changed: 7 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "node.h"
2323
#include "req_wrap.h"
2424
#include "handle_wrap.h"
25+
#include "string_bytes.h"
2526

2627
#include "ares.h"
2728
#include "uv.h"
@@ -1137,30 +1138,9 @@ enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
11371138
}
11381139

11391140
Local<Value> Encode(const void *buf, size_t len, enum encoding encoding) {
1140-
HandleScope scope;
1141-
1142-
if (encoding == BUFFER) {
1143-
return scope.Close(
1144-
Buffer::New(static_cast<const char*>(buf), len)->handle_);
1145-
}
1146-
1147-
if (!len) return scope.Close(String::Empty());
1148-
1149-
if (encoding == BINARY) {
1150-
const unsigned char *cbuf = static_cast<const unsigned char*>(buf);
1151-
uint16_t * twobytebuf = new uint16_t[len];
1152-
for (size_t i = 0; i < len; i++) {
1153-
// XXX is the following line platform independent?
1154-
twobytebuf[i] = cbuf[i];
1155-
}
1156-
Local<String> chunk = String::New(twobytebuf, len);
1157-
delete [] twobytebuf; // TODO use ExternalTwoByteString?
1158-
return scope.Close(chunk);
1159-
}
1160-
1161-
// utf8 or ascii encoding
1162-
Local<String> chunk = String::New((const char*)buf, len);
1163-
return scope.Close(chunk);
1141+
return StringBytes::Encode(static_cast<const char*>(buf),
1142+
len,
1143+
encoding);
11641144
}
11651145

11661146
// Returns -1 if the handle was not valid for decoding
@@ -1174,17 +1154,7 @@ ssize_t DecodeBytes(v8::Handle<v8::Value> val, enum encoding encoding) {
11741154
return -1;
11751155
}
11761156

1177-
if ((encoding == BUFFER || encoding == BINARY) && Buffer::HasInstance(val)) {
1178-
return Buffer::Length(val->ToObject());
1179-
}
1180-
1181-
Local<String> str = val->ToString();
1182-
1183-
if (encoding == UTF8) return str->Utf8Length();
1184-
else if (encoding == UCS2) return str->Length() * 2;
1185-
else if (encoding == HEX) return str->Length() / 2;
1186-
1187-
return str->Length();
1157+
return StringBytes::Size(val, encoding);
11881158
}
11891159

11901160
#ifndef MIN
@@ -1198,66 +1168,13 @@ ssize_t DecodeWrite(char *buf,
11981168
enum encoding encoding) {
11991169
HandleScope scope;
12001170

1201-
// XXX
1202-
// A lot of improvement can be made here. See:
1203-
// http://code.google.com/p/v8/issues/detail?id=270
1204-
// http://groups.google.com/group/v8-dev/browse_thread/thread/dba28a81d9215291/ece2b50a3b4022c
1205-
// http://groups.google.com/group/v8-users/browse_thread/thread/1f83b0ba1f0a611
1206-
12071171
if (val->IsArray()) {
1208-
fprintf(stderr, "'raw' encoding (array of integers) has been removed. "
1209-
"Use 'binary'.\n");
1172+
fprintf(stderr, "'raw' encoding (array of integers) has been removed.\n");
12101173
assert(0);
12111174
return -1;
12121175
}
12131176

1214-
bool is_buffer = Buffer::HasInstance(val);
1215-
1216-
if (is_buffer && (encoding == BINARY || encoding == BUFFER)) {
1217-
// fast path, copy buffer data
1218-
const char* data = Buffer::Data(val.As<Object>());
1219-
size_t size = Buffer::Length(val.As<Object>());
1220-
size_t len = size < buflen ? size : buflen;
1221-
memcpy(buf, data, len);
1222-
return len;
1223-
}
1224-
1225-
Local<String> str;
1226-
1227-
if (is_buffer) { // slow path, convert to binary string
1228-
Local<Value> arg = String::New("binary");
1229-
str = MakeCallback(val.As<Object>(), "toString", 1, &arg)->ToString();
1230-
}
1231-
else {
1232-
str = val->ToString();
1233-
}
1234-
1235-
if (encoding == UTF8) {
1236-
str->WriteUtf8(buf, buflen, NULL, String::HINT_MANY_WRITES_EXPECTED);
1237-
return buflen;
1238-
}
1239-
1240-
if (encoding == ASCII) {
1241-
str->WriteAscii(buf, 0, buflen, String::HINT_MANY_WRITES_EXPECTED);
1242-
return buflen;
1243-
}
1244-
1245-
// THIS IS AWFUL!!! FIXME
1246-
1247-
assert(encoding == BINARY);
1248-
1249-
uint16_t * twobytebuf = new uint16_t[buflen];
1250-
1251-
str->Write(twobytebuf, 0, buflen, String::HINT_MANY_WRITES_EXPECTED);
1252-
1253-
for (size_t i = 0; i < buflen; i++) {
1254-
unsigned char *b = reinterpret_cast<unsigned char*>(&twobytebuf[i]);
1255-
buf[i] = b[0];
1256-
}
1257-
1258-
delete [] twobytebuf;
1259-
1260-
return buflen;
1177+
return StringBytes::Write(buf, buflen, val, encoding, NULL);
12611178
}
12621179

12631180
void DisplayExceptionLine (TryCatch &try_catch) {

0 commit comments

Comments
 (0)