From da129ab38c3fb6ed3de85ffb6f8938eb31130a53 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Tue, 1 Aug 2017 15:16:59 -0400 Subject: Replace raw value for constant error value in string operations Currently Find() and other methods that return a FX_STRSIZE return -1 to indicate error/failure. This means that there is a lot of magic numbers and magic checks floating around. The standard library for similar operations uses a npos constant. This CL implements FX_STRNPOS, and replaces usages of magic number checking. It also does some type cleanup along the way where it was obvious that FX_STRSIZE should be being used. Removing the magic numbers should make eventually changing FX_STRSIZE to be unsigned easier in the future. BUG=pdfium:828 Change-Id: I67e481e44cf2f75a1698afa8fbee4f375a74c490 Reviewed-on: https://pdfium-review.googlesource.com/9651 Commit-Queue: Ryan Harrison Reviewed-by: Tom Sepez --- core/fxge/android/cfpf_skiafontmgr.cpp | 4 ++-- core/fxge/apple/fx_mac_imp.cpp | 4 ++-- core/fxge/cfx_folderfontinfo.cpp | 8 ++++---- core/fxge/cfx_font.cpp | 2 +- core/fxge/cfx_fontmapper.cpp | 6 +++--- core/fxge/cfx_renderdevice.cpp | 2 +- core/fxge/fx_ge_linux.cpp | 15 ++++++++------- core/fxge/win32/fx_win32_device.cpp | 33 +++++++++++++++++++-------------- 8 files changed, 40 insertions(+), 34 deletions(-) (limited to 'core/fxge') diff --git a/core/fxge/android/cfpf_skiafontmgr.cpp b/core/fxge/android/cfpf_skiafontmgr.cpp index 23fcd5bbec..3cee6decac 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") > -1; + return bsName.Find("symbol") != FX_STRNPOS; } bool FPF_SkiaMaybeArabic(const CFX_ByteStringC& bsFacename) { CFX_ByteString bsName(bsFacename); bsName.MakeLower(); - return bsName.Find("arabic") > -1; + return bsName.Find("arabic") != FX_STRNPOS; } const uint32_t g_FPFSkiaFontCharsets[] = { diff --git a/core/fxge/apple/fx_mac_imp.cpp b/core/fxge/apple/fx_mac_imp.cpp index 78fe16459c..54aac7bfa6 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") >= 0) { + if (face->Find("Gothic") != FX_STRNPOS) { *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") == -1 && face.Find("Italic") == -1) { + if (face.Find("Bold") == FX_STRNPOS && face.Find("Italic") == FX_STRNPOS) { 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 12e7dd25fc..f16722c4d6 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") > -1) + if (style.Find("Bold") != FX_STRNPOS) pInfo->m_Styles |= FXFONT_BOLD; - if (style.Find("Italic") > -1 || style.Find("Oblique") > -1) + if (style.Find("Italic") != FX_STRNPOS || style.Find("Oblique") != FX_STRNPOS) pInfo->m_Styles |= FXFONT_ITALIC; - if (facename.Find("Serif") > -1) + if (facename.Find("Serif") != FX_STRNPOS) pInfo->m_Styles |= FXFONT_SERIF; m_FontList[facename] = std::move(pInfo); @@ -289,7 +289,7 @@ void* CFX_FolderFontInfo::FindFont(int weight, continue; int32_t index = bsName.Find(family); - if (bMatchName && index < 0) + if (bMatchName && index == FX_STRNPOS) continue; int32_t iSimilarValue = diff --git a/core/fxge/cfx_font.cpp b/core/fxge/cfx_font.cpp index a8e271dd9a..1fd08f2676 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") != -1; + return str.Find("italic") != FX_STRNPOS; } bool CFX_Font::IsBold() const { diff --git a/core/fxge/cfx_fontmapper.cpp b/core/fxge/cfx_fontmapper.cpp index 4c0073ab73..b8ffe047a3 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) != -1) + if (str_key.Find(family->m_pFontName) != FX_STRNPOS) return 0; return FXSYS_stricmp(reinterpret_cast(key), family->m_pFontName); } @@ -187,8 +187,8 @@ CFX_ByteString TT_NormalizeName(const char* family) { norm.Remove(' '); norm.Remove('-'); norm.Remove(','); - int pos = norm.Find('+'); - if (pos > 0) + FX_STRSIZE pos = norm.Find('+'); + if (pos != 0 && pos != FX_STRNPOS) norm = norm.Left(pos); norm.MakeLower(); return norm; diff --git a/core/fxge/cfx_renderdevice.cpp b/core/fxge/cfx_renderdevice.cpp index 279e720d1e..310a16512c 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") != -1) + if (bsPsName.Find("+ZJHL") != FX_STRNPOS) return false; if (bsPsName == "CNAAJI+cmex10") diff --git a/core/fxge/fx_ge_linux.cpp b/core/fxge/fx_ge_linux.cpp index d9fac3f54c..0552f1c58c 100644 --- a/core/fxge/fx_ge_linux.cpp +++ b/core/fxge/fx_ge_linux.cpp @@ -45,17 +45,18 @@ size_t GetJapanesePreference(const char* facearr, int weight, int pitch_family) { CFX_ByteString face = facearr; - if (face.Find("Gothic") >= 0 || - face.Find("\x83\x53\x83\x56\x83\x62\x83\x4e") >= 0) { - if (face.Find("PGothic") >= 0 || - face.Find("\x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e") >= 0) { + 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) { return 0; } return 1; } - if (face.Find("Mincho") >= 0 || face.Find("\x96\xbe\x92\xa9") >= 0) { - if (face.Find("PMincho") >= 0 || - face.Find("\x82\x6f\x96\xbe\x92\xa9") >= 0) { + 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) { return 2; } return 3; diff --git a/core/fxge/win32/fx_win32_device.cpp b/core/fxge/win32/fx_win32_device.cpp index 4427755cf5..b707a76c08 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") >= 0 || face.Find("\xbf\xac") >= 0) { + if (face.Find("KaiTi") != FX_STRNPOS || face.Find("\xbf\xac") != FX_STRNPOS) { if (m_KaiTi.IsEmpty()) { m_KaiTi = FindFont("KaiTi"); if (m_KaiTi.IsEmpty()) { @@ -511,7 +511,8 @@ void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face, } } face = m_KaiTi; - } else if (face.Find("FangSong") >= 0 || face.Find("\xb7\xc2\xcb\xce") >= 0) { + } else if (face.Find("FangSong") != FX_STRNPOS || + face.Find("\xb7\xc2\xcb\xce") != FX_STRNPOS) { if (m_FangSong.IsEmpty()) { m_FangSong = FindFont("FangSong"); if (m_FangSong.IsEmpty()) { @@ -519,9 +520,11 @@ void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face, } } face = m_FangSong; - } else if (face.Find("SimSun") >= 0 || face.Find("\xcb\xce") >= 0) { + } else if (face.Find("SimSun") != FX_STRNPOS || + face.Find("\xcb\xce") != FX_STRNPOS) { face = "SimSun"; - } else if (face.Find("SimHei") >= 0 || face.Find("\xba\xda") >= 0) { + } else if (face.Find("SimHei") != FX_STRNPOS || + face.Find("\xba\xda") != FX_STRNPOS) { face = "SimHei"; } else if (!(picth_family & FF_ROMAN) && weight > 550) { face = "SimHei"; @@ -533,15 +536,16 @@ void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face, void CFX_Win32FontInfo::GetJapanesePreference(CFX_ByteString& face, int weight, int picth_family) { - if (face.Find("Gothic") >= 0 || - face.Find("\x83\x53\x83\x56\x83\x62\x83\x4e") >= 0) { - if (face.Find("PGothic") >= 0 || - face.Find("\x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e") >= 0) { + 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) { face = "MS PGothic"; - } else if (face.Find("UI Gothic") >= 0) { + } else if (face.Find("UI Gothic") != FX_STRNPOS) { face = "MS UI Gothic"; } else { - if (face.Find("HGSGothicM") >= 0 || face.Find("HGMaruGothicMPRO") >= 0) { + if (face.Find("HGSGothicM") != FX_STRNPOS || + face.Find("HGMaruGothicMPRO") != FX_STRNPOS) { face = "MS PGothic"; } else { face = "MS Gothic"; @@ -549,9 +553,10 @@ void CFX_Win32FontInfo::GetJapanesePreference(CFX_ByteString& face, } return; } - if (face.Find("Mincho") >= 0 || face.Find("\x96\xbe\x92\xa9") >= 0) { - if (face.Find("PMincho") >= 0 || - face.Find("\x82\x6f\x96\xbe\x92\xa9") >= 0) { + 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) { face = "MS PMincho"; } else { face = "MS Mincho"; @@ -635,7 +640,7 @@ void* CFX_Win32FontInfo::MapFont(int weight, face = "Gulim"; break; case FX_CHARSET_ChineseTraditional: - if (face.Find("MSung") >= 0) { + if (face.Find("MSung") != FX_STRNPOS) { face = "MingLiU"; } else { face = "PMingLiU"; -- cgit v1.2.3