Function insert

Summary

#include <include/EASTL/string.h>

(1) this_type & insert(size_type position, const this_type &x)

(2) this_type & insert(size_type position, const this_type &x, size_type beg, size_type n)

(3) this_type & insert(size_type position, const value_type *p, size_type n)

(4) this_type & insert(size_type position, const value_type *p)

(5) this_type & insert(size_type position, size_type n, value_type c)

(6) iterator insert(const_iterator p, value_type c)

(7) iterator insert(const_iterator p, size_type n, value_type c)

(8) iterator insert(const_iterator p, const value_type *pBegin, const value_type *pEnd)

(9) iterator insert(const_iterator p, std::initializer_list< value_type >)

Function overload

Synopsis

#include <include/EASTL/string.h>

this_type & insert(size_type position, const this_type &x)

Description

Insertion operations.

Source

Lines 2019-2034 in include/EASTL/string.h. Line 647 in include/EASTL/string.h.

template <typename T, typename Allocator>
basic_string<T, Allocator>& basic_string<T, Allocator>::insert(size_type position, const this_type& x)
{
    #if EASTL_STRING_OPT_RANGE_ERRORS
        if(EASTL_UNLIKELY(position > internalLayout().GetSize()))
            ThrowRangeException();
    #endif
    #if EASTL_STRING_OPT_LENGTH_ERRORS
        if(EASTL_UNLIKELY(internalLayout().GetSize() > (max_size() - x.internalLayout().GetSize())))
            ThrowLengthException();
    #endif
    insert(internalLayout().BeginPtr() + position, x.internalLayout().BeginPtr(), x.internalLayout().EndPtr());
    return *this;
}

Synopsis

#include <include/EASTL/string.h>

this_type & insert(size_type position, const this_type &x, size_type beg, size_type n)

Description

No description yet.

Source

Lines 2037-2054 in include/EASTL/string.h. Line 648 in include/EASTL/string.h.

template <typename T, typename Allocator>
basic_string<T, Allocator>& basic_string<T, Allocator>::insert(size_type position, const this_type& x, size_type beg, size_type n)
{
    #if EASTL_STRING_OPT_RANGE_ERRORS
        if(EASTL_UNLIKELY((position > internalLayout().GetSize()) || (beg > x.internalLayout().GetSize())))
            ThrowRangeException();
    #endif
    size_type nLength = eastl::min_alt(n, x.internalLayout().GetSize() - beg);
    #if EASTL_STRING_OPT_LENGTH_ERRORS
        if(EASTL_UNLIKELY(internalLayout().GetSize() > (max_size() - nLength)))
            ThrowLengthException();
    #endif
    insert(internalLayout().BeginPtr() + position, x.internalLayout().BeginPtr() + beg, x.internalLayout().BeginPtr() + beg + nLength);
    return *this;
}

Synopsis

#include <include/EASTL/string.h>

this_type & insert(size_type position, const value_type *p, size_type n)

Description

No description yet.

Source

Lines 2057-2072 in include/EASTL/string.h. Line 649 in include/EASTL/string.h.

template <typename T, typename Allocator>
basic_string<T, Allocator>& basic_string<T, Allocator>::insert(size_type position, const value_type* p, size_type n)
{
    #if EASTL_STRING_OPT_RANGE_ERRORS
        if(EASTL_UNLIKELY(position > internalLayout().GetSize()))
            ThrowRangeException();
    #endif
    #if EASTL_STRING_OPT_LENGTH_ERRORS
        if(EASTL_UNLIKELY(internalLayout().GetSize() > (max_size() - n)))
            ThrowLengthException();
    #endif
    insert(internalLayout().BeginPtr() + position, p, p + n);
    return *this;
}

Synopsis

#include <include/EASTL/string.h>

this_type & insert(size_type position, const value_type *p)

Description

No description yet.

Source

Lines 2075-2092 in include/EASTL/string.h. Line 650 in include/EASTL/string.h.

