diff --git a/nob.c b/nob.c index a580562..1eb21c0 100644 --- a/nob.c +++ b/nob.c @@ -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", diff --git a/nob.h b/nob.h index 737f61f..601edc3 100644 --- a/nob.h +++ b/nob.h @@ -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; @@ -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; diff --git a/tests/nob_sv_chop_left.c b/tests/nob_sv_chop_left.c new file mode 100644 index 0000000..82b5655 --- /dev/null +++ b/tests/nob_sv_chop_left.c @@ -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; +} \ No newline at end of file diff --git a/tests/nob_sv_chop_right.c b/tests/nob_sv_chop_right.c new file mode 100644 index 0000000..110ac27 --- /dev/null +++ b/tests/nob_sv_chop_right.c @@ -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; +} \ No newline at end of file