From: Peter Krempa Date: Wed, 3 Mar 2021 09:52:51 +0000 (+0100) Subject: virIndexToDiskName: Use g_string_prepend(_c) to improve readability X-Git-Tag: v7.2.0-rc1~215 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ec809ba4ed2040abd69859fd45f183565769bf14;p=thirdparty%2Flibvirt.git virIndexToDiskName: Use g_string_prepend(_c) to improve readability Use a dynamic string helper so that we don't have to calculate the string lengths and then iterate from the rear. Signed-off-by: Peter Krempa Reviewed-by: Michal Privoznik --- diff --git a/src/util/virutil.c b/src/util/virutil.c index 4c98e3f4bc..227997c7be 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -432,24 +432,15 @@ int virDiskNameToIndex(const char *name) char *virIndexToDiskName(unsigned int idx, const char *prefix) { - char *name = NULL; - size_t i; - int ctr; - int offset; - - for (i = 0, ctr = idx; ctr >= 0; ++i, ctr = ctr / 26 - 1) { } - - offset = strlen(prefix); - - name = g_new0(char, offset + i + 1); + GString *str = g_string_new(NULL); + long long ctr; - strcpy(name, prefix); - name[offset + i] = '\0'; + for (ctr = idx; ctr >= 0; ctr = ctr / 26 - 1) + g_string_prepend_c(str, 'a' + (ctr % 26)); - for (i = i - 1, ctr = idx; ctr >= 0; --i, ctr = ctr / 26 - 1) - name[offset + i] = 'a' + (ctr % 26); + g_string_prepend(str, prefix); - return name; + return g_string_free(str, false); } #ifndef AI_CANONIDN