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

📄 trim_functions.hpp

📁 新版本TR1的stl
💻 HPP
📖 第 1 页 / 共 2 页
字号:
//  const iterator_t            it_e    =   cstr.end();
    const iterator_t            it_l    =   it_b;
    const reverse_iterator_t    rit     =   find_if(cstr.rbegin(), cstr.rend(), not1(bind1st(ptr_fun(pfn), trimChars)));
    const iterator_t            it_r    =   rit.base();

    return string_traits_t::assign_inplace(str, it_l, it_r);
}

# if defined(__BORLANDC__)
#  pragma warn .8092
#  pragma warn .8091
# endif /* compiler */

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/** \brief Trims all the trailing whitespace characters, if any, from a string
 *
 * \ingroup group__library__string
 */
template<ss_typename_param_k S>
inline S& trim_right(S& str)
{
    // 1. typedef the string traits
    typedef string_traits<S>                                string_traits_t;

# if defined(STLSOFT_COMPILER_IS_BORLAND) && \
     __BORLANDC__ < 0x0564
    // This is needed here to tell the Borland compiler that it's a type!
    string_traits_t::char_type  *p  =   NULL;

    STLSOFT_SUPPRESS_UNUSED(n);
# endif /* compiler */

    // 2. typedef the char_t
    typedef ss_typename_type_k string_traits_t::char_type   char_t;

    ss_size_t       n;
    char_t const*   trimChars  =   default_trim_chars(static_cast<char_t const*>(0), n);

    STLSOFT_SUPPRESS_UNUSED(n);

    return trim_right_impl(str, trimChars);
}

/** \brief
 *
 * \ingroup group__library__string
 */
template<   ss_typename_param_k S0
        ,   ss_typename_param_k S1
        >
inline S0& trim_right(S0& str, S1 const& trimChars)
{
    return trim_right_impl(str, stlsoft_ns_qual(c_str_ptr)(trimChars));
}




#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

# if defined(__BORLANDC__)
#  pragma warn -8091 // Otherwise BC++ complains that rbegin()/rend() returns passed to find_if() are output iterators
#  pragma warn -8092 // Otherwise BC++ complains that rbegin()/rend() returns passed to find_if() are not iterators
# endif /* compiler */

#if defined(STLSOFT_COMPILER_IS_MSVC) && \
    _MSC_VER < 1310
template<   ss_typename_param_k S
        ,   ss_typename_param_k C
        >
inline S& trim_all_impl(S& str, C const* trimChars)
#else /* ? compiler */
template<ss_typename_param_k S>
inline S& trim_all_impl(S& str, ss_typename_type_k string_traits<S>::char_type const* trimChars)
#endif /* compiler */
{
    S const& cstr = str;

    // 1. 'use' the std namespace here, otherwise get totally clogged in stlsoft_ns_qual_std(XX)
#ifdef STLSOFT_CF_std_NAMESPACE
    using namespace std;
#endif /* STLSOFT_CF_std_NAMESPACE */
    // 2. typedef the string traits
    typedef string_traits<S>                                        string_traits_t;
    // 3. typedef the char_t
    typedef ss_typename_type_k string_traits_t::char_type           char_t;
    // 4. typedef the iterator type(s)
    typedef ss_typename_type_k string_traits_t::const_iterator            iterator_t;
    typedef ss_typename_type_k string_traits_t::const_reverse_iterator    reverse_iterator_t;
    // 5. Since ::stlsoft::strchr is an overloaded function, we disambiguate by casting to the type required
    char_t const* (*pfn)(char_t const*, char_t)   =   ::stlsoft::strchr;

    // Get an iterator to the first element that
    const iterator_t            it_b    =   cstr.begin();
//  const iterator_t            it_e    =   cstr.end();
    const iterator_t            it_l    =   find_if(it_b, cstr.end(), not1(bind1st(ptr_fun(pfn), trimChars)));
    const reverse_iterator_t    rit     =   find_if(cstr.rbegin(), cstr.rend(), not1(bind1st(ptr_fun(pfn), trimChars)));
    const iterator_t            it_r    =   rit.base();

    return string_traits_t::assign_inplace(str, it_l, it_r);
}

