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

📄 char_traits.hpp

📁 新版本TR1的stl
💻 HPP
📖 第 1 页 / 共 2 页
字号:

        return (0 < cch) ? s : NULL;
    }

    /// Represents the character \c in the character type \c char_type
    static char_type to_char_type(int_type const& c)
    {
        return static_cast<char_type>(c);
    }

    /// Represents the character \c in the integer type \c int_type
    static int_type to_int_type(char_type const& c)
    {
#if defined(STLSOFT_COMPILER_IS_WATCOM)
        return (int_type)(c);
#else /* ? compiler */
        return static_cast<int_type>(static_cast<ss_typename_type_k sign_traits<char_type>::unsigned_type>(c));
#endif /* compiler */
    }

    /// Evaluates whether \c lhs and \c rhs are equivalent
    static ss_bool_t eq_int_type(int_type const& lhs, int_type const& rhs)
    {
        return lhs == rhs;
    }

    /// Returns the value representing the end-of-file.
    static int_type eof()
    {
        return static_cast<int_type>(-1);
    }

    /// Evaluates whether the given character is the end-of-file.
    static int_type not_eof(int_type const& c)
    {
        return (c != eof() ? c : !eof());
    }
};

// class stlsoft_char_traits_safe
/** \brief Character traits, all of the operations of which can work with null pointers
 *
 * \param C The character type
 * \param N The integer type
 * \param Z The size type
 *
 * \ingroup group__library__string
 */
template<   ss_typename_param_k C
        >
struct stlsoft_char_traits_safe
    : private stlsoft_char_traits<C>
{
private:
    typedef stlsoft_char_traits<C>              parent_class_type;
public:
    /// The character type
    typedef C                                   char_type;
    /// The current parameterisation of the type
    typedef stlsoft_char_traits_safe<C>         class_type;
    /// The integer type
    typedef ss_int_t                            int_type;
    /// The size type
    typedef ss_size_t                           size_type;
    /// The position type
    typedef ss_streampos_t                      pos_type;
    /// The offset type
    typedef ss_streamoff_t                      off_type;

public:
    /// Assigns \c rhs to \c lhs
    static void assign(char_type &lhs, char_type const& rhs)
    {
        parent_class_type::assign(lhs, rhs);
    }

    /// Assigns \c cch characters of value \c c to \c dest
    static char_type *assign(char_type *dest, size_type cch, char_type const& c)
    {
        STLSOFT_MESSAGE_ASSERT("char_traits_safe<X>::assign called with NULL destination", NULL != dest);

        return parent_class_type::assign(dest, cch, c);
    }

    /// Evaluates whether \c lhs is equivalent to \c rhs
    static ss_bool_t eq(char_type const& lhs, char_type const& rhs)
    {
        return parent_class_type::eq(lhs, rhs);
    }

    /// Evaluates whether \c lhs is less than \c rhs
    static ss_bool_t lt(char_type const& lhs, char_type const& rhs)
    {
        return parent_class_type::lt(lhs, rhs);
    }

    /// Compares \c cch characters of \c s1 with \c s2
    ///
    /// \param s1 The first string to compare
    /// \param s2 The second string to compare
    /// \param cch The number of characters to compare \c s1 with \c s2
    ///
    /// \retval <0 s1 is lexicographically less than s2
    /// \retval 0 s1 is lexicographically equal to s2
    /// \retval >0 s1 is lexicographically more than s2
    static int_type compare(char_type const* s1, char_type const* s2, size_type cch)
    {
        return compare_null(s1, s2, cch);
    }

    static int_type compare_max(char_type const* s1, char_type const* s2, size_type cch)
    {
        return compare_maxnull(s1, s2, cch);
    }

    /// Compares, using compare(), \c s1 with \c s2, either or both of which may be \c null
    static int_type compare_null(char_type const* s1, char_type const* s2, size_type cch)
    {
        return parent_class_type::compare(s1, s2, cch);
    }

    /// Compares, using compare_max(), \c s1 with \c s2, either or both of which may be \c null
    static int_type compare_maxnull(char_type const* s1, char_type const* s2, size_type cch)
    {
        return parent_class_type::compare_maxnull(s1, s2, cch);
    }

    /// Evaluates the length of the string \c s up to a given number of characters
    ///
    /// \param s The string to be evaluated. It may be null
    /// \param limit The maximum number of characters to evaluate
    /// \return The length of the string (in characters) not including the null-terminator
    static size_type length_max_null(char_type const* s, size_type limit)
    {
        return (NULL == s) ? 0 : parent_class_type::length_max(s, limit);
    }

    /// Evaluates the length of the string \c s, which may be \c null, up to a given number of characters
    ///
    /// \param s The string to be evaluated. It may be null
    /// \param limit The maximum number of characters to evaluate
    /// \return The length of the string (in characters) not including the null-terminator
    static size_type length_max(char_type const* s, size_type limit)
    {
        return length_max_null(s, limit);
    }

    /// Evaluates the length of the string \c s, which may be \c null
    static size_type length_null(char_type const* s)
    {
        return (NULL == s) ? 0 : parent_class_type::length(s);
    }

    /// Evaluates the length of the string \c s
    static size_type length(char_type const* s)
    {
        return length_null(s);
    }

    /// Copies \c cch characters from \c src to \c dest
    static char_type *copy(char_type *dest, char_type const* src, size_type cch)
    {
        STLSOFT_MESSAGE_ASSERT("char_traits_safe<X>::copy called with NULL destination", NULL != dest);
        STLSOFT_MESSAGE_ASSERT("char_traits_safe<X>::copy called with NULL source", NULL != src);

        return parent_class_type::copy(dest, src, cch);
    }

    /// Copies \c cch characters from \c src to \c dest, accounting for whether the ranges overlap
    static char_type *move(char_type *dest, char_type const* src, size_type cch)
    {
        STLSOFT_MESSAGE_ASSERT("char_traits_safe<X>::move called with NULL destination", NULL != dest);
        STLSOFT_MESSAGE_ASSERT("char_traits_safe<X>::move called with NULL source", NULL != src);

        return parent_class_type::move(dest, src, cch);
    }

    /// Finds the first \c c in \c cch elements in \c s, or \c NULL if not found
    static char_type const* find(char_type const* s, size_type cch, char_type const& c)
    {
        return (NULL == s) ? NULL : parent_class_type::find(s, cch, c);
    }

    /// Represents the character \c in the character type \c char_type
    static char_type to_char_type(int_type const& c)
    {
        return parent_class_type::to_char_type(c);
    }

    /// Represents the character \c in the integer type \c int_type
    static int_type to_int_type(char_type const& c)
    {
        return parent_class_type::to_int_type(c);
    }

    /// Evaluates whether \c lhs and \c rhs are equivalent
    static ss_bool_t eq_int_type(int_type const& lhs, int_type const& rhs)
    {
        return parent_class_type::eq_int_type(lhs, rhs);
    }

    /// Returns the value representing the end-of-file.
    static int_type eof()
    {
        return parent_class_type::eof();
    }

    /// Evaluates whether the given character is the end-of-file.
    static int_type not_eof(int_type const& c)
    {
        return parent_class_type::not_eof();
    }
};

