missinggo

package
v0.0.0-...-8135c48 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2016 License: MIT, MIT Imports: 28 Imported by: 0

README

missinggo

GoDoc

Stuff that supplements Go's stdlib, or isn't significant enough to be in its own repo.

Documentation

Overview

Package missinggo contains miscellaneous helpers used in many of anacrolix' projects.

Index

Examples

Constants

View Source
const MiB = 1 << 20
View Source
const O_ACCMODE = os.O_RDONLY | os.O_WRONLY | os.O_RDWR

Variables

View Source
var ZeroReader zeroReader

Functions

func AddrIP

func AddrIP(addr net.Addr) net.IP

func AddrPort

func AddrPort(addr net.Addr) int

Extracts the port as an integer from an address string.

func CastSlice

func CastSlice(slicePtr interface{}, fromSlice interface{})

func ConvertToSliceOfEmptyInterface

func ConvertToSliceOfEmptyInterface(slice interface{}) (ret []interface{})

func CopyExact

func CopyExact(dest interface{}, src interface{})

Copy elements from src to dst. Panics if the length of src and dst are different.

func CryHeard

func CryHeard() bool

Calls CryHeard() on a Wolf that is unique to the callers program counter. i.e. every CryHeard() expression has its own Wolf.

func Dispatch

func Dispatch(dest, source interface{}) bool

Obtains the desired value pointed to by dest, from source. If it's not found in source, it will walk source's inheritance values until either blocked by visibility, an implementation is found, or there are no further bases. TODO: If an implementation is obtained from a base class that contains some methods present on a derived class, this might break encapsulation. Maybe there should be a check to ensure this isn't possible.

func Fatal

func Fatal(msg interface{})

func FileInfoAccessTime

func FileInfoAccessTime(fi os.FileInfo) time.Time

Extracts the access time from the FileInfo internals.

func FilePathExists

func FilePathExists(p string) bool

func GetTestName

func GetTestName() string

Returns the name of the test function from the call stack. See http://stackoverflow.com/q/35535635/149482 for another method.

func HTTPQuotedString

func HTTPQuotedString(s string) string

Performs quoted-string from http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html

func IsEmptyValue

func IsEmptyValue(v reflect.Value) bool

Returns whether the value represents the empty value for its type. Used for example to determine if complex types satisfy the common "omitempty" tag option for marshalling. Taken from http://stackoverflow.com/a/23555352/149482.

func JitterDuration

func JitterDuration(average, plusMinus time.Duration) (ret time.Duration)

Returns random duration in the range [average-plusMinus, average+plusMinus]. Negative plusMinus will likely panic. Be aware that if plusMinus >= average, you may get a zero or negative Duration. The distribution function is unspecified, in case I find a more appropriate one in the future.

func LimitLen

func LimitLen(b *[]byte, max interface{})

Sets an upper bound on the len of b. max can be any type that will cast to int64.

func MonotonicSince

func MonotonicSince(since MonotonicTime) (ret time.Duration)

func NewSectionReadSeeker

func NewSectionReadSeeker(base io.ReadSeeker, off, size int64) (ret io.ReadSeeker)

Returns a ReadSeeker on a section of another ReadSeeker.

func NewSelfSignedCertificate

func NewSelfSignedCertificate() (cert tls.Certificate, err error)

Creates a self-signed certificate in memory for use with tls.Config.

func PathSplitExt

func PathSplitExt(p string) (ret struct {
	Root, Ext string
})

Splits the pathname p into Root and Ext, such that Root+Ext==p.

Example
fmt.Printf("%q\n", PathSplitExt(".cshrc"))
fmt.Printf("%q\n", PathSplitExt("dir/a.ext"))
fmt.Printf("%q\n", PathSplitExt("dir/.rc"))
fmt.Printf("%q\n", PathSplitExt("home/.secret/file"))
Output:

{"" ".cshrc"}
{"dir/a" ".ext"}
{"dir/" ".rc"}
{"home/.secret/file" ""}

func StringTruth

func StringTruth(s string) (ret bool)

func Unchomp

func Unchomp(s string) string

func WriteStack

func WriteStack(w io.Writer, stack []uintptr)

Types

type ChanCond

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

func (*ChanCond) Broadcast

func (me *ChanCond) Broadcast()

func (*ChanCond) Signal

func (me *ChanCond) Signal()

func (*ChanCond) Wait

func (me *ChanCond) Wait() <-chan struct{}

type Event

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

func (*Event) C

func (me *Event) C() <-chan struct{}

func (*Event) Clear

func (me *Event) Clear()

func (*Event) IsSet

func (me *Event) IsSet() bool

func (*Event) Set

func (me *Event) Set() (first bool)

func (*Event) Wait

func (me *Event) Wait()

type File

type File interface {
	io.ReaderAt
	io.WriterAt
	io.Writer
	io.Reader
	io.Closer
	io.Seeker
	Stat() (os.FileInfo, error)
}

type FileStore

type FileStore interface {
	OpenFile(path string, flags int) (File, error)
	Stat(path string) (os.FileInfo, error)
	Rename(from, to string) error
	Remove(path string) error
}

type HTTPBytesContentRange

