From b39b72aee7f17db433809d49419019cce068b408 Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Thu, 26 Oct 2023 22:11:34 +0000 Subject: [PATCH] lvstring: fix `size` handling (#538) The chunk size is the allocated size minus one (the terminating \0). Fix methods that don't have the same definition. Details at https://github.com/koreader/crengine/pull/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 --- crengine/include/lvstring.h | 8 ++------ crengine/src/lvstring.cpp | 40 ++++++++++++++++++------------------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/crengine/include/lvstring.h b/crengine/include/lvstring.h index 29c96360a..3b8d70931 100644 --- a/crengine/include/lvstring.h +++ b/crengine/include/lvstring.h @@ -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 @@ -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 diff --git a/crengine/src/lvstring.cpp b/crengine/src/lvstring.cpp index d0429b3fc..853204814 100644 --- a/crengine/src/lvstring.cpp +++ b/crengine/src/lvstring.cpp @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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; @@ -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) { @@ -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 @@ -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 @@ -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 @@ -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; @@ -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) {