From: Fredrik Roubert Date: Sat, 15 Sep 2018 00:17:29 +0000 (-0700) Subject: ICU-13645 Clean up implementation of Locale::operator=(const Locale&). X-Git-Tag: release-63-rc~65 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e10ce23db1e81714bb9739f640d5f1d4d0be9c81;p=icu ICU-13645 Clean up implementation of Locale::operator=(const Locale&). Organizing the implementation like this instead will (hopefully) make it more clear what's being done and make it possible to use analogous control flow in the copy and move implementations of operator=(). --- diff --git a/icu4c/source/common/locid.cpp b/icu4c/source/common/locid.cpp index d47f12edd51..3d9eacedac7 100644 --- a/icu4c/source/common/locid.cpp +++ b/icu4c/source/common/locid.cpp @@ -427,56 +427,36 @@ Locale::Locale(const Locale &other) *this = other; } -Locale &Locale::operator=(const Locale &other) -{ +Locale& Locale::operator=(const Locale& other) { if (this == &other) { return *this; } - /* Free our current storage */ - if (baseName != fullName) { - uprv_free(baseName); - } - baseName = NULL; - if(fullName != fullNameBuffer) { - uprv_free(fullName); - fullName = fullNameBuffer; - } + setToBogus(); - /* Allocate the full name if necessary */ - if(other.fullName != other.fullNameBuffer) { - fullName = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(other.fullName)+1)); - if (fullName == NULL) { - // if memory allocation fails, set this object to bogus. - fIsBogus = TRUE; - return *this; - } + if (other.fullName == other.fullNameBuffer) { + uprv_strcpy(fullNameBuffer, other.fullNameBuffer); + } else if (other.fullName == nullptr) { + fullName = nullptr; + } else { + fullName = uprv_strdup(other.fullName); + if (fullName == nullptr) return *this; } - /* Copy the full name */ - uprv_strcpy(fullName, other.fullName); - /* Copy the baseName if it differs from fullName. */ if (other.baseName == other.fullName) { baseName = fullName; - } else { - if (other.baseName) { - baseName = uprv_strdup(other.baseName); - if (baseName == nullptr) { - // if memory allocation fails, set this object to bogus. - fIsBogus = TRUE; - return *this; - } - } + } else if (other.baseName != nullptr) { + baseName = uprv_strdup(other.baseName); + if (baseName == nullptr) return *this; } - /* Copy the language and country fields */ uprv_strcpy(language, other.language); uprv_strcpy(script, other.script); uprv_strcpy(country, other.country); - /* The variantBegin is an offset, just copy it */ variantBegin = other.variantBegin; fIsBogus = other.fIsBogus; + return *this; }