Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions nob.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
const char *test_names[] = {
"minimal_log_level",
"nob_sv_end_with",
"nob_sv_chop_left",
"nob_sv_chop_right",
"set_get_current_dir",
"cmd_redirect",
"cmd_args_passing",
Expand Down
24 changes: 23 additions & 1 deletion nob.h
Original file line number Diff line number Diff line change
Expand Up @@ -1952,7 +1952,11 @@ NOBDEF Nob_String_View nob_sv_chop_by_delim(Nob_String_View *sv, char delim)
return result;
}

NOBDEF Nob_String_View nob_sv_chop_left(Nob_String_View *sv, size_t n)
/* This chop right function should be used with caution as it changes the state of
the input string view struct, a better way to use it IMO is to create the input string view from
a temporary buffer allocation
*/
NOBDEF Nob_String_View nob_sv_chop_right(Nob_String_View *sv, size_t n)
{
if (n > sv->count) {
n = sv->count;
Expand All @@ -1966,6 +1970,24 @@ NOBDEF Nob_String_View nob_sv_chop_left(Nob_String_View *sv, size_t n)
return result;
}


/* This chop left function should be used with caution as it changes the state of
the input string view struct, a better way to use it IMO is to create the input string view from
a temporary buffer
*/
NOBDEF Nob_String_View nob_sv_chop_left(Nob_String_View *sv, size_t n)
{
if (n > sv->count) {
n = sv->count;
}

sv->data += n;
sv->count -= n;

Nob_String_View result = nob_sv_from_parts(sv->data, sv->count);
return result;
}

NOBDEF Nob_String_View nob_sv_from_parts(const char *data, size_t count)
{
Nob_String_View sv;
Expand Down
37 changes: 37 additions & 0 deletions tests/nob_sv_chop_left.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#define NOB_IMPLEMENTATION
#include "nob.h"

void assert_true(const char *test_case, bool result)
{
if (result) {
nob_log(NOB_INFO, "[SUCCESS] %s", test_case);
} else {
nob_log(NOB_ERROR, "[FAIL] %s", test_case);
}
}

void assert_false(const char *test_case, bool result)
{
if (!result) {
nob_log(NOB_INFO, "[SUCCESS] %s", test_case);
} else {
nob_log(NOB_ERROR, "[FAIL] %s", test_case);
}
}

int main(void)
{
Nob_String_View sv_chop_left[] = {
nob_sv_from_cstr("foobar"),
nob_sv_from_cstr("yet_another_foobar"),
nob_sv_from_cstr("example"),
nob_sv_from_cstr("another_example"),
};

assert_true("nob_sv_chop_left(sv1, \"foobar\", 2)", nob_sv_eq(nob_sv_chop_left(&sv_chop_left[0], 2), nob_sv_from_cstr("obar")));
assert_false("nob_sv_chop_left(sv2, \"yet_another_foobar\", 4)", nob_sv_eq(nob_sv_chop_left(&sv_chop_left[1], 4), nob_sv_from_cstr("obar")));
assert_true("nob_sv_chop_left(sv3, \"example\", 3)", nob_sv_eq(nob_sv_chop_left(&sv_chop_left[2], 3), nob_sv_from_cstr("mple")));
assert_false("nob_sv_chop_left(sv4, \"another_example\", 3)", nob_sv_eq(nob_sv_chop_left(&sv_chop_left[3], 3), nob_sv_from_cstr("ampl")));

return 0;
}
37 changes: 37 additions & 0 deletions tests/nob_sv_chop_right.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#define NOB_IMPLEMENTATION
#include "nob.h"

void assert_true(const char *test_case, bool result)
{
if (result) {
nob_log(NOB_INFO, "[SUCCESS] %s", test_case);
} else {
nob_log(NOB_ERROR, "[FAIL] %s", test_case);
}
}

void assert_false(const char *test_case, bool result)
{
if (!result) {
nob_log(NOB_INFO, "[SUCCESS] %s", test_case);
} else {
nob_log(NOB_ERROR, "[FAIL] %s", test_case);
}
}

int main(void)
{
Nob_String_View sv_chop_right[] = {
nob_sv_from_cstr("foobar"),
nob_sv_from_cstr("yet_another_foobar"),
nob_sv_from_cstr("example"),
nob_sv_from_cstr("another_example"),
};

assert_true("nob_sv_chop_right(sv1, \"foobar\", 2)", nob_sv_eq(nob_sv_chop_right(&sv_chop_right[0], 2), nob_sv_from_cstr("fo")));
assert_false("nob_sv_chop_right(sv2, \"yet_another_foobar\", 4)", nob_sv_eq(nob_sv_chop_right(&sv_chop_right[1], 4), nob_sv_from_cstr("obar")));
assert_true("nob_sv_chop_right(sv3, \"example\", 4)", nob_sv_eq(nob_sv_chop_right(&sv_chop_right[2], 4), nob_sv_from_cstr("exam")));
assert_false("nob_sv_chop_right(sv4, \"another_example\", 4)", nob_sv_eq(nob_sv_chop_right(&sv_chop_right[3], 4), nob_sv_from_cstr("")));

return 0;
}