From 8eeee77cbe0a2eb5729697767a3c503fc5570005 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Fri, 5 Jan 2018 13:39:22 -0500 Subject: Convert CPDF_PageLabel::GetLabel to return Optional Change-Id: I53b91aa89c0fd1e7ab766f6d3c27a0fc7573c360 Reviewed-on: https://pdfium-review.googlesource.com/22290 Commit-Queue: Ryan Harrison Reviewed-by: Henrique Nakashima --- core/fpdfdoc/cpdf_pagelabel.cpp | 27 ++++++++++++++------------- core/fpdfdoc/cpdf_pagelabel.h | 3 ++- fpdfsdk/fpdfdoc.cpp | 8 ++++---- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/core/fpdfdoc/cpdf_pagelabel.cpp b/core/fpdfdoc/cpdf_pagelabel.cpp index d0693086ab..f06e4018bb 100644 --- a/core/fpdfdoc/cpdf_pagelabel.cpp +++ b/core/fpdfdoc/cpdf_pagelabel.cpp @@ -78,20 +78,20 @@ CPDF_PageLabel::CPDF_PageLabel(CPDF_Document* pDocument) CPDF_PageLabel::~CPDF_PageLabel() {} -bool CPDF_PageLabel::GetLabel(int nPage, WideString* wsLabel) const { +Optional CPDF_PageLabel::GetLabel(int nPage) const { if (!m_pDocument) - return false; + return {}; if (nPage < 0 || nPage >= m_pDocument->GetPageCount()) - return false; + return {}; const CPDF_Dictionary* pPDFRoot = m_pDocument->GetRoot(); if (!pPDFRoot) - return false; + return {}; CPDF_Dictionary* pLabels = pPDFRoot->GetDictFor("PageLabels"); if (!pLabels) - return false; + return {}; CPDF_NumberTree numberTree(pLabels); CPDF_Object* pValue = nullptr; @@ -103,21 +103,22 @@ bool CPDF_PageLabel::GetLabel(int nPage, WideString* wsLabel) const { n--; } + WideString label; if (pValue) { pValue = pValue->GetDirect(); if (CPDF_Dictionary* pLabel = pValue->AsDictionary()) { if (pLabel->KeyExist("P")) - *wsLabel += pLabel->GetUnicodeTextFor("P"); + label += pLabel->GetUnicodeTextFor("P"); ByteString bsNumberingStyle = pLabel->GetStringFor("S", ""); int nLabelNum = nPage - n + pLabel->GetIntegerFor("St", 1); WideString wsNumPortion = GetLabelNumPortion(nLabelNum, bsNumberingStyle); - *wsLabel += wsNumPortion; - return true; + label += wsNumPortion; + return {label}; } } - *wsLabel = WideString::Format(L"%d", nPage + 1); - return true; + label = WideString::Format(L"%d", nPage + 1); + return {label}; } int32_t CPDF_PageLabel::GetPageByLabel(const ByteStringView& bsLabel) const { @@ -130,10 +131,10 @@ int32_t CPDF_PageLabel::GetPageByLabel(const ByteStringView& bsLabel) const { int nPages = m_pDocument->GetPageCount(); for (int i = 0; i < nPages; i++) { - WideString str; - if (!GetLabel(i, &str)) + Optional str = GetLabel(i); + if (!str.has_value()) continue; - if (PDF_EncodeText(str).Compare(bsLabel)) + if (PDF_EncodeText(str.value()).Compare(bsLabel)) return i; } diff --git a/core/fpdfdoc/cpdf_pagelabel.h b/core/fpdfdoc/cpdf_pagelabel.h index 8a7a33d16a..4570e97b7a 100644 --- a/core/fpdfdoc/cpdf_pagelabel.h +++ b/core/fpdfdoc/cpdf_pagelabel.h @@ -8,6 +8,7 @@ #define CORE_FPDFDOC_CPDF_PAGELABEL_H_ #include "core/fxcrt/fx_string.h" +#include "third_party/base/optional.h" class CPDF_Document; @@ -16,7 +17,7 @@ class CPDF_PageLabel { explicit CPDF_PageLabel(CPDF_Document* pDocument); ~CPDF_PageLabel(); - bool GetLabel(int nPage, WideString* wsLabel) const; + Optional GetLabel(int nPage) const; int32_t GetPageByLabel(const ByteStringView& bsLabel) const; int32_t GetPageByLabel(const WideStringView& wsLabel) const; diff --git a/fpdfsdk/fpdfdoc.cpp b/fpdfsdk/fpdfdoc.cpp index 51a1c61e77..14e4361483 100644 --- a/fpdfsdk/fpdfdoc.cpp +++ b/fpdfsdk/fpdfdoc.cpp @@ -424,8 +424,8 @@ FPDF_GetPageLabel(FPDF_DOCUMENT document, // CPDF_PageLabel can deal with NULL |document|. CPDF_PageLabel label(CPDFDocumentFromFPDFDocument(document)); - WideString str; - if (!label.GetLabel(page_index, &str)) - return 0; - return Utf16EncodeMaybeCopyAndReturnLength(str, buffer, buflen); + Optional str = label.GetLabel(page_index); + return str.has_value() + ? Utf16EncodeMaybeCopyAndReturnLength(str.value(), buffer, buflen) + : 0; } -- cgit v1.2.3