Skip to content

Commit

Permalink
lvstring: fix size handling (#538)
Browse files Browse the repository at this point in the history
The chunk size is the allocated size minus one (the terminating \0).
Fix methods that don't have the same definition.
Details at #538:
- lvstring: drop `size` method and fix `capacity`
  - `size` is unnecessary and confusing (same as `length`)
  - `capacity` return value was wrong (off by one)
- lvstring: fix `assign` implementations
  The chunk `size` is the allocated size minus one (the terminating `\0`).
- lvstring: fix `resize` implementations
  The chunk `size` is the allocated size minus one (the terminating `\0`).
- lvstring: fix `pack` implementations
  • Loading branch information
benoit-pierre authored Oct 26, 2023
1 parent 2312401 commit b39b72a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
8 changes: 2 additions & 6 deletions crengine/include/lvstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -577,12 +577,10 @@ class lString8
void reset( size_type size );
/// returns character count
size_type length() const { return pchunk->len; }
/// returns buffer size
size_type size() const { return pchunk->len; }
/// changes buffer size
void resize(size_type count = 0, value_type e = 0);
/// returns maximum number of chars that can fit into buffer
size_type capacity() const { return pchunk->size-1; }
size_type capacity() const { return pchunk->size; }
/// reserve space for specified amount of chars
void reserve(size_type count = 0);
/// returns true if string is empty
Expand Down Expand Up @@ -839,12 +837,10 @@ class lString32
void reset( size_type size );
/// returns string length, in characters
size_type length() const { return pchunk->len; }
/// returns string length, in characters
size_type size() const { return pchunk->len; }
/// resizes string buffer, appends with specified character if buffer is being extended
void resize(size_type count = 0, value_type e = 0);
/// returns string buffer size
size_type capacity() const { return pchunk->size-1; }
size_type capacity() const { return pchunk->size; }
/// ensures string buffer can hold at least count characters
void reserve(size_type count = 0);
/// erase all extra characters from end of string after size
Expand Down
40 changes: 20 additions & 20 deletions crengine/src/lvstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,11 @@ lString32 & lString32::assign(const lChar32 * str)
size_type len = _lStr_len(str);
if (pchunk->nref==1)
{
if (pchunk->size<=len)
if (pchunk->size < len)
{
// resize is necessary
pchunk->buf32 = (lChar32*) ::realloc( pchunk->buf32, sizeof(lChar32)*(len+1) );
pchunk->size = len+1;
pchunk->size = len;
}
}
else
Expand All @@ -655,11 +655,11 @@ lString32 & lString32::assign(const lChar8 * str)
size_type len = _lStr_len(str);
if (pchunk->nref==1)
{
if (pchunk->size<=len)
if (pchunk->size < len)
{
// resize is necessary
pchunk->buf32 = (lChar32*) ::realloc( pchunk->buf32, sizeof(lChar32)*(len+1) );
pchunk->size = len+1;
pchunk->size = len;
}
}
else
Expand All @@ -684,11 +684,11 @@ lString32 & lString32::assign(const lChar32 * str, size_type count)
size_type len = _lStr_nlen(str, count);
if (pchunk->nref==1)
{
if (pchunk->size<=len)
if (pchunk->size < len)
{
// resize is necessary
pchunk->buf32 = (lChar32*) ::realloc( pchunk->buf32, sizeof(lChar32)*(len+1) );
pchunk->size = len+1;
pchunk->size = len;
}
}
else
Expand All @@ -713,11 +713,11 @@ lString32 & lString32::assign(const lChar8 * str, size_type count)
size_type len = _lStr_nlen(str, count);
if (pchunk->nref==1)
{
if (pchunk->size<=len)
if (pchunk->size < len)
{
// resize is necessary
pchunk->buf32 = (lChar32*) ::realloc( pchunk->buf32, sizeof(lChar32)*(len+1) );
pchunk->size = len+1;
pchunk->size = len;
}
}
else
Expand Down Expand Up @@ -758,11 +758,11 @@ lString32 & lString32::assign(const lString32 & str, size_type offset, size_type
{
if (pchunk->nref==1)
{
if (pchunk->size<=count)
if (pchunk->size < count)
{
// resize is necessary
pchunk->buf32 = (lChar32*) ::realloc( pchunk->buf32, sizeof(lChar32)*(count+1) );
pchunk->size = count+1;
pchunk->size = count;
}
}
else
Expand Down Expand Up @@ -858,7 +858,7 @@ void lString32::reset( size_type size )
void lString32::resize(size_type n, lChar32 e)
{
lock( n );
if (n>=pchunk->size)
if (pchunk->size < n)
{
pchunk->buf32 = (lChar32*) ::realloc( pchunk->buf32, sizeof(lChar32)*(n+1) );
pchunk->size = n;
Expand Down Expand Up @@ -973,7 +973,7 @@ lString32 lString32::substr(size_type pos, size_type n) const

lString32 & lString32::pack()
{
if (pchunk->len + 4 < pchunk->size )
if (pchunk->len < pchunk->size)
{
if (pchunk->nref>1)
{
Expand Down Expand Up @@ -1784,11 +1784,11 @@ lString8 & lString8::assign(const lChar8 * str)
size_type len = _lStr_len(str);
if (pchunk->nref==1)
{
if (pchunk->size<=len)
if (pchunk->size < len)
{
// resize is necessary
pchunk->buf8 = (lChar8*) ::realloc( pchunk->buf8, sizeof(lChar8)*(len+1) );
pchunk->size = len+1;
pchunk->size = len;
}
}
else
Expand All @@ -1813,11 +1813,11 @@ lString8 & lString8::assign(const lChar8 * str, size_type count)
size_type len = _lStr_nlen(str, count);
if (pchunk->nref==1)
{
if (pchunk->size<=len)
if (pchunk->size < len)
{
// resize is necessary
pchunk->buf8 = (lChar8*) ::realloc( pchunk->buf8, sizeof(lChar8)*(len+1) );
pchunk->size = len+1;
pchunk->size = len;
}
}
else
Expand Down Expand Up @@ -1858,11 +1858,11 @@ lString8 & lString8::assign(const lString8 & str, size_type offset, size_type co
{
if (pchunk->nref==1)
{
if (pchunk->size<=count)
if (pchunk->size < count)
{
// resize is necessary
pchunk->buf8 = (lChar8*) ::realloc( pchunk->buf8, sizeof(lChar8)*(count+1) );
pchunk->size = count+1;
pchunk->size = count;
}
}
else
Expand Down Expand Up @@ -1958,7 +1958,7 @@ void lString8::reset( size_type size )
void lString8::resize(size_type n, lChar8 e)
{
lock( n );
if (n>=pchunk->size)
if (pchunk->size < n)
{
pchunk->buf8 = (lChar8*) ::realloc( pchunk->buf8, sizeof(lChar8)*(n+1) );
pchunk->size = n;
Expand Down Expand Up @@ -2448,7 +2448,7 @@ int lString32::pos(lString32 subStr) const

lString8 & lString8::pack()
{
if (pchunk->len + 4 < pchunk->size )
if (pchunk->len < pchunk->size)
{
if (pchunk->nref>1)
{
Expand Down

0 comments on commit b39b72a

Please sign in to comment.