From 28c7844c1ef5ea0c8727b890e9ff56b593119a00 Mon Sep 17 00:00:00 2001 From: tsepez Date: Thu, 12 May 2016 15:52:14 -0700 Subject: Add CFX_ByteStringC::CharAt() to avoid c_str() and casts. Most of the time, we want to operate on chars as if they were unsigned, but there are a few places where we need the default (questionably signed) values. Consolidate the casting in a single place rather than forcing callers to get a char* ptr. BUG=pdfium:493 Review-Url: https://codereview.chromium.org/1972053003 --- core/fxcrt/fx_basic_bstring.cpp | 3 +-- core/fxcrt/fx_basic_gcc.cpp | 5 ++--- core/fxcrt/fx_basic_util.cpp | 42 ++++++++++++++++++++--------------------- core/fxcrt/fx_extension.cpp | 12 ++++-------- core/fxcrt/fx_xml_composer.cpp | 20 +++++++------------- core/fxcrt/include/fx_string.h | 3 +++ 6 files changed, 37 insertions(+), 48 deletions(-) (limited to 'core/fxcrt') diff --git a/core/fxcrt/fx_basic_bstring.cpp b/core/fxcrt/fx_basic_bstring.cpp index 18e240bc84..e035dd53d1 100644 --- a/core/fxcrt/fx_basic_bstring.cpp +++ b/core/fxcrt/fx_basic_bstring.cpp @@ -99,9 +99,8 @@ CFX_ByteString::CFX_ByteString(char ch) { } CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& stringSrc) { - if (!stringSrc.IsEmpty()) { + if (!stringSrc.IsEmpty()) m_pData.Reset(StringData::Create(stringSrc.c_str(), stringSrc.GetLength())); - } } CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& str1, diff --git a/core/fxcrt/fx_basic_gcc.cpp b/core/fxcrt/fx_basic_gcc.cpp index ddbdcd5881..a55a486293 100644 --- a/core/fxcrt/fx_basic_gcc.cpp +++ b/core/fxcrt/fx_basic_gcc.cpp @@ -226,9 +226,8 @@ int FXSYS_WideCharToMultiByte(uint32_t codepage, int len = 0; for (int i = 0; i < wlen; i++) { if (wstr[i] < 0x100) { - if (buf && len < buflen) { - buf[len] = (FX_CHAR)wstr[i]; - } + if (buf && len < buflen) + buf[len] = static_cast(wstr[i]); len++; } } diff --git a/core/fxcrt/fx_basic_util.cpp b/core/fxcrt/fx_basic_util.cpp index f69185653f..abb2b9472a 100644 --- a/core/fxcrt/fx_basic_util.cpp +++ b/core/fxcrt/fx_basic_util.cpp @@ -97,19 +97,19 @@ void CFX_PrivateData::ClearAll() { void FX_atonum(const CFX_ByteStringC& strc, FX_BOOL& bInteger, void* pData) { if (strc.Find('.') == -1) { bInteger = TRUE; - int cc = 0, integer = 0; - const FX_CHAR* str = strc.c_str(); - int len = strc.GetLength(); - FX_BOOL bNegative = FALSE; - if (str[0] == '+') { + int cc = 0; + int integer = 0; + FX_STRSIZE len = strc.GetLength(); + bool bNegative = false; + if (strc[0] == '+') { cc++; - } else if (str[0] == '-') { - bNegative = TRUE; + } else if (strc[0] == '-') { + bNegative = true; cc++; } - while (cc < len && std::isdigit(str[cc])) { + while (cc < len && std::isdigit(strc[cc])) { // TODO(dsinclair): This is not the right way to handle overflow. - integer = integer * 10 + FXSYS_toDecimalDigit(str[cc]); + integer = integer * 10 + FXSYS_toDecimalDigit(strc.CharAt(cc)); if (integer < 0) break; cc++; @@ -124,31 +124,30 @@ void FX_atonum(const CFX_ByteStringC& strc, FX_BOOL& bInteger, void* pData) { } } FX_FLOAT FX_atof(const CFX_ByteStringC& strc) { - if (strc.GetLength() == 0) { + if (strc.IsEmpty()) return 0.0; - } + int cc = 0; - FX_BOOL bNegative = FALSE; - const FX_CHAR* str = strc.c_str(); + bool bNegative = false; int len = strc.GetLength(); - if (str[0] == '+') { + if (strc[0] == '+') { cc++; - } else if (str[0] == '-') { + } else if (strc[0] == '-') { bNegative = TRUE; cc++; } while (cc < len) { - if (str[cc] != '+' && str[cc] != '-') { + if (strc[cc] != '+' && strc[cc] != '-') { break; } cc++; } FX_FLOAT value = 0; while (cc < len) { - if (str[cc] == '.') { + if (strc[cc] == '.') { break; } - value = value * 10 + FXSYS_toDecimalDigit(str[cc]); + value = value * 10 + FXSYS_toDecimalDigit(strc.CharAt(cc)); cc++; } static const FX_FLOAT fraction_scales[] = { @@ -156,14 +155,13 @@ FX_FLOAT FX_atof(const CFX_ByteStringC& strc) { 0.00001f, 0.000001f, 0.0000001f, 0.00000001f, 0.000000001f, 0.0000000001f, 0.00000000001f}; int scale = 0; - if (cc < len && str[cc] == '.') { + if (cc < len && strc[cc] == '.') { cc++; while (cc < len) { - value += fraction_scales[scale] * FXSYS_toDecimalDigit(str[cc]); + value += fraction_scales[scale] * FXSYS_toDecimalDigit(strc.CharAt(cc)); scale++; - if (scale == sizeof fraction_scales / sizeof(FX_FLOAT)) { + if (scale == FX_ArraySize(fraction_scales)) break; - } cc++; } } diff --git a/core/fxcrt/fx_extension.cpp b/core/fxcrt/fx_extension.cpp index 0b55fc91dd..315e33fd7e 100644 --- a/core/fxcrt/fx_extension.cpp +++ b/core/fxcrt/fx_extension.cpp @@ -219,17 +219,13 @@ int32_t FXSYS_strnicmp(const FX_CHAR* s1, const FX_CHAR* s2, size_t count) { } uint32_t FX_HashCode_GetA(const CFX_ByteStringC& str, bool bIgnoreCase) { - const FX_CHAR* pStr = str.c_str(); - const FX_CHAR* pStrEnd = pStr + str.GetLength(); uint32_t dwHashCode = 0; if (bIgnoreCase) { - while (pStr < pStrEnd) { - dwHashCode = 31 * dwHashCode + FXSYS_tolower(*pStr++); - } + for (FX_STRSIZE i = 0; i < str.GetLength(); ++i) + dwHashCode = 31 * dwHashCode + FXSYS_tolower(str.CharAt(i)); } else { - while (pStr < pStrEnd) { - dwHashCode = 31 * dwHashCode + *pStr++; - } + for (FX_STRSIZE i = 0; i < str.GetLength(); ++i) + dwHashCode = 31 * dwHashCode + str.CharAt(i); } return dwHashCode; } diff --git a/core/fxcrt/fx_xml_composer.cpp b/core/fxcrt/fx_xml_composer.cpp index 576ff95432..1bad069c34 100644 --- a/core/fxcrt/fx_xml_composer.cpp +++ b/core/fxcrt/fx_xml_composer.cpp @@ -11,24 +11,18 @@ void FX_XML_SplitQualifiedName(const CFX_ByteStringC& bsFullName, CFX_ByteStringC& bsSpace, CFX_ByteStringC& bsName) { - if (bsFullName.IsEmpty()) { + if (bsFullName.IsEmpty()) return; - } - int32_t iStart = 0; - for (; iStart < bsFullName.GetLength(); iStart++) { - if (bsFullName.GetAt(iStart) == ':') { - break; - } - } - if (iStart >= bsFullName.GetLength()) { + + FX_STRSIZE iStart = bsFullName.Find(':'); + if (iStart == -1) { bsName = bsFullName; } else { - bsSpace = CFX_ByteStringC(bsFullName.c_str(), iStart); - iStart++; - bsName = CFX_ByteStringC(bsFullName.c_str() + iStart, - bsFullName.GetLength() - iStart); + bsSpace = bsFullName.Mid(0, iStart); + bsName = bsFullName.Mid(iStart + 1); } } + void CXML_Element::SetTag(const CFX_ByteStringC& qSpace, const CFX_ByteStringC& tagname) { m_QSpaceName = qSpace; diff --git a/core/fxcrt/include/fx_string.h b/core/fxcrt/include/fx_string.h index 8c1a6174b2..617e02ee5a 100644 --- a/core/fxcrt/include/fx_string.h +++ b/core/fxcrt/include/fx_string.h @@ -94,6 +94,9 @@ class CFX_ByteStringC { bool IsEmpty() const { return m_Length == 0; } uint8_t GetAt(FX_STRSIZE index) const { return m_Ptr[index]; } + FX_CHAR CharAt(FX_STRSIZE index) const { + return static_cast(m_Ptr[index]); + } FX_STRSIZE Find(FX_CHAR ch) const { const uint8_t* found = -- cgit v1.2.3