From d3640c792bf21a0faeb6db1750309dfd0e5c3d00 Mon Sep 17 00:00:00 2001 From: ntrifunovic Date: Wed, 26 Jul 2006 18:33:43 +0000 Subject: [PATCH] Added the code point validity check to utf::append git-svn-id: http://svn.code.sf.net/p/utfcpp/code@28 a809a056-fc17-0410-9590-b4f493f8b08e --- source/utf8.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/source/utf8.h b/source/utf8.h index 3b5ef65..729d151 100644 --- a/source/utf8.h +++ b/source/utf8.h @@ -110,6 +110,12 @@ namespace internal return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX); } + template + inline bool is_code_point_valid(u32 cp) + { + return (cp <= CODE_POINT_MAX && !is_surrogate(cp) && cp != 0xfffe && cp != 0xffff); + } + enum utf_error {OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT}; template @@ -185,7 +191,7 @@ namespace internal break; } // Is the code point valid? - if (cp > CODE_POINT_MAX || is_surrogate(cp) || cp == 0xfffe || cp == 0xffff) { + if (!is_code_point_valid(cp)) { for (octet_difference_type i = 0; i < sequence_length - 1; ++i) --it; return INVALID_CODE_POINT; @@ -260,7 +266,7 @@ namespace internal if (cp < 0x80) // one octet *(result++) = static_cast(cp); else if (cp < 0x800) { // two octets - if (internal::is_surrogate(cp)) + if (!internal::is_code_point_valid(cp)) throw invalid_code_point(cp); *(result++) = static_cast((cp >> 6) | 0xc0);