Skip to content

Commit

Permalink
Check for end of string properly when length given
Browse files Browse the repository at this point in the history
  • Loading branch information
charmander committed Dec 7, 2015
1 parent 7bf707f commit c112c4e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions shoco.c
Expand Up @@ -92,7 +92,7 @@ size_t shoco_compress(const char * const shoco_restrict original, size_t strlen,
const char * const in_end = original + strlen;

while ((*in != '\0')) {
if (strlen && (in > in_end))
if (strlen && (in == in_end))
break;

// find the longest string of known successors
Expand All @@ -103,7 +103,7 @@ size_t shoco_compress(const char * const shoco_restrict original, size_t strlen,

rest = in_end - in;
for (n_consecutive = 1; n_consecutive <= MAX_SUCCESSOR_N; ++n_consecutive) {
if (strlen && (n_consecutive > rest))
if (strlen && (n_consecutive == rest))
break;

current_index = chr_ids_by_chr[(unsigned char)in[n_consecutive]];
Expand Down
4 changes: 4 additions & 0 deletions tests.c
Expand Up @@ -41,6 +41,10 @@ int main() {
assert(ret == 1);
assert(buf_4[1] == 'x');

ret = shoco_compress("t\x80", 1, buf_4, 4);
assert(ret == 1);
assert(buf_4[1] == 'x');

buf_4[1] = 'y';
ret = shoco_compress("test", 1, buf_4, 1);
assert(ret == 1);
Expand Down

0 comments on commit c112c4e

Please sign in to comment.