bool finalReadUtf8String()

in hessian2/basic_codec/string_codec.cc [258:299]


bool finalReadUtf8String(std::string &output, bool &has_surrogate,
                         Reader &reader, size_t length) {
  // The length length refers to the length of utF8 characters,
  // and utF8 can be represented by up to 4 bytes, so it is length * 4
  output.reserve(length * 4);
  while (length > 0) {
    if (reader.byteAvailable() < length) {
      return false;
    }
    const uint64_t current_pos = output.size();

    output.resize(current_pos + length);
    // Read the 'length' bytes from the reader buffer to the output.
    reader.readNBytes(
        static_cast<void *>(const_cast<char *>(output.data() + current_pos)),
        length);

    const auto result = getUtf8StringLength(
        absl::string_view(output).substr(current_pos), has_surrogate);
    const int64_t utf8_length = result.first;
    const size_t raw_bytes_length = result.second;

    if (utf8_length == -1) {
      return false;
    }

    if (raw_bytes_length > length) {
      const size_t padding_size = raw_bytes_length - length;
      if (reader.byteAvailable() < padding_size) {
        return false;
      }
      output.resize(current_pos + raw_bytes_length);
      // Read the 'padding_size' bytes from the reader buffer to the output.
      reader.readNBytes(static_cast<void *>(const_cast<char *>(
                            output.data() + current_pos + length)),
                        padding_size);
    }

    length -= utf8_length;
  }
  return true;
}