Skip to content

Commit aca2011

Browse files
committed
string_bytes: introduce InlineDecoder
PR-URL: #664 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent c6367e7 commit aca2011

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/string_bytes.h

+47
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,60 @@
55

66
#include "v8.h"
77
#include "node.h"
8+
#include "env.h"
9+
#include "env-inl.h"
810

911
namespace node {
1012

1113
extern int WRITE_UTF8_FLAGS;
1214

1315
class StringBytes {
1416
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+
1562
// Does the string match the encoding? Quick but non-exhaustive.
1663
// Example: a HEX string must have a length that's a multiple of two.
1764
// FIXME(bnoordhuis) IsMaybeValidString()? Naming things is hard...

0 commit comments

Comments
 (0)