(l/r)trim | C++ Coding

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

ltrim

Entfernt Leerraum (Leerzeichen) links von Text.


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



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


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


#endif

C
void _csltrim(char* _Str);
void _wcsltrim(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 ltrim(typename std::basic_string<_Elem, _Traits, _Ax>& _Str);

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


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

__inline void ltrim(char* _Str)
{
::_csltrim(_Str);
}


__inline void ltrim(wchar_t* _Str)
{
::_wcsltrim(_Str);
}

#else
#include "xcscpy.inl"

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

template<class _Elem>
_inline
void ltrim(_Elem* _Str)
{
if( _Str )
{
typename std::basic_string<_Elem> _Dup(_Str);
const size_t _Cch = _Dup.length();
ltrim(_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 _csltrim(char* _Str);
extern void _wcsltrim(wchar_t* _Str);

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

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


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

// Move pointer forward over whitespace; seek for non white-
//space character, if any ...
while( '\0' != (*_Ch) && isspace(*_Ch) )
++_Ch;

// Finally repeat moving characters from beginning of non white-
//space until end of string (Eos) including terminating zero
//to first position of source string, when ...
if( _Str < _Ch && '\0' != (*(_Ch - 1)) )
{
const size_t _Copy = (strlen(_Ch) + 1);
memcpy_s(_Str, (_Copy + (_Ch - _Str)), _Ch, _Copy);
}
}
}


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

// Move pointer forward over whitespace; seek for non white-
//space character, if any ...
while( '\0' != (*_Ch) && iswspace(*_Ch) )
++_Ch;

// Finally repeat moving characters from beginning of non white-
//space until end of string (Eos) including terminating zero
//to first position of source string, when ...
if( _Str < _Ch && '\0' != (*(_Ch - 1)) )
{
const size_t _Copy = (wcslen(_Ch) + 1);
wmemcpy_s(_Str, (_Copy + (_Ch - _Str)), _Ch, _Copy);
}
}
}
Seitenanfang
Impressum Ι Rechtliche Hinweise Ι Haftungsausschluss Ι Nutzungsbedingungen