?? wilson.txt
字號(hào):
Fast, Non-intrusive, String Concatenation
by Matthew Wilson
Listing 1
template< . . . >
class fast_string_concatenator
{
public:
typedef S string_type;
typedef C char_type;
typedef T traits_type;
typedef fast_string_concatenator<S, C, T> class_type;
// Construction
public:
fast_string_concatenator(string_type const &lhs, string_type const &rhs);
fast_string_concatenator(string_type const &lhs, char_type const *rhs);
fast_string_concatenator(string_type const &lhs, char_type const rhs);
fast_string_concatenator(char_type const *lhs, string_type const &rhs);
fast_string_concatenator(char_type const lhs, string_type const &rhs);
fast_string_concatenator(class_type const &lhs, string_type const &rhs);
fast_string_concatenator(class_type const &lhs, char_type const *rhs);
fast_string_concatenator(class_type const &lhs, char_type const rhs);
// Conversion
public:
operator string_type() const;
. . .
};
Listing 2
template< . . . >
class fast_string_concatenator
{
. . . // Public interface
private:
struct Data
{
struct CString
{
size_t len;
char_type const *s;
};
union DataRef
{
CString cstring;
char_type ch;
class_type const *concat;
};
enum DataType
{
seed // Argument was the seed type
, single // Argument was a single character
, cstring // Argument was a C-string or string object
, concat // Argument was another concatenator
};
Data(string_type const &s);
Data(char_type const *s);
Data(char_type ch s);
Data(class_type const &fc);
Data(fsc_seed const &fc);
size_t length() const;
char_type *write(char_type *s) const;
DataType const type;
DataRef ref;
};
friend struct Data;
private:
char_type *write(char_type *s) const;
private:
Data m_lhs;
Data m_rhs;
. . .
};
Listing 3
template< . . . >
size_t fast_string_concatenator<S, C, T>::length() const
{
return m_lhs.length() + m_rhs.length();
}
template< . . . >
size_t fast_string_concatenator<S, C, T>::Data::length() const
{
size_t len;
assert( type == cstring || type == single ||
type == concat || type == seed);
switch(type)
{
case seed:
len = 0;
break;
case single:
len = 1;
break;
case cstring:
len = ref.cstring.len;
break;
case concat:
len = ref.concat->length();
break;
}
return len;
}
Listing 4
template< . . . >
C *fast_string_concatenator<S, C, T>::write(C *s) const
{
return m_rhs.write(m_lhs.write(s));
}
template< . . . >
C *fast_string_concatenator<S, C, T>::Data::write(C *s) const
{
size_t len;
switch(type)
{
case seed:
break;
case single:
*(s++) = ref.ch;
break;
case cstring:
len = ref.cstring.len;
memcpy(s, ref.cstring.s, sizeof(C) * len);
s += len;
break;
case concat:
s = ref.concat->write(s);
break;
}
return s;
}
Listing 5
template< . . . >
class fast_string_concatenator
{
public:
. . .
fast_string_concatenator(class_type const &lhs, class_type const &rhs);
fast_string_concatenator(string_type const &lhs, class_type const &rhs);
fast_string_concatenator(char_type const *lhs, class_type const &rhs);
fast_string_concatenator(char_type const lhs, class_type const &rhs);
. . .
};
template < . . . >
fast_string_concatenator<S, C, T>
operator +( fsc_seed const &lhs, fast_string_concatenator< > const &rhs);
template < . . . >
fast_string_concatenator<S, C, T>
operator +( fast_string_concatenator< > const & lhs
template < . . . >
fast_string_concatenator<S, C, T>
operator +( S const & lhs, fast_string_concatenator< > const &rhs);
template < . . . >
fast_string_concatenator<S, C, T>
operator +( C const *, fast_string_concatenator< > const &rhs);
fast_string_concatenator<S, C, T>
operator +( C const & lhs, fast_string_concatenator< > const &rhs);
3
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -