MemoryBuffer

public class MemoryBuffer : Sequence

MemoryBuffer provides simple read-only access to a block of memory, and provides simple methods for reading files and standard input into a memory buffer. In addition to basic access to the characters in the file, this interface guarantees you can read one character past the end of the file, and that this character will read as ‘\0’.

The ‘\0’ guarantee is needed to support an optimization – it’s intended to be more efficient for clients which are reading all the data to stop reading when they encounter a ‘\0’ than to continually check the file position to see if it has reached the end of the file.

  • Creates a MemoryBuffer with the contents of stdin, stopping once EOF is read.

    Throws

    MemoryBufferError if there was an error on creation.

    Declaration

    Swift

    public static func fromStdin() throws -> MemoryBuffer
  • Creates a MemoryBuffer that points to a specified UnsafeBufferPointer.

    Declaration

    Swift

    public init(buffer: UnsafeBufferPointer<Int8>,
                name: String,
                requiresNullTerminator: Bool = false)

    Parameters

    buffer

    The underlying buffer that contains the data.

    name

    The name for the new memory buffer.

    requiresNullTerminator

    Whether or not the MemoryBuffer should append a null terminator. Defaults to false

  • Creates a MemoryBuffer by copying the data within a specified UnsafeBufferPointer.

    Declaration

    Swift

    public init(copyingBuffer buffer: UnsafeBufferPointer<Int8>,
                name: String)

    Parameters

    buffer

    The underlying buffer that contains the data.

    name

    The name for the new memory buffer.

  • Creates a MemoryBuffer with the contents of the file at the provided path.

    Throws

    MemoryBufferError if there was a problem creating the buffer or reading the file.

    Declaration

    Swift

    public init(contentsOf file: String) throws

    Parameters

    file

    The full path of the file you’re trying to read.

  • Retrieves the start address of this buffer.

    Declaration

    Swift

    public var start: UnsafePointer<Int8> { get }
  • Retrieves the size in bytes of this buffer.

    Declaration

    Swift

    public var size: Int { get }
  • Makes an iterator so this buffer can be traversed in a for loop.

    Declaration

    Swift

    public func makeIterator() -> UnsafeBufferPointer<Int8>.Iterator