From 5746a0c90259cd39255749a33389b2854100905b Mon Sep 17 00:00:00 2001 From: mitchell Date: Wed, 17 Apr 2019 00:22:27 -0400 Subject: Backport: Move UniqueStringCopy into its own source file UniqueString.cxx to hide the implementation. Backport of changeset 7402:751b76b567f9, but with an alternative to C++17's string_view. --- src/UniqueString.cxx | 28 ++++++++++++++++++++++++++++ src/UniqueString.h | 10 +--------- 2 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 src/UniqueString.cxx (limited to 'src') diff --git a/src/UniqueString.cxx b/src/UniqueString.cxx new file mode 100644 index 000000000..dcbdc441c --- /dev/null +++ b/src/UniqueString.cxx @@ -0,0 +1,28 @@ +// Scintilla source code edit control +/** @file UniqueString.cxx + ** Define an allocator for UniqueString. + **/ +// Copyright 2017 by Neil Hodgson +// The License.txt file describes the conditions under which this software may be distributed. + +#include +#include +#include + +#include "UniqueString.h" + +namespace Scintilla { + +/// Equivalent to strdup but produces a std::unique_ptr allocation to go +/// into collections. +UniqueString UniqueStringCopy(const char *text) { + if (!text) { + return UniqueString(); + } + const size_t len = strlen(text); + std::unique_ptr upcNew(new char[len + 1]); + memcpy(&upcNew[0], text, len + 1); + return UniqueString(upcNew.release()); +} + +} diff --git a/src/UniqueString.h b/src/UniqueString.h index 8d95cb1ab..44ba26652 100644 --- a/src/UniqueString.h +++ b/src/UniqueString.h @@ -19,15 +19,7 @@ using UniqueString = std::unique_ptr; /// Equivalent to strdup but produces a std::unique_ptr allocation to go /// into collections. -inline UniqueString UniqueStringCopy(const char *text) { - if (!text) { - return UniqueString(); - } - const size_t len = strlen(text); - char *sNew = new char[len + 1]; - std::copy(text, text + len + 1, sNew); - return UniqueString(sNew); -} +UniqueString UniqueStringCopy(const char *text); } -- cgit v1.2.3