(l/r)trim | C++ Coding

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

rtrim

Entfernt Leerraum (Leerzeichen) rechts von Text.


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



#if defined(_UDOPT_INVK_C_IMPL)
void rtrim(char* _Str);
void rtrim(wchar_t* _Str);

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


#endif

C
void _csrtrim(char* _Str);
void _wcsrtrim(wchar_t* _Str);

C++ Quellcode

trimstr.hpp
Quellcode Auszug…
#pragma once

#include "isspace.inl"
#include <string>


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

{
typename std::basic_string<_Elem, _Traits, _Ax>::iterator _Where = _Str.end();
while( _Str.end() != _Where && isspace(*(_Where - 1)) )
{
--_Where;
}
_Str.erase(_Where, _Str.end());
}


#if defined(_UDOPT_INVK_C_IMPL)
#include "xstrim.inl"

__inline void rtrim(char* _Str)
{
::_csrtrim(_Str);
}


__inline void rtrim(wchar_t* _Str)
{
::_wcsrtrim(_Str);
}

#else
#include "xcscpy.inl"

// Following code lines are only intend for performance
//comparison ...

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

#endif

isspace.inl
#pragma once

#include <ctype.h>
#include <wchar.h>


__inline void isspace(char _Ch)
{
return ::isspace(_Ch) ? true : false;
}


__inline void isspace(wchar_t _Ch)
{
return ::iswspace(_Ch) ? true : false;
}

xcscpy.inl
Quellcode Auszug…
#pragma once

#include <string.h>
#include <wchar.h>


_inline errno_t _xcsncpy_s(char* _Dest, size_t _Size, const char* _Src, size_t _Count)
{
return ::strncpy_s(_Dest, _Size, _Src, _Count);
}


_inline errno_t _xcsncpy_s(wchar_t* _Dest, size_t _Size, const wchar_t* _Src, size_t _Count)
{
return ::wcsncpy_s(_Dest, _Size, _Src, _Count);
}

xstrim.inl
#pragma once

#if defined(__cplusplus)
extern "C"
{
#endif


#include "xstrim.h"

#if defined(__cplusplus)
}
#endif


C Quellcode

xstrim.h
Quellcode Auszug…
#pragma once

#include <ctype.h>
#include <wchar.h>


extern void _csrtrim(char* _Str);
extern void _wcsrtrim(wchar_t* _Str);

xstrim.c
Quellcode Auszug…
#include "pch.h"

#include <memory.h>
#include <ctype.h>
#include <wchar.h>


void _csrtrim(char* _Str)
{
if( _Str )
{
char *_Ch = _Str;

// First seek pointer to end of string (Eos) ...
while( '\0' != (*_Ch) )
++_Ch;

// Move pointer backward over whitespace; seek for non white-
//space character, if any ...
while( _Str < _Ch && isspace(*(_Ch - 1)) )
--_Ch;

// Finally terminate character string, when ...
if( '\0' != (*_Ch) )
(*_Ch) = '\0';
}
}


void _wcsrtrim(wchar_t* _Str) {
if( _Str )
{
wchar_t *_Ch = _Str;

// First seek pointer to end of string (Eos) ...
while( '\0' != (*_Ch) )
++_Ch;

// Move pointer backward over whitespace; seek for non white-
//space character, if any ...
while( _Str < _Ch && iswspace(*(_Ch - 1)) )
--_Ch;

// Finally terminate character string, when ...
if( '\0' != (*_Ch) )
(*_Ch) = '\0';
}
}
Seitenanfang
Impressum Ι Rechtliche Hinweise Ι Haftungsausschluss Ι Nutzungsbedingungen