GeezerVison is showing github.com — string_bytes: add StringBytes::IsValidString() · littledan/node@dce26cc
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.cc +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -257,6 +257,13 @@ size_t StringBytes::Write(char* buf, |
257 | 257 | } |
258 | 258 |
|
259 | 259 |
|
| 260 | +bool StringBytes::IsValidString(Handle<String> string, enum encoding enc) { |
| 261 | + if (enc == HEX && string->Length() % 2 != 0) return false; |
| 262 | + // TODO(bnoordhuis) Add BASE64 check? |
| 263 | + return true; |
| 264 | +} |
| 265 | + |
| 266 | + |
260 | 267 | // Quick and dirty size calculation |
261 | 268 | // Will always be at least big enough, but may have some extra |
262 | 269 | // UTF8 can be as much as 3x the size, Base64 can have 1-2 extra bytes |
|
Collapse file
Copy file name to clipboardExpand all lines: src/string_bytes.h +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,11 @@ using v8::Value; |
36 | 36 |
|
37 | 37 | class StringBytes { |
38 | 38 | public: |
| 39 | + // Does the string match the encoding? Quick but non-exhaustive. |
| 40 | + // Example: a HEX string must have a length that's a multiple of two. |
| 41 | + // FIXME(bnoordhuis) IsMaybeValidString()? Naming things is hard... |
| 42 | + static bool IsValidString(Handle<String> string, enum encoding enc); |
| 43 | + |
39 | 44 | // Fast, but can be 2 bytes oversized for Base64, and |
40 | 45 | // as much as triple UTF-8 strings <= 65536 chars in length |
41 | 46 | static size_t StorageSize(Handle<Value> val, enum encoding enc); |
|