From: Ben Noordhuis Date: Tue, 30 Jul 2013 12:26:11 +0000 (+0200) Subject: string_bytes: add StringBytes::IsValidString() X-Git-Tag: v0.10.16~17 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dce26ccea16455179ca538f32d2779cfbf20c51b;p=platform%2Fupstream%2Fnodejs.git 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. --- diff --git a/src/string_bytes.cc b/src/string_bytes.cc index a2ee589..e4a34fe 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -257,6 +257,13 @@ size_t StringBytes::Write(char* buf, } +bool StringBytes::IsValidString(Handle 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 diff --git a/src/string_bytes.h b/src/string_bytes.h index 05d9277..8071a49 100644 --- a/src/string_bytes.h +++ b/src/string_bytes.h @@ -36,6 +36,11 @@ using v8::Value; class StringBytes { public: + // Does the string match the encoding? Quick but non-exhaustive. + // Example: a HEX string must have a length that's a multiple of two. + // FIXME(bnoordhuis) IsMaybeValidString()? Naming things is hard... + static bool IsValidString(Handle string, enum encoding enc); + // Fast, but can be 2 bytes oversized for Base64, and // as much as triple UTF-8 strings <= 65536 chars in length static size_t StorageSize(Handle val, enum encoding enc);