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 <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2021-03-03 10:52:51 +01:00
parent 3b5eab6e25
commit ec809ba4ed

View File

@ -432,24 +432,15 @@ int virDiskNameToIndex(const char *name)
char *virIndexToDiskName(unsigned int idx, const char *prefix) char *virIndexToDiskName(unsigned int idx, const char *prefix)
{ {
char *name = NULL; GString *str = g_string_new(NULL);
size_t i; long long ctr;
int ctr;
int offset;
for (i = 0, ctr = idx; ctr >= 0; ++i, ctr = ctr / 26 - 1) { } for (ctr = idx; ctr >= 0; ctr = ctr / 26 - 1)
g_string_prepend_c(str, 'a' + (ctr % 26));
offset = strlen(prefix); g_string_prepend(str, prefix);
name = g_new0(char, offset + i + 1); return g_string_free(str, false);
strcpy(name, prefix);
name[offset + i] = '\0';
for (i = i - 1, ctr = idx; ctr >= 0; --i, ctr = ctr / 26 - 1)
name[offset + i] = 'a' + (ctr % 26);
return name;
} }
#ifndef AI_CANONIDN #ifndef AI_CANONIDN