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

Code before:
  {
    return true;
  }

  lit_utf8_size_t extra_bytes_count;

  for (const unsigned char *idx = (const unsigned char *) utf8_buf_p; (unsigned int) (idx - utf8_buf_p) < buf_size;
       idx += extra_bytes_count)
  {
    if (idx[0] <= 0x7F)
    {
      extra_bytes_count = 1;
      continue;
    }
    else if (((0xC2 <= idx[0] && idx[0] <= 0xDF) && (0x80 <= idx[1] && idx[1] <= 0xBF)))
    {
      extra_bytes_count = 2;
      continue;
    }
    else if ((idx[0] == 0xE0 && (0xA0 <= idx[1] && idx[1] <= 0xBF) && (0x80 <= idx[2] && idx[2] <= 0xBF))
              || (((0xE1 <= idx[0] && idx[0] <= 0xEC) || idx[0] == 0xEE || idx[0] == 0xEF)
              && (0x80 <= idx[1] && idx[1] <= 0xBF) && (0x80 <= idx[2] && idx[2] <= 0xBF))
              || (idx[0] == 0xED && (0x80 <= idx[1] && idx[1] <= 0x9F) && (0x80 <= idx[2] && idx[2] <= 0xBF))
              || (idx[0] == 0xED && is_strict && (((0xA0 <= idx[1] && idx[1] <= 0xAF)
              && (0x80 <= idx[2] && idx[2] <= 0xBF)) || ((0xB0 <= idx[1] && idx[1] <= 0xBF)
              && (0x80 <= idx[2] && idx[2] <= 0xBF)))))
    {
      extra_bytes_count = 3;
      continue;
    }
    else if ((idx[0] == 0xF0 && (0x90 <= idx[1] && idx[1] <= 0xBF) && (0x80 <= idx[2] && idx[2] <= 0xBF)
              && (0x80 <= idx[3] && idx[3] <= 0xBF)) || ((0xF1 <= idx[0] && idx[0] <= 0xF3)
              && (0x80 <= idx[1] && idx[1] <= 0xBF) && (0x80 <= idx[2] && idx[2] <= 0xBF)
              && (0x80 <= idx[3] && idx[3] <= 0xBF)) || (idx[0] == 0xF4 && (0x80 <= idx[1] && idx[1] <= 0x8F)
              && (0x80 <= idx[2] && idx[2] <= 0xBF) && (0x80 <= idx[3] && idx[3] <= 0xBF)))
    {
      extra_bytes_count = 4;
      continue;
    }

    return false;
  }

  return true;
} /* lit_is_valid_utf8_string */

/**
 * Validate cesu-8 string
 *
 * @return true if cesu-8 string is well-formed
 *         false otherwise

Review comment (suggestion):
This is very hard to understand. I would split it into multiple `if`s. Use lowercase for hex constants.

Very simplified example (use better names and syntax):
```
c = *buf++; // No need to increase the buf-ptr in the loop
if (c < 0x80) continue;
if (c < 0xc2) return false;
if (buf >= end) return false;
n = *buf++;
if ((n & 0xc0) != 0x80) return false;
if (c < 0xe0) continue;
if (buf >= end) return false;
if ((*buf++ & 0xc0) != 0x80) return false;
if (c < 0xf0) {
  // three byte case, check the validity
  if (c == 0xe0 && n < ...) return false;
  // Check surrogates
  continue;
}
// Continue with 4 byte case
if ((*buf++ & 0xc0) != 0x80) return false;
...
```

Code after:
                          lit_utf8_size_t buf_size, /**< string size */
                          bool is_strict) /**< true if surrogate pairs are not allowed */
{
  const unsigned char *end = buf_size + utf8_buf_p;

  const unsigned char *idx = (const unsigned char *) utf8_buf_p;

  while (idx < end)
  {
    const uint8_t first_byte = *idx++;

    if (first_byte < LIT_UTF8_EXTRA_BYTE_MARKER)
    {
      continue;
    }

    if (first_byte < LIT_UTF8_VALID_TWO_BYTE_START || idx >= end)
    {
      return false;
    }

    const uint8_t second_byte = *idx++;

    if ((second_byte & LIT_UTF8_EXTRA_BYTE_MASK) != LIT_UTF8_EXTRA_BYTE_MARKER)
    {
      return false;
    }

    if (first_byte < LIT_UTF8_3_BYTE_MARKER)
    {
      continue;
    }

    if (idx >= end || (*idx++ & LIT_UTF8_EXTRA_BYTE_MASK) != LIT_UTF8_EXTRA_BYTE_MARKER)
    {
      return false;
    }

    if (first_byte < LIT_UTF8_4_BYTE_MARKER)
    {
      if (first_byte == LIT_UTF8_3_BYTE_MARKER && (second_byte & LIT_UTF8_2_BYTE_MASK) == LIT_UTF8_EXTRA_BYTE_MARKER)
      {
        return false;
      }

      if (is_strict
          && first_byte == LIT_UTF8_SURROGATE_MARKER
          && second_byte >= LIT_UTF8_HIGH_SURROGATE_MIN
          && second_byte <= LIT_UTF8_HIGH_SURROGATE_MAX
          && idx + 3 <= end
          && idx[0] == LIT_UTF8_SURROGATE_MARKER