qdf_str.h 5.3 KB

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