From c192d2c920c71fbc21490c716ccc5b1fca0cd698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 10 Jan 2020 12:02:48 +0100 Subject: [PATCH] [poincare/serialization_helper] Better ReplaceSysPrthesesByUsrPrtheses --- .../include/poincare/serialization_helper.h | 2 +- poincare/src/serialization_helper.cpp | 36 ++++++++++--------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/poincare/include/poincare/serialization_helper.h b/poincare/include/poincare/serialization_helper.h index 4a721dd5e..0721dc111 100644 --- a/poincare/include/poincare/serialization_helper.h +++ b/poincare/include/poincare/serialization_helper.h @@ -14,7 +14,7 @@ namespace Poincare { namespace SerializationHelper { - void ReplaceSystemParenthesesByUserParentheses(char * buffer); + void ReplaceSystemParenthesesByUserParentheses(char * buffer, int length = -1); // SerializableReference to text int Infix( diff --git a/poincare/src/serialization_helper.cpp b/poincare/src/serialization_helper.cpp index 6b08e0c6a..c8645b297 100644 --- a/poincare/src/serialization_helper.cpp +++ b/poincare/src/serialization_helper.cpp @@ -6,23 +6,27 @@ namespace Poincare { -void replaceOneCharSizedCodePointWith(char * buffer, CodePoint searchedCodePoint, CodePoint newCodePoint) { - assert(UTF8Decoder::CharSizeOfCodePoint(searchedCodePoint == 1)); - assert(UTF8Decoder::CharSizeOfCodePoint(newCodePoint == 1)); - UTF8Helper::PerformAtCodePoints( - buffer, - searchedCodePoint, - [](int codePointOffset, void * text, int newCodePoint, int bufferLength) { - *((char *)text+codePointOffset) = (char)newCodePoint; - }, - [](int c1, void * c2, int c3, int c4) {}, - (void *)buffer, - newCodePoint); -} +void SerializationHelper::ReplaceSystemParenthesesByUserParentheses(char * buffer, int length) { + assert(UTF8Decoder::CharSizeOfCodePoint(UCodePointLeftSystemParenthesis == 1)); + assert(UTF8Decoder::CharSizeOfCodePoint(UCodePointRightSystemParenthesis == 1)); + assert(UTF8Decoder::CharSizeOfCodePoint('(' == 1)); + assert(UTF8Decoder::CharSizeOfCodePoint(')' == 1)); + assert(length == -1 || length > 0); -void SerializationHelper::ReplaceSystemParenthesesByUserParentheses(char * buffer) { - replaceOneCharSizedCodePointWith(buffer, UCodePointLeftSystemParenthesis, '('); - replaceOneCharSizedCodePointWith(buffer, UCodePointRightSystemParenthesis, ')'); + int offset = 0; + char c = *(buffer + offset); + while (c != 0) { + if (c == UCodePointLeftSystemParenthesis) { + *(buffer + offset) = '('; + } else if (c == UCodePointRightSystemParenthesis) { + *(buffer + offset) = ')'; + } + offset++; + if (length >= 0 && offset > length - 1) { + break; + } + c = *(buffer + offset); + } } static bool checkBufferSize(char * buffer, int bufferSize, int * result) {