template <typename T, typename Allocator>
basic_string<T, Allocator>& basic_string<T, Allocator>::insert(size_type position, const value_type* p)
{
    #if EASTL_STRING_OPT_RANGE_ERRORS
        if(EASTL_UNLIKELY(position > internalLayout().GetSize()))
            ThrowRangeException();
    #endif
    size_type nLength = (size_type)CharStrlen(p);
    #if EASTL_STRING_OPT_LENGTH_ERRORS
        if(EASTL_UNLIKELY(internalLayout().GetSize() > (max_size() - nLength)))
            ThrowLengthException();
    #endif
    insert(internalLayout().BeginPtr() + position, p, p + nLength);
    return *this;
}

Synopsis

#include <include/EASTL/string.h>

this_type & insert(size_type position, size_type n, value_type c)

Description

No description yet.

Source

Lines 2095-2110 in include/EASTL/string.h. Line 651 in include/EASTL/string.h.

template <typename T, typename Allocator>
basic_string<T, Allocator>& basic_string<T, Allocator>::insert(size_type position, size_type n, value_type c)
{
    #if EASTL_STRING_OPT_RANGE_ERRORS
        if(EASTL_UNLIKELY(position > internalLayout().GetSize()))
            ThrowRangeException();
    #endif
    #if EASTL_STRING_OPT_LENGTH_ERRORS
        if(EASTL_UNLIKELY(internalLayout().GetSize() > (max_size() - n)))
            ThrowLengthException();
    #endif
    insert(internalLayout().BeginPtr() + position, n, c);
    return *this;
}

Synopsis

#include <include/EASTL/string.h>

iterator insert(const_iterator p, value_type c)

Description

No description yet.

Source

Lines 2113-2123 in include/EASTL/string.h. Line 652 in include/EASTL/string.h.

template <typename T, typename Allocator>
inline typename basic_string<T, Allocator>::iterator
basic_string<T, Allocator>::insert(const_iterator p, value_type c)
{
    if(p == internalLayout().EndPtr())
    {
        push_back(c);
        return internalLayout().EndPtr() - 1;
    }
    return InsertInternal(p, c);
}

Synopsis

#include <include/EASTL/string.h>

iterator insert(const_iterator p, size_type n, value_type c)

Description

No description yet.

Source

Lines 2126-2203 in include/EASTL/string.h. Line 653 in include/EASTL/string.h.

