Skip to content

Commit

Permalink
Implemented SV's chop_by_space func
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Dec 14, 2024
1 parent 1a35ec2 commit a0c5766
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions carbon.h
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ CARBON_API CBN_StrView carbon_strview_trim_left(CBN_StrView sv);
CARBON_API CBN_StrView carbon_strview_trim_right(CBN_StrView sv);
CARBON_API CBN_StrView carbon_strview_trim_both(CBN_StrView sv);
CARBON_API CBN_StrView carbon_strview_chop(CBN_StrView *sv, char c);
CARBON_API CBN_StrView carbon_strview_chop_by_space(CBN_StrView *sv);
CARBON_API u8 carbon_strview_are_equal(CBN_StrView x, CBN_StrView y);

/*
Expand Down
15 changes: 15 additions & 0 deletions src/carbon_strview.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ CBN_StrView carbon_strview_chop(CBN_StrView *sv, char c) {
return new_sv;
}

CBN_StrView carbon_strview_chop_by_space(CBN_StrView *sv) {
usz i = 0;
while (i < sv->size && !isspace(sv->data[i])) ++i;
CBN_StrView new_sv = carbon_strview_from_buf(sv->data, i);
if (i < sv->size) {
sv->size -= i + 1;
sv->data += i + 1;
}
else {
sv->size -= i;
sv->data += i;
}
return new_sv;
}

u8 carbon_strview_are_equal(CBN_StrView x, CBN_StrView y) {
if (x.size != y.size) return false;
return 0 == memcmp(x.data, y.data, x.size);
Expand Down

0 comments on commit a0c5766

Please sign in to comment.