sc/string/sc_str.h

202 lines
6.6 KiB
C
Raw Normal View History

2020-11-11 01:19:49 +03:00
/*
* BSD-3-Clause
2020-11-11 01:19:49 +03:00
*
* Copyright 2021 Ozan Tezcan
* All rights reserved.
2020-11-11 01:19:49 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
2020-11-11 01:19:49 +03:00
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
2020-11-11 01:19:49 +03:00
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
2020-11-11 01:19:49 +03:00
*/
#ifndef SC_STR_H
#define SC_STR_H
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
2021-04-13 22:36:50 +03:00
#define SC_STR_VERSION "2.0.0"
2021-02-14 16:19:26 +03:00
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else
#define sc_str_malloc malloc
#define sc_str_realloc realloc
#define sc_str_free free
#endif
2020-11-11 01:19:49 +03:00
/**
2021-02-03 08:09:50 +03:00
* length prefixed C strings, length is at the start of the allocated memory
2023-04-21 00:43:26 +03:00
* e.g.,
2020-11-11 01:19:49 +03:00
* -----------------------------------------------
* | 0 | 0 | 0 | 4 | 'T' | 'E' | 'S' | 'T' | '\0'|
* -----------------------------------------------
* ^
* return
* User can keep pointer to first character, so it's like C style strings with
* additional functionality when it's used with these functions here.
*/
/**
2021-02-08 01:06:14 +03:00
* @param str '\0' terminated C string, can be NULL.
* @return length prefixed string. NULL on out of memory or if 'str' is NULL.
2020-11-11 01:19:49 +03:00
*/
char *sc_str_create(const char *str);
/**
2021-02-08 01:06:14 +03:00
* @param str string bytes, no need for '\0' termination.
2021-02-03 08:09:50 +03:00
* @param len length of the 'str'.
2021-02-08 01:06:14 +03:00
* @return length prefixed string. NULL on out of memory or if 'str' is NULL.
2020-11-11 01:19:49 +03:00
*/
char *sc_str_create_len(const char *str, uint32_t len);
/**
2021-02-03 08:09:50 +03:00
* @param fmt format
* @param ... arguments
* @return length prefixed string. NULL on out of memory.
2020-11-11 01:19:49 +03:00
*/
char *sc_str_create_fmt(const char *fmt, ...);
/**
2021-02-03 08:09:50 +03:00
* @param fmt format
2020-11-11 01:19:49 +03:00
* @param va va_list
2021-02-03 08:09:50 +03:00
* @return length prefixed string. NULL on out of memory.
2020-11-11 01:19:49 +03:00
*/
char *sc_str_create_va(const char *fmt, va_list va);
/**
* Deallocate length prefixed string.
2021-02-08 01:06:14 +03:00
* @param str length prefixed string. str may be NULL.
2020-11-11 01:19:49 +03:00
*/
void sc_str_destroy(char **str);
2020-11-11 01:19:49 +03:00
/**
2021-02-03 08:09:50 +03:00
* @param str length prefixed string. NULL values are accepted.
2021-02-05 20:46:59 +03:00
* @return length of the string. If NULL, returns -1.
2020-11-11 01:19:49 +03:00
*/
int64_t sc_str_len(const char *str);
/**
2021-02-03 08:09:50 +03:00
* @param str length prefixed string. NULL values are accepted.
2021-02-08 01:06:14 +03:00
* @return duplicate string. NULL on out of memory or if 'str' is NULL.
2020-11-11 01:19:49 +03:00
*/
char *sc_str_dup(const char *str);
/**
2021-02-08 01:06:14 +03:00
* @param str Pointer to length prefixed string, '*str' may change.
2020-11-11 01:19:49 +03:00
* @param param New value to set.
2021-02-08 01:06:14 +03:00
* @return 'true' on success, 'false' on out of memory or if '*str' is NULL
2020-11-11 01:19:49 +03:00
*/
bool sc_str_set(char **str, const char *param);
/**
2021-02-08 01:06:14 +03:00
* @param str pointer to length prefixed string, '*str' may change.
2021-02-03 08:09:50 +03:00
* @param fmt format
* @param ... arguments
2021-02-08 01:06:14 +03:00
* @return 'true' on success, 'false' on out of memory
2020-11-11 01:19:49 +03:00
*/
bool sc_str_set_fmt(char **str, const char *fmt, ...);
/**
2021-02-08 01:06:14 +03:00
* @param str pointer to length prefixed string, '*str' may change.
2021-02-03 08:09:50 +03:00
* @param text text to append.
2021-02-08 01:06:14 +03:00
* @return 'true' on success, 'false' on out of memory or if '*str' is NULL.
2020-11-11 01:19:49 +03:00
*/
bool sc_str_append(char **str, const char *text);
/**
2021-02-08 01:06:14 +03:00
* @param str pointer to length prefixed string. (char**).'*str' may change.
2021-02-03 08:09:50 +03:00
* @param fmt format
* @param ... arguments
2021-02-08 01:06:14 +03:00
* @return 'true' on success, 'false' on out of memory or if '*str' is NULL.
2020-11-11 01:19:49 +03:00
*/
#define sc_str_append_fmt(str, fmt, ...) \
((*str) ? (sc_str_set_fmt(str, "%s" fmt, *str, __VA_ARGS__)) : \
(sc_str_set_fmt(str, fmt, __VA_ARGS__)))
2020-11-11 01:19:49 +03:00
/**
2021-02-06 21:36:18 +03:00
* Compare two length prefixed strings. To compare with C string, use strcmp().
2020-11-11 01:19:49 +03:00
*
2021-02-03 08:09:50 +03:00
* @param str length prefixed string, must not be NULL.
* @param other length prefixed string, must not be NULL.
2020-11-11 01:19:49 +03:00
* @return 'true' if equals.
*/
bool sc_str_cmp(const char *str, const char *other);
2021-02-06 21:36:18 +03:00
/**
2021-02-08 01:06:14 +03:00
* @param str length prefixed string, '*str' may change
2021-02-06 21:36:18 +03:00
* @param list character list to trim.
2021-02-08 01:06:14 +03:00
* @return 'true' on success or if '*str' is NULL. 'false' on out of memory
2021-02-06 21:36:18 +03:00
*/
bool sc_str_trim(char **str, const char *list);
/**
2021-02-08 01:06:14 +03:00
* @param str length prefixed string, *str' may change.
2021-02-06 21:36:18 +03:00
* @param start start index.
* @param end end index.
2021-02-08 01:06:14 +03:00
* @return 'false' on out of range, on out of memory or if '*str' is NULL.
* 'true' on success.
2021-02-06 21:36:18 +03:00
*/
bool sc_str_substring(char **str, uint32_t start, uint32_t end);
/**
2021-02-08 01:06:14 +03:00
* @param str length prefixed string, '*str' may change.
2021-02-06 21:36:18 +03:00
* @param rep string to be replaced
* @param with string to replace with
2021-02-08 01:06:14 +03:00
* @return 'true' on success or if '*str' is NULL. 'false' on out of memory
2021-02-06 21:36:18 +03:00
*/
bool sc_str_replace(char **str, const char *rep, const char *with);
2020-11-11 01:19:49 +03:00
/**
* Tokenization is zero-copy but a bit tricky. This function will mutate 'str',
* but it is temporary. On each 'sc_str_token_begin' call, this function will
* place '\0' character at the end of a token and put delimiter at the end of
* the 'str'.
2023-04-21 00:43:26 +03:00
* e.g., user1-user2\0 after first iteration will be user1\0user2-
2020-11-11 01:19:49 +03:00
*
* sc_str_token_end() will fix original string if necessary.
*
* usage:
*
2021-02-05 20:46:59 +03:00
* char *str = sc_str_create("user1-user2-user3");
2020-11-11 01:19:49 +03:00
* char *save = NULL; // Must be initialized with NULL.
* const char *token;
*
2023-10-07 15:36:19 +08:00
* while ((token = sc_str_token_begin(str, &save, "-")) != NULL) {
2020-11-11 01:19:49 +03:00
* printf("token : %s \n", token);
* }
*
* sc_str_token_end(str, &save);
*
*
2021-02-03 08:09:50 +03:00
* @param str length prefixed string, must not be NULL.
* @param save helper variable for tokenizer code.
* @param delim delimiter list.
* @return token.
2020-11-11 01:19:49 +03:00
*/
const char *sc_str_token_begin(char *str, char **save, const char *delim);
void sc_str_token_end(char *str, char **save);
#endif