From 8cb245739c76a1766473174500275d5d8b215d98 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 25 Jan 2019 18:15:23 -0500 Subject: Add QPDFObjectHandle::getUniqueResourceName --- libqpdf/QPDFObjectHandle.cc | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'libqpdf/QPDFObjectHandle.cc') diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index b802a55c..4e77bbaa 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -931,6 +931,30 @@ QPDFObjectHandle::getResourceNames() return result; } +std::string +QPDFObjectHandle::getUniqueResourceName(std::string const& prefix, + int& min_suffix) +{ + std::set names = getResourceNames(); + int max_suffix = min_suffix + names.size(); + while (min_suffix <= max_suffix) + { + std::string candidate = prefix + QUtil::int_to_string(min_suffix); + if (names.count(candidate) == 0) + { + return candidate; + } + // Increment after return; min_suffix should be the value + // used, not the next value. + ++min_suffix; + } + // This could only happen if there is a coding error. + // The number of candidates we test is more than the + // number of keys we're checking against. + throw std::logic_error("unable to find unconflicting name in" + " QPDFObjectHandle::getUniqueResourceName"); +} + // Indirect object accessors QPDF* QPDFObjectHandle::getOwningQPDF() -- cgit v1.2.3-54-g00ecf