diff options
author | Fedor Indutny <fedor@indutny.com> | 2015-01-30 15:35:11 +0300 |
---|---|---|
committer | Fedor Indutny <fedor@indutny.com> | 2015-01-30 17:41:54 +0300 |
commit | aca20112519decef44474a2ee9936049e2a38b67 (patch) | |
tree | 21c301f01d5999e0f9bbcc81fabe2e46c55b2768 /src/string_bytes.h | |
parent | c6367e7f2a68b2418a98dfe9e829f17f62ba403a (diff) | |
download | android-node-v8-aca20112519decef44474a2ee9936049e2a38b67.tar.gz android-node-v8-aca20112519decef44474a2ee9936049e2a38b67.tar.bz2 android-node-v8-aca20112519decef44474a2ee9936049e2a38b67.zip |
string_bytes: introduce InlineDecoder
PR-URL: https://github.com/iojs/io.js/pull/664
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'src/string_bytes.h')
-rw-r--r-- | src/string_bytes.h | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/string_bytes.h b/src/string_bytes.h index 711e593a0f..424d9245aa 100644 --- a/src/string_bytes.h +++ b/src/string_bytes.h @@ -5,6 +5,8 @@ #include "v8.h" #include "node.h" +#include "env.h" +#include "env-inl.h" namespace node { @@ -12,6 +14,51 @@ extern int WRITE_UTF8_FLAGS; class StringBytes { public: + class InlineDecoder { + public: + InlineDecoder() : out_(nullptr) { + } + + ~InlineDecoder() { + if (out_ != out_st_) + delete[] out_; + out_ = nullptr; + } + + inline bool Decode(Environment* env, + v8::Handle<v8::String> string, + v8::Handle<v8::Value> encoding, + enum encoding _default) { + enum encoding enc = ParseEncoding(env->isolate(), encoding, _default); + if (!StringBytes::IsValidString(env->isolate(), string, enc)) { + env->ThrowTypeError("Bad input string"); + return false; + } + + size_t buflen = StringBytes::StorageSize(env->isolate(), string, enc); + if (buflen > sizeof(out_st_)) + out_ = new char[buflen]; + else + out_ = out_st_; + size_ = StringBytes::Write(env->isolate(), + out_, + buflen, + string, + enc); + return true; + } + + inline const char* out() const { return out_; } + inline size_t size() const { return size_; } + + private: + static const int kStorageSize = 1024; + + char out_st_[kStorageSize]; + char* out_; + size_t size_; + }; + // Does the string match the encoding? Quick but non-exhaustive. // Example: a HEX string must have a length that's a multiple of two. // FIXME(bnoordhuis) IsMaybeValidString()? Naming things is hard... |