Trim address entry field.

Sometimes a stray whitespace may be inserted in the address field, for
example when copying and pasting from somewhere else.
This commit is contained in:
Bartosz Taudul 2024-09-09 20:41:28 +02:00
parent 5795bc5766
commit d46eebf794
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3
3 changed files with 25 additions and 17 deletions

View File

@ -65,17 +65,16 @@ void ConnectionHistory::Rebuild()
std::swap( m_connHistVec, vec );
}
void ConnectionHistory::Count( const char* name )
void ConnectionHistory::Count( const std::string& name )
{
std::string addr( name );
auto it = m_connHistMap.find( addr );
auto it = m_connHistMap.find( name );
if( it != m_connHistMap.end() )
{
it->second++;
}
else
{
m_connHistMap.emplace( std::move( addr ), 1 );
m_connHistMap.emplace( name, 1 );
}
Rebuild();
}

View File

@ -14,7 +14,7 @@ public:
const std::string& Name( size_t idx ) const { return m_connHistVec[idx]->first; }
void Count( const char* name );
void Count( const std::string& name );
void Erase( size_t idx );
bool empty() const { return m_connHistVec.empty(); }

View File

@ -967,20 +967,29 @@ static void DrawContents()
connectClicked |= ImGui::Button( ICON_FA_WIFI " Connect" );
if( connectClicked && *addr && !loadThread.joinable() )
{
connHist->Count( addr );
auto aptr = addr;
while( *aptr == ' ' || *aptr == '\t' ) aptr++;
auto aend = aptr;
while( *aend && *aend != ' ' && *aend != '\t' ) aend++;
const auto addrLen = strlen( addr );
auto ptr = addr + addrLen - 1;
while( ptr > addr && *ptr != ':' ) ptr--;
if( *ptr == ':' )
if( aptr != aend )
{
std::string addrPart = std::string( addr, ptr );
uint16_t portPart = (uint16_t)atoi( ptr+1 );
view = std::make_unique<tracy::View>( RunOnMainThread, addrPart.c_str(), portPart, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, SetupScaleCallback, AttentionCallback, s_config, s_achievements );
}
else
{
view = std::make_unique<tracy::View>( RunOnMainThread, addr, port, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, SetupScaleCallback, AttentionCallback, s_config, s_achievements );
std::string address( aptr, aend );
connHist->Count( address );
auto adata = address.data();
auto ptr = adata + address.size() - 1;
while( ptr > adata && *ptr != ':' ) ptr--;
if( *ptr == ':' )
{
std::string addrPart = std::string( adata, ptr );
uint16_t portPart = (uint16_t)atoi( ptr+1 );
view = std::make_unique<tracy::View>( RunOnMainThread, addrPart.c_str(), portPart, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, SetupScaleCallback, AttentionCallback, s_config, s_achievements );
}
else
{
view = std::make_unique<tracy::View>( RunOnMainThread, address.c_str(), port, s_fixedWidth, s_smallFont, s_bigFont, SetWindowTitleCallback, SetupScaleCallback, AttentionCallback, s_config, s_achievements );
}
}
}
if( s_config.memoryLimit )