ecma_new_ecma_string_from_utf8_converted_to_cesu8 (const lit_utf8_byte_t *string_p, /**< utf-8 string */
                                                   lit_utf8_size_t string_size) /**< utf-8 string size */
{
  JERRY_ASSERT (string_p != NULL || string_size == 0);

  lit_utf8_size_t converted_string_length = 0;
  lit_utf8_size_t converted_string_size = 0;
  lit_utf8_size_t pos = 0;

  /* Calculate the required length and size information of the converted cesu-8 encoded string */
  while (pos < string_size)
  {
    if ((string_p[pos] & LIT_UTF8_1_BYTE_MASK) == LIT_UTF8_1_BYTE_MARKER)
    {
      pos++;
    }
    else if ((string_p[pos] & LIT_UTF8_2_BYTE_MASK) == LIT_UTF8_2_BYTE_MARKER)
    {
      pos += 2;
    }
    else if ((string_p[pos] & LIT_UTF8_3_BYTE_MASK) == LIT_UTF8_3_BYTE_MARKER)
    {
      pos += 3;
    }
    else
    {
      JERRY_ASSERT ((string_p[pos] & LIT_UTF8_4_BYTE_MASK) == LIT_UTF8_4_BYTE_MARKER);
      pos += 4;
      converted_string_size += 2;
      converted_string_length++;
    }

    converted_string_length++;
  }

  JERRY_ASSERT (pos == string_size);

  if (converted_string_size == 0)
  {
    return ecma_new_ecma_string_from_utf8 (string_p, string_size);
  }

  converted_string_size += string_size;

  JERRY_ASSERT (lit_is_valid_utf8_string (string_p, string_size));

  lit_utf8_byte_t *data_p;
  ecma_string_t *string_desc_p = ecma_new_ecma_string_from_utf8_buffer (converted_string_length,
                                                                        converted_string_size,
                                                                        &data_p);

  const lit_utf8_byte_t *const begin_data_p = data_p;
  pos = 0;

  while (pos < string_size)
  {
    if ((string_p[pos] & LIT_UTF8_4_BYTE_MASK) == LIT_UTF8_4_BYTE_MARKER)
    {
      /* Processing 4 byte unicode sequence. Always converted to two 3 byte long sequence. */
      lit_four_byte_utf8_char_to_cesu8 (data_p, string_p + pos);
      data_p += 3 * 2;
      pos += 4;
    }
    else
    {
      *data_p++ = string_p[pos++];
    }
  }

  JERRY_ASSERT (pos == string_size);

  string_desc_p->u.hash = lit_utf8_string_calc_hash (begin_data_p, converted_string_size);

  return (ecma_string_t *) string_desc_p;
} /* ecma_new_ecma_string_from_utf8_converted_to_cesu8 */
ecma_new_ecma_string_from_utf8_converted_to_cesu8 (const lit_utf8_byte_t *string_p,  
                                                   lit_utf8_size_t string_size)  
{
  JERRY_ASSERT (string_p != NULL || string_size == 0);

  lit_utf8_size_t converted_string_length = 0;
  lit_utf8_size_t converted_string_size = 0;
  lit_utf8_size_t pos = 0;

   
  while (pos < string_size)
  {
    if ((string_p[pos] & LIT_UTF8_1_BYTE_MASK) == LIT_UTF8_1_BYTE_MARKER)
    {
      pos++;
    }
    else if ((string_p[pos] & LIT_UTF8_2_BYTE_MASK) == LIT_UTF8_2_BYTE_MARKER)
    {
      pos += 2;
    }
    else if ((string_p[pos] & LIT_UTF8_3_BYTE_MASK) == LIT_UTF8_3_BYTE_MARKER)
    {
      pos += 3;
    }
    else
    {
      JERRY_ASSERT ((string_p[pos] & LIT_UTF8_4_BYTE_MASK) == LIT_UTF8_4_BYTE_MARKER);
      pos += 4;
      converted_string_size += 2;
      converted_string_length++;
    }

    converted_string_length++;
  }

  JERRY_ASSERT (pos == string_size);

  if (converted_string_size == 0)
  {
    return ecma_new_ecma_string_from_utf8 (string_p, string_size);
  }

  converted_string_size += string_size;

  JERRY_ASSERT (lit_is_valid_utf8_string (string_p, string_size));

  lit_utf8_byte_t *data_p;
  ecma_string_t *string_desc_p = ecma_new_ecma_string_from_utf8_buffer (converted_string_length,
                                                                        converted_string_size,
                                                                        &data_p);

  const lit_utf8_byte_t *const begin_data_p = data_p;
  pos = 0;

  while (pos < string_size)
  {
    if ((string_p[pos] & LIT_UTF8_4_BYTE_MASK) == LIT_UTF8_4_BYTE_MARKER)
    {
       
      lit_four_byte_utf8_char_to_cesu8 (data_p, string_p + pos);
      data_p += 3 * 2;
      pos += 4;
    }
    else
    {
      *data_p++ = string_p[pos++];
    }
  }

  JERRY_ASSERT (pos == string_size);

  string_desc_p->u.hash = lit_utf8_string_calc_hash (begin_data_p, converted_string_size);

  return (ecma_string_t *) string_desc_p;
}  