#include "jes/Text.h"
#include <string.h>

namespace jes
{
    Text::Text()
    {
        m_buffer = NULL;
        m_size = 0;
        m_gap_index = 0;
        m_gap_size = 0;
    }

    Text::Text(const uint8_t buf[], size_t size)
    {
        m_buffer = new uint8_t[size];
        m_size = size;
        m_gap_index = size;
        m_gap_size = 0;
        memcpy(m_buffer, &buf[0], size);
    }

    std::string Text::to_s()
    {
        if (m_buffer == NULL)
        {
            return "";
        }
        compact();
        return std::string((const char *)m_buffer, m_size - m_gap_size);
    }

    void Text::compact()
    {
        if ((m_gap_index < m_size - m_gap_size) && (m_gap_size > 0))
        {
            memmove(&m_buffer[m_gap_index], &m_buffer[m_gap_index + m_gap_size], m_size - m_gap_index);
            m_gap_index = m_size - m_gap_size;
        }
    }
}
