]> granicus.if.org Git - icu/commitdiff
ICU-13645 Clean up implementation of Locale::operator=(const Locale&).
authorFredrik Roubert <fredrik@roubert.name>
Sat, 15 Sep 2018 00:17:29 +0000 (17:17 -0700)
committerShane Carr <shane@unicode.org>
Thu, 27 Sep 2018 21:27:39 +0000 (14:27 -0700)
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=().

icu4c/source/common/locid.cpp

index d47f12edd51a68551882863ed8618083144022fe..3d9eacedac7e956e6d1f42b130791906931f5f3f 100644 (file)
@@ -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;
 }