type HTTPBytesContentRange struct {
	First, Last, Length int64
}

func ParseHTTPBytesContentRange

func ParseHTTPBytesContentRange(s string) (ret HTTPBytesContentRange, ok bool)

type HTTPBytesRange

type HTTPBytesRange struct {
	First, Last int64
}

func ParseHTTPBytesRange

func ParseHTTPBytesRange(s string) (ret HTTPBytesRange, ok bool)

type HostMaybePort

type HostMaybePort struct {
	Host   string // Just the host, with no port.
	Port   int    // The port if NoPort is false.
	NoPort bool   // Whether a port is specified.
	Err    error  // The error returned from net.SplitHostPort.
}

Represents a split host port.

func SplitHostMaybePort

func SplitHostMaybePort(hostport string) HostMaybePort

Parse a "hostport" string, a concept that floats around the stdlib a lot and is painful to work with. If no port is present, what's usually present is just the host.

func (*HostMaybePort) String

func (me *HostMaybePort) String() string

type IndentMap

type IndentMap struct {
	expvar.Map
}

func NewExpvarIndentMap

func NewExpvarIndentMap(name string) *IndentMap

func (*IndentMap) String

func (v *IndentMap) String() string

type Inheriter

type Inheriter interface {
	// Return the base-class object value.
	Base() interface{}
	// Return a pointer to an interface containing the methods retrievable
	// from the base.
	Visible() interface{}
}

A sort-of implementation of single dispatch polymorphic inheritance.

type MonotonicTime

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

Monotonic time represents time since an arbitrary point in the past, where the concept of now is only ever moving in a positive direction.

func MonotonicNow

func MonotonicNow() MonotonicTime

Consecutive calls always produce the same or greater time than previous calls.

func (MonotonicTime) Sub

func (me MonotonicTime) Sub(other MonotonicTime) time.Duration

type RWLocker

type RWLocker interface {
	sync.Locker
	RLock()
	RUnlock()
}

type RunLengthEncoder

type RunLengthEncoder interface {
	// Add a series of identical elements to the stream.
	Append(element interface{}, count uint64)
	// Emit the current element and its count if non-zero without waiting for
	// the element to change.
	Flush()
}

A RunLengthEncoder counts successive duplicate elements and emits the element and the run length when the element changes or the encoder is flushed.

func NewRunLengthEncoder

func NewRunLengthEncoder(eachRun func(element interface{}, count uint64)) RunLengthEncoder

Creates a new RunLengthEncoder. eachRun is called when an element and its count is emitted, per the RunLengthEncoder interface.

Example
package main

import (
	"fmt"

	"github.com/anacrolix/missinggo"
)

func main() {
	var s string
	rle := missinggo.NewRunLengthEncoder(func(e interface{}, count uint64) {
		s += fmt.Sprintf("%d%c", count, e)
	})
	for _, e := range "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW" {
		rle.Append(e, 1)
	}
	rle.Flush()
	fmt.Println(s)
}
Output:

12W1B12W3B24W1B14W

type SectionWriter

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

func NewSectionWriter

func NewSectionWriter(w io.WriterAt, off, len int64) *SectionWriter

func (*SectionWriter) WriteAt

func (me *SectionWriter) WriteAt(b []byte, off int64) (n int, err error)

type SingleFlight

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

func (*SingleFlight) Lock

func (me *SingleFlight) Lock(id string)

func (*SingleFlight) Unlock

func (me *SingleFlight) Unlock(id string)

type StatWriter

type StatWriter struct {
	Written int64
	// contains filtered or unexported fields
}

func NewStatWriter

func NewStatWriter(w io.Writer) *StatWriter

func (*StatWriter) Write

func (me *StatWriter) Write(b []byte) (n int, err error)

type StatusResponseWriter

type StatusResponseWriter struct {
	RW           http.ResponseWriter
	Code         int
	BytesWritten int64
	// contains filtered or unexported fields
}

A http.ResponseWriter that tracks the status of the response. The status code, and number of bytes written for example.

func (*StatusResponseWriter) Base

func (me *StatusResponseWriter) Base() interface{}

func (*StatusResponseWriter) Header

func (me *StatusResponseWriter) Header() http.Header

func (*StatusResponseWriter) Visible

func (me *StatusResponseWriter) Visible() interface{}

func (*StatusResponseWriter) Write

func (me *StatusResponseWriter) Write(b []byte) (n int, err error)

func (*StatusResponseWriter) WriteHeader

func (me *StatusResponseWriter) WriteHeader(code int)

type Wolf

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

A Wolf represents some event that becomes less and less interesting as it occurs. Call CryHeard to see if we should pay attention this time.

func (*Wolf) CryHeard

func (me *Wolf) CryHeard() bool

Returns true less and less often. Convenient for exponentially decreasing the amount of noise due to errors.

Directories

Path Synopsis
Package bitmap provides a []bool/bitmap implementation with standardized iteration.
Package bitmap provides a []bool/bitmap implementation with standardized iteration.
cmd
nop
Package prioritybitmap implements a set of integers ordered by attached priorities.
Package prioritybitmap implements a set of integers ordered by attached priorities.

Jump to

Keyboard shortcuts

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