mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
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:
parent
3b5eab6e25
commit
ec809ba4ed
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user