Skip to content

Commit dce26cc

Browse files
committed
string_bytes: add StringBytes::IsValidString()
Performs a quick, non-exhaustive check on the input string to see if it's compatible with the specified string encoding. Curently it only checks that hex strings have a length that is a multiple of two.
1 parent 4881a6a commit dce26cc

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

src/string_bytes.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ size_t StringBytes::Write(char* buf,
257257
}
258258

259259

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+
260267
// Quick and dirty size calculation
261268
// Will always be at least big enough, but may have some extra
262269
// UTF8 can be as much as 3x the size, Base64 can have 1-2 extra bytes

src/string_bytes.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ using v8::Value;
3636

3737
class StringBytes {
3838
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+
3944
// Fast, but can be 2 bytes oversized for Base64, and
4045
// as much as triple UTF-8 strings <= 65536 chars in length
4146
static size_t StorageSize(Handle<Value> val, enum encoding enc);

0 commit comments

Comments
 (0)