@ -3,78 +3,51 @@
# include <string>
# include <string>
# include <string_view>
# include <string_view>
# include <utility>
# include <utility>
# include <variant>
namespace devilution {
namespace devilution {
class StringOrView {
class StringOrView {
public :
public :
StringOrView ( )
StringOrView ( )
: owned_ ( false )
: rep_ { std : : string_view { } }
, view_ ( )
{
{
}
}
StringOrView ( StringOrView & & ) noexcept = default ;
StringOrView ( std : : string & & str )
StringOrView ( std : : string & & str )
: owned_ ( true )
: rep_ { std : : move ( str ) }
, str_ ( std : : move ( str ) )
{
{
}
}
StringOrView ( std : : string_view str )
StringOrView ( std : : string_view str )
: owned_ ( false )
: rep_ { str }
, view_ ( str )
{
{
}
}
StringOrView ( StringOrView & & other ) noexcept
StringOrView & operator = ( StringOrView & & ) noexcept = default ;
: owned_ ( other . owned_ )
{
if ( other . owned_ ) {
new ( & str_ ) std : : string ( std : : move ( other . str_ ) ) ;
} else {
new ( & view_ ) std : : string_view ( other . view_ ) ;
}
}
StringOrView & operator = ( StringOrView & & other ) noexcept
StringOrView & operator = ( std : : string & & value ) noexcept
{
{
if ( owned_ ) {
rep_ = std : : move ( value ) ;
if ( other . owned_ ) {
str_ = std : : move ( other . str_ ) ;
} else {
str_ . ~ basic_string ( ) ;
owned_ = false ;
new ( & view_ ) std : : string_view ( other . view_ ) ;
}
} else {
if ( other . owned_ ) {
view_ . ~ basic_string_view ( ) ;
owned_ = true ;
new ( & str_ ) std : : string ( std : : move ( other . str_ ) ) ;
} else {
view_ = other . view_ ;
}
}
return * this ;
return * this ;
}
}
~ StringOrView ( )
StringOrView & operator = ( std : : string_view value ) noexcept
{
{
if ( owned_ ) {
rep_ = value ;
str_ . ~ basic_string ( ) ;
return * this ;
} else {
view_ . ~ basic_string_view ( ) ;
}
}
}
bool empty ( ) const
bool empty ( ) const
{
{
return owned_ ? str_ . empty ( ) : view_ . empty ( ) ;
return std : : visit ( [ ] ( auto & & val ) - > bool { return val . empty ( ) ; } , rep_ ) ;
}
}
std : : string_view str ( ) const
std : : string_view str ( ) const
{
{
return owned_ ? str_ : view_ ;
return std : : visit ( [ ] ( auto & & val ) - > std : : string_view { return val ; } , rep_ ) ;
}
}
operator std : : string_view ( ) const
operator std : : string_view ( ) const
@ -83,11 +56,7 @@ public:
}
}
private :
private :
bool owned_ ;
std : : variant < std : : string , std : : string_view > rep_ ;
union {
std : : string str_ ;
std : : string_view view_ ;
} ;
} ;
} ;
} // namespace devilution
} // namespace devilution