Str View
Loading...
Searching...
No Matches
str_view.h
Go to the documentation of this file.
1
28#ifndef SV_STR_VIEW
29#define SV_STR_VIEW
30
31/* SV_ATTRIB_PURE has no side effects when given the same arguments with the
32 same data, producing the same return value. A SV_Str_view points to char
33 const * data which may change in between SV_Str_view calls. SV_ATTRIB_CONST
34 applies only where no pointers are accessed or dereferenced. Other
35 attributes provide more string safety and opportunities to optimize.
36 Credit Harith on Code Review Stack Exchange. */
37#if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
38# ifdef __has_attribute
39# if __has_attribute(pure)
42# define SV_ATTRIB_PURE __attribute__((pure))
43# else
46# define SV_ATTRIB_PURE
47# endif
48# if __has_attribute(pure)
51# define SV_ATTRIB_CONST __attribute__((const))
52# else
55# define SV_ATTRIB_CONST
56# endif
57# if __has_attribute(null_terminated_string_arg)
59# define SV_ATTRIB_NULLTERM(...) \
60 __attribute__((null_terminated_string_arg(__VA_ARGS__)))
61# else
63# define SV_ATTRIB_NULLTERM(...)
64# endif
65# else
68# define SV_ATTRIB_PURE
71# define SV_ATTRIB_CONST
73# define SV_ATTRIB_NULLTERM(...)
74# endif
79# define SV_STR_LITERAL(str_literal) "" str_literal ""
80#else
83# define SV_ATTRIB_PURE
86# define SV_ATTRIB_CONST
88# define SV_ATTRIB_NULLTERM(...)
91# define SV_STR_LITERAL(str_literal) str_literal
92#endif /* __GNUC__ || __clang__ || __INTEL_LLVM_COMPILER */
93
94#if defined(_MSVC_VER) || defined(_WIN32) || defined(_WIN64)
95# if defined(SV_BUILD_DLL)
97# define SV_API __declspec(dllexport)
98# elif defined(SV_CONSUME_DLL)
100# define SV_API __declspec(dllimport)
101# else
103# define SV_API
104# endif
105#else
107# define SV_API
108#endif /* _MSVC_VER */
109
110#include <stdbool.h>
111#include <stddef.h>
112
123typedef struct
124{
126 char const *str;
129 size_t len;
131
136typedef enum
137{
138 SV_ORDER_LESSER = -1,
139 SV_ORDER_EQUAL,
140 SV_ORDER_GREATER,
141 SV_ORDER_ERROR,
142} SV_Order;
143
171#define SV_from(str_literal) \
172 ((SV_Str_view){SV_STR_LITERAL(str_literal), sizeof(str_literal) - 1})
173
181
191SV_API SV_Str_view SV_from_view(size_t n, char const *str)
193
204SV_API SV_Str_view SV_from_delimiter(char const *str, char const *delim)
206
217 size_t count) SV_ATTRIB_PURE;
218
222SV_API size_t SV_str_bytes(char const *str)
224
234SV_API SV_Str_view SV_copy(size_t str_bytes, char const *src_str)
236
246SV_API size_t SV_fill(size_t dest_bytes, char *dest_buf, SV_Str_view src);
247
259
276
289
302SV_API SV_Order SV_view_compare(SV_Str_view lhs, char const *rhs, size_t n)
304
324
334
349
362
373
390
396
403
409SV_API char const *SV_next(char const *c) SV_ATTRIB_NULLTERM(1) SV_ATTRIB_PURE;
410
417SV_API char const *SV_reverse_next(char const *c) SV_ATTRIB_PURE;
418
425
433
447SV_API size_t SV_find(SV_Str_view haystack, size_t pos,
449
461SV_API size_t SV_reverse_find(SV_Str_view haystack, size_t pos,
463
470
481
492
499
510
517
528
539
551
562
574
585SV_API size_t SV_min_len(char const *str, size_t n)
587
593SV_API char const *SV_null(void) SV_ATTRIB_PURE;
594
604
612
620
629
636SV_API char const *SV_pointer(SV_Str_view sv, size_t i) SV_ATTRIB_PURE;
637
645
651
657
660#endif /* SV_STR_VIEW */
SV_API char SV_back(SV_Str_view sv) SV_ATTRIB_PURE
Obtain the character at the last position of SV_Str_view.
SV_API char const * SV_reverse_begin(SV_Str_view sv) SV_ATTRIB_PURE
Returns the reverse iterator beginning, the last character of the current view.
SV_API SV_Str_view SV_from_delimiter(char const *str, char const *delim) SV_ATTRIB_NULLTERM(1) SV_ATTRIB_NULLTERM(2) SV_ATTRIB_PURE
Constructs and returns a string view from a null terminated string broken on the first occurrence of ...
#define SV_ATTRIB_NULLTERM(...)
Describes a parameter as null terminated.
Definition: str_view.h:88
SV_API char const * SV_pointer(SV_Str_view sv, size_t i) SV_ATTRIB_PURE
Returns the character pointer at the minimum between the indicated position and the end of the string...
SV_API SV_Str_view SV_from_view(size_t n, char const *str) SV_ATTRIB_NULLTERM(2) SV_ATTRIB_PURE
Constructs and returns a string view from a sequence of valid n bytes or string length,...
SV_API char const * SV_reverse_next(char const *c) SV_ATTRIB_PURE
Advances the iterator to the next character in the SV_Str_view being iterated through in reverse.
SV_API size_t SV_reverse_find(SV_Str_view haystack, size_t pos, SV_Str_view needle) SV_ATTRIB_PURE
Searches for the last occurrence of needle in haystack starting from pos from right to left.
SV_API bool SV_token_end(SV_Str_view src, SV_Str_view token) SV_ATTRIB_PURE
Provides the status of the current tokenization for use in conditions such as loops.
SV_API size_t SV_len(SV_Str_view sv) SV_ATTRIB_CONST
Returns the length of the SV_Str_view in O(1) time.
SV_API char const * SV_begin(SV_Str_view sv) SV_ATTRIB_PURE
Returns a read only pointer to the beginning of the string view, the first valid character in the vie...
SV_API bool SV_ends_with(SV_Str_view sv, SV_Str_view suffix) SV_ATTRIB_PURE
Confirms the presence of a suffix within a string view.
SV_API SV_Str_view SV_token_next(SV_Str_view src, SV_Str_view token, SV_Str_view delim) SV_ATTRIB_PURE
Advances to the next token in the remaining view separated by the delim.
SV_API SV_Str_view SV_remove_prefix(SV_Str_view sv, size_t n) SV_ATTRIB_PURE
SV_API char const * SV_reverse_end(SV_Str_view sv) SV_ATTRIB_PURE
The ending position of a reverse iteration.
SV_API size_t SV_str_bytes(char const *str) SV_ATTRIB_NULLTERM(1) SV_ATTRIB_PURE
Returns the bytes of the string pointer to, null terminator included.
SV_API SV_Str_view SV_token_begin(SV_Str_view src, SV_Str_view delim) SV_ATTRIB_PURE
Finds the first tokenized position in the string view given any length delim SV_Str_view.
SV_Order
Standard three way comparison type in C.
Definition: str_view.h:137
SV_API SV_Str_view SV_token_reverse_next(SV_Str_view src, SV_Str_view token, SV_Str_view delim) SV_ATTRIB_PURE
Advances the token in src to the next token between two delimiters provided by delim.
SV_API SV_Order SV_compare(SV_Str_view lhs, SV_Str_view rhs) SV_ATTRIB_PURE
Returns the standard C threeway comparison between cmp(lhs, rhs) between two string views.
SV_API char const * SV_end(SV_Str_view sv) SV_ATTRIB_PURE
Returns a read only pointer to the end of the string view.
SV_API SV_Order SV_view_compare(SV_Str_view lhs, char const *rhs, size_t n) SV_ATTRIB_NULLTERM(2) SV_ATTRIB_PURE
Returns the standard C threeway comparison between cmp(lhs, rhs) between a SV_Str_view and a C string...
SV_API SV_Str_view SV_substr(SV_Str_view sv, size_t pos, size_t count) SV_ATTRIB_PURE
Creates the substring from position pos for count length. The count is the minimum value between coun...
SV_API size_t SV_fill(size_t dest_bytes, char *dest_buf, SV_Str_view src)
Fills the destination buffer with the minimum between destination size and source view size,...
SV_API bool SV_token_reverse_end(SV_Str_view src, SV_Str_view token) SV_ATTRIB_PURE
Provides the status of the current tokenization for use in conditions such as loops.
SV_API SV_Str_view SV_reverse_match(SV_Str_view haystack, SV_Str_view needle) SV_ATTRIB_PURE
Search for a substring within a source string in reverse.
SV_API char const * SV_null(void) SV_ATTRIB_PURE
A sentinel empty string. Safely dereference to view a null terminator. This may be returned from vari...
#define SV_ATTRIB_PURE
Describes a function as having no side effects when given the same arguments with same underlying dat...
Definition: str_view.h:83
SV_API char const * SV_next(char const *c) SV_ATTRIB_NULLTERM(1) SV_ATTRIB_PURE
Advances the null terminated string pointer from its previous position. If NULL is provided SV_null()...
SV_API size_t SV_find(SV_Str_view haystack, size_t pos, SV_Str_view needle) SV_ATTRIB_PURE
Searches for needle in haystack starting from pos.
SV_API bool SV_is_empty(SV_Str_view sv) SV_ATTRIB_CONST
Returns true if the provided SV_Str_view is empty, false otherwise.
SV_API size_t SV_find_first_not_of(SV_Str_view haystack, SV_Str_view set) SV_ATTRIB_PURE
SV_API size_t SV_find_last_not_of(SV_Str_view haystack, SV_Str_view set) SV_ATTRIB_PURE
#define SV_ATTRIB_CONST
Describes a function as having no side effects and not depending on any pointer input that could chan...
Definition: str_view.h:86
SV_API SV_Str_view SV_remove_suffix(SV_Str_view sv, size_t n) SV_ATTRIB_PURE
SV_API size_t SV_bytes(SV_Str_view sv) SV_ATTRIB_CONST
#define SV_API
A macro to help with library linking on windows.
Definition: str_view.h:107
SV_API size_t SV_min_len(char const *str, size_t n) SV_ATTRIB_NULLTERM(1) SV_ATTRIB_PURE
Returns the minimum between the string size vs n bytes.
SV_API bool SV_contains(SV_Str_view haystack, SV_Str_view needle) SV_ATTRIB_PURE
Tests membership of needle in haystack.
SV_API SV_Str_view SV_copy(size_t str_bytes, char const *src_str) SV_ATTRIB_NULLTERM(2) SV_ATTRIB_PURE
Copies the max of string bytes or input string length into a view, whichever ends first.
SV_API char SV_at(SV_Str_view sv, size_t i) SV_ATTRIB_PURE
Obtain a character at a position in the string view.
SV_API SV_Str_view SV_match(SV_Str_view haystack, SV_Str_view needle) SV_ATTRIB_PURE
Search for a substring within a source string view.
SV_API SV_Str_view SV_token_reverse_begin(SV_Str_view src, SV_Str_view delim) SV_ATTRIB_PURE
SV_API SV_Str_view SV_from_terminated(char const *str) SV_ATTRIB_NULLTERM(1) SV_ATTRIB_PURE
Constructs and returns a string view from a null terminated string.
SV_API size_t SV_find_first_of(SV_Str_view haystack, SV_Str_view set) SV_ATTRIB_PURE
SV_API size_t SV_npos(SV_Str_view sv) SV_ATTRIB_CONST
The end of a SV_Str_view guaranteed to be greater than or equal to size.
SV_API SV_Str_view SV_extend(SV_Str_view sv) SV_ATTRIB_PURE
Returns a SV_Str_view of the entirety of the underlying string, starting at the current view pointer ...
SV_API bool SV_starts_with(SV_Str_view sv, SV_Str_view prefix) SV_ATTRIB_PURE
Confirms the presence of a prefix within a string view.
SV_API char SV_front(SV_Str_view sv) SV_ATTRIB_PURE
Obtain the character at the first position of SV_Str_view.
SV_API size_t SV_find_last_of(SV_Str_view haystack, SV_Str_view set) SV_ATTRIB_PURE
SV_API SV_Order SV_terminated_compare(SV_Str_view lhs, char const *rhs) SV_ATTRIB_NULLTERM(2) SV_ATTRIB_PURE
Returns the standard C threeway comparison between cmp(lhs, rhs) between a SV_Str_view and a C string...
A read-only view of string data in C.
Definition: str_view.h:124
char const * str
Definition: str_view.h:126
size_t len
Definition: str_view.h:129