diff options
author | Tom Sepez <tsepez@chromium.org> | 2017-04-20 14:31:18 -0700 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-04-20 22:44:39 +0000 |
commit | aeee187c927c07f47a9e5886a417dcc58badefb6 (patch) | |
tree | 270351e970de6ac9f6530ca9c7448dc7b8c499e2 | |
parent | c16ab75eb98733c857723f9c2947c77a56b2bb02 (diff) | |
download | pdfium-chromium/3077.tar.xz |
Remove hand-written bsearch from cfx_xmlsyntaxparser.cppchromium/3077
Convert IsXMLNameChar() to static method for easier testing
without namespace pollution.
Change-Id: I004557e74fde94d3582b0a2326e5e68b907b9690
Reviewed-on: https://pdfium-review.googlesource.com/4397
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
-rw-r--r-- | core/fxcrt/xml/cfx_xmlsyntaxparser.cpp | 25 | ||||
-rw-r--r-- | core/fxcrt/xml/cfx_xmlsyntaxparser.h | 2 | ||||
-rw-r--r-- | core/fxcrt/xml/cfx_xmlsyntaxparser_unittest.cpp | 17 |
3 files changed, 29 insertions, 15 deletions
diff --git a/core/fxcrt/xml/cfx_xmlsyntaxparser.cpp b/core/fxcrt/xml/cfx_xmlsyntaxparser.cpp index aa455d5971..ac3f1b5c5d 100644 --- a/core/fxcrt/xml/cfx_xmlsyntaxparser.cpp +++ b/core/fxcrt/xml/cfx_xmlsyntaxparser.cpp @@ -7,6 +7,7 @@ #include "core/fxcrt/xml/cfx_xmlsyntaxparser.h" #include <algorithm> +#include <iterator> #include "core/fxcrt/fx_extension.h" #include "core/fxcrt/fx_safe_types.h" @@ -35,21 +36,6 @@ const FX_XMLNAMECHAR g_XMLNameChars[] = { {0xF900, 0xFDCF, true}, {0xFDF0, 0xFFFD, true}, }; -bool IsXMLNameChar(wchar_t ch, bool bFirstChar) { - int32_t iStart = 0; - int32_t iEnd = FX_ArraySize(g_XMLNameChars) - 1; - while (iStart <= iEnd) { - int32_t iMid = (iStart + iEnd) / 2; - if (ch < g_XMLNameChars[iMid].wStart) { - iEnd = iMid - 1; - } else if (ch > g_XMLNameChars[iMid].wEnd) { - iStart = iMid + 1; - } else { - return bFirstChar ? g_XMLNameChars[iMid].bStartChar : true; - } - } - return false; -} int32_t GetUTF8EncodeLength(const std::vector<wchar_t>& src, FX_FILESIZE iSrcLen) { @@ -79,6 +65,15 @@ int32_t GetUTF8EncodeLength(const std::vector<wchar_t>& src, } // namespace +// static +bool CFX_XMLSyntaxParser::IsXMLNameChar(wchar_t ch, bool bFirstChar) { + auto* it = std::lower_bound( + std::begin(g_XMLNameChars), std::end(g_XMLNameChars), ch, + [](const FX_XMLNAMECHAR& arg, wchar_t ch) { return arg.wEnd < ch; }); + return it != std::end(g_XMLNameChars) && ch >= it->wStart && + (!bFirstChar || it->bStartChar); +} + CFX_XMLSyntaxParser::CFX_XMLSyntaxParser( const CFX_RetainPtr<CFX_SeekableStreamProxy>& pStream) : m_pStream(pStream), diff --git a/core/fxcrt/xml/cfx_xmlsyntaxparser.h b/core/fxcrt/xml/cfx_xmlsyntaxparser.h index 519f2833d6..32e55f1ff0 100644 --- a/core/fxcrt/xml/cfx_xmlsyntaxparser.h +++ b/core/fxcrt/xml/cfx_xmlsyntaxparser.h @@ -36,6 +36,8 @@ enum class FX_XmlSyntaxResult { class CFX_XMLSyntaxParser { public: + static bool IsXMLNameChar(wchar_t ch, bool bFirstChar); + explicit CFX_XMLSyntaxParser( const CFX_RetainPtr<CFX_SeekableStreamProxy>& pStream); ~CFX_XMLSyntaxParser(); diff --git a/core/fxcrt/xml/cfx_xmlsyntaxparser_unittest.cpp b/core/fxcrt/xml/cfx_xmlsyntaxparser_unittest.cpp index 60c0d34655..09d4fea2d2 100644 --- a/core/fxcrt/xml/cfx_xmlsyntaxparser_unittest.cpp +++ b/core/fxcrt/xml/cfx_xmlsyntaxparser_unittest.cpp @@ -525,3 +525,20 @@ TEST_F(CFX_XMLSyntaxParserTest, EntityOverflowDecimal) { ASSERT_EQ(FX_XmlSyntaxResult::EndOfString, parser.DoSyntaxParse()); } + +TEST_F(CFX_XMLSyntaxParserTest, IsXMLNameChar) { + EXPECT_FALSE(CFX_XMLSyntaxParser::IsXMLNameChar(L'-', true)); + EXPECT_TRUE(CFX_XMLSyntaxParser::IsXMLNameChar(L'-', false)); + + EXPECT_FALSE(CFX_XMLSyntaxParser::IsXMLNameChar(0x2069, true)); + EXPECT_TRUE(CFX_XMLSyntaxParser::IsXMLNameChar(0x2070, true)); + EXPECT_TRUE(CFX_XMLSyntaxParser::IsXMLNameChar(0x2073, true)); + EXPECT_TRUE(CFX_XMLSyntaxParser::IsXMLNameChar(0x218F, true)); + EXPECT_FALSE(CFX_XMLSyntaxParser::IsXMLNameChar(0x2190, true)); + + EXPECT_FALSE(CFX_XMLSyntaxParser::IsXMLNameChar(0xFDEF, true)); + EXPECT_TRUE(CFX_XMLSyntaxParser::IsXMLNameChar(0xFDF0, true)); + EXPECT_TRUE(CFX_XMLSyntaxParser::IsXMLNameChar(0xFDF1, true)); + EXPECT_TRUE(CFX_XMLSyntaxParser::IsXMLNameChar(0xFFFD, true)); + EXPECT_FALSE(CFX_XMLSyntaxParser::IsXMLNameChar(0xFFFE, true)); +} |