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

Code before:
  JMEM_CP_SET_SECOND_BIT_TO_POINTER_TAG (field) /**< set second tag bit to jmem_cpointer_tag_t **/
#define ECMA_SET_THIRD_BIT_TO_POINTER_TAG(field) \
  JMEM_CP_SET_THIRD_BIT_TO_POINTER_TAG (field) /**< set third tag bit to jmem_cpointer_tag_t **/

/**
 * Status flags for ecma_string_get_chars function
 */
typedef enum
{
  ECMA_STRING_FLAG_EMPTY = 0,                /**< No options are provided. */
  ECMA_STRING_FLAG_IS_ASCII = (1 << 0),      /**< The string contains only ASCII characters. */
  ECMA_STRING_FLAG_REHASH_NEEDED = (1 << 1), /**< The hash of the string must be recalculated.
                                              *   For more details see ecma_append_chars_to_string */
  ECMA_STRING_FLAG_IS_UINT32 = (1 << 2),     /**< The string repesents an UINT32 number */
  ECMA_STRING_FLAG_MUST_BE_FREED = (1 << 3), /**< The returned buffer must be freed */
} ecma_string_flag_t;

/**
 * Enumerable for string trimming location.
 */
typedef enum
{
  ECMA_TRIM_STRING_START = (1u << 0), /**< Trim only from the start */
  ECMA_TRIM_STRING_END = (1u << 1), /**<  Trim only from the end */
  ECMA_TRIM_STRING_STARTEND = (1u << 2), /**< Trim from both start and end */
} ecma_string_trim_flags_t;

/**
 * Convert ecma-string's contents to a cesu-8 string and put it into a buffer.
 */
#define ECMA_STRING_TO_UTF8_STRING(ecma_str_ptr, /**< ecma string pointer */ \
                                   utf8_ptr, /**< [out] output buffer pointer */ \
                                   utf8_str_size) /**< [out] output buffer size */ \
  lit_utf8_size_t utf8_str_size; \
  uint8_t utf8_ptr ## flags = ECMA_STRING_FLAG_EMPTY; \
  const lit_utf8_byte_t *utf8_ptr = ecma_string_get_chars (ecma_str_ptr, \
                                                           &utf8_str_size, \
                                                           NULL, \
                                                           NULL, \
                                                           &utf8_ptr ## flags);

/**
 * Free the cesu-8 string buffer allocated by 'ECMA_STRING_TO_UTF8_STRING'
 */
#define ECMA_FINALIZE_UTF8_STRING(utf8_ptr, /**< pointer to character buffer */ \
                                  utf8_str_size) /**< buffer size */ \
  if (utf8_ptr ## flags & ECMA_STRING_FLAG_MUST_BE_FREED) \
  { \
    JERRY_ASSERT (utf8_ptr != NULL); \
    jmem_heap_free_block ((void *) utf8_ptr, utf8_str_size); \
  }

Review comment (refactor):
No need for these, just call `ecma_string_trim_front`/`ecma_string_trim_back` where necessary, and change `ecma_string_trim_helper` to call both, and be always inline.

Code after:
  JMEM_CP_SET_SECOND_BIT_TO_POINTER_TAG (field) /**< set second tag bit to jmem_cpointer_tag_t **/
#define ECMA_SET_THIRD_BIT_TO_POINTER_TAG(field) \
  JMEM_CP_SET_THIRD_BIT_TO_POINTER_TAG (field) /**< set third tag bit to jmem_cpointer_tag_t **/

/**
 * Status flags for ecma_string_get_chars function
 */
typedef enum
{
  ECMA_STRING_FLAG_EMPTY = 0,                /**< No options are provided. */
  ECMA_STRING_FLAG_IS_ASCII = (1 << 0),      /**< The string contains only ASCII characters. */
  ECMA_STRING_FLAG_REHASH_NEEDED = (1 << 1), /**< The hash of the string must be recalculated.
                                              *   For more details see ecma_append_chars_to_string */
  ECMA_STRING_FLAG_IS_UINT32 = (1 << 2),     /**< The string repesents an UINT32 number */
  ECMA_STRING_FLAG_MUST_BE_FREED = (1 << 3), /**< The returned buffer must be freed */
} ecma_string_flag_t;

/**
 * Convert ecma-string's contents to a cesu-8 string and put it into a buffer.
 */
#define ECMA_STRING_TO_UTF8_STRING(ecma_str_ptr, /**< ecma string pointer */ \
                                   utf8_ptr, /**< [out] output buffer pointer */ \
                                   utf8_str_size) /**< [out] output buffer size */ \
  lit_utf8_size_t utf8_str_size; \
  uint8_t utf8_ptr ## flags = ECMA_STRING_FLAG_EMPTY; \
  const lit_utf8_byte_t *utf8_ptr = ecma_string_get_chars (ecma_str_ptr, \
                                                           &utf8_str_size, \
                                                           NULL, \
                                                           NULL, \
                                                           &utf8_ptr ## flags);

/**
 * Free the cesu-8 string buffer allocated by 'ECMA_STRING_TO_UTF8_STRING'
 */
#define ECMA_FINALIZE_UTF8_STRING(utf8_ptr, /**< pointer to character buffer */ \
                                  utf8_str_size) /**< buffer size */ \
  if (utf8_ptr ## flags & ECMA_STRING_FLAG_MUST_BE_FREED) \
  { \
    JERRY_ASSERT (utf8_ptr != NULL); \
    jmem_heap_free_block ((void *) utf8_ptr, utf8_str_size); \
  }

#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY

/**
 * Set an internal property value from pointer.
 */
#define ECMA_SET_INTERNAL_VALUE_POINTER(field, pointer) \
  ((field) = ((ecma_value_t) pointer))

/**