// class char_traits
/** \brief Character traits, all of the operations of which can work with null pointers
 *
 * \param C The character type
 * \param N The integer type
 * \param Z The size type
 *
 * \ingroup group__library__string
 */

/* DMC++ special handling */
#if defined(STLSOFT_COMPILER_IS_DMC) &&   /* compiler is DMC++ */ \
    !defined(STLSOFT_CF_std_NAMESPACE)    /* std namespace is not defined */

# if !defined(__SGI_STL_STRING_FWD_H)
#  error Unexpected
# endif /* !__SGI_STL_STRING_FWD_H */
# if !defined(__SGI_STL_CHAR_TRAITS_H)
#  error Unexpected
# endif /* !__SGI_STL_CHAR_TRAITS_H */

using ::char_traits;
#else /* ? comp/lib */

/* We only define (stlsoft::)char_traits if no std::char_traits is available, or
 * the stlsoft namespace is defined, or the std namespace is defined.
 */
 #if !defined(STLSOFT_CF_std_char_traits_AVAILABLE) || /* std does not define char_traits */ \
     !defined(_STLSOFT_NO_NAMESPACE) ||                 /* stlsoft namespace is defined */ \
     defined(STLSOFT_CF_std_NAMESPACE)
template<   ss_typename_param_k C
        >
