ecma_string_pad (ecma_value_t original_string_p, /**< Input ecma string */
                 ecma_value_t max_length, /**< Length to pad to, including original length */
                 ecma_value_t fill_string,  /**< The string to pad with */
                 bool pad_on_start) /**< true - if we are padding to the start, calling with padStart
                                         false - if we are padding to the end, calling with padEnd */
{

  /* 3 */
  ecma_length_t int_max_length;
  if (ECMA_IS_VALUE_ERROR (ecma_op_to_length (max_length, &int_max_length)))
  {
    return ECMA_VALUE_ERROR;
  }
  /* 4 */
  ecma_string_t *original_str_val_p = ecma_get_string_from_value (original_string_p);
  const uint32_t string_length = ecma_string_get_length (original_str_val_p);
  /* 5 */
  if (int_max_length <= string_length)
  {
    ecma_ref_ecma_string (original_str_val_p);
    return original_string_p;
  }

  ecma_string_t *filler_p = ecma_get_magic_string (LIT_MAGIC_STRING_SPACE_CHAR);
  /* 6 - 7 */
  if (!ecma_is_value_undefined (fill_string))
  {
    filler_p = ecma_op_to_string (fill_string);
    if (filler_p == NULL)
    {
      return ECMA_VALUE_ERROR;
    }
    if (ecma_string_is_empty (filler_p))
    {
      ecma_ref_ecma_string (original_str_val_p);
      return original_string_p;
    }
  }

  if (int_max_length >= UINT32_MAX)
  {
    ecma_deref_ecma_string (filler_p);
    return ecma_raise_range_error (ECMA_ERR_MSG ("Maximum string length is reached"));
  }

  /* 9 */
  uint32_t fill_len = (uint32_t) int_max_length - string_length;

  /* 10 */
  uint32_t filler_length = ecma_string_get_length (filler_p);
  uint32_t prepend_count = fill_len / filler_length;
  ecma_stringbuilder_t builder = ecma_stringbuilder_create ();

  if (!pad_on_start)
  {
    ecma_stringbuilder_append (&builder, original_str_val_p);
  }

  for (uint32_t i = 0; i < prepend_count; i++)
  {
    ecma_stringbuilder_append (&builder, filler_p);
  }

  uint32_t remaining = fill_len - (prepend_count * filler_length);

  ECMA_STRING_TO_UTF8_STRING (filler_p, start_p, utf8_str_size);
  const lit_utf8_byte_t *temp_start_p = start_p;
  while (remaining > 0)
  {
    ecma_char_t ch;
    lit_utf8_size_t read_size = lit_read_code_unit_from_cesu8 (temp_start_p, &ch);
    ecma_stringbuilder_append_char (&builder, ch);
    temp_start_p += read_size;
    remaining--;
  }
  ECMA_FINALIZE_UTF8_STRING (start_p, utf8_str_size);
  ecma_deref_ecma_string (filler_p);

  /* 11 - 12 */
  if (pad_on_start)
  {
    ecma_stringbuilder_append (&builder, original_str_val_p);
  }

  return ecma_make_string_value (ecma_stringbuilder_finalize (&builder));
} /* ecma_string_pad */
ecma_string_pad (ecma_value_t original_string_p,  
                 ecma_value_t max_length,  
                 ecma_value_t fill_string,   
                 bool pad_on_start)  
{

   
  ecma_length_t int_max_length;
  if (ECMA_IS_VALUE_ERROR (ecma_op_to_length (max_length, &int_max_length)))
  {
    return ECMA_VALUE_ERROR;
  }
   
  ecma_string_t *original_str_val_p = ecma_get_string_from_value (original_string_p);
  const uint32_t string_length = ecma_string_get_length (original_str_val_p);
   
  if (int_max_length <= string_length)
  {
    ecma_ref_ecma_string (original_str_val_p);
    return original_string_p;
  }

  ecma_string_t *filler_p = ecma_get_magic_string (LIT_MAGIC_STRING_SPACE_CHAR);
   
  if (!ecma_is_value_undefined (fill_string))
  {
    filler_p = ecma_op_to_string (fill_string);
    if (filler_p == NULL)
    {
      return ECMA_VALUE_ERROR;
    }
    if (ecma_string_is_empty (filler_p))
    {
      ecma_ref_ecma_string (original_str_val_p);
      return original_string_p;
    }
  }

  if (int_max_length >= UINT32_MAX)
  {
    ecma_deref_ecma_string (filler_p);
    return ecma_raise_range_error (ECMA_ERR_MSG ("Maximum string length is reached"));
  }

   
  uint32_t fill_len = (uint32_t) int_max_length - string_length;

   
  uint32_t filler_length = ecma_string_get_length (filler_p);
  uint32_t prepend_count = fill_len / filler_length;
  ecma_stringbuilder_t builder = ecma_stringbuilder_create ();

  if (!pad_on_start)
  {
    ecma_stringbuilder_append (&builder, original_str_val_p);
  }

  for (uint32_t i = 0; i < prepend_count; i++)
  {
    ecma_stringbuilder_append (&builder, filler_p);
  }

  uint32_t remaining = fill_len - (prepend_count * filler_length);

  ECMA_STRING_TO_UTF8_STRING (filler_p, start_p, utf8_str_size);
  const lit_utf8_byte_t *temp_start_p = start_p;
  while (remaining > 0)
  {
    ecma_char_t ch;
    lit_utf8_size_t read_size = lit_read_code_unit_from_cesu8 (temp_start_p, &ch);
    ecma_stringbuilder_append_char (&builder, ch);
    temp_start_p += read_size;
    remaining--;
  }
  ECMA_FINALIZE_UTF8_STRING (start_p, utf8_str_size);
  ecma_deref_ecma_string (filler_p);

   
  if (pad_on_start)
  {
    ecma_stringbuilder_append (&builder, original_str_val_p);
  }

  return ecma_make_string_value (ecma_stringbuilder_finalize (&builder));
}  