From cfe1f4d3f2d1087337b71a7fbfc9e9a87528bddf Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sat, 10 Feb 2024 12:02:18 -0500 Subject: [PATCH] Add `archFromString` function and simplity arch to string conversion --- include/semver.h | 1 + semver.c | 42 +++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/include/semver.h b/include/semver.h index 28adb68..cbf3a75 100644 --- a/include/semver.h +++ b/include/semver.h @@ -91,3 +91,4 @@ u128 u128FromVersion(Version *self); Comparison compareVersion(Version *self, Version *other); int parseVersion(Version *self, const char *s); char *versionToString(Version *self); + diff --git a/semver.c b/semver.c index bddcad8..d487848 100644 --- a/semver.c +++ b/semver.c @@ -3,6 +3,7 @@ #include #include #include +#include u128 u128FromVersion(Version *self) { u128 out = 0; @@ -78,24 +79,31 @@ Comparison compareVersion(Version *self, Version *other) { return CompNone; } -char *archToString(Arch self) { - switch (self) { - case any: return "any"; - case arm: return "arm"; - case arm64: return "aarch64"; - case loongson: return "loongson"; - case mips32: return "mips32"; - case mips64: return "mips64"; - case powerepc: return "ppc"; - case powerpc64: return "ppc64"; - case riscv64: return "riscv64"; - case s390x: return "s390x"; - case sparc: return "sparc"; - case sparc64: return "sparc64"; - case x86: return "x86"; - case x86_64: return "x86_64"; +const char *ArchNames[] = { "any", "arm", "aarch64", "loongson", "mips32", "mips64", + "ppc", "ppc64", "riscv64", "s390x", "sparc", "sparc64", "x86", "x86_64" }; + +const char *archToString(Arch self) { + return ArchNames[self]; +} + +int archFromString(char *s) { + int i; + for (i = 0; i < 14; i++) { + if (strncasecmp(s, ArchNames[i], 10) == 0) + return i; } - return NULL; + if (strncasecmp(s, "arm64", 5) == 0) + return arm64; + else if (strncasecmp(s, "i386", 4) == 0) + return x86; + else if (strncasecmp(s, "i486", 4) == 0) + return x86; + else if (strncasecmp(s, "i586", 4) == 0) + return x86; + else if (strncasecmp(s, "i686", 4) == 0) + return x86; + else + return -1; } char *versionToString(Version *self) {