Repository: apple/swift-nio
File: Sources/NIOCore/ByteBuffer-aux.swift (Swift)

Code before:
                count: string.utf8CodeUnitCount
            ),
            at: index
        )
    }

    // MARK: Hex encoded string APIs
    /// Write `string` into this `ByteBuffer` in ASCII hexadecimal, moving the writer index forward appropriately.
    /// - parameters:
    ///     - plainHexEncodedBytes: The hex encoded string to write.
    /// - returns: The number of bytes written.
    @discardableResult
    @inlinable
    public mutating func writePlainHexEncodedBytes(_ plainHexEncodedBytes: String) throws -> Int {
        var slice = plainHexEncodedBytes.utf8[...]
        var written = 0
        var bytesToWrite: [UInt8] = []

        // Single pass to ensure string is a valid representation of hex bytes
        while let nextByte = try slice.popNextHexByte() {
            bytesToWrite.append(nextByte)
        }

        for byte in bytesToWrite {
            written += self.writeInteger(byte)
        }

        return written
    }

    // MARK: String APIs
    /// Write `string` into this `ByteBuffer` using UTF-8 encoding, moving the writer index forward appropriately.
    ///
    /// - parameters:
    ///     - string: The string to write.
    /// - returns: The number of bytes written.
    @discardableResult
    @inlinable
    public mutating func writeString(_ string: String) -> Int {
        let written = self.setString(string, at: self.writerIndex)
        self._moveWriterIndex(forwardBy: written)
        return written
    }

    /// Write `string` into this `ByteBuffer` null terminated using UTF-8 encoding, moving the writer index forward appropriately.
    ///
    /// - parameters:
    ///     - string: The string to write.
    /// - returns: The number of bytes written.
    @discardableResult
    @inlinable

Review comment (suggestion):
We don't need the intermediate array. Instead of using `bytesToWrite` and calling `append`, just call `writeInteger` directly.

Code after:
        self.setBytes(
            UnsafeRawBufferPointer(
                start: string.utf8Start,
                count: string.utf8CodeUnitCount
            ),
            at: index
        )
    }

    // MARK: Hex encoded string APIs
    /// Write ASCII hexadecimal `string` into this `ByteBuffer` as raw bytes, decoding the hexadecimal & moving the writer index forward appropriately.
    /// This method will throw if the string input is not of the "plain" hex encoded format.
    /// - parameters:
    ///     - plainHexEncodedBytes: The hex encoded string to write. Plain hex dump format is hex bytes optionally separated by spaces, i.e. `48 65 6c 6c 6f` or `48656c6c6f` for `Hello`.
    ///     This format is compatible with `xxd` CLI utility.
    /// - returns: The number of bytes written.
    @discardableResult
    @inlinable
    public mutating func writePlainHexEncodedBytes(_ plainHexEncodedBytes: String) throws -> Int {
        var slice = plainHexEncodedBytes.utf8[...]
        let initialWriterIndex = self.writerIndex

        do {
            while let nextByte = try slice.popNextHexByte() {
                self.writeInteger(nextByte)
            }
            return self.writerIndex - initialWriterIndex
        } catch {
            self.moveWriterIndex(to: initialWriterIndex)
            throw error
        }
    }

    // MARK: String APIs
    /// Write `string` into this `ByteBuffer` using UTF-8 encoding, moving the writer index forward appropriately.
    ///
    /// - parameters:
    ///     - string: The string to write.
    /// - returns: The number of bytes written.
    @discardableResult
    @inlinable
    public mutating func writeString(_ string: String) -> Int {
        let written = self.setString(string, at: self.writerIndex)
        self._moveWriterIndex(forwardBy: written)
        return written
    }

    /// Write `string` into this `ByteBuffer` null terminated using UTF-8 encoding, moving the writer index forward appropriately.
    ///
    /// - parameters:
    ///     - string: The string to write.