aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QUtil.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-02-05 14:15:07 +0100
committerJay Berkenbilt <ejb@ql.org>2022-02-05 17:24:56 +0100
commit5f3f78822b5d43e9b02082da5268d186ba7101c0 (patch)
treec7d2407df01e4c622371ad290f9681a5ae99a476 /libqpdf/QUtil.cc
parent88c3d556d552a94aa42eaa2fa43667952e123dc8 (diff)
downloadqpdf-5f3f78822b5d43e9b02082da5268d186ba7101c0.tar.zst
Improve use of std::unique_ptr
* Use unique_ptr in place of shared_ptr in some cases * unique_ptr for arrays does not require a custom deleter * use std::make_unique (c++14) where possible
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r--libqpdf/QUtil.cc18
1 files changed, 13 insertions, 5 deletions
diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc
index e3666d02..503d79f9 100644
--- a/libqpdf/QUtil.cc
+++ b/libqpdf/QUtil.cc
@@ -744,6 +744,16 @@ QUtil::make_shared_cstr(std::string const& str)
return result;
}
+std::unique_ptr<char[]>
+QUtil::make_unique_cstr(std::string const& str)
+{
+ auto result = std::make_unique<char[]>(str.length() + 1);
+ // Use memcpy in case string contains nulls
+ result.get()[str.length()] = '\0';
+ memcpy(result.get(), str.c_str(), str.length());
+ return result;
+}
+
std::string
QUtil::hex_encode(std::string const& input)
{
@@ -2625,7 +2635,7 @@ call_main_from_wmain(bool, int argc, wchar_t const* const argv[],
// other systems. That way the rest of qpdf.cc can just act like
// arguments are UTF-8.
- std::vector<std::shared_ptr<char>> utf8_argv;
+ std::vector<std::unique_ptr<char[]>> utf8_argv;
for (int i = 0; i < argc; ++i)
{
std::string utf16;
@@ -2638,11 +2648,9 @@ call_main_from_wmain(bool, int argc, wchar_t const* const argv[],
QIntC::to_uchar(codepoint & 0xff)));
}
std::string utf8 = QUtil::utf16_to_utf8(utf16);
- utf8_argv.push_back(QUtil::make_shared_cstr(utf8));
+ utf8_argv.push_back(QUtil::make_unique_cstr(utf8));
}
- auto utf8_argv_sp =
- std::shared_ptr<char*>(
- new char*[1+utf8_argv.size()], std::default_delete<char*[]>());
+ auto utf8_argv_sp = std::make_unique<char*[]>(1+utf8_argv.size());
char** new_argv = utf8_argv_sp.get();
for (size_t i = 0; i < utf8_argv.size(); ++i)
{