From: Dale Weiler Date: Wed, 14 Aug 2013 04:24:06 +0000 (+0000) Subject: Cleanups X-Git-Tag: v0.3.0~29 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=a68f0fcb355db42acabe72da5939fbd1b04f6016;p=xonotic%2Fgmqcc.git Cleanups --- diff --git a/util.c b/util.c index 69f805c..ecae505 100644 --- a/util.c +++ b/util.c @@ -225,28 +225,31 @@ uint16_t util_crc16(const char *k, int len, const short clamp) { } #endif -size_t util_strtocmd(const char *in, char *out, size_t outsz) { +/* + * modifier is the match to make and the transpsition from it, while add is the upper-value that determines the + * transposion from uppercase to lower case. + */ +static GMQCC_INLINE size_t util_strtransform(const char *in, char *out, size_t outsz, const char *mod, int add) { size_t sz = 1; - for (; *in && sz < outsz; ++in, ++out, ++sz) - *out = (*in == '-') ? '_' : (util_isalpha(*in) && !util_isupper(*in)) ? *in + 'A' - 'a': *in; + for (; *in && sz < outsz; ++in, ++out, ++sz) { + *out = (*in == mod[0]) + ? mod[1] + : (util_isalpha(*in) && util_isupper(*in + add)) + ? *in + add + : *in; + } *out = 0; return sz-1; } +size_t util_strtocmd(const char *in, char *out, size_t outsz) { + return util_strtransform(in, out, outsz, "-_", 'A'-'a'); +} size_t util_strtononcmd(const char *in, char *out, size_t outsz) { - size_t sz = 1; - for (; *in && sz < outsz; ++in, ++out, ++sz) - *out = (*in == '_') ? '-' : (util_isalpha(*in) && util_isupper(*in)) ? *in + 'a' - 'A' : *in; - *out = 0; - return sz-1; + return util_strtransform(in, out, outsz, "_-", 'a'-'A'); } - size_t util_optimizationtostr(const char *in, char *out, size_t outsz) { - size_t sz = 1; - for (; *in && sz < outsz; ++in, ++out, ++sz) - *out = (*in == '_') ? ' ' : (util_isalpha(*in) && util_isupper(*in)) ? *in + 'a' - 'A' : *in; - *out = 0; - return sz-1; + return util_strtransform(in, out, outsz, "_ ", 'a'-'A'); } /*