diff --git a/libretro-common/playlists/label_sanitization.c b/libretro-common/playlists/label_sanitization.c index 81c04b6457..50a95ee513 100644 --- a/libretro-common/playlists/label_sanitization.c +++ b/libretro-common/playlists/label_sanitization.c @@ -81,13 +81,23 @@ void label_sanitize(char *label, bool (*left)(char*), bool (*right)(char*)) if ((*left)(&label[lindex])) copy = false; else - new_label[rindex++] = label[lindex]; + { + const bool whitespace = label[lindex] == ' ' && (rindex == 0 || new_label[rindex - 1] == ' '); + + /* Simplify consecutive whitespaces */ + if (!whitespace) + new_label[rindex++] = label[lindex]; + } } else if ((*right)(&label[lindex])) copy = true; } - new_label[rindex] = '\0'; + /* Trim trailing whitespace */ + if (rindex > 0 && new_label[rindex - 1] == ' ') + new_label[rindex - 1] = '\0'; + else + new_label[rindex] = '\0'; strlcpy(label, new_label, PATH_MAX_LENGTH); }