#include "EncodedString.h"
#include <string.h>

uint32_t EncodedString::iterator::operator*()
{
    if (m_offset >= m_encoded_string.size())
    {
        return INVALID_CODE_POINT;
    }
    return Encoding::decode(m_encoded_string.encoding(), &m_encoded_string[m_offset]);
}

EncodedString::iterator & EncodedString::iterator::operator++()
{
    if (m_offset < m_encoded_string.size())
    {
        m_offset += Encoding::num_bytes_in_code_point(m_encoded_string.encoding(), &m_encoded_string[m_offset]);
    }
    return *this;
}

size_t EncodedString::iterator::size() const
{
    if (m_offset < m_encoded_string.size())
    {
        return Encoding::num_bytes_in_code_point(m_encoded_string.encoding(), &m_encoded_string[m_offset]);
    }
    return 0u;
}


EncodedString::EncodedString(const uint8_t * data, size_t size, Encoding::Type encoding)
    : m_encoding(encoding),
      m_size(size)
{
    m_data = std::make_shared<std::vector<uint8_t>>(size);
    memcpy(&(*m_data)[0], data, size);
}

EncodedString::iterator EncodedString::begin() const
{
    return iterator(*this, 0u);
}

EncodedString::iterator EncodedString::end() const
{
    return iterator(*this, m_size);
}
