string.h¶
The string.h module provides utility methods for interacting with strings
in a consistent and safe manner.
API¶
Views¶
-
struct mn_view¶
A portion of a string. Unlike the
mn_strtype, this type only refers to part of a backing string. This backing string must remain valid for the duration of amn_viewinstance’s lifetime.-
size_t len¶
The length of the referenced portion of some string.
-
size_t len¶
-
PRImnv¶
A utility macro for including the contents of a
mn_viewinto a printf call. Should be used withmn_view_pri. For example:struct mn_view const v = mn_view_ref("hello, world", 5); printf("View: " PRImnv "\n", mn_view_pri(v)); // Outputs: View hello\n
-
mn_view_pri(X)¶
A utility macro for including the contents of a
mn_viewinto a printf call. Should be used withPRImnv. For example:struct mn_view const v = mn_view_ref("hello, world", 5); printf("View: " PRImnv "\n", mn_view_pri(v)); // Outputs: View hello\n
- struct mn_view const mn_view_ref(
- char const ss[static 1],
- size_t const len
Create a new
mn_viewpointing tosswith lengthlen.
- struct mn_view const mn_view_substr(
- struct mn_view const v,
- size_t const i,
- size_t const j
Create a subview of another.
- Parameters:
v – The
mn_viewto take a subview of.i – The starting index of the subview, inclusive.
j – The ending index of the subview, exclusive.
- size_t mn_view_cpy(
- char *const dst,
- struct mn_view const src,
- size_t const count
Copy the contents of
srcintodst.- Parameters:
dst – The buffer to copy
srcinto.src – The string to copy into
dst.count – OPTIONAL. The maximum number of characters to copy. Ignored if greater than
src’slen. If excluded,SIZE_MAXis assumed.
- size_t mn_view_find( )¶
Find the first index of
haystackin which subviewneedleis found.- Parameters:
haystack – The view to search the
needlein.needle – The subview to search for.
- Returns:
The index the
needlewas found starting at. If not present,haystack.lenis returned instead.
Strings¶
-
struct mn_str¶
A thin wrapper around a C-style string. Unlike the
mn_viewtype, this type always refers to the entirety of a backing string. This backing string must remain valid for the duration of amn_strinstance’s lifetime.This type is primarily motivated as a convenient means of referencing C-style strings’ length.
-
char const *ss¶
The backing string.
-
size_t len¶
The length of the backing string excluding the trailing
NULcharacter.
-
char const *ss¶
-
mn_str_lit(X)¶
Create a new
mn_strinstance pointing at C-string literalX. It is assumedXdoes not contain embeddedNULcharacters.
- struct mn_str const mn_str_ref(
- char const ss[static 1],
- size_t const len
Create a new
mn_strpointing tosswith lengthlen. It is assumedssdoes not contain embeddedNULcharacters and isNUL-terminated with ss[len] == '\0'.- Parameters:
ss – The C-string to point to. Must remain valid during the lifetime of the
mn_strinstance.len – The length of
ssexcluding the trailingNULcharacter.