Skip to content

Commit

Permalink
Small clean up, add String::toUtf32.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpartanJ committed Nov 22, 2024
1 parent 3001723 commit ce1e87e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
7 changes: 7 additions & 0 deletions include/eepp/core/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
12 changes: 11 additions & 1 deletion src/eepp/core/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )
Expand Down Expand Up @@ -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
11 changes: 4 additions & 7 deletions src/tools/ecode/plugins/lsp/lspclientplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand All @@ -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 ) );
Expand All @@ -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() );
Expand Down

0 comments on commit ce1e87e

Please sign in to comment.