From 44b4d050d4a2c966ed6f4aef2e523f70b6463648 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 2 Jun 2011 10:47:18 -0700 Subject: [PATCH] util: New function for forming English lists. This follows the rules I learned in school. Some locales may prefer to omit the comma before "and" in a list of three or more items. --- lib/util.c | 11 +++++++++++ lib/util.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/lib/util.c b/lib/util.c index c05c582fe..1a42376e2 100644 --- a/lib/util.c +++ b/lib/util.c @@ -596,3 +596,14 @@ abs_file_name(const char *dir, const char *file_name) * its return value. (Note that every scalar type can be implicitly * converted to bool.) */ void ignore(bool x OVS_UNUSED) { } + +/* Returns an appropriate delimiter for inserting just before the 0-based item + * 'index' in a list that has 'total' items in it. */ +const char * +english_list_delimiter(size_t index, size_t total) +{ + return (index == 0 ? "" + : index < total - 1 ? ", " + : total > 2 ? ", and " + : " and "); +} diff --git a/lib/util.h b/lib/util.h index 2be7a7099..7615288d8 100644 --- a/lib/util.h +++ b/lib/util.h @@ -186,6 +186,8 @@ bool str_to_double(const char *, double *); int hexit_value(int c); unsigned int hexits_value(const char *s, size_t n, bool *ok); +const char *english_list_delimiter(size_t index, size_t total); + char *get_cwd(void); char *dir_name(const char *file_name); char *base_name(const char *file_name); -- 2.43.0