Repository: jerryscript-project/jerryscript
File: jerry-core/ecma/base/ecma-helpers-string.c (C)

Code before:
      *utf8_str_size = (lit_utf8_size_t) (current_p - nonws_start_p);
    }

    *utf8_str_p = nonws_start_p;
  }
} /* ecma_string_trim_helper */

/**
 * Trim leading and trailing whitespace characters from string.
 *
 * @return trimmed ecma string
 */
ecma_string_t *
ecma_string_trim (const ecma_string_t *string_p, /**< pointer to an ecma string */
                  ecma_trim_string_where trim_position) /**< enum, signaling trim position */
{
  ecma_string_t *ret_string_p;

  lit_utf8_size_t utf8_str_size;
  uint8_t flags = ECMA_STRING_FLAG_IS_ASCII;
  const lit_utf8_byte_t *utf8_str_p = ecma_string_get_chars (string_p, &utf8_str_size, NULL, NULL, &flags);

  if (utf8_str_size > 0)
  {
    ecma_string_trim_helper (&utf8_str_p, &utf8_str_size, trim_position);
    ret_string_p = ecma_new_ecma_string_from_utf8 (utf8_str_p, utf8_str_size);
  }
  else
  {
    ret_string_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
  }

  if (flags & ECMA_STRING_FLAG_MUST_BE_FREED)
  {
    jmem_heap_free_block ((void *) utf8_str_p, utf8_str_size);
  }

  return ret_string_p;
} /* ecma_string_trim */

#if ENABLED (JERRY_ESNEXT)

/**
 * Pad the beginning or the end of string with parameter given in fill_string to the length of max_length.
 *
 * @return new string from original, padded with given parameters
 */
ecma_value_t
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 */

Review comment (suggestion):
I think this should be performed in an other way. Currently you trim the string from both directions after you calculate back the required string part which is superfluous.

```c
if (flags & TRIM_START) {
  //  remove leading whitespaces
}

if (flags & TRIM_END) {
  //  remove trailing whitespaces
}

// return as before
*utf8_str_p = nonws_start_p;
*utf8_str_size = (lit_utf8_size_t) (current_p - nonws_start_p);
```


Code after:
    end_p -= read_size;
  }

  return end_p;
} /* ecma_string_trim_back */

/**
 * Helper function for trimming.
 *
 * Used by:
 *        - ecma_string_trim
 *        - ecma_utf8_string_to_number
 */
inline void JERRY_ATTR_ALWAYS_INLINE
ecma_string_trim_helper (const lit_utf8_byte_t **utf8_str_p, /**< [in, out] current string position */
                         lit_utf8_size_t *utf8_str_size)  /**< [in, out] size of the given string */
{
  const lit_utf8_byte_t *end_p = *utf8_str_p + *utf8_str_size;
  const lit_utf8_byte_t *start_p = *utf8_str_p;

  const lit_utf8_byte_t *new_start_p = ecma_string_trim_front (start_p, end_p);
  const lit_utf8_byte_t *new_end_p = ecma_string_trim_back (new_start_p, end_p);

  *utf8_str_size = (lit_utf8_size_t) (new_end_p - new_start_p);
  *utf8_str_p = new_start_p;
} /* ecma_string_trim_helper */

/**
 * Trim leading and trailing whitespace characters from string.
 *
 * @return trimmed ecma string
 */
ecma_string_t *
ecma_string_trim (const ecma_string_t *string_p) /**< pointer to an ecma string */
{
  ecma_string_t *ret_string_p;

  lit_utf8_size_t utf8_str_size;
  uint8_t flags = ECMA_STRING_FLAG_IS_ASCII;
  const lit_utf8_byte_t *utf8_str_p = ecma_string_get_chars (string_p, &utf8_str_size, NULL, NULL, &flags);

  if (utf8_str_size > 0)
  {
    ecma_string_trim_helper (&utf8_str_p, &utf8_str_size);
    ret_string_p = ecma_new_ecma_string_from_utf8 (utf8_str_p, utf8_str_size);
  }
  else
  {
    ret_string_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
  }