]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virIndexToDiskName: Use g_string_prepend(_c) to improve readability
authorPeter Krempa <pkrempa@redhat.com>
Wed, 3 Mar 2021 09:52:51 +0000 (10:52 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 5 Mar 2021 14:33:34 +0000 (15:33 +0100)
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 <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/util/virutil.c

index 4c98e3f4bcb573130576d2aeccf34fab3436a5d5..227997c7be61d2d359e4694eb169c6f45c555ef1 100644 (file)
@@ -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