Skip to content

Commit

Permalink
fix empty string bug
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Dec 25, 2014
1 parent 22b6a19 commit 45cda39
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/tbox/memory/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ tb_byte_t* tb_buffer_data(tb_buffer_t* buffer)
tb_assert_and_check_return_val(buffer, tb_null);

// the buffer data
return buffer->data;
return buffer->size? buffer->data : tb_null;
}
tb_size_t tb_buffer_size(tb_buffer_t const* buffer)
{
Expand Down Expand Up @@ -185,7 +185,7 @@ tb_byte_t* tb_buffer_resize(tb_buffer_t* buffer, tb_size_t size)
} while (0);

// trace
if (!ok) tb_trace_e("resize buffer failed: %lu => %lu", buff_size, size);
tb_assertf(ok, "resize buffer failed: %lu => %lu", buff_size, size);

// ok
return ok? (tb_byte_t*)buffer->data : tb_null;
Expand Down
10 changes: 8 additions & 2 deletions src/tbox/string/static_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ tb_bool_t tb_static_string_init(tb_static_string_t* string, tb_char_t* data, tb_
tb_assert_and_check_return_val(string, tb_false);

// init
return tb_static_buffer_init(string, (tb_byte_t*)data, maxn);
tb_bool_t ok = tb_static_buffer_init(string, (tb_byte_t*)data, maxn);

// clear it
tb_static_string_clear(string);

// ok?
return ok;
}
tb_void_t tb_static_string_exit(tb_static_string_t* string)
{
Expand All @@ -60,7 +66,7 @@ tb_char_t const* tb_static_string_cstr(tb_static_string_t const* string)
tb_assert_and_check_return_val(string, tb_null);

// the cstr
return (tb_char_t const*)tb_static_buffer_data((tb_static_buffer_t*)string);
return tb_static_buffer_size(string)? (tb_char_t const*)tb_static_buffer_data((tb_static_buffer_t*)string) : tb_null;
}
tb_size_t tb_static_string_size(tb_static_string_t const* string)
{
Expand Down
10 changes: 8 additions & 2 deletions src/tbox/string/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ tb_bool_t tb_string_init(tb_string_t* string)
tb_assert_and_check_return_val(string, tb_false);

// init
return tb_buffer_init(string);
tb_bool_t ok = tb_buffer_init(string);

// clear it
tb_string_clear(string);

// ok?
return ok;
}
tb_void_t tb_string_exit(tb_string_t* string)
{
Expand All @@ -60,7 +66,7 @@ tb_char_t const* tb_string_cstr(tb_string_t const* string)
tb_assert_and_check_return_val(string, tb_null);

// the cstr
return (tb_char_t const*)tb_buffer_data((tb_buffer_t*)string);
return tb_buffer_size(string)? (tb_char_t const*)tb_buffer_data((tb_buffer_t*)string) : tb_null;
}
tb_size_t tb_string_size(tb_string_t const* string)
{
Expand Down

0 comments on commit 45cda39

Please sign in to comment.