diff --git a/include/eepp/core/string.hpp b/include/eepp/core/string.hpp index a492aa88f..40c313807 100644 --- a/include/eepp/core/string.hpp +++ b/include/eepp/core/string.hpp @@ -415,6 +415,13 @@ class EE_API String { /** @return The next character in a utf8 null terminated string */ static Uint32 utf8Next( char*& utf8String ); + /** Converts an UTF-8 string view into an UTF-32 by using a currently allocated buffer (usefull + * for stack allocated buffers) + * @return The number of elements written into the buffer (the string length) + **/ + static size_t toUtf32( std::string_view utf8str, String::StringBaseType* buffer, + size_t bufferSize ); + /** glob matches a string against a glob ** @return True if matches */ diff --git a/src/eepp/core/string.cpp b/src/eepp/core/string.cpp index c43393c56..8ed3bc1a1 100644 --- a/src/eepp/core/string.cpp +++ b/src/eepp/core/string.cpp @@ -1452,7 +1452,7 @@ String String::fromUtf8( const std::string_view& utf8String ) { return String( utf32 ); } -#define iscont( p ) ( ( *( p ) & 0xC0 ) == 0x80 ) +#define iscont( p ) ( ( *(p)&0xC0 ) == 0x80 ) static inline const char* utf8_next( const char* s, const char* e ) { while ( s < e && iscont( s + 1 ) ) @@ -1964,4 +1964,14 @@ bool String::isWholeWord( const String& haystack, const String& needle, const In !( isAlphaNum( haystack[startPos + needle.size()] ) ) ); } +size_t String::toUtf32( std::string_view utf8str, String::StringBaseType* buffer, + size_t bufferSize ) { + auto start = utf8str.data(); + auto end = start + utf8str.size(); + size_t pos = 0; + while ( start < end && pos < bufferSize ) + buffer[pos++] = utf8::unchecked::next( start ); + return pos; +} + } // namespace EE diff --git a/src/tools/ecode/plugins/lsp/lspclientplugin.cpp b/src/tools/ecode/plugins/lsp/lspclientplugin.cpp index 6877f6adc..0c8eeeff4 100644 --- a/src/tools/ecode/plugins/lsp/lspclientplugin.cpp +++ b/src/tools/ecode/plugins/lsp/lspclientplugin.cpp @@ -1908,8 +1908,8 @@ void LSPClientPlugin::drawTop( UICodeEditor* editor, const Vector2f& screenStart // Avoid heap allocating static constexpr auto MAX_PATH_SIZE = 512; - String::StringBaseType filePath[MAX_PATH_SIZE]; - std::memset( filePath, 0, MAX_PATH_SIZE * sizeof( String::StringBaseType ) ); + String::StringBaseType utf32Path[MAX_PATH_SIZE]; + std::memset( utf32Path, 0, MAX_PATH_SIZE * sizeof( String::StringBaseType ) ); const auto& workspace = getManager()->getWorkspaceFolder(); std::string_view path( editor->getDocument().getFilePath() ); @@ -1918,10 +1918,7 @@ void LSPClientPlugin::drawTop( UICodeEditor* editor, const Vector2f& screenStart if ( path.size() > MAX_PATH_SIZE ) path = path.substr( path.size() - MAX_PATH_SIZE ); - size_t pp = 0; - const char* c = path.data(); - while ( *c != '\0' && pp < MAX_PATH_SIZE ) - filePath[pp++] = utf8::unchecked::next( c ); + size_t pathLen = String::toUtf32( path, &utf32Path[0], MAX_PATH_SIZE ); Color textColor( editor->getColorScheme().getEditorColor( mHoveringBreadcrumb == editor ? SyntaxStyleTypes::Text : SyntaxStyleTypes::LineNumber2 ) ); @@ -1930,7 +1927,7 @@ void LSPClientPlugin::drawTop( UICodeEditor* editor, const Vector2f& screenStart Vector2f pos( screenStart.x + eefloor( PixelDensity::dpToPx( 8 ) ), screenStart.y + textOffsetY ); - auto drawn = Text::draw( String::View( filePath ), pos, font, fontSize, textColor ); + auto drawn = Text::draw( String::View( utf32Path, pathLen ), pos, font, fontSize, textColor ); Lock l( mDocCurrentSymbolsMutex ); auto symbolsInfoIt = mDocCurrentSymbols.find( editor->getDocument().getURI() );