summaryrefslogtreecommitdiff
path: root/src/node.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/node.cc')
-rw-r--r--src/node.cc106
1 files changed, 6 insertions, 100 deletions
diff --git a/src/node.cc b/src/node.cc
index 1c9d6093a..a382e86ac 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -22,6 +22,7 @@
#include "node.h"
#include "req_wrap.h"
#include "handle_wrap.h"
+#include "string_bytes.h"
#include "ares.h"
#include "uv.h"
@@ -1138,30 +1139,9 @@ enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
}
Local<Value> Encode(const void *buf, size_t len, enum encoding encoding) {
- HandleScope scope(node_isolate);
-
- if (encoding == BUFFER) {
- return scope.Close(
- Buffer::New(static_cast<const char*>(buf), len)->handle_);
- }
-
- if (!len) return scope.Close(String::Empty(node_isolate));
-
- if (encoding == BINARY) {
- const unsigned char *cbuf = static_cast<const unsigned char*>(buf);
- uint16_t * twobytebuf = new uint16_t[len];
- for (size_t i = 0; i < len; i++) {
- // XXX is the following line platform independent?
- twobytebuf[i] = cbuf[i];
- }
- Local<String> chunk = String::New(twobytebuf, len);
- delete [] twobytebuf; // TODO use ExternalTwoByteString?
- return scope.Close(chunk);
- }
-
- // utf8 or ascii encoding
- Local<String> chunk = String::New((const char*)buf, len);
- return scope.Close(chunk);
+ return StringBytes::Encode(static_cast<const char*>(buf),
+ len,
+ encoding);
}
// Returns -1 if the handle was not valid for decoding
@@ -1175,17 +1155,7 @@ ssize_t DecodeBytes(v8::Handle<v8::Value> val, enum encoding encoding) {
return -1;
}
- if ((encoding == BUFFER || encoding == BINARY) && Buffer::HasInstance(val)) {
- return Buffer::Length(val->ToObject());
- }
-
- Local<String> str = val->ToString();
-
- if (encoding == UTF8) return str->Utf8Length();
- else if (encoding == UCS2) return str->Length() * 2;
- else if (encoding == HEX) return str->Length() / 2;
-
- return str->Length();
+ return StringBytes::Size(val, encoding);
}
#ifndef MIN
@@ -1197,71 +1167,7 @@ ssize_t DecodeWrite(char *buf,
size_t buflen,
v8::Handle<v8::Value> val,
enum encoding encoding) {
- HandleScope scope(node_isolate);
-
- // XXX
- // A lot of improvement can be made here. See:
- // http://code.google.com/p/v8/issues/detail?id=270
- // http://groups.google.com/group/v8-dev/browse_thread/thread/dba28a81d9215291/ece2b50a3b4022c
- // http://groups.google.com/group/v8-users/browse_thread/thread/1f83b0ba1f0a611
-
- if (val->IsArray()) {
- fprintf(stderr, "'raw' encoding (array of integers) has been removed. "
- "Use 'binary'.\n");
- assert(0);
- return -1;
- }
-
- bool is_buffer = Buffer::HasInstance(val);
-
- if (is_buffer && (encoding == BINARY || encoding == BUFFER)) {
- // fast path, copy buffer data
- const char* data = Buffer::Data(val.As<Object>());
- size_t size = Buffer::Length(val.As<Object>());
- size_t len = size < buflen ? size : buflen;
- memcpy(buf, data, len);
- return len;
- }
-
- Local<String> str;
-
- if (is_buffer) { // slow path, convert to binary string
- Local<Value> arg = String::New("binary");
- str = MakeCallback(val.As<Object>(), "toString", 1, &arg)->ToString();
- }
- else {
- str = val->ToString();
- }
-
- if (encoding == UTF8) {
- str->WriteUtf8(buf, buflen, NULL, String::HINT_MANY_WRITES_EXPECTED);
- return buflen;
- }
-
- if (encoding == ASCII) {
- str->WriteOneByte(reinterpret_cast<uint8_t*>(buf),
- 0,
- buflen,
- String::HINT_MANY_WRITES_EXPECTED);
- return buflen;
- }
-
- // THIS IS AWFUL!!! FIXME
-
- assert(encoding == BINARY);
-
- uint16_t * twobytebuf = new uint16_t[buflen];
-
- str->Write(twobytebuf, 0, buflen, String::HINT_MANY_WRITES_EXPECTED);
-
- for (size_t i = 0; i < buflen; i++) {
- unsigned char *b = reinterpret_cast<unsigned char*>(&twobytebuf[i]);
- buf[i] = b[0];
- }
-
- delete [] twobytebuf;
-
- return buflen;
+ return StringBytes::Write(buf, buflen, val, encoding, NULL);
}
void DisplayExceptionLine (TryCatch &try_catch) {