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

Code before:
    @inlinable
    public mutating func setStaticString(_ string: StaticString, at index: Int) -> Int {
        // please do not replace the code below with code that uses `string.withUTF8Buffer { ... }` (see SR-7541)
        self.setBytes(
            UnsafeRawBufferPointer(
                start: string.utf8Start,
                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

        while let nextByte = try slice.popNextHexByte() {
            written += self.writeInteger(nextByte)
        }

        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):
Will a throw here shut down the channel and mean the inconsistent state of the byte buffer with only half the values written is OK? 

OR Should I do an initial pass and validate BEFORE then going on and writing them to the buffer to ensure a consistent state? 

I did it initially the latter way, but poking around I assumed the channel closes and that bytebuffer is no longer used. What do we think, I am in 2 minds about it. 

Code after:
    ///     - string: The string to write.
    ///     - index: The index for the first serialized byte.
    /// - returns: The number of bytes written.
    @inlinable
    public mutating func setStaticString(_ string: StaticString, at index: Int) -> Int {
        // please do not replace the code below with code that uses `string.withUTF8Buffer { ... }` (see SR-7541)
        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