From 12db7515f17228798d1aa38fce0fee3e7d2d36b6 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Wed, 23 Aug 2017 10:39:35 -0400 Subject: Convert string Find methods to return an Optional The Find and ReverseFind methods for WideString, WideStringC, ByteString, and ByteStringC have been converted from returning a raw FX_STRSIZE, to returning Optional, so that success/failure can be indicated without using FX_STRNPOS. This allows for removing FX_STRNPOS and by association makes the conversion of FX_STRSIZE to size_t easier, since it forces checking the return value of Find to be explictly done as well as taking the error value out of the range of FX_STRSIZE. New Contains methods have been added for cases where the success or failure is all the call site to Find cared about, and the actual position was ignored. BUG=pdfium:828 Change-Id: Id827e508c8660affa68cc08a13d96121369364b7 Reviewed-on: https://pdfium-review.googlesource.com/11350 Commit-Queue: Ryan Harrison Reviewed-by: dsinclair --- core/fxge/android/cfpf_skiafontmgr.cpp | 4 +-- core/fxge/apple/fx_mac_imp.cpp | 4 +-- core/fxge/cfx_folderfontinfo.cpp | 9 +++---- core/fxge/cfx_font.cpp | 2 +- core/fxge/cfx_fontmapper.cpp | 47 ++++++++++++++++++---------------- core/fxge/cfx_renderdevice.cpp | 2 +- core/fxge/fx_ge_linux.cpp | 14 +++++----- core/fxge/win32/fx_win32_device.cpp | 32 ++++++++++------------- 8 files changed, 54 insertions(+), 60 deletions(-) (limited to 'core/fxge') diff --git a/core/fxge/android/cfpf_skiafontmgr.cpp b/core/fxge/android/cfpf_skiafontmgr.cpp index 3cee6decac..16a6df5f8f 100644 --- a/core/fxge/android/cfpf_skiafontmgr.cpp +++ b/core/fxge/android/cfpf_skiafontmgr.cpp @@ -194,13 +194,13 @@ bool FPF_SkiaIsCJK(uint8_t uCharset) { bool FPF_SkiaMaybeSymbol(const CFX_ByteStringC& bsFacename) { CFX_ByteString bsName(bsFacename); bsName.MakeLower(); - return bsName.Find("symbol") != FX_STRNPOS; + return bsName.Contains("symbol"); } bool FPF_SkiaMaybeArabic(const CFX_ByteStringC& bsFacename) { CFX_ByteString bsName(bsFacename); bsName.MakeLower(); - return bsName.Find("arabic") != FX_STRNPOS; + return bsName.Contains("arabic"); } const uint32_t g_FPFSkiaFontCharsets[] = { diff --git a/core/fxge/apple/fx_mac_imp.cpp b/core/fxge/apple/fx_mac_imp.cpp index 54aac7bfa6..2eee8e2e62 100644 --- a/core/fxge/apple/fx_mac_imp.cpp +++ b/core/fxge/apple/fx_mac_imp.cpp @@ -52,7 +52,7 @@ const char JAPAN_GOTHIC[] = "Hiragino Kaku Gothic Pro W6"; const char JAPAN_MINCHO[] = "Hiragino Mincho Pro W6"; void GetJapanesePreference(CFX_ByteString* face, int weight, int pitch_family) { - if (face->Find("Gothic") != FX_STRNPOS) { + if (face->Contains("Gothic")) { *face = JAPAN_GOTHIC; return; } @@ -82,7 +82,7 @@ void* CFX_MacFontInfo::MapFont(int weight, // Times New Roman. A more sophisticated approach would be to find all the // fonts in |m_FontList| with |face| in the name, and examine the fonts to // see which best matches the requested characteristics. - if (face.Find("Bold") == FX_STRNPOS && face.Find("Italic") == FX_STRNPOS) { + if (!face.Contains("Bold") && !face.Contains("Italic")) { CFX_ByteString new_face = face; if (weight > 400) new_face += " Bold"; diff --git a/core/fxge/cfx_folderfontinfo.cpp b/core/fxge/cfx_folderfontinfo.cpp index f16722c4d6..c82d55982a 100644 --- a/core/fxge/cfx_folderfontinfo.cpp +++ b/core/fxge/cfx_folderfontinfo.cpp @@ -251,11 +251,11 @@ void CFX_FolderFontInfo::ReportFace(const CFX_ByteString& path, m_pMapper->AddInstalledFont(facename, FX_CHARSET_ANSI); pInfo->m_Charsets |= CHARSET_FLAG_ANSI; pInfo->m_Styles = 0; - if (style.Find("Bold") != FX_STRNPOS) + if (style.Contains("Bold")) pInfo->m_Styles |= FXFONT_BOLD; - if (style.Find("Italic") != FX_STRNPOS || style.Find("Oblique") != FX_STRNPOS) + if (style.Contains("Italic") || style.Contains("Oblique")) pInfo->m_Styles |= FXFONT_ITALIC; - if (facename.Find("Serif") != FX_STRNPOS) + if (facename.Contains("Serif")) pInfo->m_Styles |= FXFONT_SERIF; m_FontList[facename] = std::move(pInfo); @@ -288,8 +288,7 @@ void* CFX_FolderFontInfo::FindFont(int weight, if (!(pFont->m_Charsets & charset_flag) && charset != FX_CHARSET_Default) continue; - int32_t index = bsName.Find(family); - if (bMatchName && index == FX_STRNPOS) + if (bMatchName && !bsName.Contains(family)) continue; int32_t iSimilarValue = diff --git a/core/fxge/cfx_font.cpp b/core/fxge/cfx_font.cpp index 1fd08f2676..8b34819e05 100644 --- a/core/fxge/cfx_font.cpp +++ b/core/fxge/cfx_font.cpp @@ -447,7 +447,7 @@ bool CFX_Font::IsItalic() const { CFX_ByteString str(FXFT_Get_Face_Style_Name(m_Face)); str.MakeLower(); - return str.Find("italic") != FX_STRNPOS; + return str.Contains("italic"); } bool CFX_Font::IsBold() const { diff --git a/core/fxge/cfx_fontmapper.cpp b/core/fxge/cfx_fontmapper.cpp index 005c61ae8a..bff9fad5c3 100644 --- a/core/fxge/cfx_fontmapper.cpp +++ b/core/fxge/cfx_fontmapper.cpp @@ -172,7 +172,7 @@ const struct CODEPAGE_MAP { int CompareFontFamilyString(const void* key, const void* element) { CFX_ByteString str_key((const char*)key); const AltFontFamily* family = reinterpret_cast(element); - if (str_key.Find(family->m_pFontName) != FX_STRNPOS) + if (str_key.Contains(family->m_pFontName)) return 0; return FXSYS_stricmp(reinterpret_cast(key), family->m_pFontName); } @@ -187,9 +187,9 @@ CFX_ByteString TT_NormalizeName(const char* family) { norm.Remove(' '); norm.Remove('-'); norm.Remove(','); - FX_STRSIZE pos = norm.Find('+'); - if (pos != 0 && pos != FX_STRNPOS) - norm = norm.Left(pos); + auto pos = norm.Find('+'); + if (pos.has_value() && pos.value() != 0) + norm = norm.Left(pos.value()); norm.MakeLower(); return norm; } @@ -208,14 +208,14 @@ uint8_t GetCharsetFromCodePage(uint16_t codepage) { } CFX_ByteString GetFontFamily(CFX_ByteString fontName, int nStyle) { - if (fontName.Find("Script") >= 0) { + if (fontName.Contains("Script")) { if ((nStyle & FX_FONT_STYLE_Bold) == FX_FONT_STYLE_Bold) fontName = "ScriptMTBold"; - else if (fontName.Find("Palace") >= 0) + else if (fontName.Contains("Palace")) fontName = "PalaceScriptMT"; - else if (fontName.Find("French") >= 0) + else if (fontName.Contains("French")) fontName = "FrenchScriptMT"; - else if (fontName.Find("FreeStyle") >= 0) + else if (fontName.Contains("FreeStyle")) fontName = "FreeStyleScript"; return fontName; } @@ -449,11 +449,11 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name, CFX_ByteString style; bool bHasComma = false; bool bHasHyphen = false; - int find = SubstName.Find(",", 0); - if (find >= 0) { - family = SubstName.Left(find); + auto pos = SubstName.Find(",", 0); + if (pos.has_value()) { + family = SubstName.Left(pos.value()); PDF_GetStandardFontName(&family); - style = SubstName.Right(SubstName.GetLength() - (find + 1)); + style = SubstName.Right(SubstName.GetLength() - (pos.value() + 1)); bHasComma = true; } else { family = SubstName; @@ -478,10 +478,10 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name, } else { iBaseFont = kNumStandardFonts; if (!bHasComma) { - find = family.ReverseFind('-'); - if (find >= 0) { - style = family.Right(family.GetLength() - (find + 1)); - family = family.Left(find); + pos = family.ReverseFind('-'); + if (pos.has_value()) { + style = family.Right(family.GetLength() - (pos.value() + 1)); + family = family.Left(pos.value()); bHasHyphen = true; } } @@ -580,15 +580,18 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name, weight = old_weight; } #if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ - if (SubstName.Find("Narrow") > 0 || SubstName.Find("Condensed") > 0) - family = "LiberationSansNarrow"; + const char* narrow_family = "LiberationSansNarrow"; #elif _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_ - if (family.Find("Narrow") > 0 || family.Find("Condensed") > 0) - family = "RobotoCondensed"; + const char* narrow_family = "RobotoCondensed"; #else - if (family.Find("Narrow") > 0 || family.Find("Condensed") > 0) - family = "ArialNarrow"; + const char* narrow_family = "ArialNarrow"; #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ + auto pos = SubstName.Find("Narrow"); + if (pos.has_value() && pos.value() != 0) + family = narrow_family; + pos = SubstName.Find("Condensed"); + if (pos.has_value() && pos.value() != 0) + family = narrow_family; } else { pSubstFont->m_bSubstCJK = true; if (nStyle) diff --git a/core/fxge/cfx_renderdevice.cpp b/core/fxge/cfx_renderdevice.cpp index e088bc4f9a..d0c52e8ade 100644 --- a/core/fxge/cfx_renderdevice.cpp +++ b/core/fxge/cfx_renderdevice.cpp @@ -337,7 +337,7 @@ bool ShouldDrawDeviceText(const CFX_Font* pFont, uint32_t text_flags) { return false; const CFX_ByteString bsPsName = pFont->GetPsName(); - if (bsPsName.Find("+ZJHL") != FX_STRNPOS) + if (bsPsName.Contains("+ZJHL")) return false; if (bsPsName == "CNAAJI+cmex10") diff --git a/core/fxge/fx_ge_linux.cpp b/core/fxge/fx_ge_linux.cpp index 0552f1c58c..d17ddf2d3d 100644 --- a/core/fxge/fx_ge_linux.cpp +++ b/core/fxge/fx_ge_linux.cpp @@ -45,18 +45,16 @@ size_t GetJapanesePreference(const char* facearr, int weight, int pitch_family) { CFX_ByteString face = facearr; - if (face.Find("Gothic") != FX_STRNPOS || - face.Find("\x83\x53\x83\x56\x83\x62\x83\x4e") != FX_STRNPOS) { - if (face.Find("PGothic") != FX_STRNPOS || - face.Find("\x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e") != FX_STRNPOS) { + if (face.Contains("Gothic") || + face.Contains("\x83\x53\x83\x56\x83\x62\x83\x4e")) { + if (face.Contains("PGothic") || + face.Contains("\x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e")) { return 0; } return 1; } - if (face.Find("Mincho") != FX_STRNPOS || - face.Find("\x96\xbe\x92\xa9") != FX_STRNPOS) { - if (face.Find("PMincho") != FX_STRNPOS || - face.Find("\x82\x6f\x96\xbe\x92\xa9") != FX_STRNPOS) { + if (face.Contains("Mincho") || face.Contains("\x96\xbe\x92\xa9")) { + if (face.Contains("PMincho") || face.Contains("\x82\x6f\x96\xbe\x92\xa9")) { return 2; } return 3; diff --git a/core/fxge/win32/fx_win32_device.cpp b/core/fxge/win32/fx_win32_device.cpp index fd1944a1b4..9c8da591c6 100644 --- a/core/fxge/win32/fx_win32_device.cpp +++ b/core/fxge/win32/fx_win32_device.cpp @@ -503,7 +503,7 @@ void* CFX_Win32FallbackFontInfo::MapFont(int weight, void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face, int weight, int picth_family) { - if (face.Find("KaiTi") != FX_STRNPOS || face.Find("\xbf\xac") != FX_STRNPOS) { + if (face.Contains("KaiTi") || face.Contains("\xbf\xac")) { if (m_KaiTi.IsEmpty()) { m_KaiTi = FindFont("KaiTi"); if (m_KaiTi.IsEmpty()) { @@ -511,8 +511,7 @@ void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face, } } face = m_KaiTi; - } else if (face.Find("FangSong") != FX_STRNPOS || - face.Find("\xb7\xc2\xcb\xce") != FX_STRNPOS) { + } else if (face.Contains("FangSong") || face.Contains("\xb7\xc2\xcb\xce")) { if (m_FangSong.IsEmpty()) { m_FangSong = FindFont("FangSong"); if (m_FangSong.IsEmpty()) { @@ -520,11 +519,9 @@ void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face, } } face = m_FangSong; - } else if (face.Find("SimSun") != FX_STRNPOS || - face.Find("\xcb\xce") != FX_STRNPOS) { + } else if (face.Contains("SimSun") || face.Contains("\xcb\xce")) { face = "SimSun"; - } else if (face.Find("SimHei") != FX_STRNPOS || - face.Find("\xba\xda") != FX_STRNPOS) { + } else if (face.Contains("SimHei") || face.Contains("\xba\xda")) { face = "SimHei"; } else if (!(picth_family & FF_ROMAN) && weight > 550) { face = "SimHei"; @@ -536,16 +533,15 @@ void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face, void CFX_Win32FontInfo::GetJapanesePreference(CFX_ByteString& face, int weight, int picth_family) { - if (face.Find("Gothic") != FX_STRNPOS || - face.Find("\x83\x53\x83\x56\x83\x62\x83\x4e") != FX_STRNPOS) { - if (face.Find("PGothic") != FX_STRNPOS || - face.Find("\x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e") != FX_STRNPOS) { + if (face.Contains("Gothic") || + face.Contains("\x83\x53\x83\x56\x83\x62\x83\x4e")) { + if (face.Contains("PGothic") || + face.Contains("\x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e")) { face = "MS PGothic"; - } else if (face.Find("UI Gothic") != FX_STRNPOS) { + } else if (face.Contains("UI Gothic")) { face = "MS UI Gothic"; } else { - if (face.Find("HGSGothicM") != FX_STRNPOS || - face.Find("HGMaruGothicMPRO") != FX_STRNPOS) { + if (face.Contains("HGSGothicM") || face.Contains("HGMaruGothicMPRO")) { face = "MS PGothic"; } else { face = "MS Gothic"; @@ -553,10 +549,8 @@ void CFX_Win32FontInfo::GetJapanesePreference(CFX_ByteString& face, } return; } - if (face.Find("Mincho") != FX_STRNPOS || - face.Find("\x96\xbe\x92\xa9") != FX_STRNPOS) { - if (face.Find("PMincho") != FX_STRNPOS || - face.Find("\x82\x6f\x96\xbe\x92\xa9") != FX_STRNPOS) { + if (face.Contains("Mincho") || face.Contains("\x96\xbe\x92\xa9")) { + if (face.Contains("PMincho") || face.Contains("\x82\x6f\x96\xbe\x92\xa9")) { face = "MS PMincho"; } else { face = "MS Mincho"; @@ -640,7 +634,7 @@ void* CFX_Win32FontInfo::MapFont(int weight, face = "Gulim"; break; case FX_CHARSET_ChineseTraditional: - if (face.Find("MSung") != FX_STRNPOS) { + if (face.Contains("MSung")) { face = "MingLiU"; } else { face = "PMingLiU"; -- cgit v1.2.3