# if defined(__BORLANDC__)
#  pragma warn .8092
#  pragma warn .8091
# endif /* compiler */

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/** \brief Trims all the leading and trailing whitespace characters, if any, from a string
 *
 * \ingroup group__library__string
 */
template<ss_typename_param_k S>
inline S& trim_all(S& str)
{
    // 1. typedef the string traits
    typedef string_traits<S>                                string_traits_t;

# if defined(STLSOFT_COMPILER_IS_BORLAND) && \
     __BORLANDC__ < 0x0564
    // This is needed here to tell the Borland compiler that it's a type!
    string_traits_t::char_type  *p  =   NULL;

    STLSOFT_SUPPRESS_UNUSED(n);
# endif /* compiler */

    // 2. typedef the char_t
    typedef ss_typename_type_k string_traits_t::char_type   char_t;

    ss_size_t       n;
    char_t const*   trimChars  =   default_trim_chars(static_cast<char_t const*>(0), n);

    STLSOFT_SUPPRESS_UNUSED(n);

    return trim_all_impl(str, trimChars);
}

/** \brief
 *
 * \ingroup group__library__string
 */
template<   ss_typename_param_k S0
        ,   ss_typename_param_k S1
        >
inline S0& trim_all(S0& str, S1 const& trimChars)
{
    return trim_all_impl(str, stlsoft_ns_qual(c_str_ptr)(trimChars));
}


#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

#if defined(STLSOFT_COMPILER_IS_MSVC) && \
    _MSC_VER < 1310
template<   ss_typename_param_k S
        ,   ss_typename_param_k C
        >
inline S& remove_all_impl(S& str, C const* removeChars)
#else /* ? compiler */
template<ss_typename_param_k S>
inline S& remove_all_impl(S& str, ss_typename_type_k S::value_type const* removeChars)
#endif /* compiler */
{
    // 1. 'use' the std namespace here, otherwise get totally clogged in stlsoft_ns_qual_std(XX)
#ifdef STLSOFT_CF_std_NAMESPACE
    using namespace std;
#endif /* STLSOFT_CF_std_NAMESPACE */
    // 2. typedef the string traits
    typedef string_traits<S>                                        string_traits_t;
    // 3. typedef the char_t
    typedef ss_typename_type_k string_traits_t::char_type           char_t;
    // 4. typedef the iterator type(s)
    typedef ss_typename_type_k string_traits_t::const_iterator      iterator_t;
    // 5. Since ::stlsoft::strchr is an overloaded function, we disambiguate by casting to the type required
    char_t const* (*pfn)(char_t const*, char_t)   =   ::stlsoft::strchr;

    // Get an iterator to the first element that
    /* const */ iterator_t        it_b    =   str.begin();
//  const iterator_t        it_e    =   str.end();
    const iterator_t        it_l    =   it_b;
    const iterator_t        it_r    =   remove_if(it_b, str.end(), bind1st(ptr_fun(pfn), removeChars));

    return string_traits_t::assign_inplace(str, it_l, it_r);
}

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/** \brief
 *
 * \ingroup group__library__string
 */
template<ss_typename_param_k S>
inline S& remove_all(S& str)
{
    // 1. typedef the string traits
    typedef string_traits<S>                                string_traits_t;

# if defined(STLSOFT_COMPILER_IS_BORLAND) && \
     __BORLANDC__ < 0x0564
    // This is needed here to tell the Borland compiler that it's a type!
    string_traits_t::char_type  *p  =   NULL;

    STLSOFT_SUPPRESS_UNUSED(n);
# endif /* compiler */

    // 2. typedef the char_t
    typedef ss_typename_type_k string_traits_t::char_type   char_t;

    ss_size_t       n;
    char_t const*   removeChars    =   default_trim_chars(static_cast<char_t const*>(0), n);

    STLSOFT_SUPPRESS_UNUSED(n);

    return remove_all_impl(str, removeChars);
}

/** \brief
 *
 * \ingroup group__library__string
 */
template<   ss_typename_param_k S0
        ,   ss_typename_param_k S1
        >
inline S0& remove_all(S0& str, S1 const& removeChars)
{
    return remove_all_impl(str, stlsoft_ns_qual(c_str_ptr)(removeChars));
}

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

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

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

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

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

#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_TRIM_FUNCTIONS */

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

⌨️ 快捷键说明

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