cursor

package module
v0.0.0-...-a9ae406 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2022 License: BSD-3-Clause Imports: 3 Imported by: 0

README

Cursor

Go Reference

Package cursor provides an implementation of Rust's std::io::Cursor for Go.

Example

package main

import (
    "fmt"
    "io"

    "github.com/FedericoSchonborn/cursor"
)

func main() {
    writeTenBytesAtEnd := func(ws io.WriteSeeker) error {
        if _, err := ws.Seek(-10, io.SeekEnd); err != nil {
            return err
        }

        if _, err := ws.Write([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); err != nil {
            return err
        }

        return nil
    }

    buf := cursor.From(make([]byte, 15))
    if err := writeTenBytesAtEnd(buf); err != nil {
        panic(err)
    }

    fmt.Println(buf.Bytes()[5:15])
    // Output:
    // [0 1 2 3 4 5 6 7 8 9]
}

Status

Associated Items
Implemented Tests Examples
new Yes (New) No No
into_inner Yes (Unwrap) No No
position Yes (Offset) No Yes
set_position Yes (SetOffset) No Yes
Traits
Interface Implemented Tests Examples
Read Reader Yes (Read) No No
Write Writer Yes (Write) No No
Seek Seeker Yes (Seek) No No
Clone N/A Yes (Clone) No No
Unstable Features
Implemented Feature/Build Tag Tests Examples
remaining_slice Yes (Remaining) cursor_remaining No Yes
is_empty Yes (IsEmpty) cursor_remaining No Yes

Documentation

Overview

Package cursor provides a Go implementation of Rust's Cursor.

Example
package main

import (
	"fmt"
	"io"

	"github.com/FedericoSchonborn/cursor"
)

func main() {
	writeTenBytesAtEnd := func(ws io.WriteSeeker) error {
		if _, err := ws.Seek(-10, io.SeekEnd); err != nil {
			return err
		}

		if _, err := ws.Write([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); err != nil {
			return err
		}

		return nil
	}

	buf := cursor.New(make([]byte, 15))
	if err := writeTenBytesAtEnd(buf); err != nil {
		panic(err)
	}

	fmt.Println(buf.Bytes()[5:15])
}
Output:

[0 1 2 3 4 5 6 7 8 9]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cursor

type Cursor struct {
	// contains filtered or unexported fields
}

Cursor wraps a byte slice and provides it with io.Reader, io.Writer and io.Seeker implementations.

func Clone

func Clone(other *Cursor) *Cursor

Clone sets the wrapped byte slice to a copy of the byte slice contained in other.

func New

func New(buf []byte) *Cursor

New creates a new Cursor wrapping the given slice.

func (*Cursor) Bytes

func (c *Cursor) Bytes() []byte

Bytes returns the wrapped byte slice.

func (*Cursor) Clone

func (c *Cursor) Clone() *Cursor

Clone creates a new Cursor containing a copy of the wrapped byte slice.

func (*Cursor) Position

func (c *Cursor) Position() int

Position returns the current position of this cursor.

Example
package main

import (
	"fmt"
	"io"

	"github.com/FedericoSchonborn/cursor"
)

func main() {
	buf := cursor.New([]byte{1, 2, 3, 4, 5})
	fmt.Println(buf.Position())

	if _, err := buf.Seek(2, io.SeekCurrent); err != nil {
		panic(err)
	}
	fmt.Println(buf.Position())

	if _, err := buf.Seek(-1, io.SeekCurrent); err != nil {
		panic(err)
	}
	fmt.Println(buf.Position())

}
Output:

0
2
1

func (*Cursor) Read

func (c *Cursor) Read(p []byte) (n int, err error)

Read implements io.Reader for Cursor.

func (*Cursor) Seek

func (c *Cursor) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker for Cursor.

func (*Cursor) SetPosition

func (c *Cursor) SetPosition(pos int)

SetPosition sets the position of this cursor.

Example
package main

import (
	"fmt"

	"github.com/FedericoSchonborn/cursor"
)

func main() {
	buf := cursor.New([]byte{1, 2, 3, 4, 5})
	fmt.Println(buf.Position())

	buf.SetPosition(2)
	fmt.Println(buf.Position())

	buf.SetPosition(4)
	fmt.Println(buf.Position())

}
Output:

0
2
4

func (*Cursor) Unwrap

func (c *Cursor) Unwrap() []byte

Unwrap invalidates the Cursor and returns the wrapped byte slice.

func (*Cursor) Write

func (c *Cursor) Write(p []byte) (n int, err error)

Write implements io.Writer for Cursor.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL