📄 flex_string.hpp
字号:
resize(pos + n2, c);
Storage::append(&*begin() + pos + n1, oldSize - pos - n1);
flex_string_details::pod_fill(&*begin() + pos,
&*begin() + oldSize, c);
}
else
{
if (n2 > n1)
{
const size_type delta = n2 - n1;
Storage::append(&*begin() + oldSize - delta, delta);
flex_string_details::pod_move(
&*begin() + pos + n1,
&*begin() + oldSize - delta,
&*begin() + pos + n2);
}
else
{
flex_string_details::pod_move(&*begin() + pos + n1, &*end(),
&*begin() + pos + n2);
resize(oldSize - (n1 - n2));
}
flex_string_details::pod_fill(&*begin() + pos,
&*begin() + pos + n2, c);
}
return *this;
}
flex_string& replace(iterator i1, iterator i2, const flex_string& str)
{ return replace(i1, i2, str.c_str(), str.length()); }
flex_string& replace(iterator i1, iterator i2,
const value_type* s, size_type n)
{ return replace(i1 - begin(), i2 - i1, s, n); }
flex_string& replace(iterator i1, iterator i2, const value_type* s)
{ return replace(i1, i2, s, traits_type::length(s)); }
flex_string& replace(iterator i1, iterator i2,
size_type n, value_type c)
{ return replace(i1 - begin(), i2 - i1, n, c); }
private:
template <int i> class Selector {};
// template <class U1, class U2> struct SameType
// {
// enum { result = false };
// };
//
// template <class U> struct SameType<U, U>
// {
// enum { result = true };
// };
template<class ReallyAnIntegral>
flex_string& ReplaceImpl(iterator i1, iterator i2,
ReallyAnIntegral n, ReallyAnIntegral c, Selector<1>)
{
return replace(i1, i2, static_cast<size_type>(n),
static_cast<value_type>(c));
}
template<class InputIterator>
flex_string& ReplaceImpl(iterator i1, iterator i2,
InputIterator b, InputIterator e, Selector<0>)
{
BOOST_ASSERT(false);
return *this;
}
public:
template<class InputIterator>
flex_string& replace(iterator i1, iterator i2,
InputIterator j1, InputIterator j2)
{
return ReplaceImpl(i1, i2, j1, j2,
Selector<std::numeric_limits<InputIterator>::is_specialized>());
}
size_type copy(value_type* s, size_type n, size_type pos = 0) const
{
Enforce(pos <= size(), (std::out_of_range*)0, "");
n = Min(n, size() - pos);
flex_string_details::pod_copy(
&*begin() + pos,
&*begin() + pos + n,
s);
return n;
}
void swap(flex_string& rhs)
{
Storage& srhs = rhs;
this->Storage::swap(srhs);
}
// 21.3.6 string operations:
const value_type* c_str() const
{ return Storage::c_str(); }
const value_type* data() const
{ return Storage::data(); }
allocator_type get_allocator() const
{ return Storage::get_allocator(); }
size_type find(const flex_string& str, size_type pos = 0) const
{ return find(str.data(), pos, str.length()); }
size_type find (const value_type* s, size_type pos, size_type n) const
{
for (; pos <= size(); ++pos)
{
if (traits_type::compare(&*begin() + pos, s, n) == 0)
{
return pos;
}
}
return npos;
}
size_type find (const value_type* s, size_type pos = 0) const
{ return find(s, pos, traits_type::length(s)); }
size_type find (value_type c, size_type pos = 0) const
{ return find(&c, pos, 1); }
size_type rfind(const flex_string& str, size_type pos = npos) const
{ return rfind(str.c_str(), pos, str.length()); }
size_type rfind(const value_type* s, size_type pos, size_type n) const
{
if (n > length()) return npos;
pos = Min(pos, length() - n);
if (n == 0) return pos;
const_iterator i(begin() + pos);
for (; ; --i)
{
if (traits_type::eq(*i, *s)
&& traits_type::compare(&*i, s, n) == 0)
{
return i - begin();
}
if (i == begin()) break;
}
return npos;
}
size_type rfind(const value_type* s, size_type pos = npos) const
{ return rfind(s, pos, traits_type::length(s)); }
size_type rfind(value_type c, size_type pos = npos) const
{ return rfind(&c, pos, 1); }
size_type find_first_of(const flex_string& str, size_type pos = 0) const
{ return find_first_of(str.c_str(), pos, str.length()); }
size_type find_first_of(const value_type* s,
size_type pos, size_type n) const
{
if (pos > length() || n == 0) return npos;
const_iterator i(begin() + pos),
finish(end());
for (; i != finish; ++i)
{
if (traits_type::find(s, n, *i) != 0)
{
return i - begin();
}
}
return npos;
}
size_type find_first_of(const value_type* s, size_type pos = 0) const
{ return find_first_of(s, pos, traits_type::length(s)); }
size_type find_first_of(value_type c, size_type pos = 0) const
{ return find_first_of(&c, pos, 1); }
size_type find_last_of (const flex_string& str,
size_type pos = npos) const
{ return find_last_of(str.c_str(), pos, str.length()); }
size_type find_last_of (const value_type* s, size_type pos,
size_type n) const
{
if (!empty() && n > 0)
{
pos = Min(pos, length() - 1);
const_iterator i(begin() + pos);
for (;; --i)
{
if (traits_type::find(s, n, *i) != 0)
{
return i - begin();
}
if (i == begin()) break;
}
}
return npos;
}
size_type find_last_of (const value_type* s,
size_type pos = npos) const
{ return find_last_of(s, pos, traits_type::length(s)); }
size_type find_last_of (value_type c, size_type pos = npos) const
{ return find_last_of(&c, pos, 1); }
size_type find_first_not_of(const flex_string& str,
size_type pos = 0) const
{ return find_first_not_of(str.data(), pos, str.size()); }
size_type find_first_not_of(const value_type* s, size_type pos,
size_type n) const
{
if (pos < length())
{
const_iterator
i(begin() + pos),
finish(end());
for (; i != finish; ++i)
{
if (traits_type::find(s, n, *i) == 0)
{
return i - begin();
}
}
}
return npos;
}
size_type find_first_not_of(const value_type* s,
size_type pos = 0) const
{ return find_first_not_of(s, pos, traits_type::length(s)); }
size_type find_first_not_of(value_type c, size_type pos = 0) const
{ return find_first_not_of(&c, pos, 1); }
size_type find_last_not_of(const flex_string& str,
size_type pos = npos) const
{ return find_last_not_of(str.c_str(), pos, str.length()); }
size_type find_last_not_of(const value_type* s, size_type pos,
size_type n) const
{
if (!empty())
{
pos = Min(pos, size() - 1);
const_iterator i(begin() + pos);
for (;; --i)
{
if (traits_type::find(s, n, *i) == 0)
{
return i - begin();
}
if (i == begin()) break;
}
}
return npos;
}
size_type find_last_not_of(const value_type* s,
size_type pos = npos) const
{ return find_last_not_of(s, pos, traits_type::length(s)); }
size_type find_last_not_of (value_type c, size_type pos = npos) const
{ return find_last_not_of(&c, pos, 1); }
flex_string substr(size_type pos = 0, size_type n = npos) const
{
Enforce(pos <= size(), (std::out_of_range*)0, "");
return flex_string(data() + pos, Min(n, size() - pos));
}
std::ptrdiff_t compare(const flex_string& str) const
{ return compare(0, size(), str.data(), str.length()); }
std::ptrdiff_t compare(size_type pos1, size_type n1,
const flex_string& str) const
{ return compare(pos1, n1, str.data(), str.size()); }
std::ptrdiff_t compare(size_type pos1, size_type n1,
const value_type* s, size_type n2 = npos) const
{
Enforce(pos1 <= size(), (std::out_of_range*)0, "");
n1 = Min(size() - pos1, n1);
const std::ptrdiff_t result = traits_type::compare(data() + pos1, s, Min(n1, n2));
return (result != 0) ? result : int(n1 - n2);
}
std::ptrdiff_t compare(size_type pos1, size_type n1,
const flex_string& str,
size_type pos2, size_type n2) const
{
Enforce(pos2 <= str.size(), (std::out_of_range*)0, "");
return compare(pos1, n1, str.data() + pos2, Min(n2, str.size() - pos2));
}
std::ptrdiff_t compare(const value_type* s) const
{ return compare(0, size(), s, traits_type::length(s)); }
};
// non-member functions
template <typename E, class T, class A, class S>
flex_string<E, T, A, S> operator+(const flex_string<E, T, A, S>& lhs,
const flex_string<E, T, A, S>& rhs)
{
flex_string<E, T, A, S> result;
result.reserve(lhs.size() + rhs.size());
result.append(lhs);
result.append(rhs);
return result;
}
template <typename E, class T, class A, class S>
flex_string<E, T, A, S> operator+(const typename flex_string<E, T, A, S>::value_type* lhs,
const flex_string<E, T, A, S>& rhs)
{
flex_string<E, T, A, S> result;
const typename flex_string<E, T, A, S>::size_type len =
flex_string<E, T, A, S>::traits_type::length(lhs);
result.reserve(len + rhs.size());
result.append(lhs, len);
result.append(rhs);
return result;
}
template <typename E, class T, class A, class S>
flex_string<E, T, A, S> operator+(
typename flex_string<E, T, A, S>::value_type lhs,
const flex_string<E, T, A, S>& rhs)
{
flex_string<E, T, A, S> result;
result.reserve(1 + rhs.size());
result.push_back(lhs);
result.append(rhs);
return result;
}
template <typename E, class T, class A, class S>
flex_string<E, T, A, S> operator+(const flex_string<E, T, A, S>& lhs,
const typename flex_string<E, T, A, S>::value_type* rhs)
{
typedef typename flex_string<E, T, A, S>::size_type size_type;
typedef typename flex_string<E, T, A, S>::traits_type traits_type;
flex_string<E, T, A, S> result;
const size_type len = traits_type::length(rhs);
result.reserve(lhs.size() + len);
result.append(lhs);
result.append(rhs, len);
return result;
}
template <typename E, class T, class A, class S>
flex_string<E, T, A, S> operator+(const flex_string<E, T, A, S>& lhs,
typename flex_string<E, T, A, S>::value_type rhs)
{
flex_string<E, T, A, S> result;
result.reserve(lhs.size() + 1);
result.append(lhs);
result.push_back(rhs);
return result;
}
template <typename E, class T, class A, class S>
bool operator==(const flex_string<E, T, A, S>& lhs,
const flex_string<E, T, A, S>& rhs)
{ return lhs.compare(rhs) == 0; }
template <typename E, class T, class A, class S>
bool operator==(const typename flex_string<E, T, A, S>::value_type* lhs,
const flex_string<E, T, A, S>& rhs)
{ return rhs == lhs; }
template <typename E, class T, class A, class S>
bool operator==(const flex_string<E, T, A, S>& lhs,
const typename flex_string<E, T, A, S>::value_type* rhs)
{ return lhs.compare(rhs) == 0; }
template <typename E, class T, class A, class S>
bool operator!=(const flex_string<E, T, A, S>& lhs,
const flex_string<E, T, A, S>& rhs)
{ return !(lhs == rhs); }
template <typename E, class T, class A, class S>
bool operator!=(const typename flex_string<E, T, A, S>::value_type* lhs,
const flex_string<E, T, A, S>& rhs)
{ return !(lhs == rhs); }
template <typename E, class T, class A, class S>
bool operator!=(const flex_string<E, T, A, S>& lhs,
const typename flex_string<E, T, A, S>::value_type* rhs)
{ return !(lhs == rhs); }
template <typename E, class T, class A, class S>
bool operator<(const flex_string<E, T, A, S>& lhs,
const flex_string<E, T, A, S>& rhs)
{ return lhs.compare(rhs) < 0; }
template <typename E, class T, class A, class S>
bool operator<(const flex_string<E, T, A, S>& lhs,
const typename flex_string<E, T, A, S>::value_type* rhs)
{ return lhs.compare(rhs) < 0; }
template <typename E, class T, class A, class S>
bool operator<(const typename flex_string<E, T, A, S>::value_type* lhs,
const flex_string<E, T, A, S>& rhs)
{ return rhs.compare(lhs) > 0; }
template <typename E, class T, class A, class S>
bool operator>(const flex_string<E, T, A, S>& lhs,
const flex_string<E, T, A, S>& rhs)
{ return rhs < lhs; }
template <typename E, class T, class A, class S>
bool operator>(const flex_string<E, T, A, S>& lhs,
const typename flex_string<E, T, A, S>::value_type* rhs)
{ return rhs < lhs; }
template <typename E, class T, class A, class S>
bool operator>(const typename flex_string<E, T, A, S>::value_type* lhs,
const flex_string<E, T, A, S>& rhs)
{ return rhs < lhs; }
template <typename E, class T, class A, class S>
bool operator<=(const flex_string<E, T, A, S>& lhs,
const flex_string<E, T, A, S>& rhs)
{ return !(rhs < lhs); }
template <typename E, class T, class A, class S>
bool operator<=(const flex_string<E, T, A, S>& lhs,
const typename flex_string<E, T, A, S>::value_type* rhs)
{ return !(rhs < lhs); }
template <typename E, class T, class A, class S>
bool operator<=(const typename flex_string<E, T, A, S>::value_type* lhs,
const flex_string<E, T, A, S>& rhs)
{ return !(rhs < lhs); }
template <typename E, class T, class A, class S>
bool operator>=(const flex_string<E, T, A, S>& lhs,
const flex_string<E, T, A, S>& rhs)
{ return !(lhs < rhs); }
template <typename E, class T, class A, class S>
bool operator>=(const flex_string<E, T, A, S>& lhs,
const typename flex_string<E, T, A, S>::value_type* rhs)
{ return !(lhs < rhs); }
template <typename E, class T, class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -