Skip to content

Commit

Permalink
fix: improve _z_string_compare
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Bushnev <[email protected]>
  • Loading branch information
jean-roland and sashacmc authored Dec 19, 2024
1 parent 116e10f commit 673ad15
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/collections/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,20 @@ void _z_string_free(_z_string_t **str) {
}

int _z_string_compare(const _z_string_t *left, const _z_string_t *right) {
if (_z_string_len(left) <= _z_string_len(right)) {
return strncmp(_z_string_data(left), _z_string_data(right), _z_string_len(left));
size_t len_left = _z_string_len(left);
size_t len_right = _z_string_len(right);

int result = strncmp(_z_string_data(left), _z_string_data(right), len_left < len_right ? len_left : len_right);

if (result == 0) {
if (len_left < len_right) {
return -1;
} else if (len_left > len_right) {
return 1;
}
}
return strncmp(_z_string_data(left), _z_string_data(right), _z_string_len(right));

return result;
}

bool _z_string_equals(const _z_string_t *left, const _z_string_t *right) {
Expand Down

0 comments on commit 673ad15

Please sign in to comment.