struct char_traits
    : public stlsoft_char_traits<C>
{
    typedef stlsoft_char_traits<C>                          parent_class_type;
public:
    /// The current parameterisation of the type
    typedef char_traits<C>                                  class_type;
    /// The character type
    typedef ss_typename_type_k parent_class_type::char_type char_type;
    typedef ss_typename_type_k parent_class_type::int_type  int_type;
    typedef ss_typename_type_k parent_class_type::size_type size_type;
    typedef ss_typename_type_k parent_class_type::pos_type  pos_type;
    typedef ss_typename_type_k parent_class_type::off_type  off_type;
};
# endif /* !::stlsoft && !::std */
#endif /* comp/lib */

// class char_traits_safe
/** \brief Character traits, all of the operations of which can work with null pointers
 *
 * \param C The character type
 * \param N The integer type
 * \param Z The size type
 *
 * \ingroup group__library__string
 */
template<   ss_typename_param_k C
        >
struct char_traits_safe
    : public stlsoft_char_traits_safe<C>
{
    typedef stlsoft_char_traits_safe<C>                     parent_class_type;
public:
    /// The current parameterisation of the type
    typedef char_traits_safe<C>                             class_type;
    /// The character type
    typedef ss_typename_type_k parent_class_type::char_type char_type;
    typedef ss_typename_type_k parent_class_type::int_type  int_type;
    typedef ss_typename_type_k parent_class_type::size_type size_type;
    typedef ss_typename_type_k parent_class_type::pos_type  pos_type;
    typedef ss_typename_type_k parent_class_type::off_type  off_type;
};

/* /////////////////////////////////////////////////////////////////////////
 * Specialisations
 */

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

#if !defined(STLSOFT_NO_CHAR_TRAITS_LIBRARY_CALLS) && \
    !defined(STLSOFT_COMPILER_IS_DMC) && \
    (   !defined(STLSOFT_COMPILER_IS_MSVC) || \
        _MSC_VER >= 1100) && \
    !defined(STLSOFT_COMPILER_IS_VECTORC) && \
    !defined(STLSOFT_COMPILER_IS_WATCOM)

/* char */

STLSOFT_TEMPLATE_SPECIALISATION
inline char *stlsoft_char_traits<char>::assign(char *dest, ss_size_t cch, char const& c)
{
    return static_cast<char*>(::memset(dest, c, cch * sizeof(char)));
}

STLSOFT_TEMPLATE_SPECIALISATION
inline ss_int_t stlsoft_char_traits<char>::compare(char_type const* s1, char_type const* s2, ss_size_t cch)
{
    return ::memcmp(s1, s2, cch);
}

STLSOFT_TEMPLATE_SPECIALISATION
inline char const* stlsoft_char_traits<char>::find(char_type const* s, size_type cch, char_type const& c)
{
#if defined(STLSOFT_COMPILER_IS_BORLAND) && \
    __BORLANDC__ < 0x0560
    return static_cast<char const*>(memchr(s, c, cch));
#else /* ? compiler */
    void const  *p = ::memchr(s, c, cch);

    return static_cast<char const*>(p);
#endif /* compiler */
}

STLSOFT_TEMPLATE_SPECIALISATION
inline char *stlsoft_char_traits<char>::copy(char *dest, char const* src, ss_size_t cch)
{
#ifdef _DEBUG
    ::memset(dest, 0, cch * sizeof(char));
#endif /* _DEBUG */

    return static_cast<char*>(memcpy(dest, src, cch * sizeof(char)));
}

STLSOFT_TEMPLATE_SPECIALISATION
inline ss_size_t stlsoft_char_traits<char>::length(char const* s)
{
    return ::strlen(s);
}

/* wchar_t */

STLSOFT_TEMPLATE_SPECIALISATION
inline ss_size_t stlsoft_char_traits<wchar_t>::length(wchar_t const* s)
{
    return ::wcslen(s);
}

#endif /* !STLSOFT_NO_CHAR_TRAITS_LIBRARY_CALLS && !STLSOFT_COMPILER_IS_DMC */

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

////////////////////////////////////////////////////////////////////////////
// Unit-testing

#ifdef STLSOFT_UNITTEST
# include "./unittest/char_traits_unittest_.h"
#endif /* STLSOFT_UNITTEST */

/* ////////////////////////////////////////////////////////////////////// */

#ifndef _STLSOFT_NO_NAMESPACE
} // namespace stlsoft
#endif /* _STLSOFT_NO_NAMESPACE */

/* ////////////////////////////////////////////////////////////////////// */

#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS */

/* ////////////////////////////////////////////////////////////////////// */

⌨️ 快捷键说明

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