📄 mstl_string.hpp
字号:
/*
The young Library
Copyright (c) 2005 by 杨桓
Permission to use, copy, modify, distribute and sell this software for any
purpose is hereby granted without fee, provided that the above copyright
notice appear in all copies and that both that copyright notice and this
permission notice appear in supporting documentation.
The author make no representations about the suitability of this software
for any purpose. It is provided "as is" without express or implied warranty.
*/
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_MINI_STL_STRING_HEADER_FILE__
#define __MACRO_CPLUSPLUS_MINI_STL_STRING_HEADER_FILE__
//-----------------------------------------------------------------------------
#include <iostream>
#include "string/mstl_basic_string.hpp"
#include "string/mstl_optm_string.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
typedef basic_string<char, lower_char_traits> lstring;
typedef basic_string<char, upper_char_traits> ustring;
typedef basic_string<char, cmp_no_case_char_traits> ncstring;
typedef basic_string<char, cmp_no_case_lower_char_traits> nclstring;
typedef basic_string<char, cmp_no_case_upper_char_traits> ncustring;
typedef basic_string<wchar_t, lower_wchar_traits> lwstring;
typedef basic_string<wchar_t, upper_wchar_traits> uwstring;
typedef basic_string<wchar_t, cmp_no_case_wchar_traits> ncwstring;
typedef basic_string<wchar_t, cmp_no_case_lower_wchar_traits> nclwstring;
typedef basic_string<wchar_t, cmp_no_case_upper_wchar_traits> ncuwstring;
//-----------------------------------------------------------------------------
typedef optm_string<char> o_string;
typedef optm_string<wchar_t> o_wstring;
typedef optm_string<char, lower_char_traits> o_lstring;
typedef optm_string<char, upper_char_traits> o_ustring;
typedef optm_string<char, cmp_no_case_char_traits> o_ncstring;
typedef optm_string<char, cmp_no_case_lower_char_traits> o_nclstring;
typedef optm_string<char, cmp_no_case_upper_char_traits> o_ncustring;
typedef optm_string<wchar_t, lower_wchar_traits> o_lwstring;
typedef optm_string<wchar_t, upper_wchar_traits> o_uwstring;
typedef optm_string<wchar_t, cmp_no_case_wchar_traits> o_ncwstring;
typedef optm_string<wchar_t, cmp_no_case_lower_wchar_traits> o_nclwstring;
typedef optm_string<wchar_t, cmp_no_case_upper_wchar_traits> o_ncuwstring;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
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(), 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.erase();
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, 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.erase(); //将字符串清空
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;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
inline std::basic_ostream<CharT>&
operator<<( std::basic_ostream<CharT>& os,
const optm_string<CharT, Traits, Allocator>& str )
{
return os.write( str.data(), str.size() );
}
template< typename CharT, typename Traits, typename Allocator >
std::basic_istream<CharT>&
operator>>( std::basic_istream<CharT>& is,
optm_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 optm_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.erase();
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, c) )
{
str += Traits::to_char_type( c );
++extracted;
c = buf->snextc();
}
if( Traits::eq_int_type(c, eof) )
is.setstate( std::ios_base::eofbit );
is.width( 0 );
}
if( extracted == 0 )
is.setstate( std::ios_base::failbit );
return is;
}
template< typename CharT, typename Traits, typename Allocator >
std::basic_istream<CharT>&
getline( std::basic_istream<CharT>& is,
optm_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 optm_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.erase();
const int_type eof = Traits::eof();
size_type n = str.max_size();
streambuf_type* buf = is.rdbuf();
int_type c = buf->sbumpc();
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;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -