Browse Source

string_bytes: implement new v8 API

v8 has a new API to write out strings to memory. This has been
implemented.

One other change of note is BINARY encoded strings have a new
implementation. This has improved performance substantially.
v0.11.3-release
Trevor Norris 12 years ago
parent
commit
87624ab911
  1. 44
      src/string_bytes.cc

44
src/string_bytes.cc

@ -556,27 +556,31 @@ Local<Value> StringBytes::Encode(const char* buf,
if (contains_non_ascii(buf, buflen)) { if (contains_non_ascii(buf, buflen)) {
char* out = new char[buflen]; char* out = new char[buflen];
force_ascii(buf, out, buflen); force_ascii(buf, out, buflen);
val = String::New(out, buflen); val = String::NewFromOneByte(node_isolate,
reinterpret_cast<const uint8_t*>(out),
String::kNormalString,
buflen);
delete[] out; delete[] out;
} else { } else {
val = String::New(buf, buflen); val = String::NewFromOneByte(node_isolate,
reinterpret_cast<const uint8_t*>(buf),
String::kNormalString,
buflen);
} }
break; break;
case UTF8: case UTF8:
val = String::New(buf, buflen); val = String::NewFromUtf8(node_isolate,
buf,
String::kNormalString,
buflen);
break; break;
case BINARY: { case BINARY: {
// TODO(isaacs) use ExternalTwoByteString? val = String::NewFromOneByte(node_isolate,
const unsigned char* cbuf = reinterpret_cast<const unsigned char*>(buf); reinterpret_cast<const uint8_t*>(buf),
uint16_t* twobytebuf = new uint16_t[buflen]; String::kNormalString,
for (size_t i = 0; i < buflen; i++) { buflen);
// XXX is the following line platform independent?
twobytebuf[i] = cbuf[i];
}
val = String::New(twobytebuf, buflen);
delete[] twobytebuf;
break; break;
} }
@ -587,14 +591,19 @@ Local<Value> StringBytes::Encode(const char* buf,
size_t written = base64_encode(buf, buflen, dst, dlen); size_t written = base64_encode(buf, buflen, dst, dlen);
assert(written == dlen); assert(written == dlen);
val = String::New(dst, dlen); val = String::NewFromOneByte(node_isolate,
reinterpret_cast<const uint8_t*>(dst),
String::kNormalString,
dlen);
delete[] dst; delete[] dst;
break; break;
} }
case UCS2: { case UCS2: {
const uint16_t* data = reinterpret_cast<const uint16_t*>(buf); val = String::NewFromTwoByte(node_isolate,
val = String::New(data, buflen / 2); reinterpret_cast<const uint16_t*>(buf),
String::kNormalString,
buflen / 2);
break; break;
} }
@ -604,7 +613,10 @@ Local<Value> StringBytes::Encode(const char* buf,
size_t written = hex_encode(buf, buflen, dst, dlen); size_t written = hex_encode(buf, buflen, dst, dlen);
assert(written == dlen); assert(written == dlen);
val = String::New(dst, dlen); val = String::NewFromOneByte(node_isolate,
reinterpret_cast<uint8_t*>(dst),
String::kNormalString,
dlen);
delete[] dst; delete[] dst;
break; break;
} }

Loading…
Cancel
Save