⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ycpp_string.hpp

📁 一个类STL的多平台可移植的算法容器库,主要用于嵌入式系统编程时的内存管理等方面
💻 HPP
📖 第 1 页 / 共 5 页
字号:
{
    size_type len = size(), slen = Traits::length( str );
    int result = Traits::compare( data(), str, std::min(len, slen) );
    if( result == 0 )
        result = static_cast<int>( len - slen );
    return result;
}

template< typename CharT, typename Traits, typename Allocator >
int basic_string<CharT, Traits, Allocator>::compare( size_type index,
                                                     size_type n,
                                                     const self& str,
                                                     size_type sindex,
                                                     size_type sn ) const
{
    if( index > size() || sindex > str.size() )
        throw std::out_of_range( "young::basic_string::compare out of range!" );
    size_type len = std::min( size() - index, n );
    size_type slen = std::min( str.size() - sindex, sn );
    int result = Traits::compare( data() + index, str.data() + sindex,
                                  std::min(slen, len) );
    if( result == 0 )
        result = static_cast<int>( len - slen );
    return result;
}

template< typename CharT, typename Traits, typename Allocator >
int basic_string<CharT, Traits, Allocator>::compare( size_type index,
                                                     size_type n,
                                                     const CharT* str,
                                                     size_type sn ) const
{
    if( index > size() )
        throw std::out_of_range( "young::basic_string::compare out of range!" );
    size_type slen = std::min( Traits::length(str), sn );
    size_type len = std::min( size() - index, n );
    int result = Traits::compare( data() + index, str, std::min(slen, len) );
    if( result == 0 )
        result = static_cast<int>( len - slen );
    return result;
}

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find( CharT c, size_type index ) const
{
    size_type len = size();
    if( index < len )
    {
        const CharT* d = data();
        const CharT* p = Traits::find( d + index, len - index, c );
        if( p )
            return p - d;
    }
    return npos;
}

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find( const CharT* str,
                                              size_type index,
                                              size_type n ) const
{
    const CharT* d = data();
    for( ; index + n <= size(); ++index )
    {
        if( Traits::eq( d[index], *str )
            && Traits::compare( d + index, str, n ) == 0 )
            return index;
    }
    return npos;
}

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::rfind( CharT c,
                                               size_type index ) const
{
    size_type len = size();
    if( len != 0 )
    {
        const CharT* d = data();
        if( index > --len )
            index = len;
        do
        {
            if( Traits::eq( d[index], c ) )
                return index;
        }
        while( index-- );
    }
    return npos;
}

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::rfind( const CharT* str,
                                               size_type index,
                                               size_type n ) const
{
    size_type len = size();
    if( n <= len )
    {
        const CharT* d = data();
        index = std::min( size_type(len - n), index );  //最靠前的索引
        do
        {
            if( Traits::compare( d + index, str, n ) == 0 )
                return index;
        }
        while( index-- );
    }
    return npos;
}

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find_first_of( const CharT* str,
                                                       size_type index,
                                                       size_type n ) const
{
    if( n != 0 )
    {
        size_type len = size();
        const CharT* d = data();
        for( ; index < len; ++index )
        {
            if( Traits::find( str, n, d[index] ) )
                return index;
        }
    }
    return npos;
}

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find_last_of( const CharT* str,
                                                      size_type index,
                                                      size_type n ) const
{
    size_type len = size();
    if( len != 0 && n != 0 )
    {
        const CharT* d = data();
        if( index > --len )
            index = len;
        do
        {
            if( Traits::find( str, n, d[index] ) )
                return index;
        }
        while( index-- );
    }
    return npos;
}

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find_first_not_of( CharT c,
                                                           size_type index ) const
{
    size_type len = size();
    const CharT* d = data();
    for( ; index < size; ++index )
    {
        if( !Traits::eq( c, d[index] ) )
            return index;
    }
    return npos;
}

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find_first_not_of( const CharT* str,
                                                           size_type index,
                                                           size_type n ) const
{
    if( n != 0 )
    {
        size_type len = size();
        const CharT* d = data();
        for( ; index < size; ++index )
        {
            if( !Traits::find( str, n, d[index] ) )
                return index;
        }
    }
    return npos;
}

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find_last_not_of( CharT c,
                                                          size_type index ) const
{
    size_type len = size();
    if( len != 0 )
    {
        const CharT* d = data();
        if( index > --len )
            index = len;
        do
        {
            if( !Traits::eq( c, d[index] ) )
                return index;
        }
        while( index-- );
    }
    return npos;
}

template< typename CharT, typename Traits, typename Allocator >
typename basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::find_last_not_of( const CharT* str,
                                                          size_type index,
                                                          size_type n ) const
{
    size_type len = size();
    if( len != 0 && n != 0 )
    {
        const CharT* d = data();
        if( index > --len )
            index = len;
        do
        {
            if( !Traits::find( str, n, d[index] ) )
                return index;
        }
        while( index-- );
    }
    return npos;
}



template< typename CharT, typename Traits, typename Allocator >
inline void swap( basic_string<CharT, Traits, Allocator>& lhs,
                  basic_string<CharT, Traits, Allocator>& rhs )
{
    lhs.swap( rhs );
}



template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( const basic_string<CharT, Traits, Allocator>& lhs,
           const basic_string<CharT, Traits, Allocator>& rhs )
{
    basic_string<CharT, Traits, Allocator> str( lhs );
    str.append( rhs );
    return str;
}

template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( const char* lhs, const basic_string<CharT, Traits, Allocator>& rhs )
{
    basic_string<CharT, Traits, Allocator> str( lhs );
    str.append( rhs );
    return str;
}

template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( const basic_string<CharT, Traits, Allocator>& lhs, const char* rhs )
{
    basic_string<CharT, Traits, Allocator> str( rhs );
    str.append( lhs );
    return str;
}

template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( const basic_string<CharT, Traits, Allocator>& lhs, CharT c )
{
    basic_string<CharT, Traits, Allocator> str( lhs );
    str.append( size_t(1), c );
    return str;
}

template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( CharT c, const basic_string<CharT, Traits, Allocator>& rhs )
{
    basic_string<CharT, Traits, Allocator> str( rhs );
    str.append( size_t(1), c );
    return str;
}



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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -