[unicode] Fix stop condition of CopyAndRemoveCodePoint

This commit is contained in:
Léa Saviot
2019-01-29 11:05:55 +01:00
committed by Émilie Feral
parent 9dd6b41b39
commit 12e1f6be58
2 changed files with 7 additions and 3 deletions

View File

@@ -14,7 +14,8 @@ int CountOccurrences(const char * s, CodePoint c);
const char * CodePointSearch(const char * s, CodePoint c);
/* Copy src into dst while removing all code points c. Also update an index
* that should be lower if code points where removed before it. */
* that should be lower if code points where removed before it. Ensure null-
* termination of dst. */
void CopyAndRemoveCodePoint(char * dst, size_t dstSize, const char * src, CodePoint c, const char * * indexToDUpdate = nullptr);
/* Copy src into dst until end of dst or code point c, with null termination. Return the length of the copy */

View File

@@ -55,16 +55,18 @@ const char * CodePointSearch(const char * s, CodePoint c) {
}
void CopyAndRemoveCodePoint(char * dst, size_t dstSize, const char * src, CodePoint c, const char * * pointerToUpdate) {
if (dstSize <= 0) {
return;
}
UTF8Decoder decoder(src);
const char * currentPointer = src;
const char * maxPointer = src + strlen(src) + 1;
CodePoint codePoint = decoder.nextCodePoint();
const char * nextPointer = decoder.stringPosition();
size_t bufferIndex = 0;
size_t codePointCharSize = UTF8Decoder::CharSizeOfCodePoint(c);
// Remove CodePoint c
while (currentPointer < maxPointer && bufferIndex < dstSize) {
while (codePoint != UCodePointNull && bufferIndex < dstSize) {
if (codePoint != c) {
int copySize = minInt(nextPointer - currentPointer, dstSize - bufferIndex);
memcpy(dst + bufferIndex, currentPointer, copySize);
@@ -77,6 +79,7 @@ void CopyAndRemoveCodePoint(char * dst, size_t dstSize, const char * src, CodePo
codePoint = decoder.nextCodePoint();
nextPointer = decoder.stringPosition();
}
*(dst + minInt(bufferIndex, dstSize - 1)) = 0;
}
size_t CopyUntilCodePoint(char * dst, size_t dstSize, const char * src, CodePoint c) {