forked from guozanhua/FTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrTrim.h
89 lines (71 loc) · 1.66 KB
/
StrTrim.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright 2010-2015 Fabric Software Inc. All rights reserved.
*/
#ifndef _FTL_StrTrim_h
#define _FTL_StrTrim_h
#include <FTL/Config.h>
#include <FTL/MatchCharSingle.h>
#include <string>
FTL_NAMESPACE_BEGIN
template<typename MatchChar>
void StrTrimLeft( std::string &str )
{
MatchChar const mc;
std::string::const_iterator const itBegin = str.begin();
std::string::const_iterator const itEnd = str.end();
std::string::const_iterator itLeft = itBegin;
for (;;)
{
if ( itLeft == itEnd )
break;
if ( !mc( *itLeft ) )
break;
++itLeft;
}
if ( itLeft != itBegin )
{
memmove( &str[0], &str[itLeft - itBegin], itEnd - itLeft );
str.resize( itEnd - itLeft );
}
}
template<char CharToMatch>
void StrTrimLeft( std::string &str )
{
StrTrimLeft< MatchCharSingle<CharToMatch> >( str );
}
template<typename MatchChar>
void StrTrimRight( std::string &str )
{
MatchChar const mc;
std::string::const_iterator const itBegin = str.begin();
std::string::const_iterator const itEnd = str.end();
std::string::const_iterator itRight = itEnd;
for (;;)
{
if ( itRight == itBegin )
break;
if ( !mc( *(itRight-1) ) )
break;
--itRight;
}
if ( itRight != itEnd )
str.resize( itRight - itBegin );
}
template<char CharToMatch>
void StrTrimRight( std::string &str )
{
StrTrimRight< MatchCharSingle<CharToMatch> >( str );
}
template<typename MatchChar>
void StrTrim( std::string &str )
{
StrTrimLeft<MatchChar>( str );
StrTrimRight<MatchChar>( str );
}
template<char CharToMatch>
void StrTrim( std::string &str )
{
StrTrim< MatchCharSingle<CharToMatch> >( str );
}
FTL_NAMESPACE_END
#endif //_FTL_StrTrim_h