AmendHub

Download:

jcs

/

subtext

/

amendments

/

25

util: Simplify ordinal, just return suffix

It's probably always going to be printed with sprintf anyway, just let
the caller handle concatenating the two

jcs made amendment 25 over 2 years ago
--- util.c Fri Dec 10 16:43:36 2021 +++ util.c Sat Dec 11 10:40:22 2021 @@ -167,21 +167,25 @@ strlcpy(char *dst, const char *src, size_t dsize) return (src - osrc - 1); } -char * +const char * ordinal(unsigned short n) { - static char ordbuf[8]; - - switch (n) { - case 1: - return "1st"; - case 2: - return "2nd"; - case 3: - return "3rd"; + switch (n % 100) { + case 11: + case 12: + case 13: + return "th"; default: - sprintf(ordbuf, "%dth", n); - return ordbuf; + switch (n % 10) { + case 1: + return "st"; + case 2: + return "nd"; + case 3: + return "rd"; + default: + return "th"; + } } } --- util.h Fri Dec 10 16:43:59 2021 +++ util.h Sat Dec 11 10:40:31 2021 @@ -63,7 +63,7 @@ char *xstrdup(const char *); short getline(char *str, size_t len, char **ret); size_t strlcpy(char *dst, const char *src, size_t dsize); -char *ordinal(unsigned short n); +const char *ordinal(unsigned short n); #if 0 /* from strnatcmp.c */ int strnatcmp(char const *a, char const *b);