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