1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #ifndef SRC_STRING_BYTES_H_
23 #define SRC_STRING_BYTES_H_
25 // Decodes a v8::Handle<v8::String> or Buffer to a raw char*
34 // Does the string match the encoding? Quick but non-exhaustive.
35 // Example: a HEX string must have a length that's a multiple of two.
36 // FIXME(bnoordhuis) IsMaybeValidString()? Naming things is hard...
37 static bool IsValidString(v8::Isolate* isolate,
38 v8::Handle<v8::String> string,
41 // Fast, but can be 2 bytes oversized for Base64, and
42 // as much as triple UTF-8 strings <= 65536 chars in length
43 static size_t StorageSize(v8::Isolate* isolate,
44 v8::Handle<v8::Value> val,
47 // Precise byte count, but slightly slower for Base64 and
48 // very much slower for UTF-8
49 static size_t Size(v8::Isolate* isolate,
50 v8::Handle<v8::Value> val,
53 // If the string is external then assign external properties to data and len,
54 // then return true. If not return false.
55 static bool GetExternalParts(v8::Isolate* isolate,
56 v8::Handle<v8::Value> val,
60 // Write the bytes from the string or buffer into the char*
61 // returns the number of bytes written, which will always be
62 // <= buflen. Use StorageSize/Size first to know how much
63 // memory to allocate.
64 static size_t Write(v8::Isolate* isolate,
67 v8::Handle<v8::Value> val,
69 int* chars_written = NULL);
71 // Take the bytes in the src, and turn it into a Buffer or String.
72 static v8::Local<v8::Value> Encode(v8::Isolate* isolate,
75 enum encoding encoding);
77 // Deprecated legacy interface
79 NODE_DEPRECATED("Use IsValidString(isolate, ...)",
80 static inline bool IsValidString(
81 v8::Handle<v8::String> string,
83 return IsValidString(v8::Isolate::GetCurrent(), string, enc);
86 NODE_DEPRECATED("Use StorageSize(isolate, ...)",
87 static inline size_t StorageSize(v8::Handle<v8::Value> val,
89 return StorageSize(v8::Isolate::GetCurrent(), val, enc);
92 NODE_DEPRECATED("Use Size(isolate, ...)",
93 static inline size_t Size(v8::Handle<v8::Value> val,
95 return Size(v8::Isolate::GetCurrent(), val, enc);
98 NODE_DEPRECATED("Use GetExternalParts(isolate, ...)",
99 static inline bool GetExternalParts(v8::Handle<v8::Value> val,
102 return GetExternalParts(v8::Isolate::GetCurrent(), val, data, len);
105 NODE_DEPRECATED("Use Write(isolate, ...)",
106 static inline size_t Write(char* buf,
108 v8::Handle<v8::Value> val,
110 int* chars_written = NULL) {
111 v8::Isolate* isolate = v8::Isolate::GetCurrent();
112 return Write(isolate, buf, buflen, val, enc, chars_written);
115 NODE_DEPRECATED("Use Encode(isolate, ...)",
116 static inline v8::Local<v8::Value> Encode(
119 enum encoding encoding) {
120 return Encode(v8::Isolate::GetCurrent(), buf, buflen, encoding);
126 #endif // SRC_STRING_BYTES_H_