Some FString optimizations

This commit is contained in:
Markus Gans 2021-06-03 16:08:36 +02:00
parent 7f9ba7464b
commit 5336462e68
5 changed files with 77 additions and 79 deletions

View File

@ -1,3 +1,6 @@
2021-06-03 Markus Gans <guru.mail@muenster.de>
* Some FString optimizations
2021-05-30 Markus Gans <guru.mail@muenster.de> 2021-05-30 Markus Gans <guru.mail@muenster.de>
* FString internally changed from wchar_t* to std::wstring * FString internally changed from wchar_t* to std::wstring

View File

@ -50,15 +50,13 @@ FString::FString (int len)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FString::FString (std::size_t len) FString::FString (std::size_t len)
{ : string{std::wstring(len, L'\0')}
string = std::wstring(len, L'\0'); { }
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FString::FString (std::size_t len, wchar_t c) FString::FString (std::size_t len, wchar_t c)
{ : string{std::wstring(len, c)}
string = std::wstring(len, c); { }
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FString::FString (std::size_t len, const UniChar& c) FString::FString (std::size_t len, const UniChar& c)
@ -69,7 +67,10 @@ FString::FString (std::size_t len, const UniChar& c)
FString::FString (const FString& s) // copy constructor FString::FString (const FString& s) // copy constructor
{ {
if ( ! s.isEmpty() ) if ( ! s.isEmpty() )
_assign (s.string); {
std::wstring copy(s.string);
_assign (copy);
}
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -83,14 +84,20 @@ FString::FString (FString&& s) noexcept // move constructor
FString::FString (const std::wstring& s) FString::FString (const std::wstring& s)
{ {
if ( ! s.empty() ) if ( ! s.empty() )
_assign (s); {
std::wstring str(s);
_assign (str);
}
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FString::FString (const wchar_t s[]) FString::FString (const wchar_t s[])
{ {
if ( s ) if ( s )
_assign (s); {
std::wstring str(s);
_assign (str);
}
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -98,7 +105,7 @@ FString::FString (const std::string& s)
{ {
if ( ! s.empty() ) if ( ! s.empty() )
{ {
const auto wide_string = _toWideString(s.c_str()); auto wide_string = _toWideString(s.c_str());
_assign(wide_string); _assign(wide_string);
} }
} }
@ -108,7 +115,7 @@ FString::FString (const char s[])
{ {
if ( s ) if ( s )
{ {
const auto wide_string = _toWideString(s); auto wide_string = _toWideString(s);
_assign(wide_string); _assign(wide_string);
} }
} }
@ -116,8 +123,8 @@ FString::FString (const char s[])
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FString::FString (const UniChar& c) FString::FString (const UniChar& c)
{ {
std::array<wchar_t, 2> s{{ static_cast<wchar_t>(c), L'\0' }}; std::wstring str{ static_cast<wchar_t>(c) };
_assign (s.data()); _assign (str);
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -125,8 +132,8 @@ FString::FString (const wchar_t c)
{ {
if ( c ) if ( c )
{ {
std::array<wchar_t, 2> s{{ c, L'\0' }}; std::wstring str{ c };
_assign (s.data()); _assign (str);
} }
} }
@ -135,14 +142,13 @@ FString::FString (const char c)
{ {
if ( c ) if ( c )
{ {
std::array<wchar_t, 2> s{{ wchar_t(c & 0xff), L'\0' }}; std::wstring str{ wchar_t(c & 0xff) };
_assign (s.data()); _assign (str);
} }
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FString::~FString() // destructor FString::~FString() = default; // destructor
{ }
// FString operators // FString operators
@ -150,7 +156,10 @@ FString::~FString() // destructor
FString& FString::operator = (const FString& s) FString& FString::operator = (const FString& s)
{ {
if ( &s != this ) if ( &s != this )
_assign (s.string); {
std::wstring str{s.string};
_assign (str);
}
return *this; return *this;
} }
@ -672,29 +681,30 @@ FString FString::mid (std::size_t pos, std::size_t len) const
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FStringList FString::split (const FString& delimiter) const FStringList FString::split (const FString& delimiter) const
{ {
if ( isEmpty() )
return {};
const FString s{*this}; const FString s{*this};
FStringList string_list{}; FStringList string_list{};
auto delimiter_length = delimiter.getLength();
std::wstring::size_type first = 0;
std::wstring::size_type last;
// Handle empty string while ( (last = s.string.find(delimiter.string, first)) != std::wstring::npos )
if ( isEmpty() )
return string_list;
wchar_t* rest{nullptr};
const wchar_t* token = _extractToken(&rest, s.string.c_str(), delimiter.wc_str());
while ( token )
{ {
string_list.emplace_back(token); string_list.emplace_back(std::wstring(s.string, first, last - first));
token = _extractToken (&rest, nullptr, delimiter.wc_str()); first = last + delimiter_length;
} }
string_list.emplace_back(std::wstring(s.string, first));
return string_list; return string_list;
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FString& FString::setString (const FString& s) FString& FString::setString (const FString& s)
{ {
_assign (s.string); std::wstring str{s.string};
_assign (str);
return *this; return *this;
} }
@ -720,7 +730,8 @@ FString& FString::setNumber (sInt64 num)
if ( num < 0 ) if ( num < 0 )
*--s = '-'; *--s = '-';
_assign (s); std::wstring str{s};
_assign (str);
return *this; return *this;
} }
@ -738,7 +749,8 @@ FString& FString::setNumber (uInt64 num)
} }
while ( num ); while ( num );
_assign (s); std::wstring str{s};
_assign (str);
return *this; return *this;
} }
@ -803,7 +815,8 @@ FString& FString::setFormatedNumber (sInt64 num, char separator)
if ( num < 0 ) if ( num < 0 )
*--s = '-'; *--s = '-';
_assign (s); std::wstring str{s};
_assign (str);
return *this; return *this;
} }
@ -829,7 +842,8 @@ FString& FString::setFormatedNumber (uInt64 num, char separator)
} }
while ( num ); while ( num );
_assign (s); std::wstring str{s};
_assign (str);
return *this; return *this;
} }
@ -938,24 +952,26 @@ FString FString::replaceControlCodes() const
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FString FString::expandTabs (int tabstop) const FString FString::expandTabs (int tabstop) const
{ {
FString instr{*this};
FString outstr{};
if ( tabstop <= 0 ) if ( tabstop <= 0 )
return instr; return *this;
const FStringList tab_split = instr.split("\t"); FString outstr{};
const uLong last = tab_split.size(); const auto tab_split = split(L"\t");
const auto last = tab_split.end() - 1;
auto iter = tab_split.begin();
for (std::size_t i{0}; i < last; i++) while ( iter != tab_split.end() )
{ {
const auto len = tab_split[i].getLength(); if ( iter != last )
{
const auto len = iter->getLength();
const auto tab_len = std::size_t(tabstop); const auto tab_len = std::size_t(tabstop);
outstr += *iter + std::wstring(tab_len - (len % tab_len), L' ');
if ( i == last - 1 ) }
outstr += tab_split[i];
else else
outstr += tab_split[i] + FString(tab_len - (len % tab_len), L' '); outstr += *iter;
++iter;
} }
return outstr; return outstr;
@ -1059,12 +1075,12 @@ bool FString::includes (const FString& s) const
// private methods of FString // private methods of FString
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FString::_assign (const std::wstring& s) inline void FString::_assign (std::wstring& s)
{ {
if ( string == s ) if ( string == s )
return; return;
string = s; s.swap(string);
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -1125,28 +1141,6 @@ inline std::wstring FString::_toWideString (const std::string& s) const
return {}; return {};
} }
//----------------------------------------------------------------------
inline const wchar_t* FString::_extractToken ( wchar_t* rest[]
, const wchar_t s[]
, const wchar_t delim[] ) const
{
wchar_t* token = s ? const_cast<wchar_t*>(s) : *rest;
if ( ! token )
return nullptr;
if ( ! token[0] )
return nullptr;
*rest = std::wcspbrk(token, delim);
if ( *rest )
*(*rest)++ = '\0';
else
*rest = token + std::wcslen(token);
return token;
}
// FString non-member operators // FString non-member operators
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -1179,7 +1173,7 @@ std::istream& operator >> (std::istream& instr, FString& s)
{ {
std::array<char, FString::INPBUFFER + 1> buf{}; std::array<char, FString::INPBUFFER + 1> buf{};
instr.getline (buf.data(), FString::INPBUFFER); instr.getline (buf.data(), FString::INPBUFFER);
const auto wide_string = s._toWideString(buf.data()); auto wide_string = s._toWideString(buf.data());
if ( ! wide_string.empty() ) if ( ! wide_string.empty() )
{ {
@ -1212,7 +1206,8 @@ std::wistream& operator >> (std::wistream& instr, FString& s)
{ {
std::array<wchar_t, FString::INPBUFFER + 1> buf{}; std::array<wchar_t, FString::INPBUFFER + 1> buf{};
instr.getline (buf.data(), FString::INPBUFFER); instr.getline (buf.data(), FString::INPBUFFER);
s._assign (buf.data()); std::wstring str(buf.data());
s._assign (str);
return instr; return instr;
} }

View File

@ -151,7 +151,7 @@ void FTermBuffer::write (const FColorPair& pair) const
// private methods of FTermBuffer // private methods of FTermBuffer
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FTermBuffer::add ( FString::const_iterator& begin void FTermBuffer::add ( FString::const_iterator& begin
, FString::const_iterator& end , const FString::const_iterator& end
, int& char_width ) , int& char_width )
{ {
if ( begin == end ) if ( begin == end )

View File

@ -249,10 +249,9 @@ class FString
static constexpr uInt INPBUFFER = 200; static constexpr uInt INPBUFFER = 200;
// Methods // Methods
void _assign (const std::wstring&); void _assign (std::wstring&);
std::string _toCharString (const std::wstring&) const; std::string _toCharString (const std::wstring&) const;
std::wstring _toWideString (const std::string&) const; std::wstring _toWideString (const std::string&) const;
const wchar_t* _extractToken (wchar_t*[], const wchar_t[], const wchar_t[]) const;
// Data members // Data members
std::wstring string{}; std::wstring string{};
@ -438,7 +437,8 @@ inline FString& FString::sprintf (const FString& format, Args&&... args)
std::swprintf ( buf.data(), buf.size(), format.wc_str() std::swprintf ( buf.data(), buf.size(), format.wc_str()
, std::forward<Args>(args)... ); , std::forward<Args>(args)... );
_assign(buf.data()); std::wstring s(buf.data());
_assign(s);
return *this; return *this;
} }

View File

@ -111,7 +111,7 @@ class FTermBuffer
private: private:
FCharVector data{}; FCharVector data{};
void add ( FString::const_iterator& void add ( FString::const_iterator&
, FString::const_iterator& , const FString::const_iterator&
, int& ); , int& );
// Non-member operators // Non-member operators