xref: /linux/include/linux/string_choices.h (revision c121d5cc)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_STRING_CHOICES_H_
3 #define _LINUX_STRING_CHOICES_H_
4 
5 /*
6  * Here provide a series of helpers in the str_$TRUE_$FALSE format (you can
7  * also expand some helpers as needed), where $TRUE and $FALSE are their
8  * corresponding literal strings. These helpers can be used in the printing
9  * and also in other places where constant strings are required. Using these
10  * helpers offers the following benefits:
11  *  1) Reducing the hardcoding of strings, which makes the code more elegant
12  *     through these simple literal-meaning helpers.
13  *  2) Unifying the output, which prevents the same string from being printed
14  *     in various forms, such as enable/disable, enabled/disabled, en/dis.
15  *  3) Deduping by the linker, which results in a smaller binary file.
16  */
17 
18 #include <linux/types.h>
19 
str_enable_disable(bool v)20 static inline const char *str_enable_disable(bool v)
21 {
22 	return v ? "enable" : "disable";
23 }
24 #define str_disable_enable(v)		str_enable_disable(!(v))
25 
str_enabled_disabled(bool v)26 static inline const char *str_enabled_disabled(bool v)
27 {
28 	return v ? "enabled" : "disabled";
29 }
30 #define str_disabled_enabled(v)		str_enabled_disabled(!(v))
31 
str_hi_lo(bool v)32 static inline const char *str_hi_lo(bool v)
33 {
34 	return v ? "hi" : "lo";
35 }
36 #define str_lo_hi(v)		str_hi_lo(!(v))
37 
str_high_low(bool v)38 static inline const char *str_high_low(bool v)
39 {
40 	return v ? "high" : "low";
41 }
42 #define str_low_high(v)		str_high_low(!(v))
43 
str_read_write(bool v)44 static inline const char *str_read_write(bool v)
45 {
46 	return v ? "read" : "write";
47 }
48 #define str_write_read(v)		str_read_write(!(v))
49 
str_on_off(bool v)50 static inline const char *str_on_off(bool v)
51 {
52 	return v ? "on" : "off";
53 }
54 #define str_off_on(v)		str_on_off(!(v))
55 
str_yes_no(bool v)56 static inline const char *str_yes_no(bool v)
57 {
58 	return v ? "yes" : "no";
59 }
60 #define str_no_yes(v)		str_yes_no(!(v))
61 
str_up_down(bool v)62 static inline const char *str_up_down(bool v)
63 {
64 	return v ? "up" : "down";
65 }
66 #define str_down_up(v)		str_up_down(!(v))
67 
str_true_false(bool v)68 static inline const char *str_true_false(bool v)
69 {
70 	return v ? "true" : "false";
71 }
72 #define str_false_true(v)		str_true_false(!(v))
73 
74 /**
75  * str_plural - Return the simple pluralization based on English counts
76  * @num: Number used for deciding pluralization
77  *
78  * If @num is 1, returns empty string, otherwise returns "s".
79  */
str_plural(size_t num)80 static inline const char *str_plural(size_t num)
81 {
82 	return num == 1 ? "" : "s";
83 }
84 
85 #endif
86