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.
This commit is contained in:
Ben Noordhuis committed 2013-07-30 14:32:47 +02:00
1 parent 4881a6a9a3
commit dce26ccea1
2 files changed
+12

No files matched your search

+7
View File
@@ -257,6 +257,13 @@ size_t StringBytes::Write(char* buf,
}
bool StringBytes::IsValidString(Handle<String> string, enum encoding enc) {
if (enc == HEX && string->Length() % 2 != 0) return false;
// TODO(bnoordhuis) Add BASE64 check?
return true;
}
// Quick and dirty size calculation
// Will always be at least big enough, but may have some extra
// UTF8 can be as much as 3x the size, Base64 can have 1-2 extra bytes