GeezerVison is showing github.com — string_bytes: introduce InlineDecoder · easternbloc/node@aca2011
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/string_bytes.h +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
5 | 5 |
|
6 | 6 | #include "v8.h" |
7 | 7 | #include "node.h" |
| 8 | +#include "env.h" |
| 9 | +#include "env-inl.h" |
8 | 10 |
|
9 | 11 | namespace node { |
10 | 12 |
|
11 | 13 | extern int WRITE_UTF8_FLAGS; |
12 | 14 |
|
13 | 15 | class StringBytes { |
14 | 16 | public: |
| 17 | + class InlineDecoder { |
| 18 | + public: |
| 19 | + InlineDecoder() : out_(nullptr) { |
| 20 | + } |
| 21 | + |
| 22 | + ~InlineDecoder() { |
| 23 | + if (out_ != out_st_) |
| 24 | + delete[] out_; |
| 25 | + out_ = nullptr; |
| 26 | + } |
| 27 | + |
| 28 | + inline bool Decode(Environment* env, |
| 29 | + v8::Handle<v8::String> string, |
| 30 | + v8::Handle<v8::Value> encoding, |
| 31 | + enum encoding _default) { |
| 32 | + enum encoding enc = ParseEncoding(env->isolate(), encoding, _default); |
| 33 | + if (!StringBytes::IsValidString(env->isolate(), string, enc)) { |
| 34 | + env->ThrowTypeError("Bad input string"); |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + size_t buflen = StringBytes::StorageSize(env->isolate(), string, enc); |
| 39 | + if (buflen > sizeof(out_st_)) |
| 40 | + out_ = new char[buflen]; |
| 41 | + else |
| 42 | + out_ = out_st_; |
| 43 | + size_ = StringBytes::Write(env->isolate(), |
| 44 | + out_, |
| 45 | + buflen, |
| 46 | + string, |
| 47 | + enc); |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + inline const char* out() const { return out_; } |
| 52 | + inline size_t size() const { return size_; } |
| 53 | + |
| 54 | + private: |
| 55 | + static const int kStorageSize = 1024; |
| 56 | + |
| 57 | + char out_st_[kStorageSize]; |
| 58 | + char* out_; |
| 59 | + size_t size_; |
| 60 | + }; |
| 61 | + |
15 | 62 | // Does the string match the encoding? Quick but non-exhaustive. |
16 | 63 | // Example: a HEX string must have a length that's a multiple of two. |
17 | 64 | // FIXME(bnoordhuis) IsMaybeValidString()? Naming things is hard... |
|