Repository: jerryscript-project/jerryscript
File: jerry-core/lit/lit-char-helpers.c (C)

Code before:
/**
 * Check if specified character is one of BinaryDigits characters (ECMA-262 v6, 11.8.3)
 *
 * @return true / false
 */
bool
lit_char_is_binary_digit (ecma_char_t c) /** code unit */
{
  return (c == LIT_CHAR_0 || c == LIT_CHAR_1);
} /* lit_char_is_binary_digit */
#endif /* ENABLED (JERRY_ESNEXT) */

/**
 * UnicodeEscape abstract method
 *
 * See also: ECMA-262 v10, 24.5.2.3
 */
void
lit_char_unicode_escape (ecma_stringbuilder_t *builder_p, /**< stringbuilder to append */
                         ecma_char_t c) /**< code unit to convert */
{
  ecma_stringbuilder_append_raw (builder_p, (lit_utf8_byte_t *) "\\u", 2);

  for (int8_t i = 3; i >= 0; i--)
  {
    int32_t result_char = (c & (((1 << 4) - 1) << (i << 2))) >> (i << 2);
    ecma_stringbuilder_append_byte (builder_p, (lit_utf8_byte_t) (result_char + (result_char <= 9
                                                                                ? LIT_CHAR_0 :
                                                                                (LIT_CHAR_LOWERCASE_A - 10))));
  }
} /* lit_char_unicode_escape */

/**
 * Convert a HexDigit character to its numeric value, as defined in ECMA-262 v5, 7.8.3
 *
 * @return digit value, corresponding to the hex char
 */
uint32_t
lit_char_hex_to_int (ecma_char_t c) /**< code unit, corresponding to
                                     *    one of HexDigit characters */
{
  JERRY_ASSERT (lit_char_is_hex_digit (c));

  if (c >= LIT_CHAR_ASCII_DIGITS_BEGIN && c <= LIT_CHAR_ASCII_DIGITS_END)
  {
    return (uint32_t) (c - LIT_CHAR_ASCII_DIGITS_BEGIN);
  }
  else if (c >= LIT_CHAR_ASCII_LOWERCASE_LETTERS_HEX_BEGIN && c <= LIT_CHAR_ASCII_LOWERCASE_LETTERS_HEX_END)
  {
    return (uint32_t) (c - LIT_CHAR_ASCII_LOWERCASE_LETTERS_HEX_BEGIN + 10);
  }

Review comment (suggestion):
This looks way too overcomplicated. Wouldn't `(c >> (i * 4)) & 0xF` be enough? Or you could also do `for (int8_t i = 12; i >= 0; i -= 4)`, then you could write `(c >> i) & 0xF`.

Code after:
/**
 * Check if specified character is one of BinaryDigits characters (ECMA-262 v6, 11.8.3)
 *
 * @return true / false
 */
bool
lit_char_is_binary_digit (ecma_char_t c) /** code unit */
{
  return (c == LIT_CHAR_0 || c == LIT_CHAR_1);
} /* lit_char_is_binary_digit */
#endif /* ENABLED (JERRY_ESNEXT) */

/**
 * UnicodeEscape abstract method
 *
 * See also: ECMA-262 v10, 24.5.2.3
 */
void
lit_char_unicode_escape (ecma_stringbuilder_t *builder_p, /**< stringbuilder to append */
                         ecma_char_t c) /**< code unit to convert */
{
  ecma_stringbuilder_append_raw (builder_p, (lit_utf8_byte_t *) "\\u", 2);

  for (int8_t i = 3; i >= 0; i--)
  {
    int32_t result_char = (c >> (i * 4)) & 0xF;
    ecma_stringbuilder_append_byte (builder_p, (lit_utf8_byte_t) (result_char + (result_char <= 9
                                                                                 ? LIT_CHAR_0
                                                                                 : (LIT_CHAR_LOWERCASE_A - 10))));
  }
} /* lit_char_unicode_escape */

/**
 * Convert a HexDigit character to its numeric value, as defined in ECMA-262 v5, 7.8.3
 *
 * @return digit value, corresponding to the hex char
 */
uint32_t
lit_char_hex_to_int (ecma_char_t c) /**< code unit, corresponding to
                                     *    one of HexDigit characters */
{
  JERRY_ASSERT (lit_char_is_hex_digit (c));

  if (c >= LIT_CHAR_ASCII_DIGITS_BEGIN && c <= LIT_CHAR_ASCII_DIGITS_END)
  {
    return (uint32_t) (c - LIT_CHAR_ASCII_DIGITS_BEGIN);
  }
  else if (c >= LIT_CHAR_ASCII_LOWERCASE_LETTERS_HEX_BEGIN && c <= LIT_CHAR_ASCII_LOWERCASE_LETTERS_HEX_END)
  {
    return (uint32_t) (c - LIT_CHAR_ASCII_LOWERCASE_LETTERS_HEX_BEGIN + 10);
  }