xref: /wlan-dirver/qca-wifi-host-cmn/qdf/inc/qdf_str.h (revision 97f44cd39e4ff816eaa1710279d28cf6b9e65ad9)
1 /*
2  * Copyright (c) 2018-2020 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /**
20  * DOC: qdf_str
21  * QCA driver framework (QDF) string APIs.
22  */
23 
24 #ifndef __QDF_STR_H
25 #define __QDF_STR_H
26 
27 #include "i_qdf_str.h"
28 #include "qdf_types.h"
29 
30 /**
31  * qdf_is_space() - check if @c is a whitespace character
32  * @c: the character to check
33  *
34  * Whitespace characters include HT, LF, VT, FF, CR, space, and nbsp
35  *
36  * Return: true if @ is a whitespace character
37  */
38 static inline bool qdf_is_space(char c)
39 {
40 	return __qdf_is_space(c);
41 }
42 
43 /**
44  * qdf_str_cmp - Compare two strings
45  * @str1: First string
46  * @str2: Second string
47  * Return:
48  *	 0 - strings are equal
49  *	<0 - str1 sorts lexicographically before str2
50  *	>0 - str1 sorts lexicographically after str2
51  */
52 static inline int32_t qdf_str_cmp(const char *str1, const char *str2)
53 {
54 	return __qdf_str_cmp(str1, str2);
55 }
56 
57 /**
58  * qdf_str_dup() - duplicate null-terminated string @src
59  * @dest: double pointer to be populated
60  * @src: the null-terminated string to be duplicated
61  *
62  * @dest must be freed using qdf_mem_free() to avoid memory leaks.
63  *
64  * Return: QDF_STATUS; @dest set to NULL on failure, a valid address on success
65  */
66 QDF_STATUS qdf_str_dup(char **dest, const char *src);
67 
68 /**
69  * qdf_str_eq - compare two null-terminated strings for equality
70  * @left: the string left of the equality
71  * @right: the string right of the equality
72  *
73  * This is a thin wrapper over `if (strcmp(left, right) == 0)` for clarity.
74  *
75  * Return: true if strings are equal
76  */
77 static inline bool qdf_str_eq(const char *left, const char *right)
78 {
79 	return qdf_str_cmp(left, right) == 0;
80 }
81 
82 /**
83  * qdf_str_lcopy - Bounded copy from one string to another
84  * @dest: destination string
85  * @src: source string
86  * @dest_size: max number of bytes to copy (incl. null terminator)
87  *
88  * If the return value is >= @dest_size, @dest has been truncated.
89  *
90  * Return: length of @src
91  */
92 static inline qdf_size_t
93 qdf_str_lcopy(char *dest, const char *src, uint32_t dest_size)
94 {
95 	return __qdf_str_lcopy(dest, src, dest_size);
96 }
97 
98 /**
99  * qdf_str_left_trim() - Trim any leading whitespace from @str
100  * @str: the string to trim
101  *
102  * Return: A pointer to the first non-space character in @str
103  */
104 static inline const char *qdf_str_left_trim(const char *str)
105 {
106 	return __qdf_str_left_trim(str);
107 }
108 
109 /**
110  * qdf_str_len() - returns the length of a null-terminated string
111  * @str: input string
112  *
113  * Return: length of @str (without null terminator)
114  */
115 static inline qdf_size_t qdf_str_len(const char *str)
116 {
117 	return __qdf_str_len(str);
118 }
119 
120 /**
121  * qdf_str_right_trim() - Trim any trailing whitespace from @str
122  * @str: the string to trim
123  *
124  * Note: The first trailing whitespace character is replaced with a
125  * null-terminator
126  *
127  * Return: None
128  */
129 void qdf_str_right_trim(char *str);
130 
131 /**
132  * qdf_str_trim() - Trim any leading/trailing whitespace from @str
133  * @str: the string to trim
134  *
135  * Note: The first trailing whitespace character is replaced with a
136  * null-terminator
137  *
138  * Return: A pointer to the first non-space character in @str
139  */
140 static inline char *qdf_str_trim(char *str)
141 {
142 	return __qdf_str_trim(str);
143 }
144 
145 /**
146  * qdf_str_nlen() - Get string length up to @limit characters
147  * @str: the string to get the length of
148  * @limit: the maximum number of characters to check
149  *
150  * Return: the less of @limit or the length of @str (without null terminator)
151  */
152 static inline qdf_size_t qdf_str_nlen(const char *str, qdf_size_t limit)
153 {
154 	return __qdf_str_nlen(str, limit);
155 }
156 
157 /**
158  * qdf_str_ncmp - Compare two strings
159  * @str1: First string
160  * @str2: Second string
161  * @limit: the maximum number of characters to check
162  * Return:
163  *	 0 - strings are equal
164  *	<0 - str1 sorts lexicographically before str2
165  *	>0 - str1 sorts lexicographically after str2
166  */
167 static inline int32_t
168 qdf_str_ncmp(const char *str1, const char *str2, qdf_size_t limit)
169 {
170 	return __qdf_str_ncmp(str1, str2, limit);
171 }
172 
173 /**
174  * qdf_str_sep - extract token from string
175  * @str: String buffer
176  * @delim: Delimitter
177  * Return: Pointer to the first token
178  *
179  */
180 static inline char *qdf_str_sep(char **str, char *delim)
181 {
182 	return __qdf_str_sep(str, delim);
183 }
184 
185 /**
186  * qdf_str_copy_all_before_char() - API to copy all character before a
187  * particular char provided
188  * @str: Source string
189  * @str_len: Source string legnth
190  * @dst: Destination string
191  * @dst_len: Destination string legnth
192  * @c: Character before which all characters need to be copied
193  *
194  * Return: length of the copied string, if success. zero otherwise.
195  */
196 uint32_t
197 qdf_str_copy_all_before_char(char *str, uint32_t str_len,
198 			     char *dst, uint32_t dst_len, char c);
199 #endif /* __QDF_STR_H */
200