template <typename T, typename Allocator>
typename basic_string<T, Allocator>::iterator
basic_string<T, Allocator>::insert(const_iterator p, size_type n, value_type c)
{
    const difference_type nPosition = (p - internalLayout().BeginPtr()); // Save this because we might reallocate.
    #if EASTL_ASSERT_ENABLED
        if(EASTL_UNLIKELY((p < internalLayout().BeginPtr()) || (p > internalLayout().EndPtr())))
            EASTL_FAIL_MSG("basic_string::insert -- invalid position");
    #endif
    if(n) // If there is anything to insert...
    {
        if(internalLayout().GetRemainingCapacity() >= n) // If we have enough capacity...
        {
            const size_type nElementsAfter = (size_type)(internalLayout().EndPtr() - p);
            if(nElementsAfter >= n) // If there's enough space for the new chars between the insert position and the end...
            {
                // Ensure we save the size before we do the copy, as we might overwrite the size field with the NULL
                // terminator in the edge case where we are inserting enough characters to equal our capacity
                const size_type nSavedSize = internalLayout().GetSize();
                CharStringUninitializedCopy((internalLayout().EndPtr() - n) + 1, internalLayout().EndPtr() + 1, internalLayout().EndPtr() + 1);
                internalLayout().SetSize(nSavedSize + n);
                memmove(const_cast<value_type*>(p) + n, p, (size_t)((nElementsAfter - n) + 1) * sizeof(value_type));
                CharTypeAssignN(const_cast<value_type*>(p), n, c);
            }
            else
            {
                pointer pOldEnd = internalLayout().EndPtr();
                #if EASTL_EXCEPTIONS_ENABLED
                    const size_type nOldSize = internalLayout().GetSize();
                #endif
                CharStringUninitializedFillN(internalLayout().EndPtr() + 1, n - nElementsAfter - 1, c);
                internalLayout().SetSize(internalLayout().GetSize() + (n - nElementsAfter));
                #if EASTL_EXCEPTIONS_ENABLED
                    try
                    {
                #endif
                        // See comment in if block above
                        const size_type nSavedSize = internalLayout().GetSize();
                        CharStringUninitializedCopy(p, pOldEnd + 1, internalLayout().EndPtr());
                        internalLayout().SetSize(nSavedSize + nElementsAfter);
                #if EASTL_EXCEPTIONS_ENABLED
                    }
                    catch(...)
                    {
                        internalLayout().SetSize(nOldSize);
                        throw;
                    }
                #endif
                CharTypeAssignN(const_cast<value_type*>(p), nElementsAfter + 1, c);
            }
        }
        else
        {
            const size_type nOldSize = internalLayout().GetSize();
            const size_type nOldCap  = capacity();
            const size_type nLength  = GetNewCapacity(nOldCap, (nOldSize + n) - nOldCap);
            iterator pNewBegin = DoAllocate(nLength + 1);
            iterator pNewEnd = CharStringUninitializedCopy(internalLayout().BeginPtr(), p, pNewBegin);
            pNewEnd          = CharStringUninitializedFillN(pNewEnd, n, c);
            pNewEnd          = CharStringUninitializedCopy(p, internalLayout().EndPtr(), pNewEnd);
           *pNewEnd          = 0;
            DeallocateSelf();
            internalLayout().SetHeapBeginPtr(pNewBegin);
            internalLayout().SetHeapCapacity(nLength);
            internalLayout().SetHeapSize(nOldSize + n);
        }
    }
    return internalLayout().BeginPtr() + nPosition;
}

Synopsis

#include <include/EASTL/string.h>

iterator insert(const_iterator p, const value_type *pBegin, const value_type *pEnd)

Description

No description yet.

Source

Lines 2206-2311 in include/EASTL/string.h. Line 654 in include/EASTL/string.h.

