📄 ycpp_string.hpp
字号:
template< typename CharT, typename Traits, typename Allocator >
inline bool operator<=( const basic_string<CharT, Traits, Allocator>& lhs,
const basic_string<CharT, Traits, Allocator>& rhs )
{
return lhs.compare( rhs ) <= 0;
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator<=( const basic_string<CharT, Traits, Allocator>& lhs,
const CharT* rhs )
{
return lhs.compare( rhs ) <= 0;
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator<=( const CharT* lhs,
const basic_string<CharT, Traits, Allocator>& rhs )
{
return rhs.compare( lhs ) >= 0;
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator>( const basic_string<CharT, Traits, Allocator>& lhs,
const basic_string<CharT, Traits, Allocator>& rhs )
{
return lhs.compare( rhs ) > 0;
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator>( const basic_string<CharT, Traits, Allocator>& lhs,
const CharT* rhs )
{
return lhs.compare( rhs ) > 0;
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator>( const CharT* lhs,
const basic_string<CharT, Traits, Allocator>& rhs )
{
return rhs.compare( lhs ) < 0;
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator>=( const basic_string<CharT, Traits, Allocator>& lhs,
const basic_string<CharT, Traits, Allocator>& rhs )
{
return lhs.compare( rhs ) >= 0;
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator>=( const basic_string<CharT, Traits, Allocator>& lhs,
const CharT* rhs )
{
return lhs.compare( rhs ) >= 0;
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator>=( const CharT* lhs,
const basic_string<CharT, Traits, Allocator>& rhs )
{
return rhs.compare( lhs ) <= 0;
}
template< typename CharT, typename Traits, typename Allocator >
inline std::basic_ostream<CharT>&
operator<<( std::basic_ostream<CharT>& os,
const basic_string<CharT, Traits, Allocator>& str )
{
return os.write( str.data(), static_cast<std::streamsize>(str.size()) );
}
template< typename CharT, typename Traits, typename Allocator >
std::basic_istream<CharT>&
operator>>( std::basic_istream<CharT>& is,
basic_string<CharT, Traits, Allocator>& str )
{
typedef std::ctype<CharT> ctype_t;
typedef std::basic_istream<CharT> istream_type;
typedef std::basic_streambuf<CharT> streambuf_type;
typedef basic_string<CharT, Traits, Allocator> string_type;
typedef typename istream_type::int_type int_type;
typedef typename string_type::size_type size_type;
size_type extracted = 0;
typename istream_type::sentry cerb( is, false );
if( cerb )
{
str.clear();
std::streamsize w = is.width(); //取得输入流的域宽
size_type n = w > 0 ? (size_type)w : str.max_size();
const ctype_t& ct = std::use_facet<ctype_t>( is.getloc() ); //本地化
const int_type eof = Traits::eof(); //字符串结束符
streambuf_type* buf = is.rdbuf();
int_type c = buf->sgetc(); //取当前字符
while( extracted < n && !Traits::eq_int_type( c, eof )
&& !ct.is( std::ctype_base::space, static_cast<CharT>(c) ) )
{
str += Traits::to_char_type( c );
++extracted;
c = buf->snextc(); //跳过当前字符,取下一个字符
}
if( Traits::eq_int_type(c, eof) )
is.setstate( std::ios_base::eofbit ); //设置IO状态,读取结束
is.width( 0 );
}
if( extracted == 0 )
is.setstate( std::ios_base::failbit ); //设置IO状态,读取操作失败
return is;
}
template< typename CharT, typename Traits, typename Allocator >
std::basic_istream<CharT>&
getline( std::basic_istream<CharT>& is,
basic_string<CharT, Traits, Allocator>& str,
CharT delim = '\n' )
{
typedef std::ctype<CharT> ctype_t;
typedef std::basic_istream<CharT> istream_type;
typedef std::basic_streambuf<CharT> streambuf_type;
typedef basic_string<CharT, Traits, Allocator> string_type;
typedef typename istream_type::int_type int_type;
typedef typename string_type::size_type size_type;
size_type extracted = 0;
bool testdelim = false;
typename istream_type::sentry cerb( is, true );
if( cerb )
{
str.clear(); //将字符串清空
const int_type eof = Traits::eof(); //字符串结束符
size_type n = str.max_size();
streambuf_type* buf = is.rdbuf();
int_type c = buf->sbumpc(); //将buf->gptr()推进1,即已填入的下一个字符
int_type idelim = Traits::to_int_type( delim );
testdelim = Traits::eq_int_type( c, idelim );
while( extracted <= n && !Traits::eq_int_type(c, eof) && !testdelim )
{
str += Traits::to_char_type( c );
++extracted;
c = buf->sbumpc();
testdelim = Traits::eq_int_type( c, idelim ); //下一个字符是否是结束符
}
if( Traits::eq_int_type(c, eof) )
is.setstate( std::ios_base::eofbit );
is.width( 0 );
}
if( extracted == 0 && !testdelim )
is.setstate( std::ios_base::failbit );
return is;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//shared_string
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
class shared_string;
template< typename CharT, typename Traits, typename Allocator >
std::basic_istream<CharT>&
operator>>( std::basic_istream<CharT>& is,
shared_string<CharT, Traits, Allocator>& str );
template< typename CharT, typename Traits, typename Allocator >
std::basic_istream<CharT>&
getline( std::basic_istream<CharT>& is,
shared_string<CharT, Traits, Allocator>& str,
CharT delim );
template< typename CharT, typename Traits = std::char_traits<CharT>,
typename Allocator = std::allocator<CharT> >
class shared_string
{
public:
typedef shared_string<CharT, Traits, Allocator> self;
typedef Traits traits_type;
typedef Allocator allocator_type;
typedef CharT value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef value_type* iterator;
typedef const value_type* const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
friend std::basic_istream<CharT>&
operator>> <> ( std::basic_istream<CharT>& is,
shared_string<CharT, Traits, Allocator>& str );
friend std::basic_istream<CharT>&
getline <> ( std::basic_istream<CharT>& is,
shared_string<CharT, Traits, Allocator>& str,
CharT delim );
static const size_type npos = SIZE_MAX;
shared_string() : m_pstr( alloc_str() ) {}
shared_string( const CharT* str ) : m_pstr( alloc_str() )
{
m_pstr->memstr.replace( size_type(0), size_type(0), str,
Traits::length(str) );
}
shared_string( const CharT* str, size_type n ) : m_pstr( alloc_str() )
{
m_pstr->memstr.replace( size_type(0), size_type(0), str, n );
}
shared_string( size_type n, CharT c ) : m_pstr( alloc_str() )
{
m_pstr->memstr.replace( size_type(0), size_type(0), n, c );
}
shared_string( int n, CharT c ) : m_pstr( alloc_str() )
{
m_pstr->memstr.replace( size_type(0), size_type(0), (size_type)n, c );
}
shared_string( long n, CharT c ) : m_pstr( alloc_str() )
{
m_pstr->memstr.replace( size_type(0), size_type(0), (size_type)n, c );
}
template< typename InputIterator >
shared_string( InputIterator first, InputIterator last )
: m_pstr( alloc_str() )
{
m_pstr->memstr.replace( begin(), end(), first, last );
}
shared_string( const self& rhs, size_type index = 0, size_type n = npos )
{
if( index > rhs.size() )
throw std::out_of_range( "young::shared_string::copy_constructor out of range!" );
if( rhs.m_pstr->ref != unshareable && index == 0 && n >= rhs.size() )
{
m_pstr = rhs.m_pstr;
++( m_pstr->ref );
}
else
{
m_pstr = alloc_str();
m_pstr->memstr.replace( size_type(0), size_type(0),
rhs.begin() + index,
std::min( rhs.size() - index, n) );
}
}
self& operator=( const self& rhs )
{
if( &rhs != this )
{
if( (m_pstr->ref == unshareable || rhs.m_pstr->ref == unshareable)
|| (m_pstr->ref == 1 && capacity() >= rhs.size()) )
replace( size_type(0), size(), rhs.data(), rhs.size() );
else
{
release();
m_pstr = rhs.m_pstr;
++( m_pstr->ref );
}
}
return *this;
}
shared_string( const std::basic_string<CharT, Traits, Allocator>& rhs )
: m_pstr( alloc_str() )
{
m_pstr->memstr.replace( begin(), end(), rhs.begin(), rhs.end() );
}
self& operator=( const std::basic_string<CharT, Traits, Allocator>& rhs )
{
if( m_pstr->ref != unshareable && m_pstr->ref > 1 )
release();
replace( begin(), end(), rhs.begin(), rhs.end() );
return *this;
}
#ifndef __BORLANDC__
shared_string( const basic_string<CharT, Traits, Allocator>& rhs )
: m_pstr( alloc_str() )
{
m_pstr->memstr.replace( begin(), end(), rhs.begin(), rhs.end() );
}
self& operator=( const basic_string<CharT, Traits, Allocator>& rhs )
{
if( m_pstr->ref != unshareable && m_pstr->ref > 1 )
release();
replace( begin(), end(), rhs.begin(), rhs.end() );
return *this;
}
#endif
~shared_string() { release(); }
const CharT* data() const { return m_pstr->memstr.data(); }
const CharT* c_str() const { return m_pstr->memstr.c_str(); }
self substr( size_type index = 0, size_type n = npos ) const
{ return self( *this, index, n ); }
self& operator=( CharT c )
{ return replace( size_type(0), npos, size_type(1), c ); }
self& operator=( const CharT* str )
{ return replace( size_type(0), size_type(0), str,
Traits::length(str) ); }
self& operator+=( CharT c )
{ push_back(c); return *this; }
self& operator+=( const CharT* str )
{ return replace( size(), size_type(0), str, Traits::length(str) ); }
self& operator+=( const self& rhs )
{ return replace( size(), size_type(0), rhs.data(), rhs.size() ); }
void pop_back() { copy_str( 0, size() - 1, true ); }
void push_back( const CharT& c )
{
copy_str( 0, size(), true );
m_pstr->memstr.push_back( c );
}
size_type size() const { return m_pstr->memstr.size(); }
size_type capacity() const { return m_pstr->memstr.capacity(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -