static int32_t _az_json_writer_escape_next_byte_and_copy()

in sdk/src/azure/core/az_json_writer.c [285:361]


static int32_t _az_json_writer_escape_next_byte_and_copy(
    az_span* remaining_destination,
    uint8_t next_byte)
{
  uint8_t escaped = 0;
  int32_t written = 0;

  switch (next_byte)
  {
    case '\\':
    case '"':
    {
      escaped = next_byte;
      break;
    }
    case '\b':
    {
      escaped = 'b';
      break;
    }
    case '\f':
    {
      escaped = 'f';
      break;
    }
    case '\n':
    {
      escaped = 'n';
      break;
    }
    case '\r':
    {
      escaped = 'r';
      break;
    }
    case '\t':
    {
      escaped = 't';
      break;
    }
    default:
    {
      // Check if the character has to be escaped as a UNICODE escape sequence.
      if (next_byte < _az_ASCII_SPACE_CHARACTER)
      {
        // TODO: Consider moving this array outside the loop.
        uint8_t array[_az_MAX_EXPANSION_FACTOR_WHILE_ESCAPING] = {
          '\\',
          'u',
          '0',
          '0',
          _az_number_to_upper_hex((uint8_t)(next_byte / _az_NUMBER_OF_HEX_VALUES)),
          _az_number_to_upper_hex((uint8_t)(next_byte % _az_NUMBER_OF_HEX_VALUES)),
        };
        *remaining_destination = az_span_copy(*remaining_destination, AZ_SPAN_FROM_BUFFER(array));
        written += _az_MAX_EXPANSION_FACTOR_WHILE_ESCAPING;
      }
      else
      {
        *remaining_destination = az_span_copy_u8(*remaining_destination, next_byte);
        written++;
      }
      break;
    }
  }

  // If escaped is non-zero, then we found one of the characters that needs to be escaped.
  // Otherwise, we hit the default case in the switch above, in which case, we already wrote
  // the character.
  if (escaped)
  {
    *remaining_destination = az_span_copy_u8(*remaining_destination, '\\');
    *remaining_destination = az_span_copy_u8(*remaining_destination, escaped);
    written += 2;
  }
  return written;
}