(l/r)trim | C++ Coding

Andreas Hamm
staatlich geprüfter technischer Assistent für Informatik
Startseite Ι C++Coding

trim

C++

template<class _Elem, class _Traits, class _Ax>
void trim(typename std::basic_string<_Elem, _Traits, _Ax>& _Str);

template<class _Elem>
void trim(_Elem* _Str);

C++ Quellcode

#include <string>


template<class _Elem> bool is_space(_Elem _Ch);

template<class _Elem, class _Traits, class _Ax>
void ltrim(typename std::basic_string<_Elem, _Traits, _Ax>& _Str);
template<class _Elem, class _Traits, class _Ax>
void rtrim(typename std::basic_string<_Elem, _Traits, _Ax>& _Str);


template<class _Elem, class _Traits, class _Ax>
_inline
void trim(typename std::basic_string<_Elem, _Traits, _Ax>& _Str)
{
rtrim<_Elem, _Traits, _Ax>(_Str);
ltrim<_Elem, _Traits, _Ax>(_Str);
}


template<class _Elem>
_inline
void trim(_Elem* _Str)
{
typedef typename
std::basic_string<_Elem, std::char_traits<_Elem>, std::allocator<_Elem>>
xstring;
if( _Str )
{
xstring _Dup( _Str );
const size_t _Cch = _Dup.length();
trim( _Dup );
if( _Dup.length() != _Cch )
{
if( _Dup.length() == 0 )
{
(*_Str) = '\0';
}
else
{
const size_t _Size = (_Cch * (sizeof(_Elem) + 1));
const size_t _Ccb = (_Dup.length() * (sizeof(_Elem) + 1));
memcpy_s( _Str, _Size, _Dup.c_str(), _Ccb );
}
}
}
}

ltrim

C++

template<class _Elem, class _Traits, class _Ax>
void ltrim(typename std::basic_string<_Elem, _Traits, _Ax>& _Str);

template<class _Elem>
void ltrim(_Elem* _Str);

C++ Quellcode

#include <string>


template<class _Elem> bool is_space(_Elem _Ch);


template<class _Elem, class _Traits, class _Ax>
_inline
void ltrim(typename std::basic_string<_Elem, _Traits, _Ax>& _Str)
{
if( _Str.length() > 0 )
{
typename std::basic_string<_Elem, _Traits, _Ax>::iterator _Where = _Str.begin();
if( is_space<_Elem>(*_Where) )
{
do
{
++_Where;
}
while( _Str.end() != _Where && is_space<_Elem>(*_Where) );
_Str.erase(_Str.begin(), _Where);
}
}
}


template<class _Elem>
_inline
void ltrim(_Elem* _Str)
{
typedef typename
std::basic_string<_Elem, std::char_traits<_Elem>, std::allocator<_Elem>>
xstring;
if( _Str )
{
xstring _Dup( _Str );
const size_t _Cch = _Dup.length();
ltrim( _Dup );
if( _Dup.length() != _Cch )
{
if( _Dup.length() == 0 )
{
(*_Str) = '\0';
}
else
{
const size_t _Size = (_Cch * (sizeof(_Elem) + 1));
const size_t _Ccb = (_Dup.length() * (sizeof(_Elem) + 1));
memcpy_s( _Str, _Size, _Dup.c_str(), _Ccb );
}
}
}
}

rtrim

C++

template<class _Elem, class _Traits, class _Ax>
void rtrim(typename std::basic_string<_Elem, _Traits, _Ax>& _Str);

template<class _Elem>
void rtrim(_Elem* _Str);

C++ Quellcode

#include <string>


template<class _Elem> bool is_space(_Elem _Ch);


template<class _Elem, class _Traits, class _Ax>
_inline
void rtrim(typename std::basic_string<_Elem, _Traits, _Ax>& _Str)
{
if( _Str.length() > 0 )
{
typename std::basic_string<_Elem, _Traits, _Ax>::iterator _Where = _Str.end();
if( is_space<_Elem>(*(_Where - 1)) )
{
do
{
--_Where;
}
while( _Str.begin() != _Where && is_space<_Elem>(*(_Where - 1)) );
_Str.erase(_Where, _Str.end());
}
}
}


template<class _Elem>
_inline
void rtrim(_Elem* _Str)
{
typedef typename
std::basic_string<_Elem, std::char_traits<_Elem>, std::allocator<_Elem>>
xstring;
if( _Str )
{
xstring _Dup( _Str );
const size_t _Cch = _Dup.length();
rtrim( _Dup );
if( _Dup.length() != _Cch )
{
if( _Dup.length() == 0 )
{
(*_Str) = '\0';
}
else
{
const size_t _Size = (_Cch * (sizeof(_Elem) + 1));
const size_t _Ccb = (_Dup.length() * (sizeof(_Elem) + 1));
memcpy_s( _Str, _Size, _Dup.c_str(), _Ccb );
}
}
}
}

is_space

C++

template<class _Elem> _inline bool is_space(_Elem _Ch);

C++ Quellcode

template<class _Elem> _inline bool is_space(_Elem _Ch)
{
// Prüfen auf Leerzeichen:
//0x20Leertaste
//0x09Tabulator horizontal
//0x0AZeilenvorschub
//0x0BTabulator vertical
//0x0CFormularvorschub
//0x0DWagenrücklauf

if( _Ch == 0x20 || (_Ch >= 0x09 && _Ch <= 0x0D) )
return true;
return false;
}
Seitenanfang
Impressum Ι Rechtliche Hinweise Ι Haftungsausschluss Ι Nutzungsbedingungen