diff options
author | Lars Wirzenius <liw@liw.fi> | 2010-10-24 22:59:13 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2010-10-24 22:59:13 +0100 |
commit | 05fd0b47a1d97c453676d7d92716eee8a5eb72a1 (patch) | |
tree | 59019c3eb29ac14c3381e9cfc44fe5b9ced9b933 | |
parent | f02f6787f36808663b17091d530bd91391a0b390 (diff) | |
download | dynstr-05fd0b47a1d97c453676d7d92716eee8a5eb72a1.tar.gz |
Implement dynstr_last_byte.
-rw-r--r-- | dynstr.c | 13 | ||||
-rw-r--r-- | unittests.c | 70 |
2 files changed, 83 insertions, 0 deletions
@@ -218,3 +218,16 @@ size_t dynstr_first_byte(Dynstr *dynstr, size_t offset, int byte) return (size_t) (p - dynstr->mem); } + +size_t dynstr_last_byte(Dynstr *dynstr, size_t offset, int byte) +{ + unsigned char *p; + + if (offset >= dynstr->size) + return DYNSTR_NOT_FOUND; + p = memrchr(dynstr->mem + offset, byte, dynstr->size - offset); + if (p == NULL) + return DYNSTR_NOT_FOUND; + return (size_t) (p - dynstr->mem); +} + diff --git a/unittests.c b/unittests.c index 2e2aa78..24935ad 100644 --- a/unittests.c +++ b/unittests.c @@ -616,6 +616,71 @@ static int test_first_byte_only_finds_byte_inside_range(void) } +static int test_last_byte_finds_byte(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = dynstr_new_from_cstring("123456"); + offset = dynstr_last_byte(dynstr, 0, '3'); + FAIL_UNLESS_EQUAL(offset, 2); + dynstr_free(dynstr); + return true; +} + + +static int test_last_byte_only_finds_last_byte(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = dynstr_new_from_cstring("123123"); + offset = dynstr_last_byte(dynstr, 0, '3'); + FAIL_UNLESS_EQUAL(offset, 5); + dynstr_free(dynstr); + return true; +} + + +static int test_last_byte_does_not_find_nonexistent_byte(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = dynstr_new_from_cstring("123456"); + offset = dynstr_last_byte(dynstr, 0, 'x'); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + dynstr_free(dynstr); + return true; +} + + +static int test_last_byte_does_not_find_byte_outside_range(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = dynstr_new_from_cstring("123456"); + offset = dynstr_last_byte(dynstr, 4, '3'); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + dynstr_free(dynstr); + return true; +} + + +static int test_last_byte_only_finds_byte_inside_range(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = dynstr_new_from_cstring("123123"); + offset = dynstr_last_byte(dynstr, 3, '3'); + FAIL_UNLESS_EQUAL(offset, 5); + dynstr_free(dynstr); + return true; +} + + static void setup(void) { dynstr_init(); @@ -676,6 +741,11 @@ static const struct test tests[] = { TEST(test_first_byte_does_not_find_nonexistent_byte), TEST(test_first_byte_does_not_find_byte_outside_range), TEST(test_first_byte_only_finds_byte_inside_range), + TEST(test_last_byte_finds_byte), + TEST(test_last_byte_only_finds_last_byte), + TEST(test_last_byte_does_not_find_nonexistent_byte), + TEST(test_last_byte_does_not_find_byte_outside_range), + TEST(test_last_byte_only_finds_byte_inside_range), }; static const int num_tests = sizeof(tests) / sizeof(tests[0]); |