2005-08-27 10:20:46 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// flex_string
|
|
|
|
// Copyright (c) 2001 by Andrei Alexandrescu
|
|
|
|
// Permission to use, copy, modify, distribute and sell this software for any
|
|
|
|
// purpose is hereby granted without fee, provided that the above copyright
|
|
|
|
// notice appear in all copies and that both that copyright notice and this
|
|
|
|
// permission notice appear in supporting documentation.
|
|
|
|
// The author makes no representations about the
|
|
|
|
// suitability of this software for any purpose. It is provided "as is"
|
|
|
|
// without express or implied warranty.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef FLEX_STRING_SHELL_INC_
|
|
|
|
#define FLEX_STRING_SHELL_INC_
|
|
|
|
|
2006-10-17 19:59:11 +00:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template flex_string
|
|
|
|
// This file does not include any storage policy headers
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
#include <cassert>
|
|
|
|
#include <limits>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "flex_string_details.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
// Forward declaration for default storage policy
|
|
|
|
template <typename E, class A> class AllocatorStringStorage;
|
|
|
|
|
|
|
|
|
|
|
|
template <class T> class mallocator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T value_type;
|
|
|
|
typedef value_type* pointer;
|
|
|
|
typedef const value_type* const_pointer;
|
|
|
|
typedef value_type& reference;
|
|
|
|
typedef const value_type& const_reference;
|
|
|
|
typedef std::size_t size_type;
|
|
|
|
//typedef unsigned int size_type;
|
|
|
|
//typedef std::ptrdiff_t difference_type;
|
|
|
|
typedef int difference_type;
|
|
|
|
|
2009-03-10 19:58:26 +00:00
|
|
|
template <class U>
|
2005-08-27 10:20:46 +00:00
|
|
|
struct rebind { typedef mallocator<U> other; };
|
|
|
|
|
|
|
|
mallocator() {}
|
|
|
|
mallocator(const mallocator&) {}
|
2009-03-10 19:58:26 +00:00
|
|
|
//template <class U>
|
2005-08-27 10:20:46 +00:00
|
|
|
//mallocator(const mallocator<U>&) {}
|
|
|
|
~mallocator() {}
|
|
|
|
|
|
|
|
pointer address(reference x) const { return &x; }
|
2009-03-10 19:58:26 +00:00
|
|
|
const_pointer address(const_reference x) const
|
|
|
|
{
|
2005-08-27 10:20:46 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2009-03-10 19:58:26 +00:00
|
|
|
pointer allocate(size_type n, const_pointer = 0)
|
2005-08-27 10:20:46 +00:00
|
|
|
{
|
|
|
|
using namespace std;
|
|
|
|
void* p = malloc(n * sizeof(T));
|
|
|
|
if (!p) throw bad_alloc();
|
|
|
|
return static_cast<pointer>(p);
|
|
|
|
}
|
|
|
|
|
2009-03-10 19:58:26 +00:00
|
|
|
void deallocate(pointer p, size_type)
|
|
|
|
{
|
2005-08-27 10:20:46 +00:00
|
|
|
using namespace std;
|
2009-03-10 19:58:26 +00:00
|
|
|
free(p);
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
|
|
|
|
2009-03-10 19:58:26 +00:00
|
|
|
size_type max_size() const
|
|
|
|
{
|
2005-08-27 10:20:46 +00:00
|
|
|
return static_cast<size_type>(-1) / sizeof(T);
|
|
|
|
}
|
|
|
|
|
2009-03-10 19:58:26 +00:00
|
|
|
void construct(pointer p, const value_type& x)
|
|
|
|
{
|
|
|
|
new(p) value_type(x);
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
|
|
|
|
2009-03-10 19:58:26 +00:00
|
|
|
void destroy(pointer p)
|
|
|
|
{
|
|
|
|
p->~value_type();
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void operator=(const mallocator&);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> class mallocator<void>
|
|
|
|
{
|
|
|
|
typedef void value_type;
|
|
|
|
typedef void* pointer;
|
|
|
|
typedef const void* const_pointer;
|
|
|
|
|
2009-03-10 19:58:26 +00:00
|
|
|
template <class U>
|
2005-08-27 10:20:46 +00:00
|
|
|
struct rebind { typedef mallocator<U> other; };
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2009-03-10 19:58:26 +00:00
|
|
|
inline bool operator==(const mallocator<T>&,
|
2005-08-27 10:20:46 +00:00
|
|
|
const mallocator<T>&) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2009-03-10 19:58:26 +00:00
|
|
|
inline bool operator!=(const mallocator<T>&,
|
2005-08-27 10:20:46 +00:00
|
|
|
const mallocator<T>&) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Allocator>
|
|
|
|
typename Allocator::pointer Reallocate(
|
|
|
|
Allocator& alloc,
|
2009-03-10 19:58:26 +00:00
|
|
|
typename Allocator::pointer p,
|
2005-08-27 10:20:46 +00:00
|
|
|
typename Allocator::size_type oldObjCount,
|
|
|
|
typename Allocator::size_type newObjCount,
|
|
|
|
void*)
|
|
|
|
{
|
|
|
|
// @@@ not implemented
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Allocator>
|
|
|
|
typename Allocator::pointer Reallocate(
|
|
|
|
Allocator& alloc,
|
2009-03-10 19:58:26 +00:00
|
|
|
typename Allocator::pointer p,
|
2005-08-27 10:20:46 +00:00
|
|
|
typename Allocator::size_type oldObjCount,
|
|
|
|
typename Allocator::size_type newObjCount,
|
|
|
|
mallocator<void>*)
|
|
|
|
{
|
|
|
|
// @@@ not implemented
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// class template flex_string
|
2009-03-10 19:58:26 +00:00
|
|
|
// a std::basic_string compatible implementation
|
|
|
|
// Uses a Storage policy
|
2005-08-27 10:20:46 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template <typename E,
|
|
|
|
class T = std::char_traits<E>,
|
|
|
|
class A = std::allocator<E>,
|
|
|
|
class Storage = AllocatorStringStorage<E, A> >
|
|
|
|
class flex_string : private Storage
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename Exception>
|
|
|
|
static void Enforce(bool condition, Exception*, const char* msg)
|
|
|
|
{ if (!condition) throw Exception(msg); }
|
|
|
|
|
|
|
|
bool Sane() const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
begin() <= end() &&
|
|
|
|
empty() == (size() == 0) &&
|
|
|
|
empty() == (begin() == end()) &&
|
|
|
|
size() <= max_size() &&
|
|
|
|
capacity() <= max_size() &&
|
|
|
|
size() <= capacity();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Invariant;
|
|
|
|
friend struct Invariant;
|
|
|
|
struct Invariant
|
|
|
|
{
|
|
|
|
#ifndef NDEBUG
|
2006-06-19 12:39:09 +00:00
|
|
|
Invariant(const flex_string& s) : s_(s)
|
2005-08-27 10:20:46 +00:00
|
|
|
{
|
|
|
|
assert(s_.Sane());
|
|
|
|
}
|
|
|
|
~Invariant()
|
|
|
|
{
|
|
|
|
assert(s_.Sane());
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
const flex_string& s_;
|
|
|
|
#else
|
2009-03-10 19:58:26 +00:00
|
|
|
Invariant(const flex_string&) {}
|
2005-08-27 10:20:46 +00:00
|
|
|
#endif
|
2009-03-02 13:09:05 +00:00
|
|
|
private:
|
2006-06-19 12:39:09 +00:00
|
|
|
Invariant& operator=(const Invariant&);
|
2005-08-27 10:20:46 +00:00
|
|
|
};
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
public:
|
|
|
|
// types
|
|
|
|
typedef T traits_type;
|
|
|
|
typedef typename traits_type::char_type value_type;
|
|
|
|
typedef A allocator_type;
|
|
|
|
typedef typename A::size_type size_type;
|
|
|
|
typedef typename A::difference_type difference_type;
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
typedef typename Storage::reference reference;
|
|
|
|
typedef typename A::const_reference const_reference;
|
|
|
|
typedef typename A::pointer pointer;
|
|
|
|
typedef typename A::const_pointer const_pointer;
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
typedef typename Storage::iterator iterator;
|
|
|
|
typedef typename Storage::const_iterator const_iterator;
|
|
|
|
typedef std::reverse_iterator<iterator
|
|
|
|
#ifdef NO_ITERATOR_TRAITS
|
|
|
|
, value_type
|
|
|
|
#endif
|
|
|
|
> reverse_iterator;
|
|
|
|
typedef std::reverse_iterator<const_iterator
|
|
|
|
#ifdef NO_ITERATOR_TRAITS
|
2009-03-10 19:58:26 +00:00
|
|
|
, const value_type
|
2005-08-27 10:20:46 +00:00
|
|
|
#endif
|
|
|
|
> const_reverse_iterator;
|
|
|
|
|
|
|
|
static const size_type npos; // = size_type(-1)
|
|
|
|
|
|
|
|
private:
|
|
|
|
static size_type Min(size_type lhs, size_type rhs)
|
|
|
|
{ return lhs < rhs ? lhs : rhs; }
|
2006-06-19 12:39:09 +00:00
|
|
|
static size_type Max(size_type lhs, size_type rhs)
|
2006-04-18 09:27:35 +00:00
|
|
|
{ return lhs > rhs ? lhs : rhs; }
|
2006-06-19 12:39:09 +00:00
|
|
|
static void Procust(size_type& n, size_type nmax)
|
2006-03-08 17:07:20 +00:00
|
|
|
{ if (n > nmax) n = nmax; }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
|
|
|
public:
|
2005-08-27 10:20:46 +00:00
|
|
|
// 21.3.1 construct/copy/destroy
|
|
|
|
explicit flex_string(const A& a = A())
|
2009-03-10 19:58:26 +00:00
|
|
|
: Storage(a)
|
2005-08-27 10:20:46 +00:00
|
|
|
{}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string(const flex_string& str)
|
2009-03-10 19:58:26 +00:00
|
|
|
: Storage(str)
|
2005-08-27 10:20:46 +00:00
|
|
|
{}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
|
|
|
flex_string(const flex_string& str, size_type pos,
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type n = npos, const A& a = A())
|
2009-03-10 19:58:26 +00:00
|
|
|
: Storage(a)
|
2005-08-27 10:20:46 +00:00
|
|
|
{
|
|
|
|
assign(str, pos, n);
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string(const value_type* s, const A& a = A())
|
|
|
|
: Storage(s, traits_type::length(s), a)
|
|
|
|
{}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string(const value_type* s, size_type n, const A& a = A())
|
|
|
|
: Storage(s, n, a)
|
|
|
|
{}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string(size_type n, value_type c, const A& a = A())
|
|
|
|
: Storage(n, c, a)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template <class InputIterator>
|
|
|
|
flex_string(InputIterator begin, InputIterator end, const A& a = A())
|
|
|
|
: Storage(a)
|
|
|
|
{
|
|
|
|
assign(begin, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
~flex_string()
|
|
|
|
{}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& operator=(const flex_string& str)
|
|
|
|
{
|
|
|
|
Storage& s = *this;
|
|
|
|
s = str;
|
|
|
|
return *this;
|
2009-03-10 19:58:26 +00:00
|
|
|
}
|
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& operator=(const value_type* s)
|
|
|
|
{
|
|
|
|
assign(s);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
flex_string& operator=(value_type c)
|
|
|
|
{
|
|
|
|
assign(1, c);
|
|
|
|
return *this;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
// 21.3.2 iterators:
|
|
|
|
iterator begin()
|
|
|
|
{ return Storage::begin(); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
const_iterator begin() const
|
|
|
|
{ return Storage::begin(); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
iterator end()
|
|
|
|
{ return Storage::end(); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
const_iterator end() const
|
|
|
|
{ return Storage::end(); }
|
|
|
|
|
|
|
|
reverse_iterator rbegin()
|
|
|
|
{ return reverse_iterator(end()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
const_reverse_iterator rbegin() const
|
|
|
|
{ return const_reverse_iterator(end()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
reverse_iterator rend()
|
|
|
|
{ return reverse_iterator(begin()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
const_reverse_iterator rend() const
|
|
|
|
{ return const_reverse_iterator(begin()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
// 21.3.3 capacity:
|
|
|
|
size_type size() const
|
|
|
|
{ return Storage::size(); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type length() const
|
|
|
|
{ return size(); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type max_size() const
|
|
|
|
{ return Storage::max_size(); }
|
|
|
|
|
|
|
|
void resize(size_type n, value_type c)
|
|
|
|
{ Storage::resize(n, c); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
void resize(size_type n)
|
|
|
|
{ resize(n, value_type()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type capacity() const
|
|
|
|
{ return Storage::capacity(); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
void reserve(size_type res_arg = 0)
|
|
|
|
{
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(res_arg <= max_size(), static_cast<std::length_error*>(0), "");
|
2005-08-27 10:20:46 +00:00
|
|
|
Storage::reserve(res_arg);
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
void clear()
|
2009-03-10 19:58:26 +00:00
|
|
|
{ resize(0); }
|
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
bool empty() const
|
|
|
|
{ return size() == 0; }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
// 21.3.4 element access:
|
|
|
|
const_reference operator[](size_type pos) const
|
|
|
|
{ return *(c_str() + pos); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
reference operator[](size_type pos)
|
|
|
|
{ return *(begin() + pos); }
|
|
|
|
|
|
|
|
const_reference at(size_type n) const
|
|
|
|
{
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(n <= size(), static_cast<std::out_of_range*>(0), "");
|
2005-08-27 10:20:46 +00:00
|
|
|
return (*this)[n];
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
reference at(size_type n)
|
|
|
|
{
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(n < size(), static_cast<std::out_of_range*>(0), "");
|
2005-08-27 10:20:46 +00:00
|
|
|
return (*this)[n];
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
// 21.3.5 modifiers:
|
|
|
|
flex_string& operator+=(const flex_string& str)
|
|
|
|
{ return append(str); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& operator+=(const value_type* s)
|
|
|
|
{ return append(s); }
|
|
|
|
|
|
|
|
flex_string& operator+=(const value_type c)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
push_back(c);
|
2005-08-27 10:20:46 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& append(const flex_string& str)
|
|
|
|
{ return append(str.data(), str.length()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& append(const flex_string& str, const size_type pos,
|
|
|
|
size_type n)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2005-08-27 10:20:46 +00:00
|
|
|
const size_type sz = str.size();
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(pos <= sz, static_cast<std::out_of_range*>(0), "");
|
2006-06-19 12:39:09 +00:00
|
|
|
Procust(n, sz - pos);
|
2009-03-10 19:58:26 +00:00
|
|
|
return append(str.data() + pos, n);
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& append(const value_type* s, const size_type n)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
|
|
|
Invariant checker(*this);
|
|
|
|
(void) checker;
|
2009-03-11 17:58:49 +00:00
|
|
|
if (IsAliasedRange(s, s + n))
|
2006-03-08 17:07:20 +00:00
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
const size_type offset = s - &*begin();
|
|
|
|
Storage::reserve(size() + n);
|
|
|
|
s = &*begin() + offset;
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
Storage::append(s, s + n);
|
2005-08-27 10:20:46 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& append(const value_type* s)
|
|
|
|
{ return append(s, traits_type::length(s)); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& append(size_type n, value_type c)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2005-08-27 10:20:46 +00:00
|
|
|
resize(size() + n, c);
|
|
|
|
return *this;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
template<class InputIterator>
|
|
|
|
flex_string& append(InputIterator first, InputIterator last)
|
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
insert(end(), first, last);
|
2005-08-27 10:20:46 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
void push_back(const value_type c) // primitive
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2005-08-27 10:20:46 +00:00
|
|
|
const size_type cap = capacity();
|
|
|
|
if (size() == cap)
|
|
|
|
{
|
|
|
|
reserve(cap << 1u);
|
|
|
|
}
|
2006-06-19 12:39:09 +00:00
|
|
|
Storage::append(&c, &c + 1);
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flex_string& assign(const flex_string& str)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2005-08-27 10:20:46 +00:00
|
|
|
if (&str == this) return *this;
|
|
|
|
return assign(str.data(), str.size());
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& assign(const flex_string& str, const size_type pos,
|
|
|
|
size_type n)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
const size_type sz = str.size();
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(pos <= sz, static_cast<std::out_of_range*>(0), "");
|
2006-06-19 12:39:09 +00:00
|
|
|
Procust(n, sz - pos);
|
2005-08-27 10:20:46 +00:00
|
|
|
return assign(str.data() + pos, n);
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& assign(const value_type* s, const size_type n)
|
|
|
|
{
|
2009-03-10 19:58:26 +00:00
|
|
|
Invariant checker(*this);
|
|
|
|
(void) checker;
|
2005-08-27 10:20:46 +00:00
|
|
|
if (size() >= n)
|
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
std::copy(s, s + n, begin());
|
2005-08-27 10:20:46 +00:00
|
|
|
resize(n);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
const value_type *const s2 = s + size();
|
|
|
|
std::copy(s, s2, begin());
|
2005-08-27 10:20:46 +00:00
|
|
|
append(s2, n - size());
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& assign(const value_type* s)
|
|
|
|
{ return assign(s, traits_type::length(s)); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
template <class ItOrLength, class ItOrChar>
|
|
|
|
flex_string& assign(ItOrLength first_or_n, ItOrChar last_or_c)
|
|
|
|
{ return replace(begin(), end(), first_or_n, last_or_c); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& insert(size_type pos1, const flex_string& str)
|
|
|
|
{ return insert(pos1, str.data(), str.size()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& insert(size_type pos1, const flex_string& str,
|
|
|
|
size_type pos2, size_type n)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(pos2 <= str.length(), static_cast<std::out_of_range*>(0), "");
|
2006-06-19 12:39:09 +00:00
|
|
|
Procust(n, str.length() - pos2);
|
2009-03-10 19:58:26 +00:00
|
|
|
return insert(pos1, str.data() + pos2, n);
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& insert(size_type pos, const value_type* s, size_type n)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(pos <= length(), static_cast<std::out_of_range*>(0), "");
|
2009-03-10 19:58:26 +00:00
|
|
|
insert(begin() + pos, s, s + n);
|
2006-06-19 12:39:09 +00:00
|
|
|
return *this;
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& insert(size_type pos, const value_type* s)
|
|
|
|
{ return insert(pos, s, traits_type::length(s)); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& insert(size_type pos, size_type n, value_type c)
|
|
|
|
{
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(pos <= length(), static_cast<std::out_of_range*>(0), "");
|
2006-06-19 12:39:09 +00:00
|
|
|
insert(begin() + pos, n, c);
|
|
|
|
return *this;
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
|
|
|
iterator insert(const iterator p, const value_type c)
|
2005-08-27 10:20:46 +00:00
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
const size_type pos = p - begin();
|
2005-08-27 10:20:46 +00:00
|
|
|
insert(p, 1, c);
|
|
|
|
return begin() + pos;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
private:
|
2009-03-11 17:58:49 +00:00
|
|
|
|
|
|
|
// Care must be taken when dereferencing some iterator types.
|
|
|
|
//
|
|
|
|
// Users can implement this function in their namespace if their storage uses a special iterator type,
|
|
|
|
// the function will be found through ADL.
|
|
|
|
template<class Iterator>
|
|
|
|
const typename std::iterator_traits<Iterator>::value_type * DereferenceValidIterator(Iterator it) const
|
|
|
|
{
|
|
|
|
return &*it;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Care must be taken when dereferencing a reverse iterators, hence this special case.
|
|
|
|
// This isn't in the std namespace so as not to pollute it or create name clashes.
|
|
|
|
template<typename Iterator>
|
|
|
|
const typename std::iterator_traits<Iterator>::value_type * DereferenceValidIterator(
|
|
|
|
std::reverse_iterator<Iterator> it) const
|
|
|
|
{
|
|
|
|
return &*--it;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if the range aliases the current string.
|
|
|
|
//
|
|
|
|
// This method cannot be const because calling begin/end on copy-on-write implementations must have side effects.
|
|
|
|
// A const version wouldn't make the string unique through this call.
|
|
|
|
template<class Iterator>
|
|
|
|
bool IsAliasedRange(Iterator beginIterator, Iterator endIterator)
|
|
|
|
{
|
|
|
|
if(!empty() && beginIterator != endIterator)
|
|
|
|
{
|
|
|
|
typedef const typename std::iterator_traits<Iterator>::value_type * pointer;
|
|
|
|
pointer myBegin(&*begin());
|
|
|
|
pointer myEnd(&*begin() + size());
|
|
|
|
pointer rangeBegin(DereferenceValidIterator(beginIterator));
|
|
|
|
const std::less_equal<pointer> less_equal = std::less_equal<pointer>();
|
|
|
|
if(less_equal(myBegin, rangeBegin) && less_equal(rangeBegin, myEnd))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
template <int i> class Selector {};
|
|
|
|
|
2009-03-10 19:58:26 +00:00
|
|
|
flex_string& InsertImplDiscr(iterator p,
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type n, value_type c, Selector<1>)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
|
|
|
Invariant checker(*this);
|
|
|
|
(void) checker;
|
2009-03-11 17:58:49 +00:00
|
|
|
assert(begin() <= p && p <= end());
|
2009-03-12 12:28:58 +00:00
|
|
|
const size_type insertOffset(p - begin());
|
|
|
|
const size_type originalSize(size());
|
|
|
|
if(n < originalSize - insertOffset)
|
2006-03-08 17:07:20 +00:00
|
|
|
{
|
2009-03-12 12:28:58 +00:00
|
|
|
// The new characters fit within the original string.
|
|
|
|
// The characters that are pushed back need to be moved because they're aliased.
|
|
|
|
// The appended characters will all be overwritten by the move.
|
|
|
|
append(n, value_type(0));
|
|
|
|
value_type * begin(&*begin());
|
|
|
|
flex_string_details::pod_move(begin + insertOffset, begin + originalSize, begin + insertOffset + n);
|
|
|
|
std::fill(begin + insertOffset, begin + insertOffset + n, c);
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2006-06-19 12:39:09 +00:00
|
|
|
else
|
2006-03-08 17:07:20 +00:00
|
|
|
{
|
2009-03-12 12:28:58 +00:00
|
|
|
// The new characters exceed the original string.
|
|
|
|
// The characters that are pushed back can simply be copied since they aren't aliased.
|
|
|
|
// The appended characters will partly be overwritten by the copy.
|
|
|
|
append(n, c);
|
|
|
|
value_type * begin(&*begin());
|
|
|
|
flex_string_details::pod_copy(begin + insertOffset, begin + originalSize, begin + insertOffset + n);
|
|
|
|
std::fill(begin + insertOffset, begin + originalSize, c);
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2006-06-19 12:39:09 +00:00
|
|
|
return *this;
|
2009-03-10 19:58:26 +00:00
|
|
|
}
|
2005-08-27 10:20:46 +00:00
|
|
|
|
|
|
|
template<class InputIterator>
|
|
|
|
flex_string& InsertImplDiscr(iterator i,
|
|
|
|
InputIterator b, InputIterator e, Selector<0>)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
|
|
|
InsertImpl(i, b, e,
|
2006-06-19 12:39:09 +00:00
|
|
|
typename std::iterator_traits<InputIterator>::iterator_category());
|
2005-08-27 10:20:46 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2006-06-19 12:39:09 +00:00
|
|
|
template <class FwdIterator>
|
2005-08-27 10:20:46 +00:00
|
|
|
void InsertImpl(iterator i,
|
2006-06-19 12:39:09 +00:00
|
|
|
FwdIterator s1, FwdIterator s2, std::forward_iterator_tag)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2009-03-11 17:58:49 +00:00
|
|
|
if(s1 == s2)
|
|
|
|
{
|
|
|
|
// Insert an empty range.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(IsAliasedRange(s1, s2))
|
|
|
|
{
|
|
|
|
// The source range is contained in the current string, copy it and recurse.
|
|
|
|
const flex_string temporary(s1, s2);
|
|
|
|
InsertImpl(i, temporary.begin(), temporary.end(),
|
|
|
|
typename std::iterator_traits<FwdIterator>::iterator_category());
|
|
|
|
return;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
|
|
|
Invariant checker(*this);
|
2006-03-08 17:07:20 +00:00
|
|
|
(void) checker;
|
2009-03-11 17:58:49 +00:00
|
|
|
|
2006-06-19 12:39:09 +00:00
|
|
|
const size_type pos = i - begin();
|
2009-03-10 19:58:26 +00:00
|
|
|
const typename std::iterator_traits<FwdIterator>::difference_type n2 =
|
2006-06-19 12:39:09 +00:00
|
|
|
std::distance(s1, s2);
|
|
|
|
assert(n2 >= 0);
|
2005-08-27 10:20:46 +00:00
|
|
|
using namespace flex_string_details;
|
|
|
|
assert(pos <= size());
|
|
|
|
|
2009-03-10 19:58:26 +00:00
|
|
|
const typename std::iterator_traits<FwdIterator>::difference_type maxn2 =
|
2006-06-19 12:39:09 +00:00
|
|
|
capacity() - size();
|
2005-08-27 10:20:46 +00:00
|
|
|
if (maxn2 < n2)
|
|
|
|
{
|
2009-03-11 17:58:49 +00:00
|
|
|
// Reallocate the string.
|
|
|
|
assert(!IsAliasedRange(s1, s2));
|
2005-08-27 10:20:46 +00:00
|
|
|
reserve(size() + n2);
|
2006-06-19 12:39:09 +00:00
|
|
|
i = begin() + pos;
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2006-06-19 12:39:09 +00:00
|
|
|
if (pos + n2 <= size())
|
2006-03-08 17:07:20 +00:00
|
|
|
{
|
|
|
|
//const iterator oldEnd = end();
|
|
|
|
//Storage::append(oldEnd - n2, n2);
|
|
|
|
//std::copy(i, oldEnd - n2, i + n2);
|
2006-06-19 12:39:09 +00:00
|
|
|
const iterator tailBegin = end() - n2;
|
|
|
|
Storage::append(tailBegin, tailBegin + n2);
|
2006-03-08 17:07:20 +00:00
|
|
|
//std::copy(i, tailBegin, i + n2);
|
2009-03-10 19:58:26 +00:00
|
|
|
std::copy(reverse_iterator(tailBegin), reverse_iterator(i),
|
2006-06-19 12:39:09 +00:00
|
|
|
reverse_iterator(tailBegin + n2));
|
|
|
|
std::copy(s1, s2, i);
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2006-06-19 12:39:09 +00:00
|
|
|
else
|
2006-03-08 17:07:20 +00:00
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
FwdIterator t = s1;
|
|
|
|
const size_type old_size = size();
|
|
|
|
std::advance(t, old_size - pos);
|
|
|
|
assert(std::distance(t, s2) >= 0);
|
|
|
|
Storage::append(t, s2);
|
|
|
|
Storage::append(data() + pos, data() + old_size);
|
|
|
|
std::copy(s1, t, i);
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-19 12:39:09 +00:00
|
|
|
template <class InputIterator>
|
2009-03-13 13:54:51 +00:00
|
|
|
void InsertImpl(iterator insertPosition,
|
|
|
|
InputIterator inputBegin, InputIterator inputEnd, std::input_iterator_tag)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2009-03-13 13:54:51 +00:00
|
|
|
flex_string temporary(begin(), insertPosition);
|
|
|
|
for (; inputBegin != inputEnd; ++inputBegin)
|
2006-03-08 17:07:20 +00:00
|
|
|
{
|
2009-03-13 13:54:51 +00:00
|
|
|
temporary.push_back(*inputBegin);
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2009-03-13 13:54:51 +00:00
|
|
|
temporary.append(insertPosition, end());
|
|
|
|
swap(temporary);
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <class ItOrLength, class ItOrChar>
|
|
|
|
void insert(iterator p, ItOrLength first_or_n, ItOrChar last_or_c)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
Selector<std::numeric_limits<ItOrLength>::is_specialized> sel;
|
|
|
|
InsertImplDiscr(p, first_or_n, last_or_c, sel);
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& erase(size_type pos = 0, size_type n = npos)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
|
|
|
Invariant checker(*this);
|
2006-03-08 17:07:20 +00:00
|
|
|
(void) checker;
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(pos <= length(), static_cast<std::out_of_range*>(0), "");
|
2006-06-19 12:39:09 +00:00
|
|
|
Procust(n, length() - pos);
|
|
|
|
std::copy(begin() + pos + n, end(), begin() + pos);
|
|
|
|
resize(length() - n);
|
2005-08-27 10:20:46 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
iterator erase(iterator position)
|
|
|
|
{
|
|
|
|
const size_type pos(position - begin());
|
|
|
|
erase(pos, 1);
|
|
|
|
return begin() + pos;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
iterator erase(iterator first, iterator last)
|
|
|
|
{
|
|
|
|
const size_type pos(first - begin());
|
|
|
|
erase(pos, last - first);
|
|
|
|
return begin() + pos;
|
|
|
|
}
|
|
|
|
|
2006-03-08 17:07:20 +00:00
|
|
|
// Replaces at most n1 chars of *this, starting with pos1 with the content of str
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& replace(size_type pos1, size_type n1, const flex_string& str)
|
|
|
|
{ return replace(pos1, n1, str.data(), str.size()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2006-03-08 17:07:20 +00:00
|
|
|
// Replaces at most n1 chars of *this, starting with pos1,
|
|
|
|
// with at most n2 chars of str starting with pos2
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& replace(size_type pos1, size_type n1, const flex_string& str,
|
|
|
|
size_type pos2, size_type n2)
|
|
|
|
{
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(pos2 <= str.length(), static_cast<std::out_of_range*>(0), "");
|
2009-03-10 19:58:26 +00:00
|
|
|
return replace(pos1, n1, str.data() + pos2,
|
2005-08-27 10:20:46 +00:00
|
|
|
Min(n2, str.size() - pos2));
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
/*
|
2006-03-08 17:07:20 +00:00
|
|
|
// Replaces at most n1 chars of *this, starting with pos,
|
|
|
|
// with at most n2 chars of str.
|
|
|
|
// str must have at least n2 chars.
|
2009-03-10 19:58:26 +00:00
|
|
|
flex_string& replace(const size_type pos, size_type n1,
|
2006-06-19 12:39:09 +00:00
|
|
|
const value_type* s1, const size_type n2)
|
2005-08-27 10:20:46 +00:00
|
|
|
{
|
2009-03-10 19:58:26 +00:00
|
|
|
Invariant checker(*this);
|
2006-03-08 17:07:20 +00:00
|
|
|
(void) checker;
|
2005-08-27 10:20:46 +00:00
|
|
|
Enforce(pos <= size(), (std::out_of_range*)0, "");
|
|
|
|
Procust(n1, size() - pos);
|
2006-06-19 12:39:09 +00:00
|
|
|
const iterator b = begin() + pos;
|
|
|
|
return replace(b, b + n1, s1, s1 + n2);
|
2005-08-27 10:20:46 +00:00
|
|
|
using namespace flex_string_details;
|
|
|
|
const int delta = int(n2 - n1);
|
2009-03-11 17:58:49 +00:00
|
|
|
const bool aliased = IsAliasedRange(s1, s1 + n2);
|
2005-08-27 10:20:46 +00:00
|
|
|
|
2006-03-08 17:07:20 +00:00
|
|
|
// From here on we're dealing with an aliased replace
|
2006-06-19 12:39:09 +00:00
|
|
|
if (delta <= 0)
|
2006-03-08 17:07:20 +00:00
|
|
|
{
|
|
|
|
// simple case, we're shrinking
|
2005-08-27 10:20:46 +00:00
|
|
|
pod_move(s1, s1 + n2, &*begin() + pos);
|
|
|
|
pod_move(&*begin() + pos + n1, &*end(), &*begin() + pos + n1 + delta);
|
|
|
|
resize(size() + delta);
|
2006-06-19 12:39:09 +00:00
|
|
|
return *this;
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2005-08-27 10:20:46 +00:00
|
|
|
|
2006-03-08 17:07:20 +00:00
|
|
|
// From here on we deal with aliased growth
|
2005-08-27 10:20:46 +00:00
|
|
|
if (capacity() < size() + delta)
|
|
|
|
{
|
|
|
|
// realloc the string
|
|
|
|
const size_type offset = s1 - data();
|
|
|
|
reserve(size() + delta);
|
|
|
|
s1 = data() + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
const value_type* s2 = s1 + n2;
|
|
|
|
value_type* d1 = &*begin() + pos;
|
|
|
|
value_type* d2 = d1 + n1;
|
|
|
|
|
|
|
|
const int tailLen = int(&*end() - d2);
|
|
|
|
|
|
|
|
if (delta <= tailLen)
|
|
|
|
{
|
|
|
|
value_type* oldEnd = &*end();
|
|
|
|
// simple case
|
|
|
|
Storage::append(oldEnd - delta, delta);
|
|
|
|
|
|
|
|
pod_move(d2, d2 + (tailLen - delta), d2 + delta);
|
|
|
|
if (le(d2, s1))
|
|
|
|
{
|
|
|
|
pod_copy(s1 + delta, s2 + delta, d1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// d2 > s1
|
|
|
|
if (le(d2, s2))
|
|
|
|
{
|
|
|
|
pod_move(s1, d2, d1);
|
|
|
|
pod_move(d2 + delta, s2 + delta, d1 + (d2 - s1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pod_move(s1, s2, d1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const size_type sz = delta - tailLen;
|
|
|
|
Storage::append(s2 - sz, sz);
|
|
|
|
Storage::append(d2, tailLen);
|
|
|
|
pod_move(s1, s2 - (delta - tailLen), d1);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2006-03-08 17:07:20 +00:00
|
|
|
// Replaces at most n1 chars of *this, starting with pos, with chars from s
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& replace(size_type pos, size_type n1, const value_type* s)
|
|
|
|
{ return replace(pos, n1, s, traits_type::length(s)); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2006-03-08 17:07:20 +00:00
|
|
|
// Replaces at most n1 chars of *this, starting with pos, with n2 occurences of c
|
|
|
|
// consolidated with
|
|
|
|
// Replaces at most n1 chars of *this, starting with pos,
|
|
|
|
// with at most n2 chars of str.
|
|
|
|
// str must have at least n2 chars.
|
2005-08-27 10:20:46 +00:00
|
|
|
template <class StrOrLength, class NumOrChar>
|
2009-03-10 19:58:26 +00:00
|
|
|
flex_string& replace(size_type pos, size_type n1,
|
2006-06-19 12:39:09 +00:00
|
|
|
StrOrLength s_or_n2, NumOrChar n_or_c)
|
2005-08-27 10:20:46 +00:00
|
|
|
{
|
2009-03-10 19:58:26 +00:00
|
|
|
Invariant checker(*this);
|
2006-03-08 17:07:20 +00:00
|
|
|
(void) checker;
|
2006-06-19 12:39:09 +00:00
|
|
|
Enforce(pos <= size(), static_cast<std::out_of_range*>(0), "");
|
|
|
|
Procust(n1, length() - pos);
|
|
|
|
const iterator b = begin() + pos;
|
|
|
|
return replace(b, b + n1, s_or_n2, n_or_c);
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& replace(iterator i1, iterator i2, const flex_string& str)
|
|
|
|
{ return replace(i1, i2, str.data(), str.length()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& replace(iterator i1, iterator i2, const value_type* s)
|
|
|
|
{ return replace(i1, i2, s, traits_type::length(s)); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
private:
|
2009-03-10 19:58:26 +00:00
|
|
|
flex_string& ReplaceImplDiscr(iterator i1, iterator i2,
|
2005-08-27 10:20:46 +00:00
|
|
|
const value_type* s, size_type n, Selector<2>)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
assert(i1 <= i2);
|
|
|
|
assert(begin() <= i1 && i1 <= end());
|
|
|
|
assert(begin() <= i2 && i2 <= end());
|
2009-03-10 19:58:26 +00:00
|
|
|
return replace(i1, i2, s, s + n);
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string& ReplaceImplDiscr(iterator i1, iterator i2,
|
|
|
|
size_type n2, value_type c, Selector<1>)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
const size_type n1 = i2 - i1;
|
|
|
|
if (n1 > n2)
|
2006-03-08 17:07:20 +00:00
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
std::fill(i1, i1 + n2, c);
|
|
|
|
erase(i1 + n2, i2);
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2006-06-19 12:39:09 +00:00
|
|
|
else
|
2006-03-08 17:07:20 +00:00
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
std::fill(i1, i2, c);
|
|
|
|
insert(i2, n2 - n1, c);
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2005-08-27 10:20:46 +00:00
|
|
|
return *this;
|
2009-03-10 19:58:26 +00:00
|
|
|
}
|
2005-08-27 10:20:46 +00:00
|
|
|
|
2006-06-19 12:39:09 +00:00
|
|
|
template <class InputIterator>
|
|
|
|
flex_string& ReplaceImplDiscr(iterator i1, iterator i2,
|
2005-08-27 10:20:46 +00:00
|
|
|
InputIterator b, InputIterator e, Selector<0>)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
|
|
|
ReplaceImpl(i1, i2, b, e,
|
2006-06-19 12:39:09 +00:00
|
|
|
typename std::iterator_traits<InputIterator>::iterator_category());
|
2005-08-27 10:20:46 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2006-06-19 12:39:09 +00:00
|
|
|
template <class FwdIterator>
|
2005-08-27 10:20:46 +00:00
|
|
|
void ReplaceImpl(iterator i1, iterator i2,
|
2006-06-19 12:39:09 +00:00
|
|
|
FwdIterator s1, FwdIterator s2, std::forward_iterator_tag)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
|
|
|
Invariant checker(*this);
|
2006-03-08 17:07:20 +00:00
|
|
|
(void) checker;
|
2009-03-10 19:58:26 +00:00
|
|
|
const typename std::iterator_traits<iterator>::difference_type n1 =
|
2006-06-19 12:39:09 +00:00
|
|
|
i2 - i1;
|
|
|
|
assert(n1 >= 0);
|
2009-03-03 13:32:21 +00:00
|
|
|
|
2009-03-10 19:58:26 +00:00
|
|
|
const typename std::iterator_traits<FwdIterator>::difference_type n2 =
|
2006-06-19 12:39:09 +00:00
|
|
|
std::distance(s1, s2);
|
2009-03-03 16:29:14 +00:00
|
|
|
assert(n2 >= 0);
|
2009-03-03 13:32:21 +00:00
|
|
|
|
2009-03-11 17:58:49 +00:00
|
|
|
if (IsAliasedRange(s1, s2))
|
2006-03-08 17:07:20 +00:00
|
|
|
{
|
2009-03-11 17:58:49 +00:00
|
|
|
// Aliased replace, copy to new string.
|
|
|
|
flex_string temporary;
|
|
|
|
temporary.reserve(size() - n1 + n2);
|
|
|
|
temporary.append(begin(), i1).append(s1, s2).append(i2, end());
|
|
|
|
swap(temporary);
|
|
|
|
return;
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
|
|
|
|
2006-06-19 12:39:09 +00:00
|
|
|
if (n1 > n2)
|
2006-03-08 17:07:20 +00:00
|
|
|
{
|
2009-03-11 17:58:49 +00:00
|
|
|
// Shrinks.
|
2006-06-19 12:39:09 +00:00
|
|
|
std::copy(s1, s2, i1);
|
|
|
|
erase(i1 + n2, i2);
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2006-06-19 12:39:09 +00:00
|
|
|
else
|
2006-03-08 17:07:20 +00:00
|
|
|
{
|
2009-03-11 17:58:49 +00:00
|
|
|
// Grows.
|
2006-06-19 12:39:09 +00:00
|
|
|
flex_string_details::copy_n(s1, n1, i1);
|
|
|
|
std::advance(s1, n1);
|
|
|
|
insert(i2, s1, s2);
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
|
|
|
|
2006-06-19 12:39:09 +00:00
|
|
|
template <class InputIterator>
|
2005-08-27 10:20:46 +00:00
|
|
|
void ReplaceImpl(iterator i1, iterator i2,
|
2006-06-19 12:39:09 +00:00
|
|
|
InputIterator b, InputIterator e, std::input_iterator_tag)
|
2005-08-27 10:20:46 +00:00
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
flex_string temp(begin(), i1);
|
|
|
|
temp.append(b, e).append(i2, end());
|
|
|
|
swap(temp);
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <class T1, class T2>
|
|
|
|
flex_string& replace(iterator i1, iterator i2,
|
|
|
|
T1 first_or_n_or_s, T2 last_or_c_or_n)
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
|
|
|
const bool
|
2006-06-19 12:39:09 +00:00
|
|
|
num1 = std::numeric_limits<T1>::is_specialized,
|
|
|
|
num2 = std::numeric_limits<T2>::is_specialized;
|
2009-03-10 19:58:26 +00:00
|
|
|
return ReplaceImplDiscr(i1, i2, first_or_n_or_s, last_or_c_or_n,
|
|
|
|
Selector<num1 ? (num2 ? 1 : -1) : (num2 ? 2 : 0)>());
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type copy(value_type* s, size_type n, size_type pos = 0) const
|
|
|
|
{
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(pos <= size(), static_cast<std::out_of_range*>(0), "");
|
2005-08-27 10:20:46 +00:00
|
|
|
Procust(n, size() - pos);
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string_details::pod_copy(
|
|
|
|
data() + pos,
|
|
|
|
data() + pos + n,
|
|
|
|
s);
|
|
|
|
return n;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
void swap(flex_string& rhs)
|
|
|
|
{
|
|
|
|
Storage& srhs = rhs;
|
|
|
|
this->Storage::swap(srhs);
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
// 21.3.6 string operations:
|
|
|
|
const value_type* c_str() const
|
|
|
|
{ return Storage::c_str(); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
const value_type* data() const
|
|
|
|
{ return Storage::data(); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
allocator_type get_allocator() const
|
|
|
|
{ return Storage::get_allocator(); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type find(const flex_string& str, size_type pos = 0) const
|
|
|
|
{ return find(str.data(), pos, str.length()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type find (const value_type* s, size_type pos, size_type n) const
|
|
|
|
{
|
2009-03-05 21:12:02 +00:00
|
|
|
const size_type size(size());
|
|
|
|
if (n + pos > size)
|
2009-01-26 01:55:50 +00:00
|
|
|
return npos;
|
2009-03-05 21:12:02 +00:00
|
|
|
for (; pos + n <= size; ++pos)
|
2005-08-27 10:20:46 +00:00
|
|
|
{
|
|
|
|
if (traits_type::compare(data() + pos, s, n) == 0)
|
|
|
|
{
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
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); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type rfind(const flex_string& str, size_type pos = npos) const
|
|
|
|
{ return rfind(str.data(), pos, str.length()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
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)
|
|
|
|
{
|
2009-03-10 19:58:26 +00:00
|
|
|
if (traits_type::eq(*i, *s)
|
2005-08-27 10:20:46 +00:00
|
|
|
&& 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); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type find_first_of(const flex_string& str, size_type pos = 0) const
|
|
|
|
{ return find_first_of(str.data(), pos, str.length()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
|
|
|
size_type find_first_of(const value_type* s,
|
2005-08-27 10:20:46 +00:00
|
|
|
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;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type find_first_of(const value_type* s, size_type pos = 0) const
|
|
|
|
{ return find_first_of(s, pos, traits_type::length(s)); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type find_first_of(value_type c, size_type pos = 0) const
|
|
|
|
{ return find_first_of(&c, pos, 1); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type find_last_of (const flex_string& str,
|
|
|
|
size_type pos = npos) const
|
|
|
|
{ return find_last_of(str.data(), pos, str.length()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
|
|
|
size_type find_last_of (const value_type* s, size_type pos,
|
2005-08-27 10:20:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-03-10 19:58:26 +00:00
|
|
|
size_type find_last_of (const value_type* s,
|
2005-08-27 10:20:46 +00:00
|
|
|
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); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
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()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type find_first_not_of(const value_type* s, size_type pos,
|
|
|
|
size_type n) const
|
|
|
|
{
|
|
|
|
if (pos < length())
|
|
|
|
{
|
2009-03-10 19:58:26 +00:00
|
|
|
const_iterator
|
2005-08-27 10:20:46 +00:00
|
|
|
i(begin() + pos),
|
|
|
|
finish(end());
|
|
|
|
for (; i != finish; ++i)
|
|
|
|
{
|
|
|
|
if (traits_type::find(s, n, *i) == 0)
|
|
|
|
{
|
|
|
|
return i - begin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
|
|
|
size_type find_first_not_of(const value_type* s,
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type pos = 0) const
|
|
|
|
{ return find_first_not_of(s, pos, traits_type::length(s)); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type find_first_not_of(value_type c, size_type pos = 0) const
|
|
|
|
{ return find_first_not_of(&c, pos, 1); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type find_last_not_of(const flex_string& str,
|
|
|
|
size_type pos = npos) const
|
|
|
|
{ return find_last_not_of(str.data(), pos, str.length()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type find_last_not_of(const value_type* s, size_type pos,
|
|
|
|
size_type n) const
|
|
|
|
{
|
|
|
|
if (!this->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;
|
|
|
|
}
|
|
|
|
|
2009-03-10 19:58:26 +00:00
|
|
|
size_type find_last_not_of(const value_type* s,
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type pos = npos) const
|
|
|
|
{ return find_last_not_of(s, pos, traits_type::length(s)); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
size_type find_last_not_of (value_type c, size_type pos = npos) const
|
|
|
|
{ return find_last_not_of(&c, pos, 1); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string substr(size_type pos = 0, size_type n = npos) const
|
|
|
|
{
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(pos <= size(), static_cast<std::out_of_range*>(0), "");
|
2005-08-27 10:20:46 +00:00
|
|
|
return flex_string(data() + pos, Min(n, size() - pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
int compare(const flex_string& str) const
|
2009-03-10 19:58:26 +00:00
|
|
|
{
|
2006-03-08 17:07:20 +00:00
|
|
|
// FIX due to Goncalo N M de Carvalho July 18, 2005
|
2006-06-19 12:39:09 +00:00
|
|
|
return compare(0, size(), str);
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
int compare(size_type pos1, size_type n1,
|
|
|
|
const flex_string& str) const
|
|
|
|
{ return compare(pos1, n1, str.data(), str.size()); }
|
2009-03-10 19:58:26 +00:00
|
|
|
|
|
|
|
// FIX to compare: added the TC
|
2006-03-08 17:07:20 +00:00
|
|
|
// (http://www.comeaucomputing.com/iso/lwg-defects.html number 5)
|
|
|
|
// Thanks to Caleb Epstein for the fix
|
2005-08-27 10:20:46 +00:00
|
|
|
|
|
|
|
int compare(size_type pos1, size_type n1,
|
|
|
|
const value_type* s) const
|
|
|
|
{
|
2006-06-19 12:39:09 +00:00
|
|
|
return compare(pos1, n1, s, traits_type::length(s));
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
int compare(size_type pos1, size_type n1,
|
|
|
|
const value_type* s, size_type n2) const
|
|
|
|
{
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(pos1 <= size(), static_cast<std::out_of_range*>(0), "");
|
2009-03-02 14:54:16 +00:00
|
|
|
Procust(n1, size() - pos1);
|
|
|
|
// The line below fixed by Jean-Francois Bastien, 04-23-2007. Thanks!
|
|
|
|
const int r = traits_type::compare(pos1 + data(), s, Min(n1, n2));
|
|
|
|
return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
|
2005-08-27 10:20:46 +00:00
|
|
|
}
|
2009-03-10 19:58:26 +00:00
|
|
|
|
2005-08-27 10:20:46 +00:00
|
|
|
int compare(size_type pos1, size_type n1,
|
|
|
|
const flex_string& str,
|
|
|
|
size_type pos2, size_type n2) const
|
|
|
|
{
|
2006-01-19 23:11:57 +00:00
|
|
|
Enforce(pos2 <= str.size(), static_cast<std::out_of_range*>(0), "");
|
2005-08-27 10:20:46 +00:00
|
|
|
return compare(pos1, n1, str.data() + pos2, Min(n2, str.size() - pos2));
|
|
|
|
}
|
|
|
|
|
2007-09-20 15:51:37 +00:00
|
|
|
// Code from Jean-Francois Bastien (03/26/2007)
|
2005-08-27 10:20:46 +00:00
|
|
|
int compare(const value_type* s) const
|
2007-09-20 15:51:37 +00:00
|
|
|
{
|
|
|
|
// Could forward to compare(0, size(), s, traits_type::length(s))
|
|
|
|
// but that does two extra checks
|
|
|
|
const size_type n1(size()), n2(traits_type::length(s));
|
|
|
|
const int r = traits_type::compare(data(), s, Min(n1, n2));
|
|
|
|
return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
|
2006-03-08 17:07:20 +00:00
|
|
|
}
|
2005-08-27 10:20:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// non-member functions
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
flex_string<E, T, A, S> operator+(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{
|
|
|
|
flex_string<E, T, A, S> result;
|
|
|
|
result.reserve(lhs.size() + rhs.size());
|
|
|
|
result.append(lhs).append(rhs);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
flex_string<E, T, A, S> operator+(const typename flex_string<E, T, A, S>::value_type* lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{
|
|
|
|
flex_string<E, T, A, S> result;
|
2009-03-10 19:58:26 +00:00
|
|
|
const typename flex_string<E, T, A, S>::size_type len =
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string<E, T, A, S>::traits_type::length(lhs);
|
|
|
|
result.reserve(len + rhs.size());
|
|
|
|
result.append(lhs, len).append(rhs);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
|
|
|
flex_string<E, T, A, S> operator+(
|
2009-03-10 19:58:26 +00:00
|
|
|
typename flex_string<E, T, A, S>::value_type lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
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>
|
2009-03-10 19:58:26 +00:00
|
|
|
flex_string<E, T, A, S> operator+(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
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).append(rhs, len);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
flex_string<E, T, A, S> operator+(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
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>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator==(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{ return lhs.compare(rhs) == 0; }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator==(const typename flex_string<E, T, A, S>::value_type* lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{ return rhs == lhs; }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator==(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
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>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator!=(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{ return !(lhs == rhs); }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator!=(const typename flex_string<E, T, A, S>::value_type* lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{ return !(lhs == rhs); }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator!=(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const typename flex_string<E, T, A, S>::value_type* rhs)
|
|
|
|
{ return !(lhs == rhs); }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator<(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{ return lhs.compare(rhs) < 0; }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator<(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
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>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator<(const typename flex_string<E, T, A, S>::value_type* lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{ return rhs.compare(lhs) > 0; }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator>(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{ return rhs < lhs; }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator>(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const typename flex_string<E, T, A, S>::value_type* rhs)
|
|
|
|
{ return rhs < lhs; }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator>(const typename flex_string<E, T, A, S>::value_type* lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{ return rhs < lhs; }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator<=(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{ return !(rhs < lhs); }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator<=(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const typename flex_string<E, T, A, S>::value_type* rhs)
|
|
|
|
{ return !(rhs < lhs); }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator<=(const typename flex_string<E, T, A, S>::value_type* lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{ return !(rhs < lhs); }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator>=(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{ return !(lhs < rhs); }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator>=(const flex_string<E, T, A, S>& lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const typename flex_string<E, T, A, S>::value_type* rhs)
|
|
|
|
{ return !(lhs < rhs); }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
bool operator>=(const typename flex_string<E, T, A, S>::value_type* lhs,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& rhs)
|
|
|
|
{ return !(lhs < rhs); }
|
|
|
|
|
2009-03-17 18:21:12 +00:00
|
|
|
template <typename E, class T, class A, class S>
|
|
|
|
inline void swap(flex_string<E, T, A, S>& lhs, flex_string<E, T, A, S>& rhs)
|
|
|
|
{
|
|
|
|
// 21.3.7.8
|
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
2005-08-27 10:20:46 +00:00
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
std::basic_istream<typename flex_string<E, T, A, S>::value_type,
|
2005-08-27 10:20:46 +00:00
|
|
|
typename flex_string<E, T, A, S>::traits_type>&
|
|
|
|
operator>>(
|
2009-03-10 19:58:26 +00:00
|
|
|
std::basic_istream<typename flex_string<E, T, A, S>::value_type,
|
2009-03-02 14:54:16 +00:00
|
|
|
typename flex_string<E, T, A, S>::traits_type>& is,
|
2005-08-27 10:20:46 +00:00
|
|
|
flex_string<E, T, A, S>& str);
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
|
|
|
std::basic_ostream<typename flex_string<E, T, A, S>::value_type,
|
|
|
|
typename flex_string<E, T, A, S>::traits_type>&
|
|
|
|
operator<<(
|
2009-03-10 19:58:26 +00:00
|
|
|
std::basic_ostream<typename flex_string<E, T, A, S>::value_type,
|
2009-03-02 14:54:16 +00:00
|
|
|
typename flex_string<E, T, A, S>::traits_type>& os,
|
2005-08-27 10:20:46 +00:00
|
|
|
const flex_string<E, T, A, S>& str)
|
|
|
|
{ return os << str.c_str(); }
|
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
|
|
|
std::basic_istream<typename flex_string<E, T, A, S>::value_type,
|
|
|
|
typename flex_string<E, T, A, S>::traits_type>&
|
|
|
|
getline(
|
2009-03-10 19:58:26 +00:00
|
|
|
std::basic_istream<typename flex_string<E, T, A, S>::value_type,
|
2005-08-27 10:20:46 +00:00
|
|
|
typename flex_string<E, T, A, S>::traits_type>& is,
|
|
|
|
flex_string<E, T, A, S>& str,
|
2009-03-02 14:54:16 +00:00
|
|
|
typename flex_string<E, T, A, S>::value_type delim)
|
|
|
|
{
|
|
|
|
size_t nread = 0;
|
2011-09-20 23:19:14 +00:00
|
|
|
typename basic_istream<typename flex_string<E, T, A, S>::value_type,
|
2009-03-02 14:54:16 +00:00
|
|
|
typename flex_string<E, T, A, S>::traits_type>::sentry sentry(is, true);
|
|
|
|
if (sentry) {
|
2011-09-20 23:19:14 +00:00
|
|
|
basic_streambuf<typename flex_string<E, T, A, S>::value_type,
|
2009-03-02 14:54:16 +00:00
|
|
|
typename flex_string<E, T, A, S>::traits_type>* buf = is.rdbuf();
|
|
|
|
str.clear();
|
|
|
|
|
|
|
|
while (nread < str.max_size()) {
|
|
|
|
int c1 = buf->sbumpc();
|
|
|
|
if (flex_string<E, T, A, S>::traits_type::eq_int_type(c1, flex_string<E, T, A, S>::traits_type::eof())) {
|
2011-09-20 23:19:14 +00:00
|
|
|
is.setstate(ios_base::eofbit);
|
2009-03-02 14:54:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++nread;
|
|
|
|
typename flex_string<E, T, A, S>::value_type c = flex_string<E, T, A, S>::traits_type::to_char_type(c1);
|
2009-03-10 19:58:26 +00:00
|
|
|
if (!flex_string<E, T, A, S>::traits_type::eq(c, delim))
|
2009-03-02 14:54:16 +00:00
|
|
|
str.push_back(c);
|
|
|
|
else
|
|
|
|
break; // Character is extracted but not appended.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nread == 0 || nread >= str.max_size())
|
2011-09-20 23:19:14 +00:00
|
|
|
is.setstate(ios_base::failbit);
|
2009-03-02 14:54:16 +00:00
|
|
|
|
|
|
|
return is;
|
|
|
|
}
|
2005-08-27 10:20:46 +00:00
|
|
|
|
|
|
|
template <typename E, class T, class A, class S>
|
2009-03-10 19:58:26 +00:00
|
|
|
std::basic_istream<typename flex_string<E, T, A, S>::value_type,
|
2005-08-27 10:20:46 +00:00
|
|
|
typename flex_string<E, T, A, S>::traits_type>&
|
|
|
|
getline(
|
2009-03-10 19:58:26 +00:00
|
|
|
std::basic_istream<typename flex_string<E, T, A, S>::value_type,
|
2005-08-27 10:20:46 +00:00
|
|
|
typename flex_string<E, T, A, S>::traits_type>& is,
|
2009-03-02 14:54:16 +00:00
|
|
|
flex_string<E, T, A, S>& str)
|
|
|
|
{
|
|
|
|
return getline(is, str, is.widen('\n'));
|
|
|
|
}
|
2005-08-27 10:20:46 +00:00
|
|
|
|
|
|
|
template <typename E1, class T, class A, class S>
|
|
|
|
const typename flex_string<E1, T, A, S>::size_type
|
2006-01-19 23:11:57 +00:00
|
|
|
flex_string<E1, T, A, S>::npos = static_cast<typename flex_string<E1, T, A, S>::size_type>(-1);
|
2005-08-27 10:20:46 +00:00
|
|
|
|
|
|
|
#endif // FLEX_STRING_SHELL_INC_
|