1 /******************************************************************************* 2 * @file 3 * @brief Implementation of safe string functions. 4 ******************************************************************************* 5 * # License 6 * <b>Copyright 2019 Silicon Laboratories Inc. www.silabs.com</b> 7 ******************************************************************************* 8 * 9 * SPDX-License-Identifier: Zlib 10 * 11 * The licensor of this software is Silicon Laboratories Inc. 12 * 13 * This software is provided 'as-is', without any express or implied 14 * warranty. In no event will the authors be held liable for any damages 15 * arising from the use of this software. 16 * 17 * Permission is granted to anyone to use this software for any purpose, 18 * including commercial applications, and to alter it and redistribute it 19 * freely, subject to the following restrictions: 20 * 21 * 1. The origin of this software must not be misrepresented; you must not 22 * claim that you wrote the original software. If you use this software 23 * in a product, an acknowledgment in the product documentation would be 24 * appreciated but is not required. 25 * 2. Altered source versions must be plainly marked as such, and must not be 26 * misrepresented as being the original software. 27 * 3. This notice may not be removed or altered from any source distribution. 28 * 29 ******************************************************************************/ 30 31 #ifndef SL_STRING_H 32 #define SL_STRING_H 33 34 #include <stddef.h> 35 #include <stdbool.h> 36 #include <stdio.h> 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 /******************************************************************************* 43 * @addtogroup string String 44 * @brief String module provides APIs to handle string-related operations. 45 * @{ 46 ******************************************************************************/ 47 48 // ----------------------------------------------------------------------------- 49 // Defines 50 51 // ----------------------------------------------------------------------------- 52 // Prototypes 53 54 /******************************************************************************* 55 * @brief 56 * Copy a string into a buffer. 57 * Normally, the complete source string including the '\0' termination will be 58 * copied to the destination. 59 * If the destination buffer doesn't have room to receive the 60 * complete source string, the source string will be truncated and the 61 * destination buffer will be '\0' terminated within the destination buffer. 62 * 63 * @param[in] dst Destination buffer. 64 * 65 * @param[in] dst_size The size of the destination buffer. 66 * 67 * @param[in] src Source string. 68 ******************************************************************************/ 69 void sl_strcpy_s(char *dst, size_t dst_size, const char *src); 70 71 /******************************************************************************* 72 * @brief 73 * Append the source string to the end of destination string. 74 * Normally, the complete source string including the '\0' termination will be 75 * appended to the destination, starting at the source strings '\0' termination. 76 * If the destination buffer has no room to receive the 77 * complete source string, the source string will be truncated and the 78 * destination '\0' terminated within the destination buffer. 79 * 80 * @param[in] dst Destination string. 81 * 82 * @param[in] dst_size The size of the destination string buffer. 83 * 84 * @param[in] src Source string. 85 ******************************************************************************/ 86 void sl_strcat_s(char *dst, size_t dst_size, const char *src); 87 88 /******************************************************************************* 89 * @brief 90 * Get the string length. 91 * 92 * @param[in] str The string to get the length for. 93 * 94 * @return String lenght. 95 ******************************************************************************/ 96 size_t sl_strlen(char *str); 97 98 /******************************************************************************* 99 * @brief 100 * Get the string length, limited to given length. 101 * 102 * @param[in] str The string to get the length for. 103 * 104 * @param[in] max_len The input string is searched for at most max_lencharacters. 105 * 106 * @return String lenght. 107 ******************************************************************************/ 108 size_t sl_strnlen(char *str, size_t max_len); 109 110 /******************************************************************************* 111 * @brief 112 * Check if the string is empty. 113 * 114 * @param[in] str The string to check. 115 * 116 * @return true if string is empty or null, else return false. 117 ******************************************************************************/ 118 bool sl_str_is_empty(const char *str); 119 120 /******************************************************************************* 121 * @brief 122 * Compare two strings, ignoring case. 123 * 124 * @param[in] a String to compare. 125 * 126 * @param[in] b String to compare. 127 * 128 * @return An integer greater than, or less than 0 if the strings 129 * are not equal. 0 if the strings are equal. 130 ******************************************************************************/ 131 int sl_strcasecmp(char const *a, char const *b); 132 133 /******************************************************************************* 134 * @brief 135 * Searches for the character in memory, in reverse order. 136 * 137 * @param[in] buff Address of the memory buffer. 138 * 139 * @param[in] c Character to look for. 140 * 141 * @param[in] buff_len Length of the memory buffer. 142 * 143 * @return The address of the character in the buffer if and only 144 * if it was found. 145 * NULL if no character was found. 146 ******************************************************************************/ 147 void* sl_memrchr(void const *buff, char c, size_t buff_len); 148 149 /** @} (end addtogroup string) */ 150 151 #ifdef __cplusplus 152 } 153 #endif 154 155 #endif /* SL_STRING_H */ 156