From 8a1758bf11c2d741e0cddc761b1dd2cdf564db93 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Tue, 15 Aug 2017 10:37:59 -0400 Subject: Remove GetAt from string classes This method duplicates the behaviour of the const [] operator and doesn't offer any additional safety. Folding them into one implementation. SetAt is retained, since implementing the non-const [] operator to replace SetAt has potential performance concerns. Specifically many non-obvious cases of reading an element using [] will cause a realloc & copy. BUG=pdfium:860 Change-Id: I3ef5e5e5a15376f040256b646eb0d90636e24b67 Reviewed-on: https://pdfium-review.googlesource.com/10870 Commit-Queue: Ryan Harrison Reviewed-by: Tom Sepez --- core/fpdfapi/font/cpdf_cidfont.cpp | 6 +-- core/fpdfapi/font/cpdf_cmapparser.cpp | 25 +++++----- core/fpdfapi/font/cpdf_font.cpp | 2 +- core/fpdfapi/font/cpdf_tounicodemap.cpp | 4 +- core/fpdfapi/render/cpdf_charposlist.cpp | 2 +- core/fpdfdoc/cpdf_filespec.cpp | 18 ++++---- core/fpdfdoc/cpdf_variabletext.cpp | 12 ++--- core/fpdfdoc/cpvt_generateap.cpp | 2 +- core/fpdftext/cpdf_linkextract.cpp | 6 +-- core/fpdftext/cpdf_textpage.cpp | 30 ++++++------ core/fpdftext/cpdf_textpagefind.cpp | 26 +++++------ core/fxcrt/cfx_bytestring.cpp | 16 +++---- core/fxcrt/cfx_bytestring.h | 5 +- core/fxcrt/cfx_bytestring_unittest.cpp | 79 +++++++++++--------------------- core/fxcrt/cfx_chariter.cpp | 2 +- core/fxcrt/cfx_string_c_template.h | 6 +-- core/fxcrt/cfx_widestring.cpp | 12 ++--- core/fxcrt/cfx_widestring.h | 4 +- core/fxcrt/cfx_widestring_unittest.cpp | 77 +++++++++++-------------------- core/fxcrt/xml/cxml_parser.cpp | 2 +- 20 files changed, 141 insertions(+), 195 deletions(-) (limited to 'core') diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp index 8cb59de739..9a2261caa9 100644 --- a/core/fpdfapi/font/cpdf_cidfont.cpp +++ b/core/fpdfapi/font/cpdf_cidfont.cpp @@ -627,7 +627,7 @@ int CPDF_CIDFont::GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) { if (uni_str.IsEmpty()) return cid; - unicode = uni_str.GetAt(0); + unicode = uni_str[0]; #endif } else { if (cid && m_pCID2UnicodeMap && m_pCID2UnicodeMap->IsLoaded()) @@ -637,7 +637,7 @@ int CPDF_CIDFont::GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) { if (unicode == 0) { CFX_WideString unicode_str = UnicodeFromCharCode(charcode); if (!unicode_str.IsEmpty()) - unicode = unicode_str.GetAt(0); + unicode = unicode_str[0]; } } FXFT_Face face = m_Font.GetFace(); @@ -735,7 +735,7 @@ int CPDF_CIDFont::GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) { if (unicode_str.IsEmpty()) return -1; - charcode = unicode_str.GetAt(0); + charcode = unicode_str[0]; } return GetGlyphIndex(charcode, pVertGlyph); } diff --git a/core/fpdfapi/font/cpdf_cmapparser.cpp b/core/fpdfapi/font/cpdf_cmapparser.cpp index cbf693966e..272f8deb34 100644 --- a/core/fpdfapi/font/cpdf_cmapparser.cpp +++ b/core/fpdfapi/font/cpdf_cmapparser.cpp @@ -121,7 +121,7 @@ void CPDF_CMapParser::ParseWord(const CFX_ByteStringC& word) { } m_Status = 0; } else { - if (word.GetLength() == 0 || word.GetAt(0) != '<') { + if (word.GetLength() == 0 || word[0] != '<') { return; } if (m_CodeSeq % 2) { @@ -140,18 +140,17 @@ uint32_t CPDF_CMapParser::CMap_GetCode(const CFX_ByteStringC& word) { if (word.IsEmpty()) return 0; pdfium::base::CheckedNumeric num = 0; - if (word.GetAt(0) == '<') { - for (int i = 1; i < word.GetLength() && std::isxdigit(word.GetAt(i)); ++i) { - num = num * 16 + FXSYS_HexCharToInt(word.GetAt(i)); + if (word[0] == '<') { + for (int i = 1; i < word.GetLength() && std::isxdigit(word[i]); ++i) { + num = num * 16 + FXSYS_HexCharToInt(word[i]); if (!num.IsValid()) return 0; } return num.ValueOrDie(); } - for (int i = 0; i < word.GetLength() && std::isdigit(word.GetAt(i)); ++i) { - num = - num * 10 + FXSYS_DecimalCharToInt(static_cast(word.GetAt(i))); + for (int i = 0; i < word.GetLength() && std::isdigit(word[i]); ++i) { + num = num * 10 + FXSYS_DecimalCharToInt(static_cast(word[i])); if (!num.IsValid()) return 0; } @@ -162,12 +161,12 @@ uint32_t CPDF_CMapParser::CMap_GetCode(const CFX_ByteStringC& word) { bool CPDF_CMapParser::CMap_GetCodeRange(CPDF_CMap::CodeRange& range, const CFX_ByteStringC& first, const CFX_ByteStringC& second) { - if (first.GetLength() == 0 || first.GetAt(0) != '<') + if (first.GetLength() == 0 || first[0] != '<') return false; int i; for (i = 1; i < first.GetLength(); ++i) { - if (first.GetAt(i) == '>') { + if (first[i] == '>') { break; } } @@ -176,8 +175,8 @@ bool CPDF_CMapParser::CMap_GetCodeRange(CPDF_CMap::CodeRange& range, return false; for (i = 0; i < range.m_CharSize; ++i) { - uint8_t digit1 = first.GetAt(i * 2 + 1); - uint8_t digit2 = first.GetAt(i * 2 + 2); + uint8_t digit1 = first[i * 2 + 1]; + uint8_t digit2 = first[i * 2 + 2]; range.m_Lower[i] = FXSYS_HexCharToInt(digit1) * 16 + FXSYS_HexCharToInt(digit2); } @@ -185,10 +184,10 @@ bool CPDF_CMapParser::CMap_GetCodeRange(CPDF_CMap::CodeRange& range, uint32_t size = second.GetLength(); for (i = 0; i < range.m_CharSize; ++i) { uint8_t digit1 = ((uint32_t)i * 2 + 1 < size) - ? second.GetAt((FX_STRSIZE)i * 2 + 1) + ? second[static_cast(i * 2 + 1)] : '0'; uint8_t digit2 = ((uint32_t)i * 2 + 2 < size) - ? second.GetAt((FX_STRSIZE)i * 2 + 2) + ? second[static_cast(i * 2 + 2)] : '0'; range.m_Upper[i] = FXSYS_HexCharToInt(digit1) * 16 + FXSYS_HexCharToInt(digit2); diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp index 82f9be3361..0fd8b45326 100644 --- a/core/fpdfapi/font/cpdf_font.cpp +++ b/core/fpdfapi/font/cpdf_font.cpp @@ -467,7 +467,7 @@ int CPDF_Font::FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode) { return -1; CFX_WideString str = UnicodeFromCharCode(charcode); - uint32_t unicode = !str.IsEmpty() ? str.GetAt(0) : charcode; + uint32_t unicode = !str.IsEmpty() ? str[0] : charcode; int glyph = FXFT_Get_Char_Index(m_FontFallbacks[fallbackFont]->GetFace(), unicode); if (glyph == 0) diff --git a/core/fpdfapi/font/cpdf_tounicodemap.cpp b/core/fpdfapi/font/cpdf_tounicodemap.cpp index 30bf0317ee..9076f4a737 100644 --- a/core/fpdfapi/font/cpdf_tounicodemap.cpp +++ b/core/fpdfapi/font/cpdf_tounicodemap.cpp @@ -147,7 +147,7 @@ void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream) { continue; } if (len == 1) { - m_Map[srccode] = destcode.GetAt(0); + m_Map[srccode] = destcode[0]; } else { m_Map[srccode] = GetUnicode(); m_MultiCharBuf.AppendChar(destcode.GetLength()); @@ -178,7 +178,7 @@ void CPDF_ToUnicodeMap::Load(CPDF_Stream* pStream) { continue; } if (len == 1) { - m_Map[code] = destcode.GetAt(0); + m_Map[code] = destcode[0]; } else { m_Map[code] = GetUnicode(); m_MultiCharBuf.AppendChar(destcode.GetLength()); diff --git a/core/fpdfapi/render/cpdf_charposlist.cpp b/core/fpdfapi/render/cpdf_charposlist.cpp index a87fc3334e..f5882d9944 100644 --- a/core/fpdfapi/render/cpdf_charposlist.cpp +++ b/core/fpdfapi/render/cpdf_charposlist.cpp @@ -38,7 +38,7 @@ void CPDF_CharPosList::Load(const std::vector& charCodes, if (pCIDFont) charpos.m_bFontStyle = true; CFX_WideString unicode = pFont->UnicodeFromCharCode(CharCode); - charpos.m_Unicode = !unicode.IsEmpty() ? unicode.GetAt(0) : CharCode; + charpos.m_Unicode = !unicode.IsEmpty() ? unicode[0] : CharCode; charpos.m_GlyphIndex = pFont->GlyphFromCharCode(CharCode, &bVert); uint32_t GlyphID = charpos.m_GlyphIndex; #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ diff --git a/core/fpdfdoc/cpdf_filespec.cpp b/core/fpdfdoc/cpdf_filespec.cpp index 5ef6f466c5..e34f54e4e1 100644 --- a/core/fpdfdoc/cpdf_filespec.cpp +++ b/core/fpdfdoc/cpdf_filespec.cpp @@ -69,13 +69,13 @@ CFX_WideString CPDF_FileSpec::DecodeFileName(const CFX_WideString& filepath) { return ChangeSlashToPlatform(filepath.c_str()); #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ - if (filepath.GetAt(0) != L'/') + if (filepath[0] != L'/') return ChangeSlashToPlatform(filepath.c_str()); - if (filepath.GetAt(1) == L'/') + if (filepath[1] == L'/') return ChangeSlashToPlatform(filepath.c_str() + 1); - if (filepath.GetAt(2) == L'/') { + if (filepath[2] == L'/') { CFX_WideString result; - result += filepath.GetAt(1); + result += filepath[1]; result += L':'; result += ChangeSlashToPlatform(filepath.c_str() + 2); return result; @@ -158,19 +158,19 @@ CFX_WideString CPDF_FileSpec::EncodeFileName(const CFX_WideString& filepath) { return CFX_WideString(); #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ - if (filepath.GetAt(1) == L':') { + if (filepath[1] == L':') { CFX_WideString result(L'/'); - result += filepath.GetAt(0); - if (filepath.GetAt(2) != L'\\') + result += filepath[0]; + if (filepath[2] != L'\\') result += L'/'; result += ChangeSlashToPDF(filepath.c_str() + 2); return result; } - if (filepath.GetAt(0) == L'\\' && filepath.GetAt(1) == L'\\') + if (filepath[0] == L'\\' && filepath[1] == L'\\') return ChangeSlashToPDF(filepath.c_str() + 1); - if (filepath.GetAt(0) == L'\\') + if (filepath[0] == L'\\') return L'/' + ChangeSlashToPDF(filepath.c_str()); return ChangeSlashToPDF(filepath.c_str()); #elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ diff --git a/core/fpdfdoc/cpdf_variabletext.cpp b/core/fpdfdoc/cpdf_variabletext.cpp index 74a5e3adfc..877f8ca01e 100644 --- a/core/fpdfdoc/cpdf_variabletext.cpp +++ b/core/fpdfdoc/cpdf_variabletext.cpp @@ -358,11 +358,11 @@ CPVT_WordPlace CPDF_VariableText::InsertText(const CPVT_WordPlace& place, CPVT_WordPlace wp = place; for (int32_t i = 0, sz = swText.GetLength(); i < sz; i++) { CPVT_WordPlace oldwp = wp; - uint16_t word = swText.GetAt(i); + uint16_t word = swText[i]; switch (word) { case 0x0D: if (m_bMultiLine) { - if (swText.GetAt(i + 1) == 0x0A) + if (swText[i + 1] == 0x0A) i += 1; wp = InsertSection(wp, nullptr, nullptr); @@ -370,7 +370,7 @@ CPVT_WordPlace CPDF_VariableText::InsertText(const CPVT_WordPlace& place, break; case 0x0A: if (m_bMultiLine) { - if (swText.GetAt(i + 1) == 0x0D) + if (swText[i + 1] == 0x0D) i += 1; wp = InsertSection(wp, nullptr, nullptr); @@ -426,11 +426,11 @@ void CPDF_VariableText::SetText(const CFX_WideString& swText) { if (m_nCharArray > 0 && nCharCount >= m_nCharArray) break; - uint16_t word = swText.GetAt(i); + uint16_t word = swText[i]; switch (word) { case 0x0D: if (m_bMultiLine) { - if (i + 1 < sz && swText.GetAt(i + 1) == 0x0A) + if (i + 1 < sz && swText[i + 1] == 0x0A) i++; wp.AdvanceSection(); AddSection(wp, secinfo); @@ -438,7 +438,7 @@ void CPDF_VariableText::SetText(const CFX_WideString& swText) { break; case 0x0A: if (m_bMultiLine) { - if (i + 1 < sz && swText.GetAt(i + 1) == 0x0D) + if (i + 1 < sz && swText[i + 1] == 0x0D) i++; wp.AdvanceSection(); AddSection(wp, secinfo); diff --git a/core/fpdfdoc/cpvt_generateap.cpp b/core/fpdfdoc/cpvt_generateap.cpp index d1abde681d..9b5a82a055 100644 --- a/core/fpdfdoc/cpvt_generateap.cpp +++ b/core/fpdfdoc/cpvt_generateap.cpp @@ -123,7 +123,7 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, dsBorder = CPVT_Dash(pArray->GetIntegerAt(0), pArray->GetIntegerAt(1), pArray->GetIntegerAt(2)); } - switch (pBSDict->GetStringFor("S").GetAt(0)) { + switch (pBSDict->GetStringFor("S")[0]) { case 'S': nBorderStyle = BorderStyle::SOLID; break; diff --git a/core/fpdftext/cpdf_linkextract.cpp b/core/fpdftext/cpdf_linkextract.cpp index d795e71639..fb228ec832 100644 --- a/core/fpdftext/cpdf_linkextract.cpp +++ b/core/fpdftext/cpdf_linkextract.cpp @@ -152,7 +152,7 @@ void CPDF_LinkExtract::ParseLink() { if (strBeCheck.GetLength() > 5) { while (strBeCheck.GetLength() > 0) { - wchar_t ch = strBeCheck.GetAt(strBeCheck.GetLength() - 1); + wchar_t ch = strBeCheck[strBeCheck.GetLength() - 1]; if (ch == L')' || ch == L',' || ch == L'>' || ch == L'.') { strBeCheck = strBeCheck.Left(strBeCheck.GetLength() - 1); nCount--; @@ -242,7 +242,7 @@ bool CPDF_LinkExtract::CheckMailLink(CFX_WideString* str) { // Check the local part. int pPos = aPos; // Used to track the position of '@' or '.'. for (int i = aPos - 1; i >= 0; i--) { - wchar_t ch = str->GetAt(i); + wchar_t ch = (*str)[i]; if (ch == L'_' || ch == L'-' || FXSYS_iswalnum(ch)) continue; @@ -278,7 +278,7 @@ bool CPDF_LinkExtract::CheckMailLink(CFX_WideString* str) { int nLen = str->GetLength(); pPos = 0; // Used to track the position of '.'. for (int i = aPos + 1; i < nLen; i++) { - wchar_t wch = str->GetAt(i); + wchar_t wch = (*str)[i]; if (wch == L'-' || FXSYS_iswalnum(wch)) continue; diff --git a/core/fpdftext/cpdf_textpage.cpp b/core/fpdftext/cpdf_textpage.cpp index 1470ad700d..a4c8b8fba0 100644 --- a/core/fpdftext/cpdf_textpage.cpp +++ b/core/fpdftext/cpdf_textpage.cpp @@ -701,7 +701,7 @@ void CPDF_TextPage::CloseTempLine() { CFX_WideString str = m_TempTextBuf.MakeString(); bool bPrevSpace = false; for (int i = 0; i < str.GetLength(); i++) { - if (str.GetAt(i) != ' ') { + if (str[i] != ' ') { bPrevSpace = false; continue; } @@ -841,8 +841,7 @@ FPDFText_MarkedContent CPDF_TextPage::PreMarkedContent(PDFTEXT_Obj Obj) { CPDF_Font* pFont = pTextObj->GetFont(); bExist = false; for (FX_STRSIZE i = 0; i < nItems; i++) { - if (pFont->CharCodeFromUnicode(actText.GetAt(i)) != - CPDF_Font::kInvalidCharCode) { + if (pFont->CharCodeFromUnicode(actText[i]) != CPDF_Font::kInvalidCharCode) { bExist = true; break; } @@ -852,7 +851,7 @@ FPDFText_MarkedContent CPDF_TextPage::PreMarkedContent(PDFTEXT_Obj Obj) { bExist = false; for (FX_STRSIZE i = 0; i < nItems; i++) { - wchar_t wChar = actText.GetAt(i); + wchar_t wChar = actText[i]; if ((wChar > 0x80 && wChar < 0xFFFD) || (wChar <= 0x80 && isprint(wChar))) { bExist = true; break; @@ -889,7 +888,7 @@ void CPDF_TextPage::ProcessMarkedContent(PDFTEXT_Obj Obj) { matrix.Concat(Obj.m_formMatrix); for (FX_STRSIZE k = 0; k < nItems; k++) { - wchar_t wChar = actText.GetAt(k); + wchar_t wChar = actText[k]; if (wChar <= 0x80 && !isprint(wChar)) wChar = 0x20; if (wChar >= 0xFFFD) @@ -1005,12 +1004,12 @@ void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) { pTextObj->GetFont()->UnicodeFromCharCode(item.m_CharCode); if (wstrItem.IsEmpty()) wstrItem += (wchar_t)item.m_CharCode; - wchar_t curChar = wstrItem.GetAt(0); + wchar_t curChar = wstrItem[0]; if (curChar == 0x2D || curChar == 0xAD) return; } while (m_TempTextBuf.GetSize() > 0 && - m_TempTextBuf.AsStringC().GetAt(m_TempTextBuf.GetLength() - 1) == + m_TempTextBuf.AsStringC()[m_TempTextBuf.GetLength() - 1] == 0x20) { m_TempTextBuf.Delete(m_TempTextBuf.GetLength() - 1, 1); m_TempCharList.pop_back(); @@ -1053,7 +1052,7 @@ void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) { CFX_WideString str = m_TempTextBuf.MakeString(); if (str.IsEmpty()) str = m_TextBuf.AsStringC(); - if (str.IsEmpty() || str.GetAt(str.GetLength() - 1) == TEXT_SPACE_CHAR) + if (str.IsEmpty() || str[str.GetLength() - 1] == TEXT_SPACE_CHAR) continue; float fontsize_h = pTextObj->m_TextState.GetFontSizeH(); @@ -1164,7 +1163,7 @@ void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) { } if (!bDel) { for (int nIndex = 0; nIndex < nTotal; nIndex++) { - charinfo.m_Unicode = wstrItem.GetAt(nIndex); + charinfo.m_Unicode = wstrItem[nIndex]; if (charinfo.m_Unicode) { charinfo.m_Index = m_TextBuf.GetLength(); m_TempTextBuf.AppendChar(charinfo.m_Unicode); @@ -1175,8 +1174,7 @@ void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) { } } else if (i == 0) { CFX_WideString str = m_TempTextBuf.MakeString(); - if (!str.IsEmpty() && - str.GetAt(str.GetLength() - 1) == TEXT_SPACE_CHAR) { + if (!str.IsEmpty() && str[str.GetLength() - 1] == TEXT_SPACE_CHAR) { m_TempTextBuf.Delete(m_TempTextBuf.GetLength() - 1, 1); m_TempCharList.pop_back(); } @@ -1225,12 +1223,12 @@ bool CPDF_TextPage::IsHyphen(wchar_t curChar) { if (nCount < 1) return false; int nIndex = nCount - 1; - wchar_t wcTmp = strCurText.GetAt(nIndex); + wchar_t wcTmp = strCurText[nIndex]; while (wcTmp == 0x20 && nIndex > 0 && nIndex <= nCount - 1) - wcTmp = strCurText.GetAt(--nIndex); + wcTmp = strCurText[--nIndex]; if (0x2D == wcTmp || 0xAD == wcTmp) { if (--nIndex > 0) { - wchar_t preChar = strCurText.GetAt((nIndex)); + wchar_t preChar = strCurText[nIndex]; if (FXSYS_iswalpha(preChar) && FXSYS_iswalpha(curChar)) return true; } @@ -1268,7 +1266,7 @@ CPDF_TextPage::GenerateCharacter CPDF_TextPage::ProcessInsertObject( pObj->GetFont()->UnicodeFromCharCode(item.m_CharCode); if (wstrItem.IsEmpty()) wstrItem += static_cast(item.m_CharCode); - wchar_t curChar = wstrItem.GetAt(0); + wchar_t curChar = wstrItem[0]; if (WritingMode == TextOrientation::Horizontal) { if (this_rect.Height() > 4.5 && prev_rect.Height() > 4.5) { float top = this_rect.top < prev_rect.top ? this_rect.top : prev_rect.top; @@ -1357,7 +1355,7 @@ CPDF_TextPage::GenerateCharacter CPDF_TextPage::ProcessInsertObject( m_pPreTextObj->GetFont()->UnicodeFromCharCode(PrevItem.m_CharCode); if (PrevStr.IsEmpty()) return GenerateCharacter::None; - wchar_t preChar = PrevStr.GetAt(PrevStr.GetLength() - 1); + wchar_t preChar = PrevStr[PrevStr.GetLength() - 1]; CFX_Matrix matrix = pObj->GetTextMatrix(); matrix.Concat(formMatrix); diff --git a/core/fpdftext/cpdf_textpagefind.cpp b/core/fpdftext/cpdf_textpagefind.cpp index 3c8e532a7f..55f940cad7 100644 --- a/core/fpdftext/cpdf_textpagefind.cpp +++ b/core/fpdftext/cpdf_textpagefind.cpp @@ -110,7 +110,7 @@ bool CPDF_TextPageFind::FindFirst(const CFX_WideString& findwhat, m_csFindWhatArray.clear(); int i = 0; while (i < len) { - if (findwhatStr.GetAt(i) != ' ') + if (findwhatStr[i] != ' ') break; i++; } @@ -150,7 +150,7 @@ bool CPDF_TextPageFind::FindNext() { CFX_WideString csWord = m_csFindWhatArray[iWord]; if (csWord.IsEmpty()) { if (iWord == nCount - 1) { - wchar_t strInsert = m_strText.GetAt(nStartPos); + wchar_t strInsert = m_strText[nStartPos]; if (strInsert == TEXT_LINEFEED_CHAR || strInsert == TEXT_SPACE_CHAR || strInsert == TEXT_RETURN_CHAR || strInsert == 160) { nResultPos = nStartPos + 1; @@ -174,16 +174,16 @@ bool CPDF_TextPageFind::FindNext() { bool bMatch = true; if (iWord != 0 && !bSpaceStart) { int PreResEndPos = nStartPos; - int curChar = csWord.GetAt(0); + int curChar = csWord[0]; CFX_WideString lastWord = m_csFindWhatArray[iWord - 1]; - int lastChar = lastWord.GetAt(lastWord.GetLength() - 1); + int lastChar = lastWord[lastWord.GetLength() - 1]; if (nStartPos == nResultPos && !(IsIgnoreSpaceCharacter(lastChar) || IsIgnoreSpaceCharacter(curChar))) { bMatch = false; } for (int d = PreResEndPos; d < nResultPos; d++) { - wchar_t strInsert = m_strText.GetAt(d); + wchar_t strInsert = m_strText[d]; if (strInsert != TEXT_LINEFEED_CHAR && strInsert != TEXT_SPACE_CHAR && strInsert != TEXT_RETURN_CHAR && strInsert != 160) { bMatch = false; @@ -192,7 +192,7 @@ bool CPDF_TextPageFind::FindNext() { } } else if (bSpaceStart) { if (nResultPos > 0) { - wchar_t strInsert = m_strText.GetAt(nResultPos - 1); + wchar_t strInsert = m_strText[nResultPos - 1]; if (strInsert != TEXT_LINEFEED_CHAR && strInsert != TEXT_SPACE_CHAR && strInsert != TEXT_RETURN_CHAR && strInsert != 160) { bMatch = false; @@ -293,7 +293,7 @@ void CPDF_TextPageFind::ExtractFindWhat(const CFX_WideString& findwhat) { int pos = 0; while (pos < csWord.GetLength()) { CFX_WideString curStr = csWord.Mid(pos, 1); - wchar_t curChar = csWord.GetAt(pos); + wchar_t curChar = csWord[pos]; if (IsIgnoreSpaceCharacter(curChar)) { if (pos > 0 && curChar == 0x2019) { pos++; @@ -326,12 +326,12 @@ bool CPDF_TextPageFind::IsMatchWholeWord(const CFX_WideString& csPageText, int char_count = endPos - startPos + 1; if (char_count < 1) return false; - if (char_count == 1 && csPageText.GetAt(startPos) > 255) + if (char_count == 1 && csPageText[startPos] > 255) return true; if (startPos - 1 >= 0) - char_left = csPageText.GetAt(startPos - 1); + char_left = csPageText[startPos - 1]; if (startPos + char_count < csPageText.GetLength()) - char_right = csPageText.GetAt(startPos + char_count); + char_right = csPageText[startPos + char_count]; if ((char_left > 'A' && char_left < 'a') || (char_left > 'a' && char_left < 'z') || (char_left > 0xfb00 && char_left < 0xfb06) || std::iswdigit(char_left) || @@ -348,9 +348,9 @@ bool CPDF_TextPageFind::IsMatchWholeWord(const CFX_WideString& csPageText, return false; } if (char_count > 0) { - if (std::iswdigit(char_left) && std::iswdigit(csPageText.GetAt(startPos))) + if (std::iswdigit(char_left) && std::iswdigit(csPageText[startPos])) return false; - if (std::iswdigit(char_right) && std::iswdigit(csPageText.GetAt(endPos))) + if (std::iswdigit(char_right) && std::iswdigit(csPageText[endPos])) return false; } return true; @@ -386,7 +386,7 @@ CFX_WideString CPDF_TextPageFind::MakeReverse(const CFX_WideString& str) { str2.clear(); int nlen = str.GetLength(); for (int i = nlen - 1; i >= 0; i--) - str2 += str.GetAt(i); + str2 += str[i]; return str2; } diff --git a/core/fxcrt/cfx_bytestring.cpp b/core/fxcrt/cfx_bytestring.cpp index 0aba3be12a..cd049dcc5d 100644 --- a/core/fxcrt/cfx_bytestring.cpp +++ b/core/fxcrt/cfx_bytestring.cpp @@ -518,6 +518,12 @@ void CFX_ByteString::Format(const char* pFormat, ...) { va_end(argList); } +void CFX_ByteString::SetAt(FX_STRSIZE index, char c) { + ASSERT(index >= 0 && index < GetLength()); + ReallocBeforeWrite(m_pData->m_nDataLength); + m_pData->m_String[index] = c; +} + FX_STRSIZE CFX_ByteString::Insert(FX_STRSIZE index, char ch) { const FX_STRSIZE cur_length = m_pData ? m_pData->m_nDataLength : 0; if (index != pdfium::clamp(index, 0, cur_length)) @@ -694,12 +700,6 @@ FX_STRSIZE CFX_ByteString::Replace(const CFX_ByteStringC& pOld, return nCount; } -void CFX_ByteString::SetAt(FX_STRSIZE index, char c) { - ASSERT(index >= 0 && index < GetLength()); - ReallocBeforeWrite(m_pData->m_nDataLength); - m_pData->m_String[index] = c; -} - CFX_WideString CFX_ByteString::UTF8Decode() const { CFX_UTF8Decoder decoder; for (FX_STRSIZE i = 0; i < GetLength(); i++) { @@ -721,10 +721,10 @@ int CFX_ByteString::Compare(const CFX_ByteStringC& str) const { int that_len = str.GetLength(); int min_len = this_len < that_len ? this_len : that_len; for (int i = 0; i < min_len; i++) { - if ((uint8_t)m_pData->m_String[i] < str.GetAt(i)) { + if ((uint8_t)m_pData->m_String[i] < str[i]) { return -1; } - if ((uint8_t)m_pData->m_String[i] > str.GetAt(i)) { + if ((uint8_t)m_pData->m_String[i] > str[i]) { return 1; } } diff --git a/core/fxcrt/cfx_bytestring.h b/core/fxcrt/cfx_bytestring.h index df31751273..3e8b6619c2 100644 --- a/core/fxcrt/cfx_bytestring.h +++ b/core/fxcrt/cfx_bytestring.h @@ -107,14 +107,13 @@ class CFX_ByteString { const CFX_ByteString& operator+=(const CFX_ByteString& str); const CFX_ByteString& operator+=(const CFX_ByteStringC& bstrc); - uint8_t GetAt(FX_STRSIZE index) const { + const CharType& operator[](const FX_STRSIZE index) const { ASSERT(index >= 0 && index < GetLength()); return m_pData->m_String[index]; } - uint8_t operator[](FX_STRSIZE index) const { return GetAt(index); } - void SetAt(FX_STRSIZE index, char c); + FX_STRSIZE Insert(FX_STRSIZE index, char ch); FX_STRSIZE InsertAtFront(char ch) { return Insert(0, ch); } FX_STRSIZE InsertAtBack(char ch) { return Insert(GetLength(), ch); } diff --git a/core/fxcrt/cfx_bytestring_unittest.cpp b/core/fxcrt/cfx_bytestring_unittest.cpp index 680a37e342..35f407f6ec 100644 --- a/core/fxcrt/cfx_bytestring_unittest.cpp +++ b/core/fxcrt/cfx_bytestring_unittest.cpp @@ -11,26 +11,8 @@ #include "testing/gtest/include/gtest/gtest.h" #include "third_party/base/stl_util.h" -TEST(fxcrt, ByteStringGetAt) { - CFX_ByteString short_string("a"); - CFX_ByteString longer_string("abc"); - CFX_ByteString embedded_nul_string("ab\0c", 4); - -#ifndef NDEBUG - EXPECT_DEATH({ short_string.GetAt(-1); }, ".*"); -#endif - EXPECT_EQ('a', short_string.GetAt(0)); -#ifndef NDEBUG - EXPECT_DEATH({ short_string.GetAt(1); }, ".*"); -#endif - EXPECT_EQ('c', longer_string.GetAt(2)); - EXPECT_EQ('b', embedded_nul_string.GetAt(1)); - EXPECT_EQ('\0', embedded_nul_string.GetAt(2)); - EXPECT_EQ('c', embedded_nul_string.GetAt(3)); -} - -TEST(fxcrt, ByteStringOperatorSubscript) { - CFX_ByteString abc("abc"); +TEST(fxcrt, ByteStringElementAccess) { + const CFX_ByteString abc("abc"); #ifndef NDEBUG EXPECT_DEATH({ abc[-1]; }, ".*"); #endif @@ -40,22 +22,35 @@ TEST(fxcrt, ByteStringOperatorSubscript) { #ifndef NDEBUG EXPECT_DEATH({ abc[3]; }, ".*"); #endif -} -TEST(fxcrt, ByteStringSetAt) { - // CFX_ByteString includes the NUL terminator for non-empty strings. - CFX_ByteString abc("abc"); + CFX_ByteString mutable_abc = abc; + EXPECT_EQ(abc.c_str(), mutable_abc.c_str()); + EXPECT_EQ('a', mutable_abc[0]); + EXPECT_EQ('b', mutable_abc[1]); + EXPECT_EQ('c', mutable_abc[2]); + EXPECT_EQ(abc.c_str(), mutable_abc.c_str()); + #ifndef NDEBUG - EXPECT_DEATH({ abc.SetAt(-1, 'd'); }, ".*"); + EXPECT_DEATH({ mutable_abc.SetAt(-1, 'd'); }, ".*"); + EXPECT_EQ("abc", abc); #endif - abc.SetAt(0, 'd'); - EXPECT_EQ("dbc", abc); - abc.SetAt(1, 'e'); - EXPECT_EQ("dec", abc); - abc.SetAt(2, 'f'); - EXPECT_EQ("def", abc); + const char* c_str = abc.c_str(); + mutable_abc.SetAt(0, 'd'); + EXPECT_EQ(c_str, abc.c_str()); + EXPECT_NE(c_str, mutable_abc.c_str()); + EXPECT_EQ("abc", abc); + EXPECT_EQ("dbc", mutable_abc); + + mutable_abc.SetAt(1, 'e'); + EXPECT_EQ("abc", abc); + EXPECT_EQ("dec", mutable_abc); + + mutable_abc.SetAt(2, 'f'); + EXPECT_EQ("abc", abc); + EXPECT_EQ("def", mutable_abc); #ifndef NDEBUG - EXPECT_DEATH({ abc.SetAt(3, 'g'); }, ".*"); + EXPECT_DEATH({ mutable_abc.SetAt(3, 'g'); }, ".*"); + EXPECT_EQ("abc", abc); #endif } @@ -943,25 +938,7 @@ TEST(fxcrt, ByteStringCMid) { EXPECT_EQ(trailing_substring, longer_string.Mid(4, 3)); } -TEST(fxcrt, ByteStringCGetAt) { - CFX_ByteStringC short_string("a"); - CFX_ByteStringC longer_string("abc"); - CFX_ByteStringC embedded_nul_string("ab\0c", 4); - -#ifndef NDEBUG - EXPECT_DEATH({ short_string.GetAt(-1); }, ".*"); -#endif - EXPECT_EQ('a', static_cast(short_string.GetAt(0))); -#ifndef NDEBUG - EXPECT_DEATH({ short_string.GetAt(1); }, ".*"); -#endif - EXPECT_EQ('c', static_cast(longer_string.GetAt(2))); - EXPECT_EQ('b', static_cast(embedded_nul_string.GetAt(1))); - EXPECT_EQ('\0', static_cast(embedded_nul_string.GetAt(2))); - EXPECT_EQ('c', static_cast(embedded_nul_string.GetAt(3))); -} - -TEST(fxcrt, ByteStringCOperatorSubscript) { +TEST(fxcrt, ByteStringCElementAccess) { // CFX_ByteStringC includes the NUL terminator for non-empty strings. CFX_ByteStringC abc("abc"); #ifndef NDEBUG diff --git a/core/fxcrt/cfx_chariter.cpp b/core/fxcrt/cfx_chariter.cpp index b726a7e484..d26dd2df8f 100644 --- a/core/fxcrt/cfx_chariter.cpp +++ b/core/fxcrt/cfx_chariter.cpp @@ -29,7 +29,7 @@ bool CFX_CharIter::Next(bool bPrev) { } wchar_t CFX_CharIter::GetChar() { - return m_wsText.GetAt(m_nIndex); + return m_wsText[m_nIndex]; } void CFX_CharIter::SetAt(int32_t nIndex) { diff --git a/core/fxcrt/cfx_string_c_template.h b/core/fxcrt/cfx_string_c_template.h index bc0fe1e0a0..99948e90a0 100644 --- a/core/fxcrt/cfx_string_c_template.h +++ b/core/fxcrt/cfx_string_c_template.h @@ -119,12 +119,12 @@ class CFX_StringCTemplate { FX_STRSIZE GetLength() const { return m_Length; } bool IsEmpty() const { return m_Length == 0; } - UnsignedType GetAt(FX_STRSIZE index) const { + const UnsignedType& operator[](const FX_STRSIZE index) const { ASSERT(index >= 0 && index < GetLength()); return m_Ptr.Get()[index]; } - CharType CharAt(FX_STRSIZE index) const { + const CharType CharAt(const FX_STRSIZE index) const { ASSERT(index >= 0 && index < GetLength()); return static_cast(m_Ptr.Get()[index]); } @@ -164,8 +164,6 @@ class CFX_StringCTemplate { return CFX_StringCTemplate(m_Ptr.Get() + m_Length - count, count); } - UnsignedType operator[](FX_STRSIZE index) const { return GetAt(index); } - bool operator<(const CFX_StringCTemplate& that) const { int result = FXSYS_cmp(reinterpret_cast(m_Ptr.Get()), reinterpret_cast(that.m_Ptr.Get()), diff --git a/core/fxcrt/cfx_widestring.cpp b/core/fxcrt/cfx_widestring.cpp index c2e1e4877d..01fc8c764e 100644 --- a/core/fxcrt/cfx_widestring.cpp +++ b/core/fxcrt/cfx_widestring.cpp @@ -841,12 +841,6 @@ FX_STRSIZE CFX_WideString::Replace(const CFX_WideStringC& pOld, return nCount; } -void CFX_WideString::SetAt(FX_STRSIZE index, wchar_t c) { - ASSERT(index >= 0 && index < GetLength()); - ReallocBeforeWrite(m_pData->m_nDataLength); - m_pData->m_String[index] = c; -} - // static CFX_WideString CFX_WideString::FromLocal(const CFX_ByteStringC& str) { return FromCodePage(str, 0); @@ -886,6 +880,12 @@ CFX_WideString CFX_WideString::FromUTF16LE(const unsigned short* wstr, return result; } +void CFX_WideString::SetAt(FX_STRSIZE index, wchar_t c) { + ASSERT(index >= 0 && index < GetLength()); + ReallocBeforeWrite(m_pData->m_nDataLength); + m_pData->m_String[index] = c; +} + int CFX_WideString::Compare(const wchar_t* lpsz) const { if (m_pData) return wcscmp(m_pData->m_String, lpsz); diff --git a/core/fxcrt/cfx_widestring.h b/core/fxcrt/cfx_widestring.h index b49e898f32..d57e47c62f 100644 --- a/core/fxcrt/cfx_widestring.h +++ b/core/fxcrt/cfx_widestring.h @@ -101,13 +101,11 @@ class CFX_WideString { bool operator<(const CFX_WideString& str) const; - wchar_t GetAt(FX_STRSIZE index) const { + const CharType& operator[](const FX_STRSIZE index) const { ASSERT(index >= 0 && index < GetLength()); return m_pData->m_String[index]; } - wchar_t operator[](FX_STRSIZE index) const { return GetAt(index); } - void SetAt(FX_STRSIZE index, wchar_t c); int Compare(const wchar_t* str) const; diff --git a/core/fxcrt/cfx_widestring_unittest.cpp b/core/fxcrt/cfx_widestring_unittest.cpp index a23763218d..e7206dde06 100644 --- a/core/fxcrt/cfx_widestring_unittest.cpp +++ b/core/fxcrt/cfx_widestring_unittest.cpp @@ -10,26 +10,8 @@ #include "testing/gtest/include/gtest/gtest.h" -TEST(fxcrt, WideStringGetAt) { - CFX_WideString short_string(L"a"); - CFX_WideString longer_string(L"abc"); - CFX_WideString embedded_nul_string(L"ab\0c", 4); - -#ifndef NDEBUG - EXPECT_DEATH({ short_string.GetAt(-1); }, ".*"); -#endif - EXPECT_EQ(L'a', short_string.GetAt(0)); -#ifndef NDEBUG - EXPECT_DEATH({ short_string.GetAt(1); }, ".*"); -#endif - EXPECT_EQ(L'c', longer_string.GetAt(2)); - EXPECT_EQ(L'b', embedded_nul_string.GetAt(1)); - EXPECT_EQ(L'\0', embedded_nul_string.GetAt(2)); - EXPECT_EQ(L'c', embedded_nul_string.GetAt(3)); -} - -TEST(fxcrt, WideStringOperatorSubscript) { - CFX_WideString abc(L"abc"); +TEST(fxcrt, WideStringElementAccess) { + const CFX_WideString abc(L"abc"); #ifndef NDEBUG EXPECT_DEATH({ abc[-1]; }, ".*"); #endif @@ -39,21 +21,34 @@ TEST(fxcrt, WideStringOperatorSubscript) { #ifndef NDEBUG EXPECT_DEATH({ abc[4]; }, ".*"); #endif -} -TEST(fxcrt, WideStringSetAt) { - CFX_WideString abc(L"abc"); + CFX_WideString mutable_abc = abc; + EXPECT_EQ(abc.c_str(), mutable_abc.c_str()); + EXPECT_EQ(L'a', mutable_abc[0]); + EXPECT_EQ(L'b', mutable_abc[1]); + EXPECT_EQ(L'c', mutable_abc[2]); + EXPECT_EQ(abc.c_str(), mutable_abc.c_str()); #ifndef NDEBUG - EXPECT_DEATH({ abc.SetAt(-1, L'd'); }, ".*"); + EXPECT_DEATH({ mutable_abc.SetAt(-1, L'd'); }, ".*"); + EXPECT_EQ(L"abc", abc); #endif - abc.SetAt(0, L'd'); - EXPECT_EQ(L"dbc", abc); - abc.SetAt(1, L'e'); - EXPECT_EQ(L"dec", abc); - abc.SetAt(2, L'f'); - EXPECT_EQ(L"def", abc); + const wchar_t* c_str = abc.c_str(); + mutable_abc.SetAt(0, L'd'); + EXPECT_EQ(c_str, abc.c_str()); + EXPECT_NE(c_str, mutable_abc.c_str()); + EXPECT_EQ(L"abc", abc); + EXPECT_EQ(L"dbc", mutable_abc); + + mutable_abc.SetAt(1, L'e'); + EXPECT_EQ(L"abc", abc); + EXPECT_EQ(L"dec", mutable_abc); + + mutable_abc.SetAt(2, L'f'); + EXPECT_EQ(L"abc", abc); + EXPECT_EQ(L"def", mutable_abc); #ifndef NDEBUG - EXPECT_DEATH({ abc.SetAt(3, L'g'); }, ".*"); + EXPECT_DEATH({ mutable_abc.SetAt(3, L'g'); }, ".*"); + EXPECT_EQ(L"abc", abc); #endif } @@ -780,25 +775,7 @@ TEST(fxcrt, WideStringCFromVector) { EXPECT_EQ(nullptr, cleared_string.raw_str()); } -TEST(fxcrt, WideStringCGetAt) { - CFX_WideStringC short_string(L"a"); - CFX_WideStringC longer_string(L"abc"); - CFX_WideStringC embedded_nul_string(L"ab\0c", 4); - -#ifndef NDEBUG - EXPECT_DEATH({ short_string.GetAt(-1); }, ".*"); -#endif - EXPECT_EQ(L'a', static_cast(short_string.GetAt(0))); -#ifndef NDEBUG - EXPECT_DEATH({ short_string.GetAt(1); }, ".*"); -#endif - EXPECT_EQ(L'c', static_cast(longer_string.GetAt(2))); - EXPECT_EQ(L'b', static_cast(embedded_nul_string.GetAt(1))); - EXPECT_EQ(L'\0', static_cast(embedded_nul_string.GetAt(2))); - EXPECT_EQ(L'c', static_cast(embedded_nul_string.GetAt(3))); -} - -TEST(fxcrt, WideStringCOperatorSubscript) { +TEST(fxcrt, WideStringCElementAccess) { CFX_WideStringC abc(L"abc"); #ifndef NDEBUG EXPECT_DEATH({ abc[-1]; }, ".*"); diff --git a/core/fxcrt/xml/cxml_parser.cpp b/core/fxcrt/xml/cxml_parser.cpp index 4d9e3dc469..ebd5873b33 100644 --- a/core/fxcrt/xml/cxml_parser.cpp +++ b/core/fxcrt/xml/cxml_parser.cpp @@ -171,7 +171,7 @@ void CXML_Parser::SkipLiterals(const CFX_ByteStringC& str) { int32_t i = 0, iLen = str.GetLength(); do { while (m_dwIndex < m_dwBufferSize) { - if (str.GetAt(i) != m_pBuffer[m_dwIndex++]) { + if (str[i] != m_pBuffer[m_dwIndex++]) { i = 0; continue; } -- cgit v1.2.3