MCPcopy Create free account
hub / github.com/KaisenAmin/c_std / string_tokenize

Function string_tokenize

string/std_string.c:2944–3007  ·  view source on GitHub ↗

* @brief This function tokenizes the content of the given String object using the specified delimiters. * It returns an array of String objects, each representing a token. The number of tokens is * stored in the variable pointed to by @p count. If the input String object or delimiters are NULL, * the function returns NULL and logs an error. * * @param str The String object to tokenize. Must n

Source from the content-addressed store, hash-verified

2942 * @return An array of String objects, each representing a token, or NULL if an error occurs.
2943 */
2944String** string_tokenize(const String* str, const char* delimiters, int* count) {
2945 STRING_LOG("[string_tokenize]: Function start.");
2946
2947 if (str == NULL || delimiters == NULL) {
2948 STRING_LOG("[string_tokenize]: Error - Invalid input (str or delimiters is NULL).");
2949 return NULL;
2950 }
2951
2952 size_t num_tokens = 0;
2953 char* temp_str = string_strdup(str->dataStr);
2954 if (temp_str == NULL) {
2955 STRING_LOG("[string_tokenize]: Error - Memory allocation failed for temp_str.");
2956 return NULL;
2957 }
2958
2959 char* token = strtok(temp_str, delimiters);
2960 while (token != NULL) {
2961 num_tokens++;
2962 token = strtok(NULL, delimiters);
2963 }
2964 free(temp_str);
2965
2966 STRING_LOG("[string_tokenize]: Number of tokens found: %zu.", num_tokens);
2967
2968 String** tokens = (String**)malloc(num_tokens * sizeof(String*));
2969 if (tokens == NULL) {
2970 STRING_LOG("[string_tokenize]: Error - Memory allocation failed for tokens array.");
2971 return NULL;
2972 }
2973
2974 // Tokenize again to fill the array
2975 temp_str = string_strdup(str->dataStr);
2976 if (temp_str == NULL) {
2977 STRING_LOG("[string_tokenize]: Error - Memory allocation failed for temp_str (second pass).");
2978 free(tokens);
2979 return NULL;
2980 }
2981
2982 token = strtok(temp_str, delimiters);
2983 size_t idx = 0;
2984
2985 while (token != NULL && idx < num_tokens) {
2986 tokens[idx] = string_create(token);
2987 if (tokens[idx] == NULL) {
2988 STRING_LOG("[string_tokenize]: Error - Failed to create token string at index %zu.", idx);
2989 for (size_t i = 0; i < idx; ++i) {
2990 string_deallocate(tokens[i]);
2991 }
2992 free(tokens);
2993 free(temp_str);
2994
2995 return NULL;
2996 }
2997 idx++;
2998 token = strtok(NULL, delimiters);
2999 }
3000 free(temp_str);
3001 *count = num_tokens;

Callers

nothing calls this directly

Calls 3

string_strdupFunction · 0.85
string_createFunction · 0.85
string_deallocateFunction · 0.85

Tested by

no test coverage detected