protected function writeImpl()

in src/io/MemoryHandle.php [86:116]


  protected function writeImpl(string $data): int {
    $this->checkIsOpen();
    $length = Str\length($this->buffer);
    if ($length < $this->offset) {
      $this->buffer .= Str\repeat("\0", $this->offset - $length);
      $length = $this->offset;
    }

    if ($this->writeMode === MemoryHandleWriteMode::APPEND) {
      $this->buffer .= $data;
      $this->offset = Str\length($this->buffer);
      return Str\length($data);
    }

    _OS\arg_assert(
      $this->writeMode === MemoryHandleWriteMode::OVERWRITE,
      "Write mode must be OVERWRITE or APPEND",
    );

    $data_length = Str\length($data);
    $new = Str\slice($this->buffer, 0, $this->offset).$data;
    if ($this->offset < $length) {
      $new .= Str\slice(
        $this->buffer,
        Math\minva($this->offset + $data_length, $length),
      );
    }
    $this->buffer = $new;
    $this->offset += $data_length;
    return $data_length;
  }