From 887005b68a689cc1d9f2b13e8e23dd4bfeaaf5f5 Mon Sep 17 00:00:00 2001 From: Fredrik Roubert Date: Thu, 21 Dec 2023 19:36:59 +0900 Subject: [PATCH] ICU-22520 Remove unnecessary copying of statically allocated strings. --- icu4c/source/common/locdspnm.cpp | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/icu4c/source/common/locdspnm.cpp b/icu4c/source/common/locdspnm.cpp index 73fe531c543..cc39de3b9cc 100644 --- a/icu4c/source/common/locdspnm.cpp +++ b/icu4c/source/common/locdspnm.cpp @@ -21,6 +21,7 @@ #include "cmemory.h" #include "cstring.h" #include "mutex.h" +#include "uassert.h" #include "ulocimp.h" #include "umutex.h" #include "ureslocs.h" @@ -64,12 +65,13 @@ U_NAMESPACE_BEGIN // Access resource data for locale components. // Wrap code in uloc.c for now. class ICUDataTable { - const char* path; + const char* const path; Locale locale; public: + // Note: path should be a pointer to a statically allocated string. ICUDataTable(const char* path, const Locale& locale); - ~ICUDataTable(); + ~ICUDataTable() = default; const Locale& getLocale(); @@ -95,23 +97,9 @@ ICUDataTable::getNoFallback(const char* tableKey, const char* itemKey, UnicodeSt } ICUDataTable::ICUDataTable(const char* path, const Locale& locale) - : path(nullptr), locale(Locale::getRoot()) + : path(path), locale(locale) { - if (path) { - int32_t len = static_cast(uprv_strlen(path)); - this->path = (const char*) uprv_malloc(len + 1); - if (this->path) { - uprv_strcpy((char *)this->path, path); - this->locale = locale; - } - } -} - -ICUDataTable::~ICUDataTable() { - if (path) { - uprv_free((void*) path); - path = nullptr; - } + U_ASSERT(path != nullptr); } const Locale&