string-util: move version_is_valid() into generic code

While we are at it, replace the sloppy use of filename_is_valid() by the
less sloppy filename_part_is_valid() (as added by the preceeding
commit), since we don#t want to be too restrictive here. (After all,
version strings invalid as standalone filenames might be valid as part
of filenames, and hence we should allow them).
This commit is contained in:
Lennart Poettering
2023-06-20 19:02:31 +02:00
parent ec926463c0
commit f5c6b4f4d9
8 changed files with 27 additions and 23 deletions
+15
View File
@@ -16,6 +16,7 @@
#include "macro.h"
#include "memory-util.h"
#include "memstream-util.h"
#include "path-util.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
@@ -1296,3 +1297,17 @@ char *startswith_strv(const char *string, char **strv) {
return found;
}
bool version_is_valid(const char *s) {
if (isempty(s))
return false;
if (!filename_part_is_valid(s))
return false;
/* This is a superset of the characters used by semver. We additionally allow "," and "_". */
if (!in_charset(s, ALPHANUMERICAL ".,_-+"))
return false;
return true;
}