Skip to content

Commit

Permalink
add more compatibility changes for msvc
Browse files Browse the repository at this point in the history
  • Loading branch information
agl-alexglopez committed Jul 12, 2024
1 parent b3fe15b commit 4f599ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
20 changes: 10 additions & 10 deletions str_view/str_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct sv_two_way_pack
empty, null, invalid, not found etc. Used on cases by case basis.
It is usually better to justify giving back the user pointer in a
str_view even if it sized 0 and pointing to null terminator. */
static const str_view nil = SV("");
static const str_view nil = {.s = "", .sz = 0};

/* ========================= Prototypes =============================== */

Expand Down Expand Up @@ -86,7 +86,7 @@ static size_t sv_rfourbyte_strnstrn(const unsigned char *, size_t,
/* =================== Interface Implementation ====================== */

str_view
sv(const char str[static const 1])
sv(const char str[1])
{
if (!str)
{
Expand All @@ -96,7 +96,7 @@ sv(const char str[static const 1])
}

str_view
sv_n(size_t n, const char str[static const 1])
sv_n(size_t n, const char str[1])
{
if (!str || !n)
{
Expand All @@ -106,7 +106,7 @@ sv_n(size_t n, const char str[static const 1])
}

str_view
sv_delim(const char str[static const 1], const char delim[static const 1])
sv_delim(const char str[1], const char delim[1])
{
if (!str)
{
Expand Down Expand Up @@ -140,13 +140,13 @@ sv_print(FILE *f, str_view sv)
}

str_view
sv_copy(const size_t str_sz, const char src_str[static const 1])
sv_copy(const size_t str_sz, const char src_str[1])
{
return sv_n(str_sz, src_str);
}

size_t
sv_fill(size_t dest_sz, char dest_buf[static const dest_sz], str_view src)
sv_fill(size_t dest_sz, char dest_buf[dest_sz], str_view src)
{
if (!dest_buf || !dest_sz || !src.s || !src.sz)
{
Expand Down Expand Up @@ -177,7 +177,7 @@ sv_size(str_view sv)
}

size_t
sv_strsize(const char str[static const 1])
sv_strsize(const char str[1])
{
if (!str)
{
Expand All @@ -187,7 +187,7 @@ sv_strsize(const char str[static const 1])
}

size_t
sv_minlen(const char str[static const 1], size_t n)
sv_minlen(const char str[1], size_t n)
{
if (!str)
{
Expand Down Expand Up @@ -249,7 +249,7 @@ sv_cmp(str_view lhs, str_view rhs)
}

sv_threeway_cmp
sv_strcmp(str_view lhs, const char rhs[static const 1])
sv_strcmp(str_view lhs, const char rhs[1])
{
if (!lhs.s || !rhs)
{
Expand All @@ -271,7 +271,7 @@ sv_strcmp(str_view lhs, const char rhs[static const 1])
}

sv_threeway_cmp
sv_strncmp(str_view lhs, const char rhs[static const 1], const size_t n)
sv_strncmp(str_view lhs, const char rhs[1], const size_t n)
{
if (!lhs.s || !rhs)
{
Expand Down
29 changes: 13 additions & 16 deletions str_view/str_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,20 @@ typedef enum

/* Constructs and returns a string view from a NULL TERMINATED string.
It is undefined to construct a str_view from a non terminated string. */
str_view sv(const char str[static const 1]) ATTRIB_NONNULL(1)
ATTRIB_NULLTERM(1) ATTRIB_PURE;
str_view sv(const char str[1]) ATTRIB_NONNULL(1) ATTRIB_NULLTERM(1) ATTRIB_PURE;

/* Constructs and returns a string view from a sequence of valid n bytes
or string length, whichever comes first. The resulting str_view may
or may not be null terminated at the index of its size. */
str_view sv_n(size_t n, const char str[static const 1]) ATTRIB_NONNULL(2)
str_view sv_n(size_t n, const char str[1]) ATTRIB_NONNULL(2)
ATTRIB_NULLTERM(2) ATTRIB_PURE;

/* Constructs and returns a string view from a NULL TERMINATED string
broken on the first ocurrence of delimeter if found or null
terminator if delim cannot be found. This constructor will also
skip the delimeter if that delimeter starts the string. This is similar
to the tokenizing function in the iteration section. */
str_view sv_delim(const char str[static const 1],
const char delim[static const 1]) ATTRIB_NONNULL(1, 2)
str_view sv_delim(const char str[1], const char delim[1]) ATTRIB_NONNULL(1, 2)
ATTRIB_NULLTERM(1, 2) ATTRIB_PURE;

/* Creates the substring from position pos for count length. The count is
Expand Down Expand Up @@ -149,7 +147,7 @@ size_t sv_len(str_view sv) ATTRIB_CONST;
size_t sv_size(str_view sv) ATTRIB_CONST;

/* Returns the bytes of the string pointer to, null terminator included. */
size_t sv_strsize(const char str[static const 1]) ATTRIB_NONNULL(1)
size_t sv_strsize(const char str[1]) ATTRIB_NONNULL(1)
ATTRIB_NULLTERM(1) ATTRIB_PURE;

/* Swaps the contents of a and b. Becuase these are read only views
Expand All @@ -158,15 +156,15 @@ void sv_swap(str_view *a, str_view *b) ATTRIB_NONNULL(1, 2);

/* Copies the max of str_sz or src_str length into a view, whichever
ends first. This is the same as sv_n. */
str_view sv_copy(size_t str_sz, const char src_str[static const 1])
ATTRIB_NONNULL(2) ATTRIB_NULLTERM(1) ATTRIB_PURE;
str_view sv_copy(size_t str_sz, const char src_str[1]) ATTRIB_NONNULL(2)
ATTRIB_NULLTERM(1) ATTRIB_PURE;

/* Fills the destination buffer with the minimum between
destination size and source view size, null terminating
the string. This may cut off src data if dest_sz < src.sz.
Returns how many bytes were written to the buffer. */
size_t sv_fill(size_t dest_sz, char dest_buf[static const dest_sz],
str_view src) ATTRIB_NONNULL(2);
size_t sv_fill(size_t dest_sz, char dest_buf[dest_sz], str_view src)
ATTRIB_NONNULL(2);

/* Returns a str_view of the entirety of the underlying string, starting
at the current view pointer position. This guarantees that the str_view
Expand Down Expand Up @@ -196,8 +194,8 @@ sv_threeway_cmp sv_cmp(str_view lhs, str_view rhs) ATTRIB_PURE;
Comparison is bounded by the shorter str_view length. ERR is
returned if bad input is provided such as a str_view with a
NULL pointer field. */
sv_threeway_cmp sv_strcmp(str_view lhs, const char rhs[static const 1])
ATTRIB_NONNULL(2) ATTRIB_NULLTERM(2) ATTRIB_PURE;
sv_threeway_cmp sv_strcmp(str_view lhs, const char rhs[1]) ATTRIB_NONNULL(2)
ATTRIB_NULLTERM(2) ATTRIB_PURE;

/* Returns the standard C threeway comparison between cmp(lhs, rhs)
between a str_view and the first n bytes (inclusive) of str
Expand All @@ -208,12 +206,11 @@ sv_threeway_cmp sv_strcmp(str_view lhs, const char rhs[static const 1])
Comparison is bounded by the shorter str_view length. ERR is
returned if bad input is provided such as a str_view with a
NULL pointer field. */
sv_threeway_cmp sv_strncmp(str_view lhs, const char rhs[static const 1],
size_t n) ATTRIB_NONNULL(2)
ATTRIB_NULLTERM(2) ATTRIB_PURE;
sv_threeway_cmp sv_strncmp(str_view lhs, const char rhs[1], size_t n)
ATTRIB_NONNULL(2) ATTRIB_NULLTERM(2) ATTRIB_PURE;

/* Returns the minimum between the string size vs n bytes. */
size_t sv_minlen(const char str[static const 1], size_t n) ATTRIB_NONNULL(1)
size_t sv_minlen(const char str[1], size_t n) ATTRIB_NONNULL(1)
ATTRIB_NULLTERM(1) ATTRIB_PURE;

/*============================ Iteration ==================================*/
Expand Down

0 comments on commit 4f599ea

Please sign in to comment.