template <typename T, typename Allocator>
typename basic_string<T, Allocator>::iterator
basic_string<T, Allocator>::insert(const_iterator p, const value_type* pBegin, const value_type* pEnd)
{
    const difference_type nPosition = (p - internalLayout().BeginPtr()); // Save this because we might reallocate.
    #if EASTL_ASSERT_ENABLED
        if(EASTL_UNLIKELY((p < internalLayout().BeginPtr()) || (p > internalLayout().EndPtr())))
            EASTL_FAIL_MSG("basic_string::insert -- invalid position");
    #endif
    const size_type n = (size_type)(pEnd - pBegin);
    if(n)
    {
        const bool bCapacityIsSufficient = (internalLayout().GetRemainingCapacity() >= n);
        const bool bSourceIsFromSelf     = ((pEnd >= internalLayout().BeginPtr()) && (pBegin <= internalLayout().EndPtr()));
        if(bSourceIsFromSelf && internalLayout().IsSSO())
        {
            // pBegin to pEnd will be <= this->GetSize(), so stackTemp will guaranteed be an SSO String
            // If we are inserting ourself into ourself and we are SSO, then on the recursive call we can
            // guarantee 0 or 1 allocation depending if we need to realloc
            // We don't do this for Heap strings as then this path may do 1 or 2 allocations instead of
            // only 1 allocation when we fall through to the last else case below
            const this_type stackTemp(pBegin, pEnd, get_allocator());
            return insert(p, stackTemp.data(), stackTemp.data() + stackTemp.size());
        }
        // If bSourceIsFromSelf is true, then we reallocate. This is because we are
        // inserting ourself into ourself and thus both the source and destination
        // be modified, making it rather tricky to attempt to do in place. The simplest
        // resolution is to reallocate. To consider: there may be a way to implement this
        // whereby we don't need to reallocate or can often avoid reallocating.
        if(bCapacityIsSufficient && !bSourceIsFromSelf)
        {
            const size_type nElementsAfter = (size_type)(internalLayout().EndPtr() - p);
            if(nElementsAfter >= n) // If there are enough characters between insert pos and end
            {
                // Ensure we save the size before we do the copy, as we might overwrite the size field with the NULL
                // terminator in the edge case where we are inserting enough characters to equal our capacity
                const size_type nSavedSize = internalLayout().GetSize();
                CharStringUninitializedCopy((internalLayout().EndPtr() - n) + 1, internalLayout().EndPtr() + 1, internalLayout().EndPtr() + 1);
                internalLayout().SetSize(nSavedSize + n);
                memmove(const_cast<value_type*>(p) + n, p, (size_t)((nElementsAfter - n) + 1) * sizeof(value_type));
                memmove(const_cast<value_type*>(p), pBegin, (size_t)(n) * sizeof(value_type));
            }
            else
            {
                pointer pOldEnd = internalLayout().EndPtr();
                #if EASTL_EXCEPTIONS_ENABLED
                    const size_type nOldSize = internalLayout().GetSize();
                #endif
                const value_type* const pMid = pBegin + (nElementsAfter + 1);
                CharStringUninitializedCopy(pMid, pEnd, internalLayout().EndPtr() + 1);
                internalLayout().SetSize(internalLayout().GetSize() + (n - nElementsAfter));
                #if EASTL_EXCEPTIONS_ENABLED
                    try
                    {
                #endif
                        // See comment in if block above
                        const size_type nSavedSize = internalLayout().GetSize();
                        CharStringUninitializedCopy(p, pOldEnd + 1, internalLayout().EndPtr());
                        internalLayout().SetSize(nSavedSize + nElementsAfter);
                #if EASTL_EXCEPTIONS_ENABLED
                    }
                    catch(...)
                    {
                        internalLayout().SetSize(nOldSize);
                        throw;
                    }
                #endif
                CharStringUninitializedCopy(pBegin, pMid, const_cast<value_type*>(p));
            }
        }
        else // Else we need to reallocate to implement this.
        {
            const size_type nOldSize = internalLayout().GetSize();
            const size_type nOldCap  = capacity();
            size_type nLength;
            if(bCapacityIsSufficient) // If bCapacityIsSufficient is true, then bSourceIsFromSelf must be true.
                nLength = nOldSize + n;
            else
                nLength = GetNewCapacity(nOldCap, (nOldSize + n) - nOldCap);
            pointer pNewBegin = DoAllocate(nLength + 1);
            pointer pNewEnd = CharStringUninitializedCopy(internalLayout().BeginPtr(), p, pNewBegin);
            pNewEnd         = CharStringUninitializedCopy(pBegin, pEnd, pNewEnd);
            pNewEnd         = CharStringUninitializedCopy(p, internalLayout().EndPtr(), pNewEnd);
           *pNewEnd         = 0;
            DeallocateSelf();
            internalLayout().SetHeapBeginPtr(pNewBegin);
            internalLayout().SetHeapCapacity(nLength);
            internalLayout().SetHeapSize(nOldSize + n);
        }
    }
    return internalLayout().BeginPtr() + nPosition;
}

Synopsis

#include <include/EASTL/string.h>

iterator insert(const_iterator p, std::initializer_list< value_type >)

Description

No description yet.

Source

Lines 2314-2319 in include/EASTL/string.h. Line 655 in include/EASTL/string.h.

template <typename T, typename Allocator>
typename basic_string<T, Allocator>::iterator
basic_string<T, Allocator>::insert(const_iterator p, std::initializer_list<value_type> ilist)
{
    return insert(p, ilist.begin(), ilist.end());
}





Add Discussion as Guest

Log in