Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created May 1, 2013 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/5497196 to your computer and use it in GitHub Desktop.
Save isaacs/5497196 to your computer and use it in GitHub Desktop.
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "node.h"
#include "node_buffer.h"
#include "v8.h"
#include "v8-profiler.h"
#include <assert.h>
#include <string.h> // memcpy
#include <limits.h> // INT_MAX
namespace node {
using v8::Handle;
using v8::String;
// TODO: base64, hex, binary
enum StringEncoding {
kAscii,
kUtf8,
kUcs2
};
class StringDecoder {
private:
size_t len_;
char* data_;
char* storage_;
size_t GetStorageSize(Handle<String> string, enum StringEncoding encoding);
void WriteData(Handle<String> string, enum StringEncoding encoding, size_t storage_size);
public:
char* Data() {
return data_;
}
size_t Length() {
return len_;
}
StringDecoder (Handle<String> string, enum StringEncoding encoding);
~StringDecoder();
};
StringDecoder::StringDecoder (
Handle<String> string, enum StringEncoding encoding) {
// TODO If the length is really small, make data a pointer to
// a location on the stack instead of using heap allocated memory
size_t storage_size = GetStorageSize(string, encoding);
if (storage_size > INT_MAX) {
len_ = 0;
data_ = NULL;
return;
}
// always align the data to word boundary
storage_ = new char[storage_size + 15];
data_ = reinterpret_cast<char*>(ROUND_UP(
reinterpret_cast<uintptr_t>(storage_), 16));
WriteData(string, encoding, storage_size);
}
void StringDecoder::WriteData(
Handle<String> string, enum StringEncoding encoding, size_t storage_size) {
switch (encoding) {
case kAscii:
len_ = string->WriteAscii(data_, 0, -1,
String::NO_NULL_TERMINATION | String::HINT_MANY_WRITES_EXPECTED);
break;
case kUtf8:
len_ = string->WriteUtf8(data_, -1, NULL,
String::NO_NULL_TERMINATION | String::HINT_MANY_WRITES_EXPECTED);
break;
case kUcs2: {
int chars_copied = string->Write((uint16_t*) data_, 0, -1,
String::NO_NULL_TERMINATION | String::HINT_MANY_WRITES_EXPECTED);
len_ = chars_copied * sizeof(uint16_t);
break;
}
default:
// Unreachable
assert(0);
}
assert(len_ <= storage_size);
}
StringDecoder::~StringDecoder() {
if (storage_ != NULL)
delete[] storage_;
}
// Compute the size of the storage that the string will be flattened into.
size_t StringDecoder::GetStorageSize(
Handle<String> string, enum StringEncoding encoding) {
size_t storage_size;
switch (encoding) {
case kAscii:
storage_size = string->Length();
break;
case kUtf8:
if (!(string->MayContainNonAscii())) {
// If the string has only ascii characters, we know exactly how big
// the storage should be.
storage_size = string->Length();
} else if (string->Length() < 65536) {
// A single UCS2 codepoint never takes up more than 3 utf8 bytes.
// Unless the string is really long we just allocate so much space that
// we're certain the string fits in there entirely.
// TODO: maybe check handle->write_queue_size instead of string length?
storage_size = 3 * string->Length();
} else {
// The string is really long. Compute the allocation size that we
// actually need.
storage_size = string->Utf8Length();
}
break;
case kUcs2:
storage_size = string->Length() * sizeof(uint16_t);
break;
default:
// Unreachable.
assert(0);
}
return storage_size;
}
} // namespace node
@trevnorris
Copy link

If this is going into master then you'll need a different way to MayContainNonAscii. Comment says "This function is no longer useful." and is, or will be soon, deprecated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment