src: deduplicate CHECK_EQ/CHECK_NE macros
[platform/upstream/nodejs.git] / src / string_bytes.h
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22 #ifndef SRC_STRING_BYTES_H_
23 #define SRC_STRING_BYTES_H_
24
25 // Decodes a v8::Handle<v8::String> or Buffer to a raw char*
26
27 #include "v8.h"
28 #include "node.h"
29
30 namespace node {
31
32 class StringBytes {
33  public:
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,
39                             enum encoding enc);
40
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,
45                             enum encoding enc);
46
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,
51                      enum encoding enc);
52
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,
57                                const char** data,
58                                size_t* len);
59
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,
65                       char* buf,
66                       size_t buflen,
67                       v8::Handle<v8::Value> val,
68                       enum encoding enc,
69                       int* chars_written = NULL);
70
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,
73                                      const char* buf,
74                                      size_t buflen,
75                                      enum encoding encoding);
76
77   // Deprecated legacy interface
78
79   NODE_DEPRECATED("Use IsValidString(isolate, ...)",
80                   static inline bool IsValidString(
81       v8::Handle<v8::String> string,
82       enum encoding enc) {
83     return IsValidString(v8::Isolate::GetCurrent(), string, enc);
84   })
85
86   NODE_DEPRECATED("Use StorageSize(isolate, ...)",
87                   static inline size_t StorageSize(v8::Handle<v8::Value> val,
88                                                   enum encoding enc) {
89     return StorageSize(v8::Isolate::GetCurrent(), val, enc);
90   })
91
92   NODE_DEPRECATED("Use Size(isolate, ...)",
93                   static inline size_t Size(v8::Handle<v8::Value> val,
94                                             enum encoding enc) {
95     return Size(v8::Isolate::GetCurrent(), val, enc);
96   })
97
98   NODE_DEPRECATED("Use GetExternalParts(isolate, ...)",
99                   static inline bool GetExternalParts(v8::Handle<v8::Value> val,
100                                                       const char** data,
101                                                       size_t* len) {
102     return GetExternalParts(v8::Isolate::GetCurrent(), val, data, len);
103   })
104
105   NODE_DEPRECATED("Use Write(isolate, ...)",
106                   static inline size_t Write(char* buf,
107                                              size_t buflen,
108                                              v8::Handle<v8::Value> val,
109                                              enum encoding enc,
110                                              int* chars_written = NULL) {
111     v8::Isolate* isolate = v8::Isolate::GetCurrent();
112     return Write(isolate, buf, buflen, val, enc, chars_written);
113   })
114
115   NODE_DEPRECATED("Use Encode(isolate, ...)",
116                   static inline v8::Local<v8::Value> Encode(
117       const char* buf,
118       size_t buflen,
119       enum encoding encoding) {
120     return Encode(v8::Isolate::GetCurrent(), buf, buflen, encoding);
121   })
122 };
123
124 }  // namespace node
125
126 #endif  // SRC_STRING_BYTES_H_