id
int32 0
167k
| repo
stringlengths 5
54
| path
stringlengths 4
155
| func_name
stringlengths 1
118
| original_string
stringlengths 52
85.5k
| language
stringclasses 1
value | code
stringlengths 52
85.5k
| code_tokens
listlengths 21
1.41k
| docstring
stringlengths 6
2.61k
| docstring_tokens
listlengths 3
215
| sha
stringlengths 40
40
| url
stringlengths 85
252
|
---|---|---|---|---|---|---|---|---|---|---|---|
15,000 |
antchfx/xpath
|
xpath.go
|
MoveNext
|
func (t *NodeIterator) MoveNext() bool {
n := t.query.Select(t)
if n != nil {
if !t.node.MoveTo(n) {
t.node = n.Copy()
}
return true
}
return false
}
|
go
|
func (t *NodeIterator) MoveNext() bool {
n := t.query.Select(t)
if n != nil {
if !t.node.MoveTo(n) {
t.node = n.Copy()
}
return true
}
return false
}
|
[
"func",
"(",
"t",
"*",
"NodeIterator",
")",
"MoveNext",
"(",
")",
"bool",
"{",
"n",
":=",
"t",
".",
"query",
".",
"Select",
"(",
"t",
")",
"\n",
"if",
"n",
"!=",
"nil",
"{",
"if",
"!",
"t",
".",
"node",
".",
"MoveTo",
"(",
"n",
")",
"{",
"t",
".",
"node",
"=",
"n",
".",
"Copy",
"(",
")",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] |
// MoveNext moves Navigator to the next match node.
|
[
"MoveNext",
"moves",
"Navigator",
"to",
"the",
"next",
"match",
"node",
"."
] |
ce1d48779e67a1ddfb380995fe532b2e0015919c
|
https://github.com/antchfx/xpath/blob/ce1d48779e67a1ddfb380995fe532b2e0015919c/xpath.go#L84-L93
|
15,001 |
antchfx/xpath
|
xpath.go
|
Select
|
func (expr *Expr) Select(root NodeNavigator) *NodeIterator {
return &NodeIterator{query: expr.q.Clone(), node: root}
}
|
go
|
func (expr *Expr) Select(root NodeNavigator) *NodeIterator {
return &NodeIterator{query: expr.q.Clone(), node: root}
}
|
[
"func",
"(",
"expr",
"*",
"Expr",
")",
"Select",
"(",
"root",
"NodeNavigator",
")",
"*",
"NodeIterator",
"{",
"return",
"&",
"NodeIterator",
"{",
"query",
":",
"expr",
".",
"q",
".",
"Clone",
"(",
")",
",",
"node",
":",
"root",
"}",
"\n",
"}"
] |
// Select selects a node set using the specified XPath expression.
|
[
"Select",
"selects",
"a",
"node",
"set",
"using",
"the",
"specified",
"XPath",
"expression",
"."
] |
ce1d48779e67a1ddfb380995fe532b2e0015919c
|
https://github.com/antchfx/xpath/blob/ce1d48779e67a1ddfb380995fe532b2e0015919c/xpath.go#L129-L131
|
15,002 |
antchfx/xpath
|
xpath.go
|
Compile
|
func Compile(expr string) (*Expr, error) {
if expr == "" {
return nil, errors.New("expr expression is nil")
}
qy, err := build(expr)
if err != nil {
return nil, err
}
return &Expr{s: expr, q: qy}, nil
}
|
go
|
func Compile(expr string) (*Expr, error) {
if expr == "" {
return nil, errors.New("expr expression is nil")
}
qy, err := build(expr)
if err != nil {
return nil, err
}
return &Expr{s: expr, q: qy}, nil
}
|
[
"func",
"Compile",
"(",
"expr",
"string",
")",
"(",
"*",
"Expr",
",",
"error",
")",
"{",
"if",
"expr",
"==",
"\"",
"\"",
"{",
"return",
"nil",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"qy",
",",
"err",
":=",
"build",
"(",
"expr",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"&",
"Expr",
"{",
"s",
":",
"expr",
",",
"q",
":",
"qy",
"}",
",",
"nil",
"\n",
"}"
] |
// Compile compiles an XPath expression string.
|
[
"Compile",
"compiles",
"an",
"XPath",
"expression",
"string",
"."
] |
ce1d48779e67a1ddfb380995fe532b2e0015919c
|
https://github.com/antchfx/xpath/blob/ce1d48779e67a1ddfb380995fe532b2e0015919c/xpath.go#L139-L148
|
15,003 |
antchfx/xpath
|
xpath.go
|
MustCompile
|
func MustCompile(expr string) *Expr {
exp, err := Compile(expr)
if err != nil {
return nil
}
return exp
}
|
go
|
func MustCompile(expr string) *Expr {
exp, err := Compile(expr)
if err != nil {
return nil
}
return exp
}
|
[
"func",
"MustCompile",
"(",
"expr",
"string",
")",
"*",
"Expr",
"{",
"exp",
",",
"err",
":=",
"Compile",
"(",
"expr",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"return",
"exp",
"\n",
"}"
] |
// MustCompile compiles an XPath expression string and ignored error.
|
[
"MustCompile",
"compiles",
"an",
"XPath",
"expression",
"string",
"and",
"ignored",
"error",
"."
] |
ce1d48779e67a1ddfb380995fe532b2e0015919c
|
https://github.com/antchfx/xpath/blob/ce1d48779e67a1ddfb380995fe532b2e0015919c/xpath.go#L151-L157
|
15,004 |
antchfx/xpath
|
func.go
|
predicate
|
func predicate(q query) func(NodeNavigator) bool {
type Predicater interface {
Test(NodeNavigator) bool
}
if p, ok := q.(Predicater); ok {
return p.Test
}
return func(NodeNavigator) bool { return true }
}
|
go
|
func predicate(q query) func(NodeNavigator) bool {
type Predicater interface {
Test(NodeNavigator) bool
}
if p, ok := q.(Predicater); ok {
return p.Test
}
return func(NodeNavigator) bool { return true }
}
|
[
"func",
"predicate",
"(",
"q",
"query",
")",
"func",
"(",
"NodeNavigator",
")",
"bool",
"{",
"type",
"Predicater",
"interface",
"{",
"Test",
"(",
"NodeNavigator",
")",
"bool",
"\n",
"}",
"\n",
"if",
"p",
",",
"ok",
":=",
"q",
".",
"(",
"Predicater",
")",
";",
"ok",
"{",
"return",
"p",
".",
"Test",
"\n",
"}",
"\n",
"return",
"func",
"(",
"NodeNavigator",
")",
"bool",
"{",
"return",
"true",
"}",
"\n",
"}"
] |
// The XPath function list.
|
[
"The",
"XPath",
"function",
"list",
"."
] |
ce1d48779e67a1ddfb380995fe532b2e0015919c
|
https://github.com/antchfx/xpath/blob/ce1d48779e67a1ddfb380995fe532b2e0015919c/func.go#L14-L22
|
15,005 |
antchfx/xpath
|
func.go
|
substringFunc
|
func substringFunc(arg1, arg2, arg3 query) func(query, iterator) interface{} {
return func(q query, t iterator) interface{} {
var m string
switch typ := arg1.Evaluate(t).(type) {
case string:
m = typ
case query:
node := typ.Select(t)
if node == nil {
return ""
}
m = node.Value()
}
var start, length float64
var ok bool
if start, ok = arg2.Evaluate(t).(float64); !ok {
panic(errors.New("substring() function first argument type must be int"))
} else if start < 1 {
panic(errors.New("substring() function first argument type must be >= 1"))
}
start--
if arg3 != nil {
if length, ok = arg3.Evaluate(t).(float64); !ok {
panic(errors.New("substring() function second argument type must be int"))
}
}
if (len(m) - int(start)) < int(length) {
panic(errors.New("substring() function start and length argument out of range"))
}
if length > 0 {
return m[int(start):int(length+start)]
}
return m[int(start):]
}
}
|
go
|
func substringFunc(arg1, arg2, arg3 query) func(query, iterator) interface{} {
return func(q query, t iterator) interface{} {
var m string
switch typ := arg1.Evaluate(t).(type) {
case string:
m = typ
case query:
node := typ.Select(t)
if node == nil {
return ""
}
m = node.Value()
}
var start, length float64
var ok bool
if start, ok = arg2.Evaluate(t).(float64); !ok {
panic(errors.New("substring() function first argument type must be int"))
} else if start < 1 {
panic(errors.New("substring() function first argument type must be >= 1"))
}
start--
if arg3 != nil {
if length, ok = arg3.Evaluate(t).(float64); !ok {
panic(errors.New("substring() function second argument type must be int"))
}
}
if (len(m) - int(start)) < int(length) {
panic(errors.New("substring() function start and length argument out of range"))
}
if length > 0 {
return m[int(start):int(length+start)]
}
return m[int(start):]
}
}
|
[
"func",
"substringFunc",
"(",
"arg1",
",",
"arg2",
",",
"arg3",
"query",
")",
"func",
"(",
"query",
",",
"iterator",
")",
"interface",
"{",
"}",
"{",
"return",
"func",
"(",
"q",
"query",
",",
"t",
"iterator",
")",
"interface",
"{",
"}",
"{",
"var",
"m",
"string",
"\n",
"switch",
"typ",
":=",
"arg1",
".",
"Evaluate",
"(",
"t",
")",
".",
"(",
"type",
")",
"{",
"case",
"string",
":",
"m",
"=",
"typ",
"\n",
"case",
"query",
":",
"node",
":=",
"typ",
".",
"Select",
"(",
"t",
")",
"\n",
"if",
"node",
"==",
"nil",
"{",
"return",
"\"",
"\"",
"\n",
"}",
"\n",
"m",
"=",
"node",
".",
"Value",
"(",
")",
"\n",
"}",
"\n\n",
"var",
"start",
",",
"length",
"float64",
"\n",
"var",
"ok",
"bool",
"\n\n",
"if",
"start",
",",
"ok",
"=",
"arg2",
".",
"Evaluate",
"(",
"t",
")",
".",
"(",
"float64",
")",
";",
"!",
"ok",
"{",
"panic",
"(",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
")",
"\n",
"}",
"else",
"if",
"start",
"<",
"1",
"{",
"panic",
"(",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
")",
"\n",
"}",
"\n",
"start",
"--",
"\n",
"if",
"arg3",
"!=",
"nil",
"{",
"if",
"length",
",",
"ok",
"=",
"arg3",
".",
"Evaluate",
"(",
"t",
")",
".",
"(",
"float64",
")",
";",
"!",
"ok",
"{",
"panic",
"(",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"(",
"len",
"(",
"m",
")",
"-",
"int",
"(",
"start",
")",
")",
"<",
"int",
"(",
"length",
")",
"{",
"panic",
"(",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
")",
"\n",
"}",
"\n",
"if",
"length",
">",
"0",
"{",
"return",
"m",
"[",
"int",
"(",
"start",
")",
":",
"int",
"(",
"length",
"+",
"start",
")",
"]",
"\n",
"}",
"\n",
"return",
"m",
"[",
"int",
"(",
"start",
")",
":",
"]",
"\n",
"}",
"\n",
"}"
] |
// substringFunc is XPath functions substring function returns a part of a given string.
|
[
"substringFunc",
"is",
"XPath",
"functions",
"substring",
"function",
"returns",
"a",
"part",
"of",
"a",
"given",
"string",
"."
] |
ce1d48779e67a1ddfb380995fe532b2e0015919c
|
https://github.com/antchfx/xpath/blob/ce1d48779e67a1ddfb380995fe532b2e0015919c/func.go#L337-L373
|
15,006 |
anacrolix/go-libutp
|
socket.go
|
utpProcessUdp
|
func (s *Socket) utpProcessUdp(b []byte, addr net.Addr) (utp bool) {
if len(b) == 0 {
// The implementation of utp_process_udp rejects null buffers, and
// anything smaller than the UTP header size. It's also prone to
// assert on those, which we don't want to trigger.
return false
}
if missinggo.AddrPort(addr) == 0 {
return false
}
mu.Unlock()
block := func() bool {
if s.firewallCallback == nil {
return false
}
return s.firewallCallback(addr)
}()
mu.Lock()
s.block = block
if s.closed {
return false
}
var sal C.socklen_t
staticRsa, sal = netAddrToLibSockaddr(addr)
ret := C.utp_process_udp(s.ctx, (*C.byte)(&b[0]), C.size_t(len(b)), (*C.struct_sockaddr)(unsafe.Pointer(&staticRsa)), sal)
switch ret {
case 1:
return true
case 0:
return false
default:
panic(ret)
}
}
|
go
|
func (s *Socket) utpProcessUdp(b []byte, addr net.Addr) (utp bool) {
if len(b) == 0 {
// The implementation of utp_process_udp rejects null buffers, and
// anything smaller than the UTP header size. It's also prone to
// assert on those, which we don't want to trigger.
return false
}
if missinggo.AddrPort(addr) == 0 {
return false
}
mu.Unlock()
block := func() bool {
if s.firewallCallback == nil {
return false
}
return s.firewallCallback(addr)
}()
mu.Lock()
s.block = block
if s.closed {
return false
}
var sal C.socklen_t
staticRsa, sal = netAddrToLibSockaddr(addr)
ret := C.utp_process_udp(s.ctx, (*C.byte)(&b[0]), C.size_t(len(b)), (*C.struct_sockaddr)(unsafe.Pointer(&staticRsa)), sal)
switch ret {
case 1:
return true
case 0:
return false
default:
panic(ret)
}
}
|
[
"func",
"(",
"s",
"*",
"Socket",
")",
"utpProcessUdp",
"(",
"b",
"[",
"]",
"byte",
",",
"addr",
"net",
".",
"Addr",
")",
"(",
"utp",
"bool",
")",
"{",
"if",
"len",
"(",
"b",
")",
"==",
"0",
"{",
"// The implementation of utp_process_udp rejects null buffers, and",
"// anything smaller than the UTP header size. It's also prone to",
"// assert on those, which we don't want to trigger.",
"return",
"false",
"\n",
"}",
"\n",
"if",
"missinggo",
".",
"AddrPort",
"(",
"addr",
")",
"==",
"0",
"{",
"return",
"false",
"\n",
"}",
"\n",
"mu",
".",
"Unlock",
"(",
")",
"\n",
"block",
":=",
"func",
"(",
")",
"bool",
"{",
"if",
"s",
".",
"firewallCallback",
"==",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"s",
".",
"firewallCallback",
"(",
"addr",
")",
"\n",
"}",
"(",
")",
"\n",
"mu",
".",
"Lock",
"(",
")",
"\n",
"s",
".",
"block",
"=",
"block",
"\n",
"if",
"s",
".",
"closed",
"{",
"return",
"false",
"\n",
"}",
"\n",
"var",
"sal",
"C",
".",
"socklen_t",
"\n",
"staticRsa",
",",
"sal",
"=",
"netAddrToLibSockaddr",
"(",
"addr",
")",
"\n",
"ret",
":=",
"C",
".",
"utp_process_udp",
"(",
"s",
".",
"ctx",
",",
"(",
"*",
"C",
".",
"byte",
")",
"(",
"&",
"b",
"[",
"0",
"]",
")",
",",
"C",
".",
"size_t",
"(",
"len",
"(",
"b",
")",
")",
",",
"(",
"*",
"C",
".",
"struct_sockaddr",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"staticRsa",
")",
")",
",",
"sal",
")",
"\n",
"switch",
"ret",
"{",
"case",
"1",
":",
"return",
"true",
"\n",
"case",
"0",
":",
"return",
"false",
"\n",
"default",
":",
"panic",
"(",
"ret",
")",
"\n",
"}",
"\n",
"}"
] |
// Wraps libutp's utp_process_udp, returning relevant information.
|
[
"Wraps",
"libutp",
"s",
"utp_process_udp",
"returning",
"relevant",
"information",
"."
] |
d7dd83bce17eb552d89dc42b7acdb8498e7e70db
|
https://github.com/anacrolix/go-libutp/blob/d7dd83bce17eb552d89dc42b7acdb8498e7e70db/socket.go#L285-L318
|
15,007 |
anacrolix/go-libutp
|
socket.go
|
DialContext
|
func (s *Socket) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
c, err := s.NewConn()
if err != nil {
return nil, err
}
err = c.Connect(ctx, network, addr)
if err != nil {
c.Close()
return nil, err
}
return c, nil
}
|
go
|
func (s *Socket) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
c, err := s.NewConn()
if err != nil {
return nil, err
}
err = c.Connect(ctx, network, addr)
if err != nil {
c.Close()
return nil, err
}
return c, nil
}
|
[
"func",
"(",
"s",
"*",
"Socket",
")",
"DialContext",
"(",
"ctx",
"context",
".",
"Context",
",",
"network",
",",
"addr",
"string",
")",
"(",
"net",
".",
"Conn",
",",
"error",
")",
"{",
"c",
",",
"err",
":=",
"s",
".",
"NewConn",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"err",
"=",
"c",
".",
"Connect",
"(",
"ctx",
",",
"network",
",",
"addr",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"c",
".",
"Close",
"(",
")",
"\n",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"c",
",",
"nil",
"\n",
"}"
] |
// Passing an empty network will use the network of the Socket's listener.
|
[
"Passing",
"an",
"empty",
"network",
"will",
"use",
"the",
"network",
"of",
"the",
"Socket",
"s",
"listener",
"."
] |
d7dd83bce17eb552d89dc42b7acdb8498e7e70db
|
https://github.com/anacrolix/go-libutp/blob/d7dd83bce17eb552d89dc42b7acdb8498e7e70db/socket.go#L405-L416
|
15,008 |
goburrow/serial
|
serial_linux.go
|
fdget
|
func fdget(fd int, fds *syscall.FdSet) (index, offset int) {
index = fd / (syscall.FD_SETSIZE / len(fds.Bits)) % len(fds.Bits)
offset = fd % (syscall.FD_SETSIZE / len(fds.Bits))
return
}
|
go
|
func fdget(fd int, fds *syscall.FdSet) (index, offset int) {
index = fd / (syscall.FD_SETSIZE / len(fds.Bits)) % len(fds.Bits)
offset = fd % (syscall.FD_SETSIZE / len(fds.Bits))
return
}
|
[
"func",
"fdget",
"(",
"fd",
"int",
",",
"fds",
"*",
"syscall",
".",
"FdSet",
")",
"(",
"index",
",",
"offset",
"int",
")",
"{",
"index",
"=",
"fd",
"/",
"(",
"syscall",
".",
"FD_SETSIZE",
"/",
"len",
"(",
"fds",
".",
"Bits",
")",
")",
"%",
"len",
"(",
"fds",
".",
"Bits",
")",
"\n",
"offset",
"=",
"fd",
"%",
"(",
"syscall",
".",
"FD_SETSIZE",
"/",
"len",
"(",
"fds",
".",
"Bits",
")",
")",
"\n",
"return",
"\n",
"}"
] |
// fdget returns index and offset of fd in fds.
|
[
"fdget",
"returns",
"index",
"and",
"offset",
"of",
"fd",
"in",
"fds",
"."
] |
5efbe925ecf714f8ba147bf2226f2e7afc7111bc
|
https://github.com/goburrow/serial/blob/5efbe925ecf714f8ba147bf2226f2e7afc7111bc/serial_linux.go#L87-L91
|
15,009 |
goburrow/serial
|
serial_linux.go
|
fdset
|
func fdset(fd int, fds *syscall.FdSet) {
idx, pos := fdget(fd, fds)
fds.Bits[idx] = 1 << uint(pos)
}
|
go
|
func fdset(fd int, fds *syscall.FdSet) {
idx, pos := fdget(fd, fds)
fds.Bits[idx] = 1 << uint(pos)
}
|
[
"func",
"fdset",
"(",
"fd",
"int",
",",
"fds",
"*",
"syscall",
".",
"FdSet",
")",
"{",
"idx",
",",
"pos",
":=",
"fdget",
"(",
"fd",
",",
"fds",
")",
"\n",
"fds",
".",
"Bits",
"[",
"idx",
"]",
"=",
"1",
"<<",
"uint",
"(",
"pos",
")",
"\n",
"}"
] |
// fdset implements FD_SET macro.
|
[
"fdset",
"implements",
"FD_SET",
"macro",
"."
] |
5efbe925ecf714f8ba147bf2226f2e7afc7111bc
|
https://github.com/goburrow/serial/blob/5efbe925ecf714f8ba147bf2226f2e7afc7111bc/serial_linux.go#L94-L97
|
15,010 |
goburrow/serial
|
serial_linux.go
|
fdisset
|
func fdisset(fd int, fds *syscall.FdSet) bool {
idx, pos := fdget(fd, fds)
return fds.Bits[idx]&(1<<uint(pos)) != 0
}
|
go
|
func fdisset(fd int, fds *syscall.FdSet) bool {
idx, pos := fdget(fd, fds)
return fds.Bits[idx]&(1<<uint(pos)) != 0
}
|
[
"func",
"fdisset",
"(",
"fd",
"int",
",",
"fds",
"*",
"syscall",
".",
"FdSet",
")",
"bool",
"{",
"idx",
",",
"pos",
":=",
"fdget",
"(",
"fd",
",",
"fds",
")",
"\n",
"return",
"fds",
".",
"Bits",
"[",
"idx",
"]",
"&",
"(",
"1",
"<<",
"uint",
"(",
"pos",
")",
")",
"!=",
"0",
"\n",
"}"
] |
// fdisset implements FD_ISSET macro.
|
[
"fdisset",
"implements",
"FD_ISSET",
"macro",
"."
] |
5efbe925ecf714f8ba147bf2226f2e7afc7111bc
|
https://github.com/goburrow/serial/blob/5efbe925ecf714f8ba147bf2226f2e7afc7111bc/serial_linux.go#L100-L103
|
15,011 |
goburrow/serial
|
serial_windows.go
|
Read
|
func (p *port) Read(b []byte) (n int, err error) {
var done uint32
if err = syscall.ReadFile(p.handle, b, &done, nil); err != nil {
return
}
if done == 0 {
err = ErrTimeout
return
}
n = int(done)
return
}
|
go
|
func (p *port) Read(b []byte) (n int, err error) {
var done uint32
if err = syscall.ReadFile(p.handle, b, &done, nil); err != nil {
return
}
if done == 0 {
err = ErrTimeout
return
}
n = int(done)
return
}
|
[
"func",
"(",
"p",
"*",
"port",
")",
"Read",
"(",
"b",
"[",
"]",
"byte",
")",
"(",
"n",
"int",
",",
"err",
"error",
")",
"{",
"var",
"done",
"uint32",
"\n",
"if",
"err",
"=",
"syscall",
".",
"ReadFile",
"(",
"p",
".",
"handle",
",",
"b",
",",
"&",
"done",
",",
"nil",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"if",
"done",
"==",
"0",
"{",
"err",
"=",
"ErrTimeout",
"\n",
"return",
"\n",
"}",
"\n",
"n",
"=",
"int",
"(",
"done",
")",
"\n",
"return",
"\n",
"}"
] |
// Read reads from serial port.
// It is blocked until data received or timeout after p.timeout.
|
[
"Read",
"reads",
"from",
"serial",
"port",
".",
"It",
"is",
"blocked",
"until",
"data",
"received",
"or",
"timeout",
"after",
"p",
".",
"timeout",
"."
] |
5efbe925ecf714f8ba147bf2226f2e7afc7111bc
|
https://github.com/goburrow/serial/blob/5efbe925ecf714f8ba147bf2226f2e7afc7111bc/serial_windows.go#L62-L73
|
15,012 |
goburrow/serial
|
serial_darwin.go
|
syscallSelect
|
func syscallSelect(n int, r *syscall.FdSet, w *syscall.FdSet, e *syscall.FdSet, tv *syscall.Timeval) error {
return syscall.Select(n, r, w, e, tv)
}
|
go
|
func syscallSelect(n int, r *syscall.FdSet, w *syscall.FdSet, e *syscall.FdSet, tv *syscall.Timeval) error {
return syscall.Select(n, r, w, e, tv)
}
|
[
"func",
"syscallSelect",
"(",
"n",
"int",
",",
"r",
"*",
"syscall",
".",
"FdSet",
",",
"w",
"*",
"syscall",
".",
"FdSet",
",",
"e",
"*",
"syscall",
".",
"FdSet",
",",
"tv",
"*",
"syscall",
".",
"Timeval",
")",
"error",
"{",
"return",
"syscall",
".",
"Select",
"(",
"n",
",",
"r",
",",
"w",
",",
"e",
",",
"tv",
")",
"\n",
"}"
] |
// syscallSelect is a wapper for syscall.Select that only returns error.
|
[
"syscallSelect",
"is",
"a",
"wapper",
"for",
"syscall",
".",
"Select",
"that",
"only",
"returns",
"error",
"."
] |
5efbe925ecf714f8ba147bf2226f2e7afc7111bc
|
https://github.com/goburrow/serial/blob/5efbe925ecf714f8ba147bf2226f2e7afc7111bc/serial_darwin.go#L38-L40
|
15,013 |
goburrow/serial
|
serial_posix.go
|
Read
|
func (p *port) Read(b []byte) (n int, err error) {
var rfds syscall.FdSet
fd := p.fd
fdset(fd, &rfds)
var tv *syscall.Timeval
if p.timeout > 0 {
timeout := syscall.NsecToTimeval(p.timeout.Nanoseconds())
tv = &timeout
}
for {
// If syscall.Select() returns EINTR (Interrupted system call), retry it
if err = syscallSelect(fd+1, &rfds, nil, nil, tv); err == nil {
break
}
if err != syscall.EINTR {
err = fmt.Errorf("serial: could not select: %v", err)
return
}
}
if !fdisset(fd, &rfds) {
// Timeout
err = ErrTimeout
return
}
n, err = syscall.Read(fd, b)
return
}
|
go
|
func (p *port) Read(b []byte) (n int, err error) {
var rfds syscall.FdSet
fd := p.fd
fdset(fd, &rfds)
var tv *syscall.Timeval
if p.timeout > 0 {
timeout := syscall.NsecToTimeval(p.timeout.Nanoseconds())
tv = &timeout
}
for {
// If syscall.Select() returns EINTR (Interrupted system call), retry it
if err = syscallSelect(fd+1, &rfds, nil, nil, tv); err == nil {
break
}
if err != syscall.EINTR {
err = fmt.Errorf("serial: could not select: %v", err)
return
}
}
if !fdisset(fd, &rfds) {
// Timeout
err = ErrTimeout
return
}
n, err = syscall.Read(fd, b)
return
}
|
[
"func",
"(",
"p",
"*",
"port",
")",
"Read",
"(",
"b",
"[",
"]",
"byte",
")",
"(",
"n",
"int",
",",
"err",
"error",
")",
"{",
"var",
"rfds",
"syscall",
".",
"FdSet",
"\n\n",
"fd",
":=",
"p",
".",
"fd",
"\n",
"fdset",
"(",
"fd",
",",
"&",
"rfds",
")",
"\n\n",
"var",
"tv",
"*",
"syscall",
".",
"Timeval",
"\n",
"if",
"p",
".",
"timeout",
">",
"0",
"{",
"timeout",
":=",
"syscall",
".",
"NsecToTimeval",
"(",
"p",
".",
"timeout",
".",
"Nanoseconds",
"(",
")",
")",
"\n",
"tv",
"=",
"&",
"timeout",
"\n",
"}",
"\n",
"for",
"{",
"// If syscall.Select() returns EINTR (Interrupted system call), retry it",
"if",
"err",
"=",
"syscallSelect",
"(",
"fd",
"+",
"1",
",",
"&",
"rfds",
",",
"nil",
",",
"nil",
",",
"tv",
")",
";",
"err",
"==",
"nil",
"{",
"break",
"\n",
"}",
"\n",
"if",
"err",
"!=",
"syscall",
".",
"EINTR",
"{",
"err",
"=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"!",
"fdisset",
"(",
"fd",
",",
"&",
"rfds",
")",
"{",
"// Timeout",
"err",
"=",
"ErrTimeout",
"\n",
"return",
"\n",
"}",
"\n",
"n",
",",
"err",
"=",
"syscall",
".",
"Read",
"(",
"fd",
",",
"b",
")",
"\n",
"return",
"\n",
"}"
] |
// Read reads from serial port. Port must be opened before calling this method.
// It is blocked until all data received or timeout after p.timeout.
|
[
"Read",
"reads",
"from",
"serial",
"port",
".",
"Port",
"must",
"be",
"opened",
"before",
"calling",
"this",
"method",
".",
"It",
"is",
"blocked",
"until",
"all",
"data",
"received",
"or",
"timeout",
"after",
"p",
".",
"timeout",
"."
] |
5efbe925ecf714f8ba147bf2226f2e7afc7111bc
|
https://github.com/goburrow/serial/blob/5efbe925ecf714f8ba147bf2226f2e7afc7111bc/serial_posix.go#L87-L115
|
15,014 |
goburrow/serial
|
serial_posix.go
|
backupTermios
|
func (p *port) backupTermios() {
oldTermios := &syscall.Termios{}
if err := tcgetattr(p.fd, oldTermios); err != nil {
// Warning only.
log.Printf("serial: could not get setting: %v\n", err)
return
}
// Will be reloaded when closing.
p.oldTermios = oldTermios
}
|
go
|
func (p *port) backupTermios() {
oldTermios := &syscall.Termios{}
if err := tcgetattr(p.fd, oldTermios); err != nil {
// Warning only.
log.Printf("serial: could not get setting: %v\n", err)
return
}
// Will be reloaded when closing.
p.oldTermios = oldTermios
}
|
[
"func",
"(",
"p",
"*",
"port",
")",
"backupTermios",
"(",
")",
"{",
"oldTermios",
":=",
"&",
"syscall",
".",
"Termios",
"{",
"}",
"\n",
"if",
"err",
":=",
"tcgetattr",
"(",
"p",
".",
"fd",
",",
"oldTermios",
")",
";",
"err",
"!=",
"nil",
"{",
"// Warning only.",
"log",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"err",
")",
"\n",
"return",
"\n",
"}",
"\n",
"// Will be reloaded when closing.",
"p",
".",
"oldTermios",
"=",
"oldTermios",
"\n",
"}"
] |
// backupTermios saves current termios setting.
// Make sure that device file has been opened before calling this function.
|
[
"backupTermios",
"saves",
"current",
"termios",
"setting",
".",
"Make",
"sure",
"that",
"device",
"file",
"has",
"been",
"opened",
"before",
"calling",
"this",
"function",
"."
] |
5efbe925ecf714f8ba147bf2226f2e7afc7111bc
|
https://github.com/goburrow/serial/blob/5efbe925ecf714f8ba147bf2226f2e7afc7111bc/serial_posix.go#L132-L141
|
15,015 |
goburrow/serial
|
serial_posix.go
|
restoreTermios
|
func (p *port) restoreTermios() {
if p.oldTermios == nil {
return
}
if err := tcsetattr(p.fd, p.oldTermios); err != nil {
// Warning only.
log.Printf("serial: could not restore setting: %v\n", err)
return
}
p.oldTermios = nil
}
|
go
|
func (p *port) restoreTermios() {
if p.oldTermios == nil {
return
}
if err := tcsetattr(p.fd, p.oldTermios); err != nil {
// Warning only.
log.Printf("serial: could not restore setting: %v\n", err)
return
}
p.oldTermios = nil
}
|
[
"func",
"(",
"p",
"*",
"port",
")",
"restoreTermios",
"(",
")",
"{",
"if",
"p",
".",
"oldTermios",
"==",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"if",
"err",
":=",
"tcsetattr",
"(",
"p",
".",
"fd",
",",
"p",
".",
"oldTermios",
")",
";",
"err",
"!=",
"nil",
"{",
"// Warning only.",
"log",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"err",
")",
"\n",
"return",
"\n",
"}",
"\n",
"p",
".",
"oldTermios",
"=",
"nil",
"\n",
"}"
] |
// restoreTermios restores backed up termios setting.
// Make sure that device file has been opened before calling this function.
|
[
"restoreTermios",
"restores",
"backed",
"up",
"termios",
"setting",
".",
"Make",
"sure",
"that",
"device",
"file",
"has",
"been",
"opened",
"before",
"calling",
"this",
"function",
"."
] |
5efbe925ecf714f8ba147bf2226f2e7afc7111bc
|
https://github.com/goburrow/serial/blob/5efbe925ecf714f8ba147bf2226f2e7afc7111bc/serial_posix.go#L145-L155
|
15,016 |
goburrow/serial
|
serial_posix.go
|
newTermios
|
func newTermios(c *Config) (termios *syscall.Termios, err error) {
termios = &syscall.Termios{}
flag := termios.Cflag
// Baud rate
if c.BaudRate == 0 {
// 19200 is the required default.
flag = syscall.B19200
} else {
var ok bool
flag, ok = baudRates[c.BaudRate]
if !ok {
err = fmt.Errorf("serial: unsupported baud rate %v", c.BaudRate)
return
}
}
termios.Cflag |= flag
// Input baud.
cfSetIspeed(termios, flag)
// Output baud.
cfSetOspeed(termios, flag)
// Character size.
if c.DataBits == 0 {
flag = syscall.CS8
} else {
var ok bool
flag, ok = charSizes[c.DataBits]
if !ok {
err = fmt.Errorf("serial: unsupported character size %v", c.DataBits)
return
}
}
termios.Cflag |= flag
// Stop bits
switch c.StopBits {
case 0, 1:
// Default is one stop bit.
// noop
case 2:
// CSTOPB: Set two stop bits.
termios.Cflag |= syscall.CSTOPB
default:
err = fmt.Errorf("serial: unsupported stop bits %v", c.StopBits)
return
}
switch c.Parity {
case "N":
// noop
case "O":
// PARODD: Parity is odd.
termios.Cflag |= syscall.PARODD
fallthrough
case "", "E":
// As mentioned in the modbus spec, the default parity mode must be Even parity
// PARENB: Enable parity generation on output.
termios.Cflag |= syscall.PARENB
// INPCK: Enable input parity checking.
termios.Iflag |= syscall.INPCK
default:
err = fmt.Errorf("serial: unsupported parity %v", c.Parity)
return
}
// Control modes.
// CREAD: Enable receiver.
// CLOCAL: Ignore control lines.
termios.Cflag |= syscall.CREAD | syscall.CLOCAL
// Special characters.
// VMIN: Minimum number of characters for noncanonical read.
// VTIME: Time in deciseconds for noncanonical read.
// Both are unused as NDELAY is we utilized when opening device.
return
}
|
go
|
func newTermios(c *Config) (termios *syscall.Termios, err error) {
termios = &syscall.Termios{}
flag := termios.Cflag
// Baud rate
if c.BaudRate == 0 {
// 19200 is the required default.
flag = syscall.B19200
} else {
var ok bool
flag, ok = baudRates[c.BaudRate]
if !ok {
err = fmt.Errorf("serial: unsupported baud rate %v", c.BaudRate)
return
}
}
termios.Cflag |= flag
// Input baud.
cfSetIspeed(termios, flag)
// Output baud.
cfSetOspeed(termios, flag)
// Character size.
if c.DataBits == 0 {
flag = syscall.CS8
} else {
var ok bool
flag, ok = charSizes[c.DataBits]
if !ok {
err = fmt.Errorf("serial: unsupported character size %v", c.DataBits)
return
}
}
termios.Cflag |= flag
// Stop bits
switch c.StopBits {
case 0, 1:
// Default is one stop bit.
// noop
case 2:
// CSTOPB: Set two stop bits.
termios.Cflag |= syscall.CSTOPB
default:
err = fmt.Errorf("serial: unsupported stop bits %v", c.StopBits)
return
}
switch c.Parity {
case "N":
// noop
case "O":
// PARODD: Parity is odd.
termios.Cflag |= syscall.PARODD
fallthrough
case "", "E":
// As mentioned in the modbus spec, the default parity mode must be Even parity
// PARENB: Enable parity generation on output.
termios.Cflag |= syscall.PARENB
// INPCK: Enable input parity checking.
termios.Iflag |= syscall.INPCK
default:
err = fmt.Errorf("serial: unsupported parity %v", c.Parity)
return
}
// Control modes.
// CREAD: Enable receiver.
// CLOCAL: Ignore control lines.
termios.Cflag |= syscall.CREAD | syscall.CLOCAL
// Special characters.
// VMIN: Minimum number of characters for noncanonical read.
// VTIME: Time in deciseconds for noncanonical read.
// Both are unused as NDELAY is we utilized when opening device.
return
}
|
[
"func",
"newTermios",
"(",
"c",
"*",
"Config",
")",
"(",
"termios",
"*",
"syscall",
".",
"Termios",
",",
"err",
"error",
")",
"{",
"termios",
"=",
"&",
"syscall",
".",
"Termios",
"{",
"}",
"\n",
"flag",
":=",
"termios",
".",
"Cflag",
"\n",
"// Baud rate",
"if",
"c",
".",
"BaudRate",
"==",
"0",
"{",
"// 19200 is the required default.",
"flag",
"=",
"syscall",
".",
"B19200",
"\n",
"}",
"else",
"{",
"var",
"ok",
"bool",
"\n",
"flag",
",",
"ok",
"=",
"baudRates",
"[",
"c",
".",
"BaudRate",
"]",
"\n",
"if",
"!",
"ok",
"{",
"err",
"=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"c",
".",
"BaudRate",
")",
"\n",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"termios",
".",
"Cflag",
"|=",
"flag",
"\n",
"// Input baud.",
"cfSetIspeed",
"(",
"termios",
",",
"flag",
")",
"\n",
"// Output baud.",
"cfSetOspeed",
"(",
"termios",
",",
"flag",
")",
"\n",
"// Character size.",
"if",
"c",
".",
"DataBits",
"==",
"0",
"{",
"flag",
"=",
"syscall",
".",
"CS8",
"\n",
"}",
"else",
"{",
"var",
"ok",
"bool",
"\n",
"flag",
",",
"ok",
"=",
"charSizes",
"[",
"c",
".",
"DataBits",
"]",
"\n",
"if",
"!",
"ok",
"{",
"err",
"=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"c",
".",
"DataBits",
")",
"\n",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"termios",
".",
"Cflag",
"|=",
"flag",
"\n",
"// Stop bits",
"switch",
"c",
".",
"StopBits",
"{",
"case",
"0",
",",
"1",
":",
"// Default is one stop bit.",
"// noop",
"case",
"2",
":",
"// CSTOPB: Set two stop bits.",
"termios",
".",
"Cflag",
"|=",
"syscall",
".",
"CSTOPB",
"\n",
"default",
":",
"err",
"=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"c",
".",
"StopBits",
")",
"\n",
"return",
"\n",
"}",
"\n",
"switch",
"c",
".",
"Parity",
"{",
"case",
"\"",
"\"",
":",
"// noop",
"case",
"\"",
"\"",
":",
"// PARODD: Parity is odd.",
"termios",
".",
"Cflag",
"|=",
"syscall",
".",
"PARODD",
"\n",
"fallthrough",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"// As mentioned in the modbus spec, the default parity mode must be Even parity",
"// PARENB: Enable parity generation on output.",
"termios",
".",
"Cflag",
"|=",
"syscall",
".",
"PARENB",
"\n",
"// INPCK: Enable input parity checking.",
"termios",
".",
"Iflag",
"|=",
"syscall",
".",
"INPCK",
"\n",
"default",
":",
"err",
"=",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"c",
".",
"Parity",
")",
"\n",
"return",
"\n",
"}",
"\n",
"// Control modes.",
"// CREAD: Enable receiver.",
"// CLOCAL: Ignore control lines.",
"termios",
".",
"Cflag",
"|=",
"syscall",
".",
"CREAD",
"|",
"syscall",
".",
"CLOCAL",
"\n",
"// Special characters.",
"// VMIN: Minimum number of characters for noncanonical read.",
"// VTIME: Time in deciseconds for noncanonical read.",
"// Both are unused as NDELAY is we utilized when opening device.",
"return",
"\n",
"}"
] |
// Helpers for termios
|
[
"Helpers",
"for",
"termios"
] |
5efbe925ecf714f8ba147bf2226f2e7afc7111bc
|
https://github.com/goburrow/serial/blob/5efbe925ecf714f8ba147bf2226f2e7afc7111bc/serial_posix.go#L159-L229
|
15,017 |
goburrow/serial
|
serial_posix.go
|
enableRS485
|
func enableRS485(fd int, config *RS485Config) error {
if !config.Enabled {
return nil
}
rs485 := rs485_ioctl_opts{
rs485Enabled,
uint32(config.DelayRtsBeforeSend / time.Millisecond),
uint32(config.DelayRtsAfterSend / time.Millisecond),
[5]uint32{0, 0, 0, 0, 0},
}
if config.RtsHighDuringSend {
rs485.flags |= rs485RTSOnSend
}
if config.RtsHighAfterSend {
rs485.flags |= rs485RTSAfterSend
}
if config.RxDuringTx {
rs485.flags |= rs485RXDuringTX
}
r, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(fd),
uintptr(rs485Tiocs),
uintptr(unsafe.Pointer(&rs485)))
if errno != 0 {
return os.NewSyscallError("SYS_IOCTL (RS485)", errno)
}
if r != 0 {
return errors.New("serial: unknown error from SYS_IOCTL (RS485)")
}
return nil
}
|
go
|
func enableRS485(fd int, config *RS485Config) error {
if !config.Enabled {
return nil
}
rs485 := rs485_ioctl_opts{
rs485Enabled,
uint32(config.DelayRtsBeforeSend / time.Millisecond),
uint32(config.DelayRtsAfterSend / time.Millisecond),
[5]uint32{0, 0, 0, 0, 0},
}
if config.RtsHighDuringSend {
rs485.flags |= rs485RTSOnSend
}
if config.RtsHighAfterSend {
rs485.flags |= rs485RTSAfterSend
}
if config.RxDuringTx {
rs485.flags |= rs485RXDuringTX
}
r, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(fd),
uintptr(rs485Tiocs),
uintptr(unsafe.Pointer(&rs485)))
if errno != 0 {
return os.NewSyscallError("SYS_IOCTL (RS485)", errno)
}
if r != 0 {
return errors.New("serial: unknown error from SYS_IOCTL (RS485)")
}
return nil
}
|
[
"func",
"enableRS485",
"(",
"fd",
"int",
",",
"config",
"*",
"RS485Config",
")",
"error",
"{",
"if",
"!",
"config",
".",
"Enabled",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"rs485",
":=",
"rs485_ioctl_opts",
"{",
"rs485Enabled",
",",
"uint32",
"(",
"config",
".",
"DelayRtsBeforeSend",
"/",
"time",
".",
"Millisecond",
")",
",",
"uint32",
"(",
"config",
".",
"DelayRtsAfterSend",
"/",
"time",
".",
"Millisecond",
")",
",",
"[",
"5",
"]",
"uint32",
"{",
"0",
",",
"0",
",",
"0",
",",
"0",
",",
"0",
"}",
",",
"}",
"\n\n",
"if",
"config",
".",
"RtsHighDuringSend",
"{",
"rs485",
".",
"flags",
"|=",
"rs485RTSOnSend",
"\n",
"}",
"\n",
"if",
"config",
".",
"RtsHighAfterSend",
"{",
"rs485",
".",
"flags",
"|=",
"rs485RTSAfterSend",
"\n",
"}",
"\n",
"if",
"config",
".",
"RxDuringTx",
"{",
"rs485",
".",
"flags",
"|=",
"rs485RXDuringTX",
"\n",
"}",
"\n\n",
"r",
",",
"_",
",",
"errno",
":=",
"syscall",
".",
"Syscall",
"(",
"syscall",
".",
"SYS_IOCTL",
",",
"uintptr",
"(",
"fd",
")",
",",
"uintptr",
"(",
"rs485Tiocs",
")",
",",
"uintptr",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"rs485",
")",
")",
")",
"\n",
"if",
"errno",
"!=",
"0",
"{",
"return",
"os",
".",
"NewSyscallError",
"(",
"\"",
"\"",
",",
"errno",
")",
"\n",
"}",
"\n",
"if",
"r",
"!=",
"0",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// enableRS485 enables RS485 functionality of driver via an ioctl if the config says so
|
[
"enableRS485",
"enables",
"RS485",
"functionality",
"of",
"driver",
"via",
"an",
"ioctl",
"if",
"the",
"config",
"says",
"so"
] |
5efbe925ecf714f8ba147bf2226f2e7afc7111bc
|
https://github.com/goburrow/serial/blob/5efbe925ecf714f8ba147bf2226f2e7afc7111bc/serial_posix.go#L232-L265
|
15,018 |
goburrow/serial
|
serial.go
|
Open
|
func Open(c *Config) (p Port, err error) {
p = New()
err = p.Open(c)
return
}
|
go
|
func Open(c *Config) (p Port, err error) {
p = New()
err = p.Open(c)
return
}
|
[
"func",
"Open",
"(",
"c",
"*",
"Config",
")",
"(",
"p",
"Port",
",",
"err",
"error",
")",
"{",
"p",
"=",
"New",
"(",
")",
"\n",
"err",
"=",
"p",
".",
"Open",
"(",
"c",
")",
"\n",
"return",
"\n",
"}"
] |
// Open opens a serial port.
|
[
"Open",
"opens",
"a",
"serial",
"port",
"."
] |
5efbe925ecf714f8ba147bf2226f2e7afc7111bc
|
https://github.com/goburrow/serial/blob/5efbe925ecf714f8ba147bf2226f2e7afc7111bc/serial.go#L60-L64
|
15,019 |
pin/tftp
|
server.go
|
NewServer
|
func NewServer(readHandler func(filename string, rf io.ReaderFrom) error,
writeHandler func(filename string, wt io.WriterTo) error) *Server {
s := &Server{
timeout: defaultTimeout,
retries: defaultRetries,
runGC: make(chan []string),
gcInterval: 1 * time.Minute,
packetReadTimeout: 100 * time.Millisecond,
readHandler: readHandler,
writeHandler: writeHandler,
}
return s
}
|
go
|
func NewServer(readHandler func(filename string, rf io.ReaderFrom) error,
writeHandler func(filename string, wt io.WriterTo) error) *Server {
s := &Server{
timeout: defaultTimeout,
retries: defaultRetries,
runGC: make(chan []string),
gcInterval: 1 * time.Minute,
packetReadTimeout: 100 * time.Millisecond,
readHandler: readHandler,
writeHandler: writeHandler,
}
return s
}
|
[
"func",
"NewServer",
"(",
"readHandler",
"func",
"(",
"filename",
"string",
",",
"rf",
"io",
".",
"ReaderFrom",
")",
"error",
",",
"writeHandler",
"func",
"(",
"filename",
"string",
",",
"wt",
"io",
".",
"WriterTo",
")",
"error",
")",
"*",
"Server",
"{",
"s",
":=",
"&",
"Server",
"{",
"timeout",
":",
"defaultTimeout",
",",
"retries",
":",
"defaultRetries",
",",
"runGC",
":",
"make",
"(",
"chan",
"[",
"]",
"string",
")",
",",
"gcInterval",
":",
"1",
"*",
"time",
".",
"Minute",
",",
"packetReadTimeout",
":",
"100",
"*",
"time",
".",
"Millisecond",
",",
"readHandler",
":",
"readHandler",
",",
"writeHandler",
":",
"writeHandler",
",",
"}",
"\n",
"return",
"s",
"\n",
"}"
] |
// NewServer creates TFTP server. It requires two functions to handle
// read and write requests.
// In case nil is provided for read or write handler the respective
// operation is disabled.
|
[
"NewServer",
"creates",
"TFTP",
"server",
".",
"It",
"requires",
"two",
"functions",
"to",
"handle",
"read",
"and",
"write",
"requests",
".",
"In",
"case",
"nil",
"is",
"provided",
"for",
"read",
"or",
"write",
"handler",
"the",
"respective",
"operation",
"is",
"disabled",
"."
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/server.go#L18-L30
|
15,020 |
pin/tftp
|
server.go
|
SetAnticipate
|
func (s *Server) SetAnticipate(winsz uint) {
if winsz > 1 {
s.sendAEnable = true
s.sendAWinSz = winsz
} else {
s.sendAEnable = false
s.sendAWinSz = 1
}
}
|
go
|
func (s *Server) SetAnticipate(winsz uint) {
if winsz > 1 {
s.sendAEnable = true
s.sendAWinSz = winsz
} else {
s.sendAEnable = false
s.sendAWinSz = 1
}
}
|
[
"func",
"(",
"s",
"*",
"Server",
")",
"SetAnticipate",
"(",
"winsz",
"uint",
")",
"{",
"if",
"winsz",
">",
"1",
"{",
"s",
".",
"sendAEnable",
"=",
"true",
"\n",
"s",
".",
"sendAWinSz",
"=",
"winsz",
"\n",
"}",
"else",
"{",
"s",
".",
"sendAEnable",
"=",
"false",
"\n",
"s",
".",
"sendAWinSz",
"=",
"1",
"\n",
"}",
"\n",
"}"
] |
// SetAnticipate provides an experimental feature in which when a packets
// is requested the server will keep sending a number of packets before
// checking whether an ack has been received. It improves tftp downloading
// speed by a few times.
// The argument winsz specifies how many packets will be sent before
// waiting for an ack packet.
// When winsz is bigger than 1, the feature is enabled, and the server
// runs through a different experimental code path. When winsz is 0 or 1,
// the feature is disabled.
|
[
"SetAnticipate",
"provides",
"an",
"experimental",
"feature",
"in",
"which",
"when",
"a",
"packets",
"is",
"requested",
"the",
"server",
"will",
"keep",
"sending",
"a",
"number",
"of",
"packets",
"before",
"checking",
"whether",
"an",
"ack",
"has",
"been",
"received",
".",
"It",
"improves",
"tftp",
"downloading",
"speed",
"by",
"a",
"few",
"times",
".",
"The",
"argument",
"winsz",
"specifies",
"how",
"many",
"packets",
"will",
"be",
"sent",
"before",
"waiting",
"for",
"an",
"ack",
"packet",
".",
"When",
"winsz",
"is",
"bigger",
"than",
"1",
"the",
"feature",
"is",
"enabled",
"and",
"the",
"server",
"runs",
"through",
"a",
"different",
"experimental",
"code",
"path",
".",
"When",
"winsz",
"is",
"0",
"or",
"1",
"the",
"feature",
"is",
"disabled",
"."
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/server.go#L76-L84
|
15,021 |
pin/tftp
|
server.go
|
EnableSinglePort
|
func (s *Server) EnableSinglePort() {
s.singlePort = true
s.handlers = make(map[string]chan []byte, datagramLength)
s.gcCollect = make(chan string)
s.bufPool = sync.Pool{
New: func() interface{} {
return make([]byte, datagramLength)
},
}
go s.internalGC()
}
|
go
|
func (s *Server) EnableSinglePort() {
s.singlePort = true
s.handlers = make(map[string]chan []byte, datagramLength)
s.gcCollect = make(chan string)
s.bufPool = sync.Pool{
New: func() interface{} {
return make([]byte, datagramLength)
},
}
go s.internalGC()
}
|
[
"func",
"(",
"s",
"*",
"Server",
")",
"EnableSinglePort",
"(",
")",
"{",
"s",
".",
"singlePort",
"=",
"true",
"\n",
"s",
".",
"handlers",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"chan",
"[",
"]",
"byte",
",",
"datagramLength",
")",
"\n",
"s",
".",
"gcCollect",
"=",
"make",
"(",
"chan",
"string",
")",
"\n",
"s",
".",
"bufPool",
"=",
"sync",
".",
"Pool",
"{",
"New",
":",
"func",
"(",
")",
"interface",
"{",
"}",
"{",
"return",
"make",
"(",
"[",
"]",
"byte",
",",
"datagramLength",
")",
"\n",
"}",
",",
"}",
"\n",
"go",
"s",
".",
"internalGC",
"(",
")",
"\n",
"}"
] |
// EnableSinglePort enables an experimental mode where the server will
// serve all connections on port 69 only. There will be no random TIDs
// on the server side.
//
// Enabling this will negatively impact performance
|
[
"EnableSinglePort",
"enables",
"an",
"experimental",
"mode",
"where",
"the",
"server",
"will",
"serve",
"all",
"connections",
"on",
"port",
"69",
"only",
".",
"There",
"will",
"be",
"no",
"random",
"TIDs",
"on",
"the",
"server",
"side",
".",
"Enabling",
"this",
"will",
"negatively",
"impact",
"performance"
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/server.go#L91-L101
|
15,022 |
pin/tftp
|
server.go
|
SetTimeout
|
func (s *Server) SetTimeout(t time.Duration) {
if t <= 0 {
s.timeout = defaultTimeout
} else {
s.timeout = t
}
}
|
go
|
func (s *Server) SetTimeout(t time.Duration) {
if t <= 0 {
s.timeout = defaultTimeout
} else {
s.timeout = t
}
}
|
[
"func",
"(",
"s",
"*",
"Server",
")",
"SetTimeout",
"(",
"t",
"time",
".",
"Duration",
")",
"{",
"if",
"t",
"<=",
"0",
"{",
"s",
".",
"timeout",
"=",
"defaultTimeout",
"\n",
"}",
"else",
"{",
"s",
".",
"timeout",
"=",
"t",
"\n",
"}",
"\n",
"}"
] |
// SetTimeout sets maximum time server waits for single network
// round-trip to succeed.
// Default is 5 seconds.
|
[
"SetTimeout",
"sets",
"maximum",
"time",
"server",
"waits",
"for",
"single",
"network",
"round",
"-",
"trip",
"to",
"succeed",
".",
"Default",
"is",
"5",
"seconds",
"."
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/server.go#L106-L112
|
15,023 |
pin/tftp
|
server.go
|
SetRetries
|
func (s *Server) SetRetries(count int) {
if count < 1 {
s.retries = defaultRetries
} else {
s.retries = count
}
}
|
go
|
func (s *Server) SetRetries(count int) {
if count < 1 {
s.retries = defaultRetries
} else {
s.retries = count
}
}
|
[
"func",
"(",
"s",
"*",
"Server",
")",
"SetRetries",
"(",
"count",
"int",
")",
"{",
"if",
"count",
"<",
"1",
"{",
"s",
".",
"retries",
"=",
"defaultRetries",
"\n",
"}",
"else",
"{",
"s",
".",
"retries",
"=",
"count",
"\n",
"}",
"\n",
"}"
] |
// SetRetries sets maximum number of attempts server made to transmit a
// packet.
// Default is 5 attempts.
|
[
"SetRetries",
"sets",
"maximum",
"number",
"of",
"attempts",
"server",
"made",
"to",
"transmit",
"a",
"packet",
".",
"Default",
"is",
"5",
"attempts",
"."
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/server.go#L130-L136
|
15,024 |
pin/tftp
|
server.go
|
ListenAndServe
|
func (s *Server) ListenAndServe(addr string) error {
a, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return err
}
conn, err := net.ListenUDP("udp", a)
if err != nil {
return err
}
return s.Serve(conn)
}
|
go
|
func (s *Server) ListenAndServe(addr string) error {
a, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return err
}
conn, err := net.ListenUDP("udp", a)
if err != nil {
return err
}
return s.Serve(conn)
}
|
[
"func",
"(",
"s",
"*",
"Server",
")",
"ListenAndServe",
"(",
"addr",
"string",
")",
"error",
"{",
"a",
",",
"err",
":=",
"net",
".",
"ResolveUDPAddr",
"(",
"\"",
"\"",
",",
"addr",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"conn",
",",
"err",
":=",
"net",
".",
"ListenUDP",
"(",
"\"",
"\"",
",",
"a",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"s",
".",
"Serve",
"(",
"conn",
")",
"\n",
"}"
] |
// ListenAndServe binds to address provided and start the server.
// ListenAndServe returns when Shutdown is called.
|
[
"ListenAndServe",
"binds",
"to",
"address",
"provided",
"and",
"start",
"the",
"server",
".",
"ListenAndServe",
"returns",
"when",
"Shutdown",
"is",
"called",
"."
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/server.go#L146-L156
|
15,025 |
pin/tftp
|
server.go
|
Serve
|
func (s *Server) Serve(conn *net.UDPConn) error {
defer conn.Close()
laddr := conn.LocalAddr()
host, _, err := net.SplitHostPort(laddr.String())
if err != nil {
return err
}
s.conn = conn
// Having seperate control paths for IP4 and IP6 is annoying,
// but necessary at this point.
addr := net.ParseIP(host)
if addr == nil {
return fmt.Errorf("Failed to determine IP class of listening address")
}
if addr.To4() != nil {
s.conn4 = ipv4.NewPacketConn(conn)
if err := s.conn4.SetControlMessage(ipv4.FlagDst|ipv4.FlagInterface, true); err != nil {
s.conn4 = nil
}
} else {
s.conn6 = ipv6.NewPacketConn(conn)
if err := s.conn6.SetControlMessage(ipv6.FlagDst|ipv6.FlagInterface, true); err != nil {
s.conn6 = nil
}
}
s.quit = make(chan chan struct{})
if s.singlePort {
s.singlePortProcessRequests()
} else {
for {
select {
case q := <-s.quit:
q <- struct{}{}
return nil
default:
var err error
if s.conn4 != nil {
err = s.processRequest4()
} else if s.conn6 != nil {
err = s.processRequest6()
} else {
err = s.processRequest()
}
if err != nil {
// TODO: add logging handler
}
}
}
}
return nil
}
|
go
|
func (s *Server) Serve(conn *net.UDPConn) error {
defer conn.Close()
laddr := conn.LocalAddr()
host, _, err := net.SplitHostPort(laddr.String())
if err != nil {
return err
}
s.conn = conn
// Having seperate control paths for IP4 and IP6 is annoying,
// but necessary at this point.
addr := net.ParseIP(host)
if addr == nil {
return fmt.Errorf("Failed to determine IP class of listening address")
}
if addr.To4() != nil {
s.conn4 = ipv4.NewPacketConn(conn)
if err := s.conn4.SetControlMessage(ipv4.FlagDst|ipv4.FlagInterface, true); err != nil {
s.conn4 = nil
}
} else {
s.conn6 = ipv6.NewPacketConn(conn)
if err := s.conn6.SetControlMessage(ipv6.FlagDst|ipv6.FlagInterface, true); err != nil {
s.conn6 = nil
}
}
s.quit = make(chan chan struct{})
if s.singlePort {
s.singlePortProcessRequests()
} else {
for {
select {
case q := <-s.quit:
q <- struct{}{}
return nil
default:
var err error
if s.conn4 != nil {
err = s.processRequest4()
} else if s.conn6 != nil {
err = s.processRequest6()
} else {
err = s.processRequest()
}
if err != nil {
// TODO: add logging handler
}
}
}
}
return nil
}
|
[
"func",
"(",
"s",
"*",
"Server",
")",
"Serve",
"(",
"conn",
"*",
"net",
".",
"UDPConn",
")",
"error",
"{",
"defer",
"conn",
".",
"Close",
"(",
")",
"\n",
"laddr",
":=",
"conn",
".",
"LocalAddr",
"(",
")",
"\n",
"host",
",",
"_",
",",
"err",
":=",
"net",
".",
"SplitHostPort",
"(",
"laddr",
".",
"String",
"(",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"s",
".",
"conn",
"=",
"conn",
"\n",
"// Having seperate control paths for IP4 and IP6 is annoying,",
"// but necessary at this point.",
"addr",
":=",
"net",
".",
"ParseIP",
"(",
"host",
")",
"\n",
"if",
"addr",
"==",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"addr",
".",
"To4",
"(",
")",
"!=",
"nil",
"{",
"s",
".",
"conn4",
"=",
"ipv4",
".",
"NewPacketConn",
"(",
"conn",
")",
"\n",
"if",
"err",
":=",
"s",
".",
"conn4",
".",
"SetControlMessage",
"(",
"ipv4",
".",
"FlagDst",
"|",
"ipv4",
".",
"FlagInterface",
",",
"true",
")",
";",
"err",
"!=",
"nil",
"{",
"s",
".",
"conn4",
"=",
"nil",
"\n",
"}",
"\n",
"}",
"else",
"{",
"s",
".",
"conn6",
"=",
"ipv6",
".",
"NewPacketConn",
"(",
"conn",
")",
"\n",
"if",
"err",
":=",
"s",
".",
"conn6",
".",
"SetControlMessage",
"(",
"ipv6",
".",
"FlagDst",
"|",
"ipv6",
".",
"FlagInterface",
",",
"true",
")",
";",
"err",
"!=",
"nil",
"{",
"s",
".",
"conn6",
"=",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"s",
".",
"quit",
"=",
"make",
"(",
"chan",
"chan",
"struct",
"{",
"}",
")",
"\n",
"if",
"s",
".",
"singlePort",
"{",
"s",
".",
"singlePortProcessRequests",
"(",
")",
"\n",
"}",
"else",
"{",
"for",
"{",
"select",
"{",
"case",
"q",
":=",
"<-",
"s",
".",
"quit",
":",
"q",
"<-",
"struct",
"{",
"}",
"{",
"}",
"\n",
"return",
"nil",
"\n",
"default",
":",
"var",
"err",
"error",
"\n",
"if",
"s",
".",
"conn4",
"!=",
"nil",
"{",
"err",
"=",
"s",
".",
"processRequest4",
"(",
")",
"\n",
"}",
"else",
"if",
"s",
".",
"conn6",
"!=",
"nil",
"{",
"err",
"=",
"s",
".",
"processRequest6",
"(",
")",
"\n",
"}",
"else",
"{",
"err",
"=",
"s",
".",
"processRequest",
"(",
")",
"\n",
"}",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"// TODO: add logging handler",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Serve starts server provided already opened UDP connecton. It is
// useful for the case when you want to run server in separate goroutine
// but still want to be able to handle any errors opening connection.
// Serve returns when Shutdown is called or connection is closed.
|
[
"Serve",
"starts",
"server",
"provided",
"already",
"opened",
"UDP",
"connecton",
".",
"It",
"is",
"useful",
"for",
"the",
"case",
"when",
"you",
"want",
"to",
"run",
"server",
"in",
"separate",
"goroutine",
"but",
"still",
"want",
"to",
"be",
"able",
"to",
"handle",
"any",
"errors",
"opening",
"connection",
".",
"Serve",
"returns",
"when",
"Shutdown",
"is",
"called",
"or",
"connection",
"is",
"closed",
"."
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/server.go#L162-L213
|
15,026 |
pin/tftp
|
server.go
|
Shutdown
|
func (s *Server) Shutdown() {
s.conn.Close()
q := make(chan struct{})
s.quit <- q
<-q
s.wg.Wait()
}
|
go
|
func (s *Server) Shutdown() {
s.conn.Close()
q := make(chan struct{})
s.quit <- q
<-q
s.wg.Wait()
}
|
[
"func",
"(",
"s",
"*",
"Server",
")",
"Shutdown",
"(",
")",
"{",
"s",
".",
"conn",
".",
"Close",
"(",
")",
"\n",
"q",
":=",
"make",
"(",
"chan",
"struct",
"{",
"}",
")",
"\n",
"s",
".",
"quit",
"<-",
"q",
"\n",
"<-",
"q",
"\n",
"s",
".",
"wg",
".",
"Wait",
"(",
")",
"\n",
"}"
] |
// Shutdown make server stop listening for new requests, allows
// server to finish outstanding transfers and stops server.
|
[
"Shutdown",
"make",
"server",
"stop",
"listening",
"for",
"new",
"requests",
"allows",
"server",
"to",
"finish",
"outstanding",
"transfers",
"and",
"stops",
"server",
"."
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/server.go#L271-L277
|
15,027 |
pin/tftp
|
single_port.go
|
internalGC
|
func (s *Server) internalGC() {
var completedHandlers []string
for {
select {
case newHandler := <-s.gcCollect:
completedHandlers = append(completedHandlers, newHandler)
case <-time.After(s.gcInterval):
s.runGC <- completedHandlers
completedHandlers = nil
}
}
}
|
go
|
func (s *Server) internalGC() {
var completedHandlers []string
for {
select {
case newHandler := <-s.gcCollect:
completedHandlers = append(completedHandlers, newHandler)
case <-time.After(s.gcInterval):
s.runGC <- completedHandlers
completedHandlers = nil
}
}
}
|
[
"func",
"(",
"s",
"*",
"Server",
")",
"internalGC",
"(",
")",
"{",
"var",
"completedHandlers",
"[",
"]",
"string",
"\n",
"for",
"{",
"select",
"{",
"case",
"newHandler",
":=",
"<-",
"s",
".",
"gcCollect",
":",
"completedHandlers",
"=",
"append",
"(",
"completedHandlers",
",",
"newHandler",
")",
"\n",
"case",
"<-",
"time",
".",
"After",
"(",
"s",
".",
"gcInterval",
")",
":",
"s",
".",
"runGC",
"<-",
"completedHandlers",
"\n",
"completedHandlers",
"=",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// internalGC collects all the finished signals from each connection's goroutine
// The main loop is sent the key to be nil'ed after the gcInterval has passed
|
[
"internalGC",
"collects",
"all",
"the",
"finished",
"signals",
"from",
"each",
"connection",
"s",
"goroutine",
"The",
"main",
"loop",
"is",
"sent",
"the",
"key",
"to",
"be",
"nil",
"ed",
"after",
"the",
"gcInterval",
"has",
"passed"
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/single_port.go#L92-L103
|
15,028 |
pin/tftp
|
packet.go
|
packRQ
|
func packRQ(p []byte, op uint16, filename, mode string, opts options) int {
binary.BigEndian.PutUint16(p, op)
n := 2
n += copy(p[2:len(p)-10], filename)
p[n] = 0
n++
n += copy(p[n:], mode)
p[n] = 0
n++
for name, value := range opts {
n += copy(p[n:], name)
p[n] = 0
n++
n += copy(p[n:], value)
p[n] = 0
n++
}
return n
}
|
go
|
func packRQ(p []byte, op uint16, filename, mode string, opts options) int {
binary.BigEndian.PutUint16(p, op)
n := 2
n += copy(p[2:len(p)-10], filename)
p[n] = 0
n++
n += copy(p[n:], mode)
p[n] = 0
n++
for name, value := range opts {
n += copy(p[n:], name)
p[n] = 0
n++
n += copy(p[n:], value)
p[n] = 0
n++
}
return n
}
|
[
"func",
"packRQ",
"(",
"p",
"[",
"]",
"byte",
",",
"op",
"uint16",
",",
"filename",
",",
"mode",
"string",
",",
"opts",
"options",
")",
"int",
"{",
"binary",
".",
"BigEndian",
".",
"PutUint16",
"(",
"p",
",",
"op",
")",
"\n",
"n",
":=",
"2",
"\n",
"n",
"+=",
"copy",
"(",
"p",
"[",
"2",
":",
"len",
"(",
"p",
")",
"-",
"10",
"]",
",",
"filename",
")",
"\n",
"p",
"[",
"n",
"]",
"=",
"0",
"\n",
"n",
"++",
"\n",
"n",
"+=",
"copy",
"(",
"p",
"[",
"n",
":",
"]",
",",
"mode",
")",
"\n",
"p",
"[",
"n",
"]",
"=",
"0",
"\n",
"n",
"++",
"\n",
"for",
"name",
",",
"value",
":=",
"range",
"opts",
"{",
"n",
"+=",
"copy",
"(",
"p",
"[",
"n",
":",
"]",
",",
"name",
")",
"\n",
"p",
"[",
"n",
"]",
"=",
"0",
"\n",
"n",
"++",
"\n",
"n",
"+=",
"copy",
"(",
"p",
"[",
"n",
":",
"]",
",",
"value",
")",
"\n",
"p",
"[",
"n",
"]",
"=",
"0",
"\n",
"n",
"++",
"\n",
"}",
"\n",
"return",
"n",
"\n",
"}"
] |
// packRQ returns length of the packet in b
|
[
"packRQ",
"returns",
"length",
"of",
"the",
"packet",
"in",
"b"
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/packet.go#L35-L53
|
15,029 |
pin/tftp
|
client.go
|
NewClient
|
func NewClient(addr string) (*Client, error) {
a, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, fmt.Errorf("resolving address %s: %v", addr, err)
}
return &Client{
addr: a,
timeout: defaultTimeout,
retries: defaultRetries,
}, nil
}
|
go
|
func NewClient(addr string) (*Client, error) {
a, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, fmt.Errorf("resolving address %s: %v", addr, err)
}
return &Client{
addr: a,
timeout: defaultTimeout,
retries: defaultRetries,
}, nil
}
|
[
"func",
"NewClient",
"(",
"addr",
"string",
")",
"(",
"*",
"Client",
",",
"error",
")",
"{",
"a",
",",
"err",
":=",
"net",
".",
"ResolveUDPAddr",
"(",
"\"",
"\"",
",",
"addr",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"addr",
",",
"err",
")",
"\n",
"}",
"\n",
"return",
"&",
"Client",
"{",
"addr",
":",
"a",
",",
"timeout",
":",
"defaultTimeout",
",",
"retries",
":",
"defaultRetries",
",",
"}",
",",
"nil",
"\n",
"}"
] |
// NewClient creates TFTP client for server on address provided.
|
[
"NewClient",
"creates",
"TFTP",
"client",
"for",
"server",
"on",
"address",
"provided",
"."
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/client.go#L12-L22
|
15,030 |
pin/tftp
|
client.go
|
SetTimeout
|
func (c *Client) SetTimeout(t time.Duration) {
if t <= 0 {
c.timeout = defaultTimeout
}
c.timeout = t
}
|
go
|
func (c *Client) SetTimeout(t time.Duration) {
if t <= 0 {
c.timeout = defaultTimeout
}
c.timeout = t
}
|
[
"func",
"(",
"c",
"*",
"Client",
")",
"SetTimeout",
"(",
"t",
"time",
".",
"Duration",
")",
"{",
"if",
"t",
"<=",
"0",
"{",
"c",
".",
"timeout",
"=",
"defaultTimeout",
"\n",
"}",
"\n",
"c",
".",
"timeout",
"=",
"t",
"\n",
"}"
] |
// SetTimeout sets maximum time client waits for single network round-trip to succeed.
// Default is 5 seconds.
|
[
"SetTimeout",
"sets",
"maximum",
"time",
"client",
"waits",
"for",
"single",
"network",
"round",
"-",
"trip",
"to",
"succeed",
".",
"Default",
"is",
"5",
"seconds",
"."
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/client.go#L26-L31
|
15,031 |
pin/tftp
|
client.go
|
SetRetries
|
func (c *Client) SetRetries(count int) {
if count < 1 {
c.retries = defaultRetries
}
c.retries = count
}
|
go
|
func (c *Client) SetRetries(count int) {
if count < 1 {
c.retries = defaultRetries
}
c.retries = count
}
|
[
"func",
"(",
"c",
"*",
"Client",
")",
"SetRetries",
"(",
"count",
"int",
")",
"{",
"if",
"count",
"<",
"1",
"{",
"c",
".",
"retries",
"=",
"defaultRetries",
"\n",
"}",
"\n",
"c",
".",
"retries",
"=",
"count",
"\n",
"}"
] |
// SetRetries sets maximum number of attempts client made to transmit a packet.
// Default is 5 attempts.
|
[
"SetRetries",
"sets",
"maximum",
"number",
"of",
"attempts",
"client",
"made",
"to",
"transmit",
"a",
"packet",
".",
"Default",
"is",
"5",
"attempts",
"."
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/client.go#L35-L40
|
15,032 |
pin/tftp
|
client.go
|
Send
|
func (c Client) Send(filename string, mode string) (io.ReaderFrom, error) {
conn, err := net.ListenUDP("udp", &net.UDPAddr{})
if err != nil {
return nil, err
}
s := &sender{
send: make([]byte, datagramLength),
receive: make([]byte, datagramLength),
conn: &connConnection{conn: conn},
retry: &backoff{handler: c.backoff},
timeout: c.timeout,
retries: c.retries,
addr: c.addr,
mode: mode,
}
if c.blksize != 0 {
s.opts = make(options)
s.opts["blksize"] = strconv.Itoa(c.blksize)
}
n := packRQ(s.send, opWRQ, filename, mode, s.opts)
addr, err := s.sendWithRetry(n)
if err != nil {
return nil, err
}
s.addr = addr
s.opts = nil
return s, nil
}
|
go
|
func (c Client) Send(filename string, mode string) (io.ReaderFrom, error) {
conn, err := net.ListenUDP("udp", &net.UDPAddr{})
if err != nil {
return nil, err
}
s := &sender{
send: make([]byte, datagramLength),
receive: make([]byte, datagramLength),
conn: &connConnection{conn: conn},
retry: &backoff{handler: c.backoff},
timeout: c.timeout,
retries: c.retries,
addr: c.addr,
mode: mode,
}
if c.blksize != 0 {
s.opts = make(options)
s.opts["blksize"] = strconv.Itoa(c.blksize)
}
n := packRQ(s.send, opWRQ, filename, mode, s.opts)
addr, err := s.sendWithRetry(n)
if err != nil {
return nil, err
}
s.addr = addr
s.opts = nil
return s, nil
}
|
[
"func",
"(",
"c",
"Client",
")",
"Send",
"(",
"filename",
"string",
",",
"mode",
"string",
")",
"(",
"io",
".",
"ReaderFrom",
",",
"error",
")",
"{",
"conn",
",",
"err",
":=",
"net",
".",
"ListenUDP",
"(",
"\"",
"\"",
",",
"&",
"net",
".",
"UDPAddr",
"{",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"s",
":=",
"&",
"sender",
"{",
"send",
":",
"make",
"(",
"[",
"]",
"byte",
",",
"datagramLength",
")",
",",
"receive",
":",
"make",
"(",
"[",
"]",
"byte",
",",
"datagramLength",
")",
",",
"conn",
":",
"&",
"connConnection",
"{",
"conn",
":",
"conn",
"}",
",",
"retry",
":",
"&",
"backoff",
"{",
"handler",
":",
"c",
".",
"backoff",
"}",
",",
"timeout",
":",
"c",
".",
"timeout",
",",
"retries",
":",
"c",
".",
"retries",
",",
"addr",
":",
"c",
".",
"addr",
",",
"mode",
":",
"mode",
",",
"}",
"\n",
"if",
"c",
".",
"blksize",
"!=",
"0",
"{",
"s",
".",
"opts",
"=",
"make",
"(",
"options",
")",
"\n",
"s",
".",
"opts",
"[",
"\"",
"\"",
"]",
"=",
"strconv",
".",
"Itoa",
"(",
"c",
".",
"blksize",
")",
"\n",
"}",
"\n",
"n",
":=",
"packRQ",
"(",
"s",
".",
"send",
",",
"opWRQ",
",",
"filename",
",",
"mode",
",",
"s",
".",
"opts",
")",
"\n",
"addr",
",",
"err",
":=",
"s",
".",
"sendWithRetry",
"(",
"n",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"s",
".",
"addr",
"=",
"addr",
"\n",
"s",
".",
"opts",
"=",
"nil",
"\n",
"return",
"s",
",",
"nil",
"\n",
"}"
] |
// Send starts outgoing file transmission. It returns io.ReaderFrom or error.
|
[
"Send",
"starts",
"outgoing",
"file",
"transmission",
".",
"It",
"returns",
"io",
".",
"ReaderFrom",
"or",
"error",
"."
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/client.go#L68-L95
|
15,033 |
pin/tftp
|
client.go
|
Receive
|
func (c Client) Receive(filename string, mode string) (io.WriterTo, error) {
conn, err := net.ListenUDP("udp", &net.UDPAddr{})
if err != nil {
return nil, err
}
if c.timeout == 0 {
c.timeout = defaultTimeout
}
r := &receiver{
send: make([]byte, datagramLength),
receive: make([]byte, datagramLength),
conn: &connConnection{conn: conn},
retry: &backoff{handler: c.backoff},
timeout: c.timeout,
retries: c.retries,
addr: c.addr,
autoTerm: true,
block: 1,
mode: mode,
}
if c.blksize != 0 || c.tsize {
r.opts = make(options)
}
if c.blksize != 0 {
r.opts["blksize"] = strconv.Itoa(c.blksize)
// Clean it up so we don't send options twice
defer func() { delete(r.opts, "blksize") }()
}
if c.tsize {
r.opts["tsize"] = "0"
}
n := packRQ(r.send, opRRQ, filename, mode, r.opts)
l, addr, err := r.receiveWithRetry(n)
if err != nil {
return nil, err
}
r.l = l
r.addr = addr
return r, nil
}
|
go
|
func (c Client) Receive(filename string, mode string) (io.WriterTo, error) {
conn, err := net.ListenUDP("udp", &net.UDPAddr{})
if err != nil {
return nil, err
}
if c.timeout == 0 {
c.timeout = defaultTimeout
}
r := &receiver{
send: make([]byte, datagramLength),
receive: make([]byte, datagramLength),
conn: &connConnection{conn: conn},
retry: &backoff{handler: c.backoff},
timeout: c.timeout,
retries: c.retries,
addr: c.addr,
autoTerm: true,
block: 1,
mode: mode,
}
if c.blksize != 0 || c.tsize {
r.opts = make(options)
}
if c.blksize != 0 {
r.opts["blksize"] = strconv.Itoa(c.blksize)
// Clean it up so we don't send options twice
defer func() { delete(r.opts, "blksize") }()
}
if c.tsize {
r.opts["tsize"] = "0"
}
n := packRQ(r.send, opRRQ, filename, mode, r.opts)
l, addr, err := r.receiveWithRetry(n)
if err != nil {
return nil, err
}
r.l = l
r.addr = addr
return r, nil
}
|
[
"func",
"(",
"c",
"Client",
")",
"Receive",
"(",
"filename",
"string",
",",
"mode",
"string",
")",
"(",
"io",
".",
"WriterTo",
",",
"error",
")",
"{",
"conn",
",",
"err",
":=",
"net",
".",
"ListenUDP",
"(",
"\"",
"\"",
",",
"&",
"net",
".",
"UDPAddr",
"{",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"if",
"c",
".",
"timeout",
"==",
"0",
"{",
"c",
".",
"timeout",
"=",
"defaultTimeout",
"\n",
"}",
"\n",
"r",
":=",
"&",
"receiver",
"{",
"send",
":",
"make",
"(",
"[",
"]",
"byte",
",",
"datagramLength",
")",
",",
"receive",
":",
"make",
"(",
"[",
"]",
"byte",
",",
"datagramLength",
")",
",",
"conn",
":",
"&",
"connConnection",
"{",
"conn",
":",
"conn",
"}",
",",
"retry",
":",
"&",
"backoff",
"{",
"handler",
":",
"c",
".",
"backoff",
"}",
",",
"timeout",
":",
"c",
".",
"timeout",
",",
"retries",
":",
"c",
".",
"retries",
",",
"addr",
":",
"c",
".",
"addr",
",",
"autoTerm",
":",
"true",
",",
"block",
":",
"1",
",",
"mode",
":",
"mode",
",",
"}",
"\n",
"if",
"c",
".",
"blksize",
"!=",
"0",
"||",
"c",
".",
"tsize",
"{",
"r",
".",
"opts",
"=",
"make",
"(",
"options",
")",
"\n",
"}",
"\n",
"if",
"c",
".",
"blksize",
"!=",
"0",
"{",
"r",
".",
"opts",
"[",
"\"",
"\"",
"]",
"=",
"strconv",
".",
"Itoa",
"(",
"c",
".",
"blksize",
")",
"\n",
"// Clean it up so we don't send options twice",
"defer",
"func",
"(",
")",
"{",
"delete",
"(",
"r",
".",
"opts",
",",
"\"",
"\"",
")",
"}",
"(",
")",
"\n",
"}",
"\n",
"if",
"c",
".",
"tsize",
"{",
"r",
".",
"opts",
"[",
"\"",
"\"",
"]",
"=",
"\"",
"\"",
"\n",
"}",
"\n",
"n",
":=",
"packRQ",
"(",
"r",
".",
"send",
",",
"opRRQ",
",",
"filename",
",",
"mode",
",",
"r",
".",
"opts",
")",
"\n",
"l",
",",
"addr",
",",
"err",
":=",
"r",
".",
"receiveWithRetry",
"(",
"n",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"r",
".",
"l",
"=",
"l",
"\n",
"r",
".",
"addr",
"=",
"addr",
"\n",
"return",
"r",
",",
"nil",
"\n",
"}"
] |
// Receive starts incoming file transmission. It returns io.WriterTo or error.
|
[
"Receive",
"starts",
"incoming",
"file",
"transmission",
".",
"It",
"returns",
"io",
".",
"WriterTo",
"or",
"error",
"."
] |
70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4
|
https://github.com/pin/tftp/blob/70c6a2187577a9ecf3e5e89f672ef3d8e1e95dc4/client.go#L98-L137
|
15,034 |
99designs/keyring
|
keyring.go
|
AvailableBackends
|
func AvailableBackends() []BackendType {
b := []BackendType{}
for k := range supportedBackends {
if k != FileBackend {
b = append(b, k)
}
}
// make sure FileBackend is last
return append(b, FileBackend)
}
|
go
|
func AvailableBackends() []BackendType {
b := []BackendType{}
for k := range supportedBackends {
if k != FileBackend {
b = append(b, k)
}
}
// make sure FileBackend is last
return append(b, FileBackend)
}
|
[
"func",
"AvailableBackends",
"(",
")",
"[",
"]",
"BackendType",
"{",
"b",
":=",
"[",
"]",
"BackendType",
"{",
"}",
"\n",
"for",
"k",
":=",
"range",
"supportedBackends",
"{",
"if",
"k",
"!=",
"FileBackend",
"{",
"b",
"=",
"append",
"(",
"b",
",",
"k",
")",
"\n",
"}",
"\n",
"}",
"\n",
"// make sure FileBackend is last",
"return",
"append",
"(",
"b",
",",
"FileBackend",
")",
"\n",
"}"
] |
// AvailableBackends provides a slice of all available backend keys on the current OS
|
[
"AvailableBackends",
"provides",
"a",
"slice",
"of",
"all",
"available",
"backend",
"keys",
"on",
"the",
"current",
"OS"
] |
82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d
|
https://github.com/99designs/keyring/blob/82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d/keyring.go#L27-L36
|
15,035 |
99designs/keyring
|
keyring.go
|
Open
|
func Open(cfg Config) (Keyring, error) {
if cfg.AllowedBackends == nil {
cfg.AllowedBackends = AvailableBackends()
}
debugf("Considering backends: %v", cfg.AllowedBackends)
for _, backend := range cfg.AllowedBackends {
if opener, ok := supportedBackends[backend]; ok {
openBackend, err := opener(cfg)
if err != nil {
debugf("Failed backend %s: %s", backend, err)
continue
}
return openBackend, nil
}
}
return nil, ErrNoAvailImpl
}
|
go
|
func Open(cfg Config) (Keyring, error) {
if cfg.AllowedBackends == nil {
cfg.AllowedBackends = AvailableBackends()
}
debugf("Considering backends: %v", cfg.AllowedBackends)
for _, backend := range cfg.AllowedBackends {
if opener, ok := supportedBackends[backend]; ok {
openBackend, err := opener(cfg)
if err != nil {
debugf("Failed backend %s: %s", backend, err)
continue
}
return openBackend, nil
}
}
return nil, ErrNoAvailImpl
}
|
[
"func",
"Open",
"(",
"cfg",
"Config",
")",
"(",
"Keyring",
",",
"error",
")",
"{",
"if",
"cfg",
".",
"AllowedBackends",
"==",
"nil",
"{",
"cfg",
".",
"AllowedBackends",
"=",
"AvailableBackends",
"(",
")",
"\n",
"}",
"\n",
"debugf",
"(",
"\"",
"\"",
",",
"cfg",
".",
"AllowedBackends",
")",
"\n",
"for",
"_",
",",
"backend",
":=",
"range",
"cfg",
".",
"AllowedBackends",
"{",
"if",
"opener",
",",
"ok",
":=",
"supportedBackends",
"[",
"backend",
"]",
";",
"ok",
"{",
"openBackend",
",",
"err",
":=",
"opener",
"(",
"cfg",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"debugf",
"(",
"\"",
"\"",
",",
"backend",
",",
"err",
")",
"\n",
"continue",
"\n",
"}",
"\n",
"return",
"openBackend",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
",",
"ErrNoAvailImpl",
"\n",
"}"
] |
// Open will open a specific keyring backend
|
[
"Open",
"will",
"open",
"a",
"specific",
"keyring",
"backend"
] |
82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d
|
https://github.com/99designs/keyring/blob/82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d/keyring.go#L41-L57
|
15,036 |
99designs/keyring
|
array.go
|
NewArrayKeyring
|
func NewArrayKeyring(initial []Item) *ArrayKeyring {
kr := &ArrayKeyring{}
for _, i := range initial {
_ = kr.Set(i)
}
return kr
}
|
go
|
func NewArrayKeyring(initial []Item) *ArrayKeyring {
kr := &ArrayKeyring{}
for _, i := range initial {
_ = kr.Set(i)
}
return kr
}
|
[
"func",
"NewArrayKeyring",
"(",
"initial",
"[",
"]",
"Item",
")",
"*",
"ArrayKeyring",
"{",
"kr",
":=",
"&",
"ArrayKeyring",
"{",
"}",
"\n",
"for",
"_",
",",
"i",
":=",
"range",
"initial",
"{",
"_",
"=",
"kr",
".",
"Set",
"(",
"i",
")",
"\n",
"}",
"\n",
"return",
"kr",
"\n",
"}"
] |
// NewArrayKeyring returns an ArrayKeyring, optionally constructed with an initial slice
// of items
|
[
"NewArrayKeyring",
"returns",
"an",
"ArrayKeyring",
"optionally",
"constructed",
"with",
"an",
"initial",
"slice",
"of",
"items"
] |
82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d
|
https://github.com/99designs/keyring/blob/82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d/array.go#L12-L18
|
15,037 |
99designs/keyring
|
array.go
|
Get
|
func (k *ArrayKeyring) Get(key string) (Item, error) {
if i, ok := k.items[key]; ok {
return i, nil
}
return Item{}, ErrKeyNotFound
}
|
go
|
func (k *ArrayKeyring) Get(key string) (Item, error) {
if i, ok := k.items[key]; ok {
return i, nil
}
return Item{}, ErrKeyNotFound
}
|
[
"func",
"(",
"k",
"*",
"ArrayKeyring",
")",
"Get",
"(",
"key",
"string",
")",
"(",
"Item",
",",
"error",
")",
"{",
"if",
"i",
",",
"ok",
":=",
"k",
".",
"items",
"[",
"key",
"]",
";",
"ok",
"{",
"return",
"i",
",",
"nil",
"\n",
"}",
"\n",
"return",
"Item",
"{",
"}",
",",
"ErrKeyNotFound",
"\n",
"}"
] |
// Get returns an Item matching Key
|
[
"Get",
"returns",
"an",
"Item",
"matching",
"Key"
] |
82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d
|
https://github.com/99designs/keyring/blob/82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d/array.go#L21-L26
|
15,038 |
99designs/keyring
|
array.go
|
Set
|
func (k *ArrayKeyring) Set(i Item) error {
if k.items == nil {
k.items = map[string]Item{}
}
k.items[i.Key] = i
return nil
}
|
go
|
func (k *ArrayKeyring) Set(i Item) error {
if k.items == nil {
k.items = map[string]Item{}
}
k.items[i.Key] = i
return nil
}
|
[
"func",
"(",
"k",
"*",
"ArrayKeyring",
")",
"Set",
"(",
"i",
"Item",
")",
"error",
"{",
"if",
"k",
".",
"items",
"==",
"nil",
"{",
"k",
".",
"items",
"=",
"map",
"[",
"string",
"]",
"Item",
"{",
"}",
"\n",
"}",
"\n",
"k",
".",
"items",
"[",
"i",
".",
"Key",
"]",
"=",
"i",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Set will store an item on the mock Keyring
|
[
"Set",
"will",
"store",
"an",
"item",
"on",
"the",
"mock",
"Keyring"
] |
82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d
|
https://github.com/99designs/keyring/blob/82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d/array.go#L29-L35
|
15,039 |
99designs/keyring
|
array.go
|
Remove
|
func (k *ArrayKeyring) Remove(key string) error {
delete(k.items, key)
return nil
}
|
go
|
func (k *ArrayKeyring) Remove(key string) error {
delete(k.items, key)
return nil
}
|
[
"func",
"(",
"k",
"*",
"ArrayKeyring",
")",
"Remove",
"(",
"key",
"string",
")",
"error",
"{",
"delete",
"(",
"k",
".",
"items",
",",
"key",
")",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Remove will delete an Item from the Keyring
|
[
"Remove",
"will",
"delete",
"an",
"Item",
"from",
"the",
"Keyring"
] |
82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d
|
https://github.com/99designs/keyring/blob/82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d/array.go#L38-L41
|
15,040 |
99designs/keyring
|
array.go
|
Keys
|
func (k *ArrayKeyring) Keys() ([]string, error) {
var keys = []string{}
for key := range k.items {
keys = append(keys, key)
}
return keys, nil
}
|
go
|
func (k *ArrayKeyring) Keys() ([]string, error) {
var keys = []string{}
for key := range k.items {
keys = append(keys, key)
}
return keys, nil
}
|
[
"func",
"(",
"k",
"*",
"ArrayKeyring",
")",
"Keys",
"(",
")",
"(",
"[",
"]",
"string",
",",
"error",
")",
"{",
"var",
"keys",
"=",
"[",
"]",
"string",
"{",
"}",
"\n",
"for",
"key",
":=",
"range",
"k",
".",
"items",
"{",
"keys",
"=",
"append",
"(",
"keys",
",",
"key",
")",
"\n",
"}",
"\n",
"return",
"keys",
",",
"nil",
"\n",
"}"
] |
// Keys provides a slice of all Item keys on the Keyring
|
[
"Keys",
"provides",
"a",
"slice",
"of",
"all",
"Item",
"keys",
"on",
"the",
"Keyring"
] |
82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d
|
https://github.com/99designs/keyring/blob/82da6802f65f1ac7963cfc3b7c62ae12dab8ee5d/array.go#L44-L50
|
15,041 |
digitalocean/go-metadata
|
client.go
|
WithHTTPClient
|
func WithHTTPClient(client *http.Client) ClientOption {
return func(metaclient *Client) { metaclient.client = client }
}
|
go
|
func WithHTTPClient(client *http.Client) ClientOption {
return func(metaclient *Client) { metaclient.client = client }
}
|
[
"func",
"WithHTTPClient",
"(",
"client",
"*",
"http",
".",
"Client",
")",
"ClientOption",
"{",
"return",
"func",
"(",
"metaclient",
"*",
"Client",
")",
"{",
"metaclient",
".",
"client",
"=",
"client",
"}",
"\n",
"}"
] |
// WithHTTPClient makes the metadata client use the given HTTP client.
|
[
"WithHTTPClient",
"makes",
"the",
"metadata",
"client",
"use",
"the",
"given",
"HTTP",
"client",
"."
] |
15bd36e5f6f745f32074d0705a27aee05ae0a216
|
https://github.com/digitalocean/go-metadata/blob/15bd36e5f6f745f32074d0705a27aee05ae0a216/client.go#L44-L46
|
15,042 |
digitalocean/go-metadata
|
client.go
|
WithBaseURL
|
func WithBaseURL(base *url.URL) ClientOption {
return func(metaclient *Client) { metaclient.baseURL = base }
}
|
go
|
func WithBaseURL(base *url.URL) ClientOption {
return func(metaclient *Client) { metaclient.baseURL = base }
}
|
[
"func",
"WithBaseURL",
"(",
"base",
"*",
"url",
".",
"URL",
")",
"ClientOption",
"{",
"return",
"func",
"(",
"metaclient",
"*",
"Client",
")",
"{",
"metaclient",
".",
"baseURL",
"=",
"base",
"}",
"\n",
"}"
] |
// WithBaseURL makes the metadata client reach the metadata API using the
// given base URL.
|
[
"WithBaseURL",
"makes",
"the",
"metadata",
"client",
"reach",
"the",
"metadata",
"API",
"using",
"the",
"given",
"base",
"URL",
"."
] |
15bd36e5f6f745f32074d0705a27aee05ae0a216
|
https://github.com/digitalocean/go-metadata/blob/15bd36e5f6f745f32074d0705a27aee05ae0a216/client.go#L50-L52
|
15,043 |
digitalocean/go-metadata
|
client.go
|
NewClient
|
func NewClient(opts ...ClientOption) *Client {
client := &Client{
client: &http.Client{Timeout: defaultTimeout},
baseURL: defaultBaseURL,
}
for _, opt := range opts {
opt(client)
}
return client
}
|
go
|
func NewClient(opts ...ClientOption) *Client {
client := &Client{
client: &http.Client{Timeout: defaultTimeout},
baseURL: defaultBaseURL,
}
for _, opt := range opts {
opt(client)
}
return client
}
|
[
"func",
"NewClient",
"(",
"opts",
"...",
"ClientOption",
")",
"*",
"Client",
"{",
"client",
":=",
"&",
"Client",
"{",
"client",
":",
"&",
"http",
".",
"Client",
"{",
"Timeout",
":",
"defaultTimeout",
"}",
",",
"baseURL",
":",
"defaultBaseURL",
",",
"}",
"\n",
"for",
"_",
",",
"opt",
":=",
"range",
"opts",
"{",
"opt",
"(",
"client",
")",
"\n",
"}",
"\n",
"return",
"client",
"\n",
"}"
] |
// NewClient creates a client for the metadata API.
|
[
"NewClient",
"creates",
"a",
"client",
"for",
"the",
"metadata",
"API",
"."
] |
15bd36e5f6f745f32074d0705a27aee05ae0a216
|
https://github.com/digitalocean/go-metadata/blob/15bd36e5f6f745f32074d0705a27aee05ae0a216/client.go#L62-L71
|
15,044 |
digitalocean/go-metadata
|
client.go
|
Metadata
|
func (c *Client) Metadata() (*Metadata, error) {
metadata := new(Metadata)
err := c.doGetURL(c.resolve("/metadata/v1.json"), func(r io.Reader) error {
return json.NewDecoder(r).Decode(metadata)
})
return metadata, err
}
|
go
|
func (c *Client) Metadata() (*Metadata, error) {
metadata := new(Metadata)
err := c.doGetURL(c.resolve("/metadata/v1.json"), func(r io.Reader) error {
return json.NewDecoder(r).Decode(metadata)
})
return metadata, err
}
|
[
"func",
"(",
"c",
"*",
"Client",
")",
"Metadata",
"(",
")",
"(",
"*",
"Metadata",
",",
"error",
")",
"{",
"metadata",
":=",
"new",
"(",
"Metadata",
")",
"\n",
"err",
":=",
"c",
".",
"doGetURL",
"(",
"c",
".",
"resolve",
"(",
"\"",
"\"",
")",
",",
"func",
"(",
"r",
"io",
".",
"Reader",
")",
"error",
"{",
"return",
"json",
".",
"NewDecoder",
"(",
"r",
")",
".",
"Decode",
"(",
"metadata",
")",
"\n",
"}",
")",
"\n",
"return",
"metadata",
",",
"err",
"\n",
"}"
] |
// Metadata contains the entire contents of a Droplet's metadata.
// This method is unique because it returns all of the
// metadata at once, instead of individual metadata items.
|
[
"Metadata",
"contains",
"the",
"entire",
"contents",
"of",
"a",
"Droplet",
"s",
"metadata",
".",
"This",
"method",
"is",
"unique",
"because",
"it",
"returns",
"all",
"of",
"the",
"metadata",
"at",
"once",
"instead",
"of",
"individual",
"metadata",
"items",
"."
] |
15bd36e5f6f745f32074d0705a27aee05ae0a216
|
https://github.com/digitalocean/go-metadata/blob/15bd36e5f6f745f32074d0705a27aee05ae0a216/client.go#L76-L82
|
15,045 |
digitalocean/go-metadata
|
client.go
|
DropletID
|
func (c *Client) DropletID() (int, error) {
dropletID := new(int)
err := c.doGet("id", func(r io.Reader) error {
_, err := fmt.Fscanf(r, "%d", dropletID)
return err
})
return *dropletID, err
}
|
go
|
func (c *Client) DropletID() (int, error) {
dropletID := new(int)
err := c.doGet("id", func(r io.Reader) error {
_, err := fmt.Fscanf(r, "%d", dropletID)
return err
})
return *dropletID, err
}
|
[
"func",
"(",
"c",
"*",
"Client",
")",
"DropletID",
"(",
")",
"(",
"int",
",",
"error",
")",
"{",
"dropletID",
":=",
"new",
"(",
"int",
")",
"\n",
"err",
":=",
"c",
".",
"doGet",
"(",
"\"",
"\"",
",",
"func",
"(",
"r",
"io",
".",
"Reader",
")",
"error",
"{",
"_",
",",
"err",
":=",
"fmt",
".",
"Fscanf",
"(",
"r",
",",
"\"",
"\"",
",",
"dropletID",
")",
"\n",
"return",
"err",
"\n",
"}",
")",
"\n",
"return",
"*",
"dropletID",
",",
"err",
"\n",
"}"
] |
// DropletID returns the Droplet's unique identifier. This is
// automatically generated upon Droplet creation.
|
[
"DropletID",
"returns",
"the",
"Droplet",
"s",
"unique",
"identifier",
".",
"This",
"is",
"automatically",
"generated",
"upon",
"Droplet",
"creation",
"."
] |
15bd36e5f6f745f32074d0705a27aee05ae0a216
|
https://github.com/digitalocean/go-metadata/blob/15bd36e5f6f745f32074d0705a27aee05ae0a216/client.go#L86-L93
|
15,046 |
digitalocean/go-metadata
|
client.go
|
Hostname
|
func (c *Client) Hostname() (string, error) {
var hostname string
err := c.doGet("hostname", func(r io.Reader) error {
hostnameraw, err := ioutil.ReadAll(r)
hostname = string(hostnameraw)
return err
})
return hostname, err
}
|
go
|
func (c *Client) Hostname() (string, error) {
var hostname string
err := c.doGet("hostname", func(r io.Reader) error {
hostnameraw, err := ioutil.ReadAll(r)
hostname = string(hostnameraw)
return err
})
return hostname, err
}
|
[
"func",
"(",
"c",
"*",
"Client",
")",
"Hostname",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"var",
"hostname",
"string",
"\n",
"err",
":=",
"c",
".",
"doGet",
"(",
"\"",
"\"",
",",
"func",
"(",
"r",
"io",
".",
"Reader",
")",
"error",
"{",
"hostnameraw",
",",
"err",
":=",
"ioutil",
".",
"ReadAll",
"(",
"r",
")",
"\n",
"hostname",
"=",
"string",
"(",
"hostnameraw",
")",
"\n",
"return",
"err",
"\n",
"}",
")",
"\n",
"return",
"hostname",
",",
"err",
"\n",
"}"
] |
// Hostname returns the Droplet's hostname, as specified by the
// user during Droplet creation.
|
[
"Hostname",
"returns",
"the",
"Droplet",
"s",
"hostname",
"as",
"specified",
"by",
"the",
"user",
"during",
"Droplet",
"creation",
"."
] |
15bd36e5f6f745f32074d0705a27aee05ae0a216
|
https://github.com/digitalocean/go-metadata/blob/15bd36e5f6f745f32074d0705a27aee05ae0a216/client.go#L97-L105
|
15,047 |
digitalocean/go-metadata
|
client.go
|
UserData
|
func (c *Client) UserData() (string, error) {
var userdata string
err := c.doGet("user-data", func(r io.Reader) error {
userdataraw, err := ioutil.ReadAll(r)
userdata = string(userdataraw)
return err
})
return userdata, err
}
|
go
|
func (c *Client) UserData() (string, error) {
var userdata string
err := c.doGet("user-data", func(r io.Reader) error {
userdataraw, err := ioutil.ReadAll(r)
userdata = string(userdataraw)
return err
})
return userdata, err
}
|
[
"func",
"(",
"c",
"*",
"Client",
")",
"UserData",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"var",
"userdata",
"string",
"\n",
"err",
":=",
"c",
".",
"doGet",
"(",
"\"",
"\"",
",",
"func",
"(",
"r",
"io",
".",
"Reader",
")",
"error",
"{",
"userdataraw",
",",
"err",
":=",
"ioutil",
".",
"ReadAll",
"(",
"r",
")",
"\n",
"userdata",
"=",
"string",
"(",
"userdataraw",
")",
"\n",
"return",
"err",
"\n",
"}",
")",
"\n",
"return",
"userdata",
",",
"err",
"\n",
"}"
] |
// UserData returns the user data that was provided by the user
// during Droplet creation. User data can contain arbitrary data
// for miscellaneous use or, with certain Linux distributions,
// an arbitrary shell script or cloud-config file that will be
// consumed by a variation of cloud-init upon boot. At this time,
// cloud-config support is included with CoreOS, Ubuntu 14.04, and
// CentOS 7 images on DigitalOcean.
|
[
"UserData",
"returns",
"the",
"user",
"data",
"that",
"was",
"provided",
"by",
"the",
"user",
"during",
"Droplet",
"creation",
".",
"User",
"data",
"can",
"contain",
"arbitrary",
"data",
"for",
"miscellaneous",
"use",
"or",
"with",
"certain",
"Linux",
"distributions",
"an",
"arbitrary",
"shell",
"script",
"or",
"cloud",
"-",
"config",
"file",
"that",
"will",
"be",
"consumed",
"by",
"a",
"variation",
"of",
"cloud",
"-",
"init",
"upon",
"boot",
".",
"At",
"this",
"time",
"cloud",
"-",
"config",
"support",
"is",
"included",
"with",
"CoreOS",
"Ubuntu",
"14",
".",
"04",
"and",
"CentOS",
"7",
"images",
"on",
"DigitalOcean",
"."
] |
15bd36e5f6f745f32074d0705a27aee05ae0a216
|
https://github.com/digitalocean/go-metadata/blob/15bd36e5f6f745f32074d0705a27aee05ae0a216/client.go#L114-L122
|
15,048 |
digitalocean/go-metadata
|
client.go
|
VendorData
|
func (c *Client) VendorData() (string, error) {
var vendordata string
err := c.doGet("vendor-data", func(r io.Reader) error {
vendordataraw, err := ioutil.ReadAll(r)
vendordata = string(vendordataraw)
return err
})
return vendordata, err
}
|
go
|
func (c *Client) VendorData() (string, error) {
var vendordata string
err := c.doGet("vendor-data", func(r io.Reader) error {
vendordataraw, err := ioutil.ReadAll(r)
vendordata = string(vendordataraw)
return err
})
return vendordata, err
}
|
[
"func",
"(",
"c",
"*",
"Client",
")",
"VendorData",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"var",
"vendordata",
"string",
"\n",
"err",
":=",
"c",
".",
"doGet",
"(",
"\"",
"\"",
",",
"func",
"(",
"r",
"io",
".",
"Reader",
")",
"error",
"{",
"vendordataraw",
",",
"err",
":=",
"ioutil",
".",
"ReadAll",
"(",
"r",
")",
"\n",
"vendordata",
"=",
"string",
"(",
"vendordataraw",
")",
"\n",
"return",
"err",
"\n",
"}",
")",
"\n",
"return",
"vendordata",
",",
"err",
"\n",
"}"
] |
// VendorData provided data that can be used to configure Droplets
// upon their creation. This is similar to user data, but it is
// provided by DigitalOcean instead of the user.
|
[
"VendorData",
"provided",
"data",
"that",
"can",
"be",
"used",
"to",
"configure",
"Droplets",
"upon",
"their",
"creation",
".",
"This",
"is",
"similar",
"to",
"user",
"data",
"but",
"it",
"is",
"provided",
"by",
"DigitalOcean",
"instead",
"of",
"the",
"user",
"."
] |
15bd36e5f6f745f32074d0705a27aee05ae0a216
|
https://github.com/digitalocean/go-metadata/blob/15bd36e5f6f745f32074d0705a27aee05ae0a216/client.go#L127-L135
|
15,049 |
digitalocean/go-metadata
|
client.go
|
Region
|
func (c *Client) Region() (string, error) {
var region string
err := c.doGet("region", func(r io.Reader) error {
regionraw, err := ioutil.ReadAll(r)
region = string(regionraw)
return err
})
return region, err
}
|
go
|
func (c *Client) Region() (string, error) {
var region string
err := c.doGet("region", func(r io.Reader) error {
regionraw, err := ioutil.ReadAll(r)
region = string(regionraw)
return err
})
return region, err
}
|
[
"func",
"(",
"c",
"*",
"Client",
")",
"Region",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"var",
"region",
"string",
"\n",
"err",
":=",
"c",
".",
"doGet",
"(",
"\"",
"\"",
",",
"func",
"(",
"r",
"io",
".",
"Reader",
")",
"error",
"{",
"regionraw",
",",
"err",
":=",
"ioutil",
".",
"ReadAll",
"(",
"r",
")",
"\n",
"region",
"=",
"string",
"(",
"regionraw",
")",
"\n",
"return",
"err",
"\n",
"}",
")",
"\n",
"return",
"region",
",",
"err",
"\n",
"}"
] |
// Region returns the region code of where the Droplet resides.
|
[
"Region",
"returns",
"the",
"region",
"code",
"of",
"where",
"the",
"Droplet",
"resides",
"."
] |
15bd36e5f6f745f32074d0705a27aee05ae0a216
|
https://github.com/digitalocean/go-metadata/blob/15bd36e5f6f745f32074d0705a27aee05ae0a216/client.go#L138-L146
|
15,050 |
digitalocean/go-metadata
|
client.go
|
AuthToken
|
func (c *Client) AuthToken() (string, error) {
var authToken string
err := c.doGet("auth-token", func(r io.Reader) error {
authTokenraw, err := ioutil.ReadAll(r)
authToken = string(authTokenraw)
return err
})
return authToken, err
}
|
go
|
func (c *Client) AuthToken() (string, error) {
var authToken string
err := c.doGet("auth-token", func(r io.Reader) error {
authTokenraw, err := ioutil.ReadAll(r)
authToken = string(authTokenraw)
return err
})
return authToken, err
}
|
[
"func",
"(",
"c",
"*",
"Client",
")",
"AuthToken",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"var",
"authToken",
"string",
"\n",
"err",
":=",
"c",
".",
"doGet",
"(",
"\"",
"\"",
",",
"func",
"(",
"r",
"io",
".",
"Reader",
")",
"error",
"{",
"authTokenraw",
",",
"err",
":=",
"ioutil",
".",
"ReadAll",
"(",
"r",
")",
"\n",
"authToken",
"=",
"string",
"(",
"authTokenraw",
")",
"\n",
"return",
"err",
"\n",
"}",
")",
"\n",
"return",
"authToken",
",",
"err",
"\n",
"}"
] |
// AuthToken returns the authentication token.
|
[
"AuthToken",
"returns",
"the",
"authentication",
"token",
"."
] |
15bd36e5f6f745f32074d0705a27aee05ae0a216
|
https://github.com/digitalocean/go-metadata/blob/15bd36e5f6f745f32074d0705a27aee05ae0a216/client.go#L149-L157
|
15,051 |
digitalocean/go-metadata
|
client.go
|
FloatingIPv4Active
|
func (c *Client) FloatingIPv4Active() (bool, error) {
var active bool
err := c.doGet("floating_ip/ipv4/active", func(r io.Reader) error {
activeraw, err := ioutil.ReadAll(r)
if string(activeraw) == "true" {
active = true
}
return err
})
return active, err
}
|
go
|
func (c *Client) FloatingIPv4Active() (bool, error) {
var active bool
err := c.doGet("floating_ip/ipv4/active", func(r io.Reader) error {
activeraw, err := ioutil.ReadAll(r)
if string(activeraw) == "true" {
active = true
}
return err
})
return active, err
}
|
[
"func",
"(",
"c",
"*",
"Client",
")",
"FloatingIPv4Active",
"(",
")",
"(",
"bool",
",",
"error",
")",
"{",
"var",
"active",
"bool",
"\n",
"err",
":=",
"c",
".",
"doGet",
"(",
"\"",
"\"",
",",
"func",
"(",
"r",
"io",
".",
"Reader",
")",
"error",
"{",
"activeraw",
",",
"err",
":=",
"ioutil",
".",
"ReadAll",
"(",
"r",
")",
"\n",
"if",
"string",
"(",
"activeraw",
")",
"==",
"\"",
"\"",
"{",
"active",
"=",
"true",
"\n",
"}",
"\n",
"return",
"err",
"\n",
"}",
")",
"\n",
"return",
"active",
",",
"err",
"\n",
"}"
] |
// FloatingIPv4Active returns true if an IPv4 Floating IP
// Address is assigned to the Droplet.
|
[
"FloatingIPv4Active",
"returns",
"true",
"if",
"an",
"IPv4",
"Floating",
"IP",
"Address",
"is",
"assigned",
"to",
"the",
"Droplet",
"."
] |
15bd36e5f6f745f32074d0705a27aee05ae0a216
|
https://github.com/digitalocean/go-metadata/blob/15bd36e5f6f745f32074d0705a27aee05ae0a216/client.go#L204-L214
|
15,052 |
goml/gobrain
|
persist/persist.go
|
Save
|
func Save(path string, v interface{}) error {
lock.Lock()
defer lock.Unlock()
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
r, err := Marshal(v)
if err != nil {
return err
}
_, err = io.Copy(f, r)
return err
}
|
go
|
func Save(path string, v interface{}) error {
lock.Lock()
defer lock.Unlock()
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
r, err := Marshal(v)
if err != nil {
return err
}
_, err = io.Copy(f, r)
return err
}
|
[
"func",
"Save",
"(",
"path",
"string",
",",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"f",
",",
"err",
":=",
"os",
".",
"Create",
"(",
"path",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"defer",
"f",
".",
"Close",
"(",
")",
"\n",
"r",
",",
"err",
":=",
"Marshal",
"(",
"v",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"_",
",",
"err",
"=",
"io",
".",
"Copy",
"(",
"f",
",",
"r",
")",
"\n",
"return",
"err",
"\n",
"}"
] |
// Save saves a representation of v to the file at path.
|
[
"Save",
"saves",
"a",
"representation",
"of",
"v",
"to",
"the",
"file",
"at",
"path",
"."
] |
499982ed3a4862e26a28b65fef3544129b26aa0f
|
https://github.com/goml/gobrain/blob/499982ed3a4862e26a28b65fef3544129b26aa0f/persist/persist.go#L28-L42
|
15,053 |
djimenez/iconv-go
|
reader.go
|
Read
|
func (this *Reader) Read(p []byte) (n int, err error) {
// checks for when we have no data
for this.writePos == 0 || this.readPos == this.writePos {
// if we have an error / EOF, just return it
if this.err != nil {
return n, this.err
}
// else, fill our buffer
this.fillBuffer()
}
// TODO: checks for when we have less data than len(p)
// we should have an appropriate amount of data, convert it into the given buffer
bytesRead, bytesWritten, err := this.converter.Convert(this.buffer[this.readPos:this.writePos], p)
// adjust byte counters
this.readPos += bytesRead
n += bytesWritten
// if we experienced an iconv error, check it
if err != nil {
// E2BIG errors can be ignored (we'll get them often) as long
// as at least 1 byte was written. If we experienced an E2BIG
// and no bytes were written then the buffer is too small for
// even the next character
if err != syscall.E2BIG || bytesWritten == 0 {
// track anything else
this.err = err
}
}
// return our results
return n, this.err
}
|
go
|
func (this *Reader) Read(p []byte) (n int, err error) {
// checks for when we have no data
for this.writePos == 0 || this.readPos == this.writePos {
// if we have an error / EOF, just return it
if this.err != nil {
return n, this.err
}
// else, fill our buffer
this.fillBuffer()
}
// TODO: checks for when we have less data than len(p)
// we should have an appropriate amount of data, convert it into the given buffer
bytesRead, bytesWritten, err := this.converter.Convert(this.buffer[this.readPos:this.writePos], p)
// adjust byte counters
this.readPos += bytesRead
n += bytesWritten
// if we experienced an iconv error, check it
if err != nil {
// E2BIG errors can be ignored (we'll get them often) as long
// as at least 1 byte was written. If we experienced an E2BIG
// and no bytes were written then the buffer is too small for
// even the next character
if err != syscall.E2BIG || bytesWritten == 0 {
// track anything else
this.err = err
}
}
// return our results
return n, this.err
}
|
[
"func",
"(",
"this",
"*",
"Reader",
")",
"Read",
"(",
"p",
"[",
"]",
"byte",
")",
"(",
"n",
"int",
",",
"err",
"error",
")",
"{",
"// checks for when we have no data",
"for",
"this",
".",
"writePos",
"==",
"0",
"||",
"this",
".",
"readPos",
"==",
"this",
".",
"writePos",
"{",
"// if we have an error / EOF, just return it",
"if",
"this",
".",
"err",
"!=",
"nil",
"{",
"return",
"n",
",",
"this",
".",
"err",
"\n",
"}",
"\n\n",
"// else, fill our buffer",
"this",
".",
"fillBuffer",
"(",
")",
"\n",
"}",
"\n\n",
"// TODO: checks for when we have less data than len(p)",
"// we should have an appropriate amount of data, convert it into the given buffer",
"bytesRead",
",",
"bytesWritten",
",",
"err",
":=",
"this",
".",
"converter",
".",
"Convert",
"(",
"this",
".",
"buffer",
"[",
"this",
".",
"readPos",
":",
"this",
".",
"writePos",
"]",
",",
"p",
")",
"\n\n",
"// adjust byte counters",
"this",
".",
"readPos",
"+=",
"bytesRead",
"\n",
"n",
"+=",
"bytesWritten",
"\n\n",
"// if we experienced an iconv error, check it",
"if",
"err",
"!=",
"nil",
"{",
"// E2BIG errors can be ignored (we'll get them often) as long",
"// as at least 1 byte was written. If we experienced an E2BIG",
"// and no bytes were written then the buffer is too small for",
"// even the next character",
"if",
"err",
"!=",
"syscall",
".",
"E2BIG",
"||",
"bytesWritten",
"==",
"0",
"{",
"// track anything else",
"this",
".",
"err",
"=",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"// return our results",
"return",
"n",
",",
"this",
".",
"err",
"\n",
"}"
] |
// implement the io.Reader interface
|
[
"implement",
"the",
"io",
".",
"Reader",
"interface"
] |
8960e66bd3dacbc8773962b40eb82fc18c63c127
|
https://github.com/djimenez/iconv-go/blob/8960e66bd3dacbc8773962b40eb82fc18c63c127/reader.go#L65-L100
|
15,054 |
djimenez/iconv-go
|
converter.go
|
NewConverter
|
func NewConverter(fromEncoding string, toEncoding string) (converter *Converter, err error) {
converter = new(Converter)
// convert to C strings
toEncodingC := C.CString(toEncoding)
fromEncodingC := C.CString(fromEncoding)
// open an iconv descriptor
converter.context, err = C.iconv_open(toEncodingC, fromEncodingC)
// free the C Strings
C.free(unsafe.Pointer(toEncodingC))
C.free(unsafe.Pointer(fromEncodingC))
// check err
if err == nil {
// no error, mark the context as open
converter.open = true
}
return
}
|
go
|
func NewConverter(fromEncoding string, toEncoding string) (converter *Converter, err error) {
converter = new(Converter)
// convert to C strings
toEncodingC := C.CString(toEncoding)
fromEncodingC := C.CString(fromEncoding)
// open an iconv descriptor
converter.context, err = C.iconv_open(toEncodingC, fromEncodingC)
// free the C Strings
C.free(unsafe.Pointer(toEncodingC))
C.free(unsafe.Pointer(fromEncodingC))
// check err
if err == nil {
// no error, mark the context as open
converter.open = true
}
return
}
|
[
"func",
"NewConverter",
"(",
"fromEncoding",
"string",
",",
"toEncoding",
"string",
")",
"(",
"converter",
"*",
"Converter",
",",
"err",
"error",
")",
"{",
"converter",
"=",
"new",
"(",
"Converter",
")",
"\n\n",
"// convert to C strings",
"toEncodingC",
":=",
"C",
".",
"CString",
"(",
"toEncoding",
")",
"\n",
"fromEncodingC",
":=",
"C",
".",
"CString",
"(",
"fromEncoding",
")",
"\n\n",
"// open an iconv descriptor",
"converter",
".",
"context",
",",
"err",
"=",
"C",
".",
"iconv_open",
"(",
"toEncodingC",
",",
"fromEncodingC",
")",
"\n\n",
"// free the C Strings",
"C",
".",
"free",
"(",
"unsafe",
".",
"Pointer",
"(",
"toEncodingC",
")",
")",
"\n",
"C",
".",
"free",
"(",
"unsafe",
".",
"Pointer",
"(",
"fromEncodingC",
")",
")",
"\n\n",
"// check err",
"if",
"err",
"==",
"nil",
"{",
"// no error, mark the context as open",
"converter",
".",
"open",
"=",
"true",
"\n",
"}",
"\n\n",
"return",
"\n",
"}"
] |
// Initialize a new Converter. If fromEncoding or toEncoding are not supported by
// iconv then an EINVAL error will be returned. An ENOMEM error maybe returned if
// there is not enough memory to initialize an iconv descriptor
|
[
"Initialize",
"a",
"new",
"Converter",
".",
"If",
"fromEncoding",
"or",
"toEncoding",
"are",
"not",
"supported",
"by",
"iconv",
"then",
"an",
"EINVAL",
"error",
"will",
"be",
"returned",
".",
"An",
"ENOMEM",
"error",
"maybe",
"returned",
"if",
"there",
"is",
"not",
"enough",
"memory",
"to",
"initialize",
"an",
"iconv",
"descriptor"
] |
8960e66bd3dacbc8773962b40eb82fc18c63c127
|
https://github.com/djimenez/iconv-go/blob/8960e66bd3dacbc8773962b40eb82fc18c63c127/converter.go#L29-L50
|
15,055 |
djimenez/iconv-go
|
converter.go
|
Close
|
func (this *Converter) Close() (err error) {
if this.open {
_, err = C.iconv_close(this.context)
}
return
}
|
go
|
func (this *Converter) Close() (err error) {
if this.open {
_, err = C.iconv_close(this.context)
}
return
}
|
[
"func",
"(",
"this",
"*",
"Converter",
")",
"Close",
"(",
")",
"(",
"err",
"error",
")",
"{",
"if",
"this",
".",
"open",
"{",
"_",
",",
"err",
"=",
"C",
".",
"iconv_close",
"(",
"this",
".",
"context",
")",
"\n",
"}",
"\n\n",
"return",
"\n",
"}"
] |
// Close a Converter's iconv description explicitly
|
[
"Close",
"a",
"Converter",
"s",
"iconv",
"description",
"explicitly"
] |
8960e66bd3dacbc8773962b40eb82fc18c63c127
|
https://github.com/djimenez/iconv-go/blob/8960e66bd3dacbc8773962b40eb82fc18c63c127/converter.go#L58-L64
|
15,056 |
djimenez/iconv-go
|
converter.go
|
Convert
|
func (this *Converter) Convert(input []byte, output []byte) (bytesRead int, bytesWritten int, err error) {
// make sure we are still open
if this.open {
inputLeft := C.size_t(len(input))
outputLeft := C.size_t(len(output))
if inputLeft > 0 && outputLeft > 0 {
// we have to give iconv a pointer to a pointer of the underlying
// storage of each byte slice - so far this is the simplest
// way i've found to do that in Go, but it seems ugly
inputPointer := (*C.char)(unsafe.Pointer(&input[0]))
outputPointer := (*C.char)(unsafe.Pointer(&output[0]))
_, err = C.call_iconv(this.context, inputPointer, &inputLeft, outputPointer, &outputLeft)
// update byte counters
bytesRead = len(input) - int(inputLeft)
bytesWritten = len(output) - int(outputLeft)
} else if inputLeft == 0 && outputLeft > 0 {
// inputPointer will be nil, outputPointer is generated as above
outputPointer := (*C.char)(unsafe.Pointer(&output[0]))
_, err = C.call_iconv(this.context, nil, &inputLeft, outputPointer, &outputLeft)
// update write byte counter
bytesWritten = len(output) - int(outputLeft)
} else {
// both input and output are zero length, do a shift state reset
_, err = C.call_iconv(this.context, nil, &inputLeft, nil, &outputLeft)
}
} else {
err = syscall.EBADF
}
return bytesRead, bytesWritten, err
}
|
go
|
func (this *Converter) Convert(input []byte, output []byte) (bytesRead int, bytesWritten int, err error) {
// make sure we are still open
if this.open {
inputLeft := C.size_t(len(input))
outputLeft := C.size_t(len(output))
if inputLeft > 0 && outputLeft > 0 {
// we have to give iconv a pointer to a pointer of the underlying
// storage of each byte slice - so far this is the simplest
// way i've found to do that in Go, but it seems ugly
inputPointer := (*C.char)(unsafe.Pointer(&input[0]))
outputPointer := (*C.char)(unsafe.Pointer(&output[0]))
_, err = C.call_iconv(this.context, inputPointer, &inputLeft, outputPointer, &outputLeft)
// update byte counters
bytesRead = len(input) - int(inputLeft)
bytesWritten = len(output) - int(outputLeft)
} else if inputLeft == 0 && outputLeft > 0 {
// inputPointer will be nil, outputPointer is generated as above
outputPointer := (*C.char)(unsafe.Pointer(&output[0]))
_, err = C.call_iconv(this.context, nil, &inputLeft, outputPointer, &outputLeft)
// update write byte counter
bytesWritten = len(output) - int(outputLeft)
} else {
// both input and output are zero length, do a shift state reset
_, err = C.call_iconv(this.context, nil, &inputLeft, nil, &outputLeft)
}
} else {
err = syscall.EBADF
}
return bytesRead, bytesWritten, err
}
|
[
"func",
"(",
"this",
"*",
"Converter",
")",
"Convert",
"(",
"input",
"[",
"]",
"byte",
",",
"output",
"[",
"]",
"byte",
")",
"(",
"bytesRead",
"int",
",",
"bytesWritten",
"int",
",",
"err",
"error",
")",
"{",
"// make sure we are still open",
"if",
"this",
".",
"open",
"{",
"inputLeft",
":=",
"C",
".",
"size_t",
"(",
"len",
"(",
"input",
")",
")",
"\n",
"outputLeft",
":=",
"C",
".",
"size_t",
"(",
"len",
"(",
"output",
")",
")",
"\n\n",
"if",
"inputLeft",
">",
"0",
"&&",
"outputLeft",
">",
"0",
"{",
"// we have to give iconv a pointer to a pointer of the underlying",
"// storage of each byte slice - so far this is the simplest",
"// way i've found to do that in Go, but it seems ugly",
"inputPointer",
":=",
"(",
"*",
"C",
".",
"char",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"input",
"[",
"0",
"]",
")",
")",
"\n",
"outputPointer",
":=",
"(",
"*",
"C",
".",
"char",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"output",
"[",
"0",
"]",
")",
")",
"\n\n",
"_",
",",
"err",
"=",
"C",
".",
"call_iconv",
"(",
"this",
".",
"context",
",",
"inputPointer",
",",
"&",
"inputLeft",
",",
"outputPointer",
",",
"&",
"outputLeft",
")",
"\n\n",
"// update byte counters",
"bytesRead",
"=",
"len",
"(",
"input",
")",
"-",
"int",
"(",
"inputLeft",
")",
"\n",
"bytesWritten",
"=",
"len",
"(",
"output",
")",
"-",
"int",
"(",
"outputLeft",
")",
"\n",
"}",
"else",
"if",
"inputLeft",
"==",
"0",
"&&",
"outputLeft",
">",
"0",
"{",
"// inputPointer will be nil, outputPointer is generated as above",
"outputPointer",
":=",
"(",
"*",
"C",
".",
"char",
")",
"(",
"unsafe",
".",
"Pointer",
"(",
"&",
"output",
"[",
"0",
"]",
")",
")",
"\n\n",
"_",
",",
"err",
"=",
"C",
".",
"call_iconv",
"(",
"this",
".",
"context",
",",
"nil",
",",
"&",
"inputLeft",
",",
"outputPointer",
",",
"&",
"outputLeft",
")",
"\n\n",
"// update write byte counter",
"bytesWritten",
"=",
"len",
"(",
"output",
")",
"-",
"int",
"(",
"outputLeft",
")",
"\n",
"}",
"else",
"{",
"// both input and output are zero length, do a shift state reset",
"_",
",",
"err",
"=",
"C",
".",
"call_iconv",
"(",
"this",
".",
"context",
",",
"nil",
",",
"&",
"inputLeft",
",",
"nil",
",",
"&",
"outputLeft",
")",
"\n",
"}",
"\n",
"}",
"else",
"{",
"err",
"=",
"syscall",
".",
"EBADF",
"\n",
"}",
"\n\n",
"return",
"bytesRead",
",",
"bytesWritten",
",",
"err",
"\n",
"}"
] |
// Convert bytes from an input byte slice into a give output byte slice
//
// As many bytes that can converted and fit into the size of output will be
// processed and the number of bytes read for input as well as the number of
// bytes written to output will be returned. If not all converted bytes can fit
// into output and E2BIG error will also be returned. If input contains an invalid
// sequence of bytes for the Converter's fromEncoding an EILSEQ error will be returned
//
// For shift based output encodings, any end shift byte sequences can be generated by
// passing a 0 length byte slice as input. Also passing a 0 length byte slice for output
// will simply reset the iconv descriptor shift state without writing any bytes.
|
[
"Convert",
"bytes",
"from",
"an",
"input",
"byte",
"slice",
"into",
"a",
"give",
"output",
"byte",
"slice",
"As",
"many",
"bytes",
"that",
"can",
"converted",
"and",
"fit",
"into",
"the",
"size",
"of",
"output",
"will",
"be",
"processed",
"and",
"the",
"number",
"of",
"bytes",
"read",
"for",
"input",
"as",
"well",
"as",
"the",
"number",
"of",
"bytes",
"written",
"to",
"output",
"will",
"be",
"returned",
".",
"If",
"not",
"all",
"converted",
"bytes",
"can",
"fit",
"into",
"output",
"and",
"E2BIG",
"error",
"will",
"also",
"be",
"returned",
".",
"If",
"input",
"contains",
"an",
"invalid",
"sequence",
"of",
"bytes",
"for",
"the",
"Converter",
"s",
"fromEncoding",
"an",
"EILSEQ",
"error",
"will",
"be",
"returned",
"For",
"shift",
"based",
"output",
"encodings",
"any",
"end",
"shift",
"byte",
"sequences",
"can",
"be",
"generated",
"by",
"passing",
"a",
"0",
"length",
"byte",
"slice",
"as",
"input",
".",
"Also",
"passing",
"a",
"0",
"length",
"byte",
"slice",
"for",
"output",
"will",
"simply",
"reset",
"the",
"iconv",
"descriptor",
"shift",
"state",
"without",
"writing",
"any",
"bytes",
"."
] |
8960e66bd3dacbc8773962b40eb82fc18c63c127
|
https://github.com/djimenez/iconv-go/blob/8960e66bd3dacbc8773962b40eb82fc18c63c127/converter.go#L77-L112
|
15,057 |
djimenez/iconv-go
|
converter.go
|
ConvertString
|
func (this *Converter) ConvertString(input string) (output string, err error) {
// make sure we are still open
if this.open {
// construct the buffers
inputBuffer := []byte(input)
outputBuffer := make([]byte, len(inputBuffer)*2) // we use a larger buffer to help avoid resizing later
// call Convert until all input bytes are read or an error occurs
var bytesRead, totalBytesRead, bytesWritten, totalBytesWritten int
for totalBytesRead < len(inputBuffer) && err == nil {
// use the totals to create buffer slices
bytesRead, bytesWritten, err = this.Convert(inputBuffer[totalBytesRead:], outputBuffer[totalBytesWritten:])
totalBytesRead += bytesRead
totalBytesWritten += bytesWritten
// check for the E2BIG error specifically, we can add to the output
// buffer to correct for it and then continue
if err == syscall.E2BIG {
// increase the size of the output buffer by another input length
// first, create a new buffer
tempBuffer := make([]byte, len(outputBuffer)+len(inputBuffer))
// copy the existing data
copy(tempBuffer, outputBuffer)
// switch the buffers
outputBuffer = tempBuffer
// forget the error
err = nil
}
}
if err == nil {
// perform a final shift state reset
_, bytesWritten, err = this.Convert([]byte{}, outputBuffer[totalBytesWritten:])
// update total count
totalBytesWritten += bytesWritten
}
// construct the final output string
output = string(outputBuffer[:totalBytesWritten])
} else {
err = syscall.EBADF
}
return output, err
}
|
go
|
func (this *Converter) ConvertString(input string) (output string, err error) {
// make sure we are still open
if this.open {
// construct the buffers
inputBuffer := []byte(input)
outputBuffer := make([]byte, len(inputBuffer)*2) // we use a larger buffer to help avoid resizing later
// call Convert until all input bytes are read or an error occurs
var bytesRead, totalBytesRead, bytesWritten, totalBytesWritten int
for totalBytesRead < len(inputBuffer) && err == nil {
// use the totals to create buffer slices
bytesRead, bytesWritten, err = this.Convert(inputBuffer[totalBytesRead:], outputBuffer[totalBytesWritten:])
totalBytesRead += bytesRead
totalBytesWritten += bytesWritten
// check for the E2BIG error specifically, we can add to the output
// buffer to correct for it and then continue
if err == syscall.E2BIG {
// increase the size of the output buffer by another input length
// first, create a new buffer
tempBuffer := make([]byte, len(outputBuffer)+len(inputBuffer))
// copy the existing data
copy(tempBuffer, outputBuffer)
// switch the buffers
outputBuffer = tempBuffer
// forget the error
err = nil
}
}
if err == nil {
// perform a final shift state reset
_, bytesWritten, err = this.Convert([]byte{}, outputBuffer[totalBytesWritten:])
// update total count
totalBytesWritten += bytesWritten
}
// construct the final output string
output = string(outputBuffer[:totalBytesWritten])
} else {
err = syscall.EBADF
}
return output, err
}
|
[
"func",
"(",
"this",
"*",
"Converter",
")",
"ConvertString",
"(",
"input",
"string",
")",
"(",
"output",
"string",
",",
"err",
"error",
")",
"{",
"// make sure we are still open",
"if",
"this",
".",
"open",
"{",
"// construct the buffers",
"inputBuffer",
":=",
"[",
"]",
"byte",
"(",
"input",
")",
"\n",
"outputBuffer",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"len",
"(",
"inputBuffer",
")",
"*",
"2",
")",
"// we use a larger buffer to help avoid resizing later",
"\n\n",
"// call Convert until all input bytes are read or an error occurs",
"var",
"bytesRead",
",",
"totalBytesRead",
",",
"bytesWritten",
",",
"totalBytesWritten",
"int",
"\n\n",
"for",
"totalBytesRead",
"<",
"len",
"(",
"inputBuffer",
")",
"&&",
"err",
"==",
"nil",
"{",
"// use the totals to create buffer slices",
"bytesRead",
",",
"bytesWritten",
",",
"err",
"=",
"this",
".",
"Convert",
"(",
"inputBuffer",
"[",
"totalBytesRead",
":",
"]",
",",
"outputBuffer",
"[",
"totalBytesWritten",
":",
"]",
")",
"\n\n",
"totalBytesRead",
"+=",
"bytesRead",
"\n",
"totalBytesWritten",
"+=",
"bytesWritten",
"\n\n",
"// check for the E2BIG error specifically, we can add to the output",
"// buffer to correct for it and then continue",
"if",
"err",
"==",
"syscall",
".",
"E2BIG",
"{",
"// increase the size of the output buffer by another input length",
"// first, create a new buffer",
"tempBuffer",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"len",
"(",
"outputBuffer",
")",
"+",
"len",
"(",
"inputBuffer",
")",
")",
"\n\n",
"// copy the existing data",
"copy",
"(",
"tempBuffer",
",",
"outputBuffer",
")",
"\n\n",
"// switch the buffers",
"outputBuffer",
"=",
"tempBuffer",
"\n\n",
"// forget the error",
"err",
"=",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"err",
"==",
"nil",
"{",
"// perform a final shift state reset",
"_",
",",
"bytesWritten",
",",
"err",
"=",
"this",
".",
"Convert",
"(",
"[",
"]",
"byte",
"{",
"}",
",",
"outputBuffer",
"[",
"totalBytesWritten",
":",
"]",
")",
"\n\n",
"// update total count",
"totalBytesWritten",
"+=",
"bytesWritten",
"\n",
"}",
"\n\n",
"// construct the final output string",
"output",
"=",
"string",
"(",
"outputBuffer",
"[",
":",
"totalBytesWritten",
"]",
")",
"\n",
"}",
"else",
"{",
"err",
"=",
"syscall",
".",
"EBADF",
"\n",
"}",
"\n\n",
"return",
"output",
",",
"err",
"\n",
"}"
] |
// Convert an input string
//
// EILSEQ error may be returned if input contains invalid bytes for the
// Converter's fromEncoding.
|
[
"Convert",
"an",
"input",
"string",
"EILSEQ",
"error",
"may",
"be",
"returned",
"if",
"input",
"contains",
"invalid",
"bytes",
"for",
"the",
"Converter",
"s",
"fromEncoding",
"."
] |
8960e66bd3dacbc8773962b40eb82fc18c63c127
|
https://github.com/djimenez/iconv-go/blob/8960e66bd3dacbc8773962b40eb82fc18c63c127/converter.go#L118-L168
|
15,058 |
djimenez/iconv-go
|
iconv.go
|
Convert
|
func Convert(input []byte, output []byte, fromEncoding string, toEncoding string) (bytesRead int, bytesWritten int, err error) {
// create a temporary converter
converter, err := NewConverter(fromEncoding, toEncoding)
if err == nil {
// call converter's Convert
bytesRead, bytesWritten, err = converter.Convert(input, output)
if err == nil {
var shiftBytesWritten int
// call Convert with a nil input to generate any end shift sequences
_, shiftBytesWritten, err = converter.Convert(nil, output[bytesWritten:])
// add shift bytes to total bytes
bytesWritten += shiftBytesWritten
}
// close the converter
converter.Close()
}
return
}
|
go
|
func Convert(input []byte, output []byte, fromEncoding string, toEncoding string) (bytesRead int, bytesWritten int, err error) {
// create a temporary converter
converter, err := NewConverter(fromEncoding, toEncoding)
if err == nil {
// call converter's Convert
bytesRead, bytesWritten, err = converter.Convert(input, output)
if err == nil {
var shiftBytesWritten int
// call Convert with a nil input to generate any end shift sequences
_, shiftBytesWritten, err = converter.Convert(nil, output[bytesWritten:])
// add shift bytes to total bytes
bytesWritten += shiftBytesWritten
}
// close the converter
converter.Close()
}
return
}
|
[
"func",
"Convert",
"(",
"input",
"[",
"]",
"byte",
",",
"output",
"[",
"]",
"byte",
",",
"fromEncoding",
"string",
",",
"toEncoding",
"string",
")",
"(",
"bytesRead",
"int",
",",
"bytesWritten",
"int",
",",
"err",
"error",
")",
"{",
"// create a temporary converter",
"converter",
",",
"err",
":=",
"NewConverter",
"(",
"fromEncoding",
",",
"toEncoding",
")",
"\n\n",
"if",
"err",
"==",
"nil",
"{",
"// call converter's Convert",
"bytesRead",
",",
"bytesWritten",
",",
"err",
"=",
"converter",
".",
"Convert",
"(",
"input",
",",
"output",
")",
"\n\n",
"if",
"err",
"==",
"nil",
"{",
"var",
"shiftBytesWritten",
"int",
"\n\n",
"// call Convert with a nil input to generate any end shift sequences",
"_",
",",
"shiftBytesWritten",
",",
"err",
"=",
"converter",
".",
"Convert",
"(",
"nil",
",",
"output",
"[",
"bytesWritten",
":",
"]",
")",
"\n\n",
"// add shift bytes to total bytes",
"bytesWritten",
"+=",
"shiftBytesWritten",
"\n",
"}",
"\n\n",
"// close the converter",
"converter",
".",
"Close",
"(",
")",
"\n",
"}",
"\n\n",
"return",
"\n",
"}"
] |
// All in one Convert method, rather than requiring the construction of an iconv.Converter
|
[
"All",
"in",
"one",
"Convert",
"method",
"rather",
"than",
"requiring",
"the",
"construction",
"of",
"an",
"iconv",
".",
"Converter"
] |
8960e66bd3dacbc8773962b40eb82fc18c63c127
|
https://github.com/djimenez/iconv-go/blob/8960e66bd3dacbc8773962b40eb82fc18c63c127/iconv.go#L9-L32
|
15,059 |
djimenez/iconv-go
|
iconv.go
|
ConvertString
|
func ConvertString(input string, fromEncoding string, toEncoding string) (output string, err error) {
// create a temporary converter
converter, err := NewConverter(fromEncoding, toEncoding)
if err == nil {
// convert the string
output, err = converter.ConvertString(input)
// close the converter
converter.Close()
}
return
}
|
go
|
func ConvertString(input string, fromEncoding string, toEncoding string) (output string, err error) {
// create a temporary converter
converter, err := NewConverter(fromEncoding, toEncoding)
if err == nil {
// convert the string
output, err = converter.ConvertString(input)
// close the converter
converter.Close()
}
return
}
|
[
"func",
"ConvertString",
"(",
"input",
"string",
",",
"fromEncoding",
"string",
",",
"toEncoding",
"string",
")",
"(",
"output",
"string",
",",
"err",
"error",
")",
"{",
"// create a temporary converter",
"converter",
",",
"err",
":=",
"NewConverter",
"(",
"fromEncoding",
",",
"toEncoding",
")",
"\n\n",
"if",
"err",
"==",
"nil",
"{",
"// convert the string",
"output",
",",
"err",
"=",
"converter",
".",
"ConvertString",
"(",
"input",
")",
"\n\n",
"// close the converter",
"converter",
".",
"Close",
"(",
")",
"\n",
"}",
"\n\n",
"return",
"\n",
"}"
] |
// All in one ConvertString method, rather than requiring the construction of an iconv.Converter
|
[
"All",
"in",
"one",
"ConvertString",
"method",
"rather",
"than",
"requiring",
"the",
"construction",
"of",
"an",
"iconv",
".",
"Converter"
] |
8960e66bd3dacbc8773962b40eb82fc18c63c127
|
https://github.com/djimenez/iconv-go/blob/8960e66bd3dacbc8773962b40eb82fc18c63c127/iconv.go#L35-L48
|
15,060 |
djimenez/iconv-go
|
writer.go
|
Write
|
func (this *Writer) Write(p []byte) (n int, err error) {
// write data into our internal buffer
bytesRead, bytesWritten, err := this.converter.Convert(p, this.buffer[this.writePos:])
// update bytes written for return
n += bytesRead
this.writePos += bytesWritten
// checks for when we have a full buffer
for this.writePos > 0 {
// if we have an error, just return it
if this.err != nil {
return
}
// else empty the buffer
this.emptyBuffer()
}
return n, err
}
|
go
|
func (this *Writer) Write(p []byte) (n int, err error) {
// write data into our internal buffer
bytesRead, bytesWritten, err := this.converter.Convert(p, this.buffer[this.writePos:])
// update bytes written for return
n += bytesRead
this.writePos += bytesWritten
// checks for when we have a full buffer
for this.writePos > 0 {
// if we have an error, just return it
if this.err != nil {
return
}
// else empty the buffer
this.emptyBuffer()
}
return n, err
}
|
[
"func",
"(",
"this",
"*",
"Writer",
")",
"Write",
"(",
"p",
"[",
"]",
"byte",
")",
"(",
"n",
"int",
",",
"err",
"error",
")",
"{",
"// write data into our internal buffer",
"bytesRead",
",",
"bytesWritten",
",",
"err",
":=",
"this",
".",
"converter",
".",
"Convert",
"(",
"p",
",",
"this",
".",
"buffer",
"[",
"this",
".",
"writePos",
":",
"]",
")",
"\n\n",
"// update bytes written for return",
"n",
"+=",
"bytesRead",
"\n",
"this",
".",
"writePos",
"+=",
"bytesWritten",
"\n\n",
"// checks for when we have a full buffer",
"for",
"this",
".",
"writePos",
">",
"0",
"{",
"// if we have an error, just return it",
"if",
"this",
".",
"err",
"!=",
"nil",
"{",
"return",
"\n",
"}",
"\n\n",
"// else empty the buffer",
"this",
".",
"emptyBuffer",
"(",
")",
"\n",
"}",
"\n\n",
"return",
"n",
",",
"err",
"\n",
"}"
] |
// implement the io.Writer interface
|
[
"implement",
"the",
"io",
".",
"Writer",
"interface"
] |
8960e66bd3dacbc8773962b40eb82fc18c63c127
|
https://github.com/djimenez/iconv-go/blob/8960e66bd3dacbc8773962b40eb82fc18c63c127/writer.go#L62-L82
|
15,061 |
limetext/backend
|
render/renderer.go
|
Transform
|
func Transform(scheme ColourScheme, data ViewRegionMap, viewport text.Region) Recipe {
pe := util.Prof.Enter("render.Transform")
defer pe.Exit()
data.Cull(viewport)
recipe := make(Recipe)
for _, v := range data {
k := scheme.Spice(&v)
rs := recipe[k]
a := util.Prof.Enter("render.Transform.(Regions)")
r := v.Regions.Regions()
a.Exit()
a = util.Prof.Enter("render.Transform.(AddAll)")
rs.AddAll(r)
a.Exit()
recipe[k] = rs
}
return recipe
}
|
go
|
func Transform(scheme ColourScheme, data ViewRegionMap, viewport text.Region) Recipe {
pe := util.Prof.Enter("render.Transform")
defer pe.Exit()
data.Cull(viewport)
recipe := make(Recipe)
for _, v := range data {
k := scheme.Spice(&v)
rs := recipe[k]
a := util.Prof.Enter("render.Transform.(Regions)")
r := v.Regions.Regions()
a.Exit()
a = util.Prof.Enter("render.Transform.(AddAll)")
rs.AddAll(r)
a.Exit()
recipe[k] = rs
}
return recipe
}
|
[
"func",
"Transform",
"(",
"scheme",
"ColourScheme",
",",
"data",
"ViewRegionMap",
",",
"viewport",
"text",
".",
"Region",
")",
"Recipe",
"{",
"pe",
":=",
"util",
".",
"Prof",
".",
"Enter",
"(",
"\"",
"\"",
")",
"\n",
"defer",
"pe",
".",
"Exit",
"(",
")",
"\n\n",
"data",
".",
"Cull",
"(",
"viewport",
")",
"\n",
"recipe",
":=",
"make",
"(",
"Recipe",
")",
"\n",
"for",
"_",
",",
"v",
":=",
"range",
"data",
"{",
"k",
":=",
"scheme",
".",
"Spice",
"(",
"&",
"v",
")",
"\n",
"rs",
":=",
"recipe",
"[",
"k",
"]",
"\n",
"a",
":=",
"util",
".",
"Prof",
".",
"Enter",
"(",
"\"",
"\"",
")",
"\n",
"r",
":=",
"v",
".",
"Regions",
".",
"Regions",
"(",
")",
"\n",
"a",
".",
"Exit",
"(",
")",
"\n",
"a",
"=",
"util",
".",
"Prof",
".",
"Enter",
"(",
"\"",
"\"",
")",
"\n",
"rs",
".",
"AddAll",
"(",
"r",
")",
"\n",
"a",
".",
"Exit",
"(",
")",
"\n",
"recipe",
"[",
"k",
"]",
"=",
"rs",
"\n",
"}",
"\n",
"return",
"recipe",
"\n",
"}"
] |
// Transform takes a ColourScheme, a ViewRegionMap and a viewport as input.
//
// The viewport would be the text.Region of the current buffer that is visible to the user
// and any ViewRegions outside of this area are not forwarded for further processing.
//
// The remaining ViewRegions are then passed on to the ColourScheme for determining the exact Flavour
// for which that RegionSet should be styled, adding Regions of the same Flavour to the same RegionSet.
//
// Typically there are more ViewRegions available in a text buffer than there are unique Flavours in
// a ColourScheme, so this operation can be viewed as reducing the number of state changes required to
// display the text to the user.
//
// The final output, the Recipe, contains a mapping of all unique Flavours and that Flavour's
// associated RegionSet.
|
[
"Transform",
"takes",
"a",
"ColourScheme",
"a",
"ViewRegionMap",
"and",
"a",
"viewport",
"as",
"input",
".",
"The",
"viewport",
"would",
"be",
"the",
"text",
".",
"Region",
"of",
"the",
"current",
"buffer",
"that",
"is",
"visible",
"to",
"the",
"user",
"and",
"any",
"ViewRegions",
"outside",
"of",
"this",
"area",
"are",
"not",
"forwarded",
"for",
"further",
"processing",
".",
"The",
"remaining",
"ViewRegions",
"are",
"then",
"passed",
"on",
"to",
"the",
"ColourScheme",
"for",
"determining",
"the",
"exact",
"Flavour",
"for",
"which",
"that",
"RegionSet",
"should",
"be",
"styled",
"adding",
"Regions",
"of",
"the",
"same",
"Flavour",
"to",
"the",
"same",
"RegionSet",
".",
"Typically",
"there",
"are",
"more",
"ViewRegions",
"available",
"in",
"a",
"text",
"buffer",
"than",
"there",
"are",
"unique",
"Flavours",
"in",
"a",
"ColourScheme",
"so",
"this",
"operation",
"can",
"be",
"viewed",
"as",
"reducing",
"the",
"number",
"of",
"state",
"changes",
"required",
"to",
"display",
"the",
"text",
"to",
"the",
"user",
".",
"The",
"final",
"output",
"the",
"Recipe",
"contains",
"a",
"mapping",
"of",
"all",
"unique",
"Flavours",
"and",
"that",
"Flavour",
"s",
"associated",
"RegionSet",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/render/renderer.go#L68-L86
|
15,062 |
limetext/backend
|
render/renderer.go
|
Transcribe
|
func (r Recipe) Transcribe() (ret TranscribedRecipe) {
pe := util.Prof.Enter("render.Transcribe")
defer pe.Exit()
for flav, set := range r {
for _, r := range set.Regions() {
ret = append(ret, RenderUnit{Flavour: flav, Region: r})
}
}
sort.Sort(&ret)
return
}
|
go
|
func (r Recipe) Transcribe() (ret TranscribedRecipe) {
pe := util.Prof.Enter("render.Transcribe")
defer pe.Exit()
for flav, set := range r {
for _, r := range set.Regions() {
ret = append(ret, RenderUnit{Flavour: flav, Region: r})
}
}
sort.Sort(&ret)
return
}
|
[
"func",
"(",
"r",
"Recipe",
")",
"Transcribe",
"(",
")",
"(",
"ret",
"TranscribedRecipe",
")",
"{",
"pe",
":=",
"util",
".",
"Prof",
".",
"Enter",
"(",
"\"",
"\"",
")",
"\n",
"defer",
"pe",
".",
"Exit",
"(",
")",
"\n",
"for",
"flav",
",",
"set",
":=",
"range",
"r",
"{",
"for",
"_",
",",
"r",
":=",
"range",
"set",
".",
"Regions",
"(",
")",
"{",
"ret",
"=",
"append",
"(",
"ret",
",",
"RenderUnit",
"{",
"Flavour",
":",
"flav",
",",
"Region",
":",
"r",
"}",
")",
"\n",
"}",
"\n",
"}",
"\n",
"sort",
".",
"Sort",
"(",
"&",
"ret",
")",
"\n",
"return",
"\n",
"}"
] |
// Transcribing the Recipe creates a linear step-by-step
// representation of it, which might or might not
// make it easier for Renderers to work with.
|
[
"Transcribing",
"the",
"Recipe",
"creates",
"a",
"linear",
"step",
"-",
"by",
"-",
"step",
"representation",
"of",
"it",
"which",
"might",
"or",
"might",
"not",
"make",
"it",
"easier",
"for",
"Renderers",
"to",
"work",
"with",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/render/renderer.go#L91-L101
|
15,063 |
limetext/backend
|
edit.go
|
String
|
func (e *Edit) String() string {
return fmt.Sprintf("%s: %v, %v, %v", e.command, e.args, e.bypassUndo, e.composite)
}
|
go
|
func (e *Edit) String() string {
return fmt.Sprintf("%s: %v, %v, %v", e.command, e.args, e.bypassUndo, e.composite)
}
|
[
"func",
"(",
"e",
"*",
"Edit",
")",
"String",
"(",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"e",
".",
"command",
",",
"e",
".",
"args",
",",
"e",
".",
"bypassUndo",
",",
"e",
".",
"composite",
")",
"\n",
"}"
] |
// Returns a string describing this Edit object. Should typically not be manually called.
|
[
"Returns",
"a",
"string",
"describing",
"this",
"Edit",
"object",
".",
"Should",
"typically",
"not",
"be",
"manually",
"called",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/edit.go#L43-L45
|
15,064 |
limetext/backend
|
edit.go
|
Undo
|
func (e *Edit) Undo() {
e.composite.Undo()
e.v.Sel().Clear()
for _, r := range e.savedSel.Regions() {
e.v.Sel().Add(r)
}
}
|
go
|
func (e *Edit) Undo() {
e.composite.Undo()
e.v.Sel().Clear()
for _, r := range e.savedSel.Regions() {
e.v.Sel().Add(r)
}
}
|
[
"func",
"(",
"e",
"*",
"Edit",
")",
"Undo",
"(",
")",
"{",
"e",
".",
"composite",
".",
"Undo",
"(",
")",
"\n",
"e",
".",
"v",
".",
"Sel",
"(",
")",
".",
"Clear",
"(",
")",
"\n",
"for",
"_",
",",
"r",
":=",
"range",
"e",
".",
"savedSel",
".",
"Regions",
"(",
")",
"{",
"e",
".",
"v",
".",
"Sel",
"(",
")",
".",
"Add",
"(",
"r",
")",
"\n",
"}",
"\n",
"}"
] |
// Reverses the application of this Edit object. Should typically not be manually called.
|
[
"Reverses",
"the",
"application",
"of",
"this",
"Edit",
"object",
".",
"Should",
"typically",
"not",
"be",
"manually",
"called",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/edit.go#L53-L59
|
15,065 |
limetext/backend
|
keys/keybinding.go
|
Less
|
func (k *KeyBindings) Less(i, j int) bool {
return k.Bindings[i].Keys[k.seqIndex].Index() < k.Bindings[j].Keys[k.seqIndex].Index()
}
|
go
|
func (k *KeyBindings) Less(i, j int) bool {
return k.Bindings[i].Keys[k.seqIndex].Index() < k.Bindings[j].Keys[k.seqIndex].Index()
}
|
[
"func",
"(",
"k",
"*",
"KeyBindings",
")",
"Less",
"(",
"i",
",",
"j",
"int",
")",
"bool",
"{",
"return",
"k",
".",
"Bindings",
"[",
"i",
"]",
".",
"Keys",
"[",
"k",
".",
"seqIndex",
"]",
".",
"Index",
"(",
")",
"<",
"k",
".",
"Bindings",
"[",
"j",
"]",
".",
"Keys",
"[",
"k",
".",
"seqIndex",
"]",
".",
"Index",
"(",
")",
"\n",
"}"
] |
// Compares one KeyBinding to another for sorting purposes.
|
[
"Compares",
"one",
"KeyBinding",
"to",
"another",
"for",
"sorting",
"purposes",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/keys/keybinding.go#L56-L58
|
15,066 |
limetext/backend
|
keys/keybinding.go
|
Swap
|
func (k *KeyBindings) Swap(i, j int) {
k.Bindings[i], k.Bindings[j] = k.Bindings[j], k.Bindings[i]
}
|
go
|
func (k *KeyBindings) Swap(i, j int) {
k.Bindings[i], k.Bindings[j] = k.Bindings[j], k.Bindings[i]
}
|
[
"func",
"(",
"k",
"*",
"KeyBindings",
")",
"Swap",
"(",
"i",
",",
"j",
"int",
")",
"{",
"k",
".",
"Bindings",
"[",
"i",
"]",
",",
"k",
".",
"Bindings",
"[",
"j",
"]",
"=",
"k",
".",
"Bindings",
"[",
"j",
"]",
",",
"k",
".",
"Bindings",
"[",
"i",
"]",
"\n",
"}"
] |
// Swaps the two KeyBindings at the given positions.
|
[
"Swaps",
"the",
"two",
"KeyBindings",
"at",
"the",
"given",
"positions",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/keys/keybinding.go#L61-L63
|
15,067 |
limetext/backend
|
keys/keybinding.go
|
DropLessEqualKeys
|
func (k *KeyBindings) DropLessEqualKeys(count int) {
for {
for i := 0; i < len(k.Bindings); {
if len(k.Bindings[i].Keys) <= count {
k.Bindings[i] = k.Bindings[len(k.Bindings)-1]
k.Bindings = k.Bindings[:len(k.Bindings)-1]
} else {
i++
}
}
sort.Sort(k)
if k.parent == nil {
break
}
k = k.parent.KeyBindings()
}
}
|
go
|
func (k *KeyBindings) DropLessEqualKeys(count int) {
for {
for i := 0; i < len(k.Bindings); {
if len(k.Bindings[i].Keys) <= count {
k.Bindings[i] = k.Bindings[len(k.Bindings)-1]
k.Bindings = k.Bindings[:len(k.Bindings)-1]
} else {
i++
}
}
sort.Sort(k)
if k.parent == nil {
break
}
k = k.parent.KeyBindings()
}
}
|
[
"func",
"(",
"k",
"*",
"KeyBindings",
")",
"DropLessEqualKeys",
"(",
"count",
"int",
")",
"{",
"for",
"{",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"len",
"(",
"k",
".",
"Bindings",
")",
";",
"{",
"if",
"len",
"(",
"k",
".",
"Bindings",
"[",
"i",
"]",
".",
"Keys",
")",
"<=",
"count",
"{",
"k",
".",
"Bindings",
"[",
"i",
"]",
"=",
"k",
".",
"Bindings",
"[",
"len",
"(",
"k",
".",
"Bindings",
")",
"-",
"1",
"]",
"\n",
"k",
".",
"Bindings",
"=",
"k",
".",
"Bindings",
"[",
":",
"len",
"(",
"k",
".",
"Bindings",
")",
"-",
"1",
"]",
"\n",
"}",
"else",
"{",
"i",
"++",
"\n",
"}",
"\n",
"}",
"\n",
"sort",
".",
"Sort",
"(",
"k",
")",
"\n",
"if",
"k",
".",
"parent",
"==",
"nil",
"{",
"break",
"\n",
"}",
"\n",
"k",
"=",
"k",
".",
"parent",
".",
"KeyBindings",
"(",
")",
"\n",
"}",
"\n",
"}"
] |
// Drops all KeyBindings that are a sequence of key presses less or equal
// to the given number.
|
[
"Drops",
"all",
"KeyBindings",
"that",
"are",
"a",
"sequence",
"of",
"key",
"presses",
"less",
"or",
"equal",
"to",
"the",
"given",
"number",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/keys/keybinding.go#L67-L83
|
15,068 |
limetext/backend
|
keys/keybinding.go
|
Filter
|
func (k *KeyBindings) Filter(kp KeyPress) (ret KeyBindings) {
p := Prof.Enter("key.filter")
defer p.Exit()
kp.fix()
k.DropLessEqualKeys(k.seqIndex)
ret.seqIndex = k.seqIndex + 1
ki := kp.Index()
k.filter(ki, &ret)
if kp.IsCharacter() {
k.filter(int(Any), &ret)
}
return
}
|
go
|
func (k *KeyBindings) Filter(kp KeyPress) (ret KeyBindings) {
p := Prof.Enter("key.filter")
defer p.Exit()
kp.fix()
k.DropLessEqualKeys(k.seqIndex)
ret.seqIndex = k.seqIndex + 1
ki := kp.Index()
k.filter(ki, &ret)
if kp.IsCharacter() {
k.filter(int(Any), &ret)
}
return
}
|
[
"func",
"(",
"k",
"*",
"KeyBindings",
")",
"Filter",
"(",
"kp",
"KeyPress",
")",
"(",
"ret",
"KeyBindings",
")",
"{",
"p",
":=",
"Prof",
".",
"Enter",
"(",
"\"",
"\"",
")",
"\n",
"defer",
"p",
".",
"Exit",
"(",
")",
"\n\n",
"kp",
".",
"fix",
"(",
")",
"\n",
"k",
".",
"DropLessEqualKeys",
"(",
"k",
".",
"seqIndex",
")",
"\n",
"ret",
".",
"seqIndex",
"=",
"k",
".",
"seqIndex",
"+",
"1",
"\n",
"ki",
":=",
"kp",
".",
"Index",
"(",
")",
"\n\n",
"k",
".",
"filter",
"(",
"ki",
",",
"&",
"ret",
")",
"\n\n",
"if",
"kp",
".",
"IsCharacter",
"(",
")",
"{",
"k",
".",
"filter",
"(",
"int",
"(",
"Any",
")",
",",
"&",
"ret",
")",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] |
// Filters the KeyBindings, returning a new KeyBindings object containing
// a subset of matches for the given key press.
|
[
"Filters",
"the",
"KeyBindings",
"returning",
"a",
"new",
"KeyBindings",
"object",
"containing",
"a",
"subset",
"of",
"matches",
"for",
"the",
"given",
"key",
"press",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/keys/keybinding.go#L127-L142
|
15,069 |
limetext/backend
|
keys/keybinding.go
|
Action
|
func (k *KeyBindings) Action(qc func(key string, operator Op, operand interface{}, match_all bool) bool) (kb *KeyBinding) {
p := Prof.Enter("key.action")
defer p.Exit()
for {
for i := range k.Bindings {
if len(k.Bindings[i].Keys) > k.seqIndex {
// This key binding is of a key sequence longer than what is currently
// probed for. For example, the binding is for the sequence ['a','b','c'], but
// the user has only pressed ['a','b'] so far.
continue
}
for _, c := range k.Bindings[i].Context {
if !qc(c.Key, c.Operator, c.Operand, c.MatchAll) {
goto skip
}
}
if kb == nil || kb.priority < k.Bindings[i].priority {
kb = k.Bindings[i]
}
skip:
}
if kb != nil || k.parent == nil {
break
}
k = k.parent.KeyBindings()
}
return
}
|
go
|
func (k *KeyBindings) Action(qc func(key string, operator Op, operand interface{}, match_all bool) bool) (kb *KeyBinding) {
p := Prof.Enter("key.action")
defer p.Exit()
for {
for i := range k.Bindings {
if len(k.Bindings[i].Keys) > k.seqIndex {
// This key binding is of a key sequence longer than what is currently
// probed for. For example, the binding is for the sequence ['a','b','c'], but
// the user has only pressed ['a','b'] so far.
continue
}
for _, c := range k.Bindings[i].Context {
if !qc(c.Key, c.Operator, c.Operand, c.MatchAll) {
goto skip
}
}
if kb == nil || kb.priority < k.Bindings[i].priority {
kb = k.Bindings[i]
}
skip:
}
if kb != nil || k.parent == nil {
break
}
k = k.parent.KeyBindings()
}
return
}
|
[
"func",
"(",
"k",
"*",
"KeyBindings",
")",
"Action",
"(",
"qc",
"func",
"(",
"key",
"string",
",",
"operator",
"Op",
",",
"operand",
"interface",
"{",
"}",
",",
"match_all",
"bool",
")",
"bool",
")",
"(",
"kb",
"*",
"KeyBinding",
")",
"{",
"p",
":=",
"Prof",
".",
"Enter",
"(",
"\"",
"\"",
")",
"\n",
"defer",
"p",
".",
"Exit",
"(",
")",
"\n\n",
"for",
"{",
"for",
"i",
":=",
"range",
"k",
".",
"Bindings",
"{",
"if",
"len",
"(",
"k",
".",
"Bindings",
"[",
"i",
"]",
".",
"Keys",
")",
">",
"k",
".",
"seqIndex",
"{",
"// This key binding is of a key sequence longer than what is currently",
"// probed for. For example, the binding is for the sequence ['a','b','c'], but",
"// the user has only pressed ['a','b'] so far.",
"continue",
"\n",
"}",
"\n",
"for",
"_",
",",
"c",
":=",
"range",
"k",
".",
"Bindings",
"[",
"i",
"]",
".",
"Context",
"{",
"if",
"!",
"qc",
"(",
"c",
".",
"Key",
",",
"c",
".",
"Operator",
",",
"c",
".",
"Operand",
",",
"c",
".",
"MatchAll",
")",
"{",
"goto",
"skip",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"kb",
"==",
"nil",
"||",
"kb",
".",
"priority",
"<",
"k",
".",
"Bindings",
"[",
"i",
"]",
".",
"priority",
"{",
"kb",
"=",
"k",
".",
"Bindings",
"[",
"i",
"]",
"\n",
"}",
"\n",
"skip",
":",
"}",
"\n",
"if",
"kb",
"!=",
"nil",
"||",
"k",
".",
"parent",
"==",
"nil",
"{",
"break",
"\n",
"}",
"\n",
"k",
"=",
"k",
".",
"parent",
".",
"KeyBindings",
"(",
")",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] |
// Tries to resolve all the current KeyBindings in k to a single
// action. If any action is appropriate as determined by context,
// the return value will be the specific KeyBinding that is possible
// to execute now, otherwise it is nil.
|
[
"Tries",
"to",
"resolve",
"all",
"the",
"current",
"KeyBindings",
"in",
"k",
"to",
"a",
"single",
"action",
".",
"If",
"any",
"action",
"is",
"appropriate",
"as",
"determined",
"by",
"context",
"the",
"return",
"value",
"will",
"be",
"the",
"specific",
"KeyBinding",
"that",
"is",
"possible",
"to",
"execute",
"now",
"otherwise",
"it",
"is",
"nil",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/keys/keybinding.go#L148-L176
|
15,070 |
limetext/backend
|
keys/keypress.go
|
IsCharacter
|
func (k KeyPress) IsCharacter() bool {
return unicode.IsPrint(rune(k.Key)) && !k.Super && !k.Ctrl
}
|
go
|
func (k KeyPress) IsCharacter() bool {
return unicode.IsPrint(rune(k.Key)) && !k.Super && !k.Ctrl
}
|
[
"func",
"(",
"k",
"KeyPress",
")",
"IsCharacter",
"(",
")",
"bool",
"{",
"return",
"unicode",
".",
"IsPrint",
"(",
"rune",
"(",
"k",
".",
"Key",
")",
")",
"&&",
"!",
"k",
".",
"Super",
"&&",
"!",
"k",
".",
"Ctrl",
"\n",
"}"
] |
// Returns whether this KeyPress is a print character or not.
|
[
"Returns",
"whether",
"this",
"KeyPress",
"is",
"a",
"print",
"character",
"or",
"not",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/keys/keypress.go#L45-L47
|
15,071 |
limetext/backend
|
keys/keypress.go
|
fix
|
func (k *KeyPress) fix() {
lower := Key(unicode.ToLower(rune(k.Key)))
if lower != k.Key {
k.Shift = true
k.Key = lower
}
}
|
go
|
func (k *KeyPress) fix() {
lower := Key(unicode.ToLower(rune(k.Key)))
if lower != k.Key {
k.Shift = true
k.Key = lower
}
}
|
[
"func",
"(",
"k",
"*",
"KeyPress",
")",
"fix",
"(",
")",
"{",
"lower",
":=",
"Key",
"(",
"unicode",
".",
"ToLower",
"(",
"rune",
"(",
"k",
".",
"Key",
")",
")",
")",
"\n",
"if",
"lower",
"!=",
"k",
".",
"Key",
"{",
"k",
".",
"Shift",
"=",
"true",
"\n",
"k",
".",
"Key",
"=",
"lower",
"\n",
"}",
"\n",
"}"
] |
// Modifies the KeyPress so that it's Key is a unicode lower case
// rune and if it was in uppercase before this modification, the
// "Shift" modifier is also enabled.
|
[
"Modifies",
"the",
"KeyPress",
"so",
"that",
"it",
"s",
"Key",
"is",
"a",
"unicode",
"lower",
"case",
"rune",
"and",
"if",
"it",
"was",
"in",
"uppercase",
"before",
"this",
"modification",
"the",
"Shift",
"modifier",
"is",
"also",
"enabled",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/keys/keypress.go#L52-L58
|
15,072 |
limetext/backend
|
packages/json.go
|
LoadJSON
|
func LoadJSON(path string, marshal json.Unmarshaler) error {
j := NewJSON(path, marshal)
watch(j)
j.Load()
return j.err
}
|
go
|
func LoadJSON(path string, marshal json.Unmarshaler) error {
j := NewJSON(path, marshal)
watch(j)
j.Load()
return j.err
}
|
[
"func",
"LoadJSON",
"(",
"path",
"string",
",",
"marshal",
"json",
".",
"Unmarshaler",
")",
"error",
"{",
"j",
":=",
"NewJSON",
"(",
"path",
",",
"marshal",
")",
"\n",
"watch",
"(",
"j",
")",
"\n",
"j",
".",
"Load",
"(",
")",
"\n",
"return",
"j",
".",
"err",
"\n",
"}"
] |
// Won't return the json type itself just watch & load
|
[
"Won",
"t",
"return",
"the",
"json",
"type",
"itself",
"just",
"watch",
"&",
"load"
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/packages/json.go#L28-L33
|
15,073 |
limetext/backend
|
view.go
|
reparse
|
func (v *View) reparse(forced bool) {
if v.isClosed() {
// No point in issuing a re-parse if the view has been closed
return
}
if len(v.reparseChan) < cap(v.reparseChan) || forced {
v.reparseChan <- parseReq{forced}
}
}
|
go
|
func (v *View) reparse(forced bool) {
if v.isClosed() {
// No point in issuing a re-parse if the view has been closed
return
}
if len(v.reparseChan) < cap(v.reparseChan) || forced {
v.reparseChan <- parseReq{forced}
}
}
|
[
"func",
"(",
"v",
"*",
"View",
")",
"reparse",
"(",
"forced",
"bool",
")",
"{",
"if",
"v",
".",
"isClosed",
"(",
")",
"{",
"// No point in issuing a re-parse if the view has been closed",
"return",
"\n",
"}",
"\n",
"if",
"len",
"(",
"v",
".",
"reparseChan",
")",
"<",
"cap",
"(",
"v",
".",
"reparseChan",
")",
"||",
"forced",
"{",
"v",
".",
"reparseChan",
"<-",
"parseReq",
"{",
"forced",
"}",
"\n",
"}",
"\n",
"}"
] |
// Send a reparse request via the reparse channel.
// If "forced" is set to true, then a reparse will be made
// even if the Buffer appears to not have changed.
//
// The actual parsing is done in a separate go-routine, for which the
// "lime.syntax.updated" setting will be set once it has finished.
//
// Note that it's presumed that the function calling this function
// has locked the view!
|
[
"Send",
"a",
"reparse",
"request",
"via",
"the",
"reparse",
"channel",
".",
"If",
"forced",
"is",
"set",
"to",
"true",
"then",
"a",
"reparse",
"will",
"be",
"made",
"even",
"if",
"the",
"Buffer",
"appears",
"to",
"not",
"have",
"changed",
".",
"The",
"actual",
"parsing",
"is",
"done",
"in",
"a",
"separate",
"go",
"-",
"routine",
"for",
"which",
"the",
"lime",
".",
"syntax",
".",
"updated",
"setting",
"will",
"be",
"set",
"once",
"it",
"has",
"finished",
".",
"Note",
"that",
"it",
"s",
"presumed",
"that",
"the",
"function",
"calling",
"this",
"function",
"has",
"locked",
"the",
"view!"
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/view.go#L236-L244
|
15,074 |
limetext/backend
|
view.go
|
Erase
|
func (v *View) Erase(edit *Edit, r text.Region) {
edit.composite.AddExec(text.NewEraseAction(v.buffer, r))
}
|
go
|
func (v *View) Erase(edit *Edit, r text.Region) {
edit.composite.AddExec(text.NewEraseAction(v.buffer, r))
}
|
[
"func",
"(",
"v",
"*",
"View",
")",
"Erase",
"(",
"edit",
"*",
"Edit",
",",
"r",
"text",
".",
"Region",
")",
"{",
"edit",
".",
"composite",
".",
"AddExec",
"(",
"text",
".",
"NewEraseAction",
"(",
"v",
".",
"buffer",
",",
"r",
")",
")",
"\n",
"}"
] |
// Adds an Erase action of the given Region to the provided Edit object.
|
[
"Adds",
"an",
"Erase",
"action",
"of",
"the",
"given",
"Region",
"to",
"the",
"provided",
"Edit",
"object",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/view.go#L378-L380
|
15,075 |
limetext/backend
|
view.go
|
Replace
|
func (v *View) Replace(edit *Edit, r text.Region, value string) {
edit.composite.AddExec(text.NewReplaceAction(v.buffer, r, value))
}
|
go
|
func (v *View) Replace(edit *Edit, r text.Region, value string) {
edit.composite.AddExec(text.NewReplaceAction(v.buffer, r, value))
}
|
[
"func",
"(",
"v",
"*",
"View",
")",
"Replace",
"(",
"edit",
"*",
"Edit",
",",
"r",
"text",
".",
"Region",
",",
"value",
"string",
")",
"{",
"edit",
".",
"composite",
".",
"AddExec",
"(",
"text",
".",
"NewReplaceAction",
"(",
"v",
".",
"buffer",
",",
"r",
",",
"value",
")",
")",
"\n",
"}"
] |
// Adds a Replace action of the given Region to the provided Edit object.
|
[
"Adds",
"a",
"Replace",
"action",
"of",
"the",
"given",
"Region",
"to",
"the",
"provided",
"Edit",
"object",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/view.go#L383-L385
|
15,076 |
limetext/backend
|
view.go
|
EndEdit
|
func (v *View) EndEdit(edit *Edit) {
if edit.invalid {
// This happens when nesting Edits and the child Edit ends after the parent edit.
log.Fine("This edit has already been invalidated: %v, %v", edit, v.editstack)
return
}
// Find the position of this Edit object in this View's Edit stack.
// If plugins, commands, etc are well-behaved the ended edit should be
// last in the stack, but shit happens and we cannot count on this being the case.
i := len(v.editstack) - 1
for i := len(v.editstack) - 1; i >= 0; i-- {
if v.editstack[i] == edit {
break
}
}
if i == -1 {
// TODO(.): Under what instances does this happen again?
log.Error("This edit isn't even in the stack... where did it come from? %v, %v", edit, v.editstack)
return
}
var selection_modified bool
if l := len(v.editstack) - 1; i != l {
// TODO(.): See TODO in BeginEdit
log.Error("This edit wasn't last in the stack... %d != %d: %v, %v", i, l, edit, v.editstack)
}
// Invalidate all Edits "below" and including this Edit.
for j := len(v.editstack) - 1; j >= i; j-- {
current_edit := v.editstack[j]
current_edit.invalid = true
sel_same := reflect.DeepEqual(*v.Sel(), current_edit.savedSel)
buf_same := v.ChangeCount() == current_edit.savedCount
eq := (sel_same && buf_same && current_edit.composite.Len() == 0)
if !eq && !sel_same {
selection_modified = true
}
if v.IsScratch() || current_edit.bypassUndo || eq {
continue
}
switch {
case i == 0:
// Well-behaved, no nested edits!
fallthrough
case j != i:
// BOO! Someone began another Edit without finishing the first one.
// In this instance, the parent Edit ended before the child.
// TODO(.): What would be the correct way to handle this?
v.undoStack.Add(edit)
default:
// BOO! Also poorly-behaved. This Edit object began after the parent began,
// but was finished before the parent finished.
//
// Add it as a child of the parent Edit so that undoing the parent
// will undo this edit as well.
v.editstack[i-1].composite.Add(current_edit)
}
}
// Pop this Edit and all the children off the Edit stack.
v.editstack = v.editstack[:i]
if selection_modified {
OnSelectionModified.Call(v)
}
}
|
go
|
func (v *View) EndEdit(edit *Edit) {
if edit.invalid {
// This happens when nesting Edits and the child Edit ends after the parent edit.
log.Fine("This edit has already been invalidated: %v, %v", edit, v.editstack)
return
}
// Find the position of this Edit object in this View's Edit stack.
// If plugins, commands, etc are well-behaved the ended edit should be
// last in the stack, but shit happens and we cannot count on this being the case.
i := len(v.editstack) - 1
for i := len(v.editstack) - 1; i >= 0; i-- {
if v.editstack[i] == edit {
break
}
}
if i == -1 {
// TODO(.): Under what instances does this happen again?
log.Error("This edit isn't even in the stack... where did it come from? %v, %v", edit, v.editstack)
return
}
var selection_modified bool
if l := len(v.editstack) - 1; i != l {
// TODO(.): See TODO in BeginEdit
log.Error("This edit wasn't last in the stack... %d != %d: %v, %v", i, l, edit, v.editstack)
}
// Invalidate all Edits "below" and including this Edit.
for j := len(v.editstack) - 1; j >= i; j-- {
current_edit := v.editstack[j]
current_edit.invalid = true
sel_same := reflect.DeepEqual(*v.Sel(), current_edit.savedSel)
buf_same := v.ChangeCount() == current_edit.savedCount
eq := (sel_same && buf_same && current_edit.composite.Len() == 0)
if !eq && !sel_same {
selection_modified = true
}
if v.IsScratch() || current_edit.bypassUndo || eq {
continue
}
switch {
case i == 0:
// Well-behaved, no nested edits!
fallthrough
case j != i:
// BOO! Someone began another Edit without finishing the first one.
// In this instance, the parent Edit ended before the child.
// TODO(.): What would be the correct way to handle this?
v.undoStack.Add(edit)
default:
// BOO! Also poorly-behaved. This Edit object began after the parent began,
// but was finished before the parent finished.
//
// Add it as a child of the parent Edit so that undoing the parent
// will undo this edit as well.
v.editstack[i-1].composite.Add(current_edit)
}
}
// Pop this Edit and all the children off the Edit stack.
v.editstack = v.editstack[:i]
if selection_modified {
OnSelectionModified.Call(v)
}
}
|
[
"func",
"(",
"v",
"*",
"View",
")",
"EndEdit",
"(",
"edit",
"*",
"Edit",
")",
"{",
"if",
"edit",
".",
"invalid",
"{",
"// This happens when nesting Edits and the child Edit ends after the parent edit.",
"log",
".",
"Fine",
"(",
"\"",
"\"",
",",
"edit",
",",
"v",
".",
"editstack",
")",
"\n",
"return",
"\n",
"}",
"\n\n",
"// Find the position of this Edit object in this View's Edit stack.",
"// If plugins, commands, etc are well-behaved the ended edit should be",
"// last in the stack, but shit happens and we cannot count on this being the case.",
"i",
":=",
"len",
"(",
"v",
".",
"editstack",
")",
"-",
"1",
"\n",
"for",
"i",
":=",
"len",
"(",
"v",
".",
"editstack",
")",
"-",
"1",
";",
"i",
">=",
"0",
";",
"i",
"--",
"{",
"if",
"v",
".",
"editstack",
"[",
"i",
"]",
"==",
"edit",
"{",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"i",
"==",
"-",
"1",
"{",
"// TODO(.): Under what instances does this happen again?",
"log",
".",
"Error",
"(",
"\"",
"\"",
",",
"edit",
",",
"v",
".",
"editstack",
")",
"\n",
"return",
"\n",
"}",
"\n\n",
"var",
"selection_modified",
"bool",
"\n\n",
"if",
"l",
":=",
"len",
"(",
"v",
".",
"editstack",
")",
"-",
"1",
";",
"i",
"!=",
"l",
"{",
"// TODO(.): See TODO in BeginEdit",
"log",
".",
"Error",
"(",
"\"",
"\"",
",",
"i",
",",
"l",
",",
"edit",
",",
"v",
".",
"editstack",
")",
"\n",
"}",
"\n\n",
"// Invalidate all Edits \"below\" and including this Edit.",
"for",
"j",
":=",
"len",
"(",
"v",
".",
"editstack",
")",
"-",
"1",
";",
"j",
">=",
"i",
";",
"j",
"--",
"{",
"current_edit",
":=",
"v",
".",
"editstack",
"[",
"j",
"]",
"\n",
"current_edit",
".",
"invalid",
"=",
"true",
"\n",
"sel_same",
":=",
"reflect",
".",
"DeepEqual",
"(",
"*",
"v",
".",
"Sel",
"(",
")",
",",
"current_edit",
".",
"savedSel",
")",
"\n",
"buf_same",
":=",
"v",
".",
"ChangeCount",
"(",
")",
"==",
"current_edit",
".",
"savedCount",
"\n",
"eq",
":=",
"(",
"sel_same",
"&&",
"buf_same",
"&&",
"current_edit",
".",
"composite",
".",
"Len",
"(",
")",
"==",
"0",
")",
"\n",
"if",
"!",
"eq",
"&&",
"!",
"sel_same",
"{",
"selection_modified",
"=",
"true",
"\n",
"}",
"\n",
"if",
"v",
".",
"IsScratch",
"(",
")",
"||",
"current_edit",
".",
"bypassUndo",
"||",
"eq",
"{",
"continue",
"\n",
"}",
"\n",
"switch",
"{",
"case",
"i",
"==",
"0",
":",
"// Well-behaved, no nested edits!",
"fallthrough",
"\n",
"case",
"j",
"!=",
"i",
":",
"// BOO! Someone began another Edit without finishing the first one.",
"// In this instance, the parent Edit ended before the child.",
"// TODO(.): What would be the correct way to handle this?",
"v",
".",
"undoStack",
".",
"Add",
"(",
"edit",
")",
"\n",
"default",
":",
"// BOO! Also poorly-behaved. This Edit object began after the parent began,",
"// but was finished before the parent finished.",
"//",
"// Add it as a child of the parent Edit so that undoing the parent",
"// will undo this edit as well.",
"v",
".",
"editstack",
"[",
"i",
"-",
"1",
"]",
".",
"composite",
".",
"Add",
"(",
"current_edit",
")",
"\n",
"}",
"\n",
"}",
"\n",
"// Pop this Edit and all the children off the Edit stack.",
"v",
".",
"editstack",
"=",
"v",
".",
"editstack",
"[",
":",
"i",
"]",
"\n",
"if",
"selection_modified",
"{",
"OnSelectionModified",
".",
"Call",
"(",
"v",
")",
"\n",
"}",
"\n",
"}"
] |
// Ends the given Edit object.
|
[
"Ends",
"the",
"given",
"Edit",
"object",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/view.go#L401-L466
|
15,077 |
limetext/backend
|
view.go
|
IsDirty
|
func (v *View) IsDirty() bool {
if v.IsScratch() {
return false
}
lastSave := v.Settings().Int("lime.last_save_change_count", -1)
return v.ChangeCount() != lastSave
}
|
go
|
func (v *View) IsDirty() bool {
if v.IsScratch() {
return false
}
lastSave := v.Settings().Int("lime.last_save_change_count", -1)
return v.ChangeCount() != lastSave
}
|
[
"func",
"(",
"v",
"*",
"View",
")",
"IsDirty",
"(",
")",
"bool",
"{",
"if",
"v",
".",
"IsScratch",
"(",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"lastSave",
":=",
"v",
".",
"Settings",
"(",
")",
".",
"Int",
"(",
"\"",
"\"",
",",
"-",
"1",
")",
"\n",
"return",
"v",
".",
"ChangeCount",
"(",
")",
"!=",
"lastSave",
"\n",
"}"
] |
// Returns whether the underlying Buffer has any unsaved modifications.
// Note that Scratch buffers are never considered dirty.
|
[
"Returns",
"whether",
"the",
"underlying",
"Buffer",
"has",
"any",
"unsaved",
"modifications",
".",
"Note",
"that",
"Scratch",
"buffers",
"are",
"never",
"considered",
"dirty",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/view.go#L502-L508
|
15,078 |
limetext/backend
|
view.go
|
SaveAs
|
func (v *View) SaveAs(name string) (err error) {
log.Fine("SaveAs(%s)", name)
v.Settings().Set("lime.saving", true)
defer v.Settings().Erase("lime.saving")
OnPreSave.Call(v)
if atomic := v.Settings().Bool("atomic_save", true); v.FileName() == "" || !atomic {
if err := v.nonAtomicSave(name); err != nil {
return err
}
} else {
n, err := ioutil.TempDir(path.Dir(v.FileName()), "lime")
if err != nil {
return err
}
tmpf := path.Join(n, "tmp")
if err := v.nonAtomicSave(tmpf); err != nil {
return err
}
if err := os.Rename(tmpf, name); err != nil {
// When we want to save as a file in another directory
// we can't go with os.Rename so we need to force
// not atomic saving sometimes as 4th test in TestSaveAsOpenFile
if err := v.nonAtomicSave(name); err != nil {
return err
}
}
if err := os.RemoveAll(n); err != nil {
return err
}
}
ed := GetEditor()
if fn := v.FileName(); fn != name {
v.SetFileName(name)
if fn != "" {
ed.UnWatch(fn, v)
}
ed.Watch(name, v)
}
v.Settings().Set("lime.last_save_change_count", v.ChangeCount())
OnPostSave.Call(v)
return nil
}
|
go
|
func (v *View) SaveAs(name string) (err error) {
log.Fine("SaveAs(%s)", name)
v.Settings().Set("lime.saving", true)
defer v.Settings().Erase("lime.saving")
OnPreSave.Call(v)
if atomic := v.Settings().Bool("atomic_save", true); v.FileName() == "" || !atomic {
if err := v.nonAtomicSave(name); err != nil {
return err
}
} else {
n, err := ioutil.TempDir(path.Dir(v.FileName()), "lime")
if err != nil {
return err
}
tmpf := path.Join(n, "tmp")
if err := v.nonAtomicSave(tmpf); err != nil {
return err
}
if err := os.Rename(tmpf, name); err != nil {
// When we want to save as a file in another directory
// we can't go with os.Rename so we need to force
// not atomic saving sometimes as 4th test in TestSaveAsOpenFile
if err := v.nonAtomicSave(name); err != nil {
return err
}
}
if err := os.RemoveAll(n); err != nil {
return err
}
}
ed := GetEditor()
if fn := v.FileName(); fn != name {
v.SetFileName(name)
if fn != "" {
ed.UnWatch(fn, v)
}
ed.Watch(name, v)
}
v.Settings().Set("lime.last_save_change_count", v.ChangeCount())
OnPostSave.Call(v)
return nil
}
|
[
"func",
"(",
"v",
"*",
"View",
")",
"SaveAs",
"(",
"name",
"string",
")",
"(",
"err",
"error",
")",
"{",
"log",
".",
"Fine",
"(",
"\"",
"\"",
",",
"name",
")",
"\n",
"v",
".",
"Settings",
"(",
")",
".",
"Set",
"(",
"\"",
"\"",
",",
"true",
")",
"\n",
"defer",
"v",
".",
"Settings",
"(",
")",
".",
"Erase",
"(",
"\"",
"\"",
")",
"\n",
"OnPreSave",
".",
"Call",
"(",
"v",
")",
"\n",
"if",
"atomic",
":=",
"v",
".",
"Settings",
"(",
")",
".",
"Bool",
"(",
"\"",
"\"",
",",
"true",
")",
";",
"v",
".",
"FileName",
"(",
")",
"==",
"\"",
"\"",
"||",
"!",
"atomic",
"{",
"if",
"err",
":=",
"v",
".",
"nonAtomicSave",
"(",
"name",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"else",
"{",
"n",
",",
"err",
":=",
"ioutil",
".",
"TempDir",
"(",
"path",
".",
"Dir",
"(",
"v",
".",
"FileName",
"(",
")",
")",
",",
"\"",
"\"",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"tmpf",
":=",
"path",
".",
"Join",
"(",
"n",
",",
"\"",
"\"",
")",
"\n",
"if",
"err",
":=",
"v",
".",
"nonAtomicSave",
"(",
"tmpf",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"if",
"err",
":=",
"os",
".",
"Rename",
"(",
"tmpf",
",",
"name",
")",
";",
"err",
"!=",
"nil",
"{",
"// When we want to save as a file in another directory",
"// we can't go with os.Rename so we need to force",
"// not atomic saving sometimes as 4th test in TestSaveAsOpenFile",
"if",
"err",
":=",
"v",
".",
"nonAtomicSave",
"(",
"name",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"err",
":=",
"os",
".",
"RemoveAll",
"(",
"n",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"ed",
":=",
"GetEditor",
"(",
")",
"\n",
"if",
"fn",
":=",
"v",
".",
"FileName",
"(",
")",
";",
"fn",
"!=",
"name",
"{",
"v",
".",
"SetFileName",
"(",
"name",
")",
"\n",
"if",
"fn",
"!=",
"\"",
"\"",
"{",
"ed",
".",
"UnWatch",
"(",
"fn",
",",
"v",
")",
"\n",
"}",
"\n",
"ed",
".",
"Watch",
"(",
"name",
",",
"v",
")",
"\n",
"}",
"\n\n",
"v",
".",
"Settings",
"(",
")",
".",
"Set",
"(",
"\"",
"\"",
",",
"v",
".",
"ChangeCount",
"(",
")",
")",
"\n",
"OnPostSave",
".",
"Call",
"(",
"v",
")",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Saves the file to the specified filename
|
[
"Saves",
"the",
"file",
"to",
"the",
"specified",
"filename"
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/view.go#L537-L580
|
15,079 |
limetext/backend
|
view.go
|
AddRegions
|
func (v *View) AddRegions(key string, regions []text.Region, scope, icon string, flags render.ViewRegionFlags) {
vr := render.ViewRegions{Scope: scope, Icon: icon, Flags: flags}
vr.Regions.AddAll(regions)
v.lock.Lock()
defer v.lock.Unlock()
v.regions[key] = vr
}
|
go
|
func (v *View) AddRegions(key string, regions []text.Region, scope, icon string, flags render.ViewRegionFlags) {
vr := render.ViewRegions{Scope: scope, Icon: icon, Flags: flags}
vr.Regions.AddAll(regions)
v.lock.Lock()
defer v.lock.Unlock()
v.regions[key] = vr
}
|
[
"func",
"(",
"v",
"*",
"View",
")",
"AddRegions",
"(",
"key",
"string",
",",
"regions",
"[",
"]",
"text",
".",
"Region",
",",
"scope",
",",
"icon",
"string",
",",
"flags",
"render",
".",
"ViewRegionFlags",
")",
"{",
"vr",
":=",
"render",
".",
"ViewRegions",
"{",
"Scope",
":",
"scope",
",",
"Icon",
":",
"icon",
",",
"Flags",
":",
"flags",
"}",
"\n",
"vr",
".",
"Regions",
".",
"AddAll",
"(",
"regions",
")",
"\n\n",
"v",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"v",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"v",
".",
"regions",
"[",
"key",
"]",
"=",
"vr",
"\n",
"}"
] |
// AddRegions lets users mark text regions in a view with a scope name, gutter icon and ViewRegionflags
// which are then optionally used to alter the display of those regions.
//
// Typical uses would be to draw squiggly lines under misspelled words, show an icon in the gutter to
// indicate a breakpoint, keeping track of snippet or auto-completion fields, highlight code compilation
// warnings, etc.
//
// The regions will be automatically adjusted as appropriate when the underlying buffer is changed.
|
[
"AddRegions",
"lets",
"users",
"mark",
"text",
"regions",
"in",
"a",
"view",
"with",
"a",
"scope",
"name",
"gutter",
"icon",
"and",
"ViewRegionflags",
"which",
"are",
"then",
"optionally",
"used",
"to",
"alter",
"the",
"display",
"of",
"those",
"regions",
".",
"Typical",
"uses",
"would",
"be",
"to",
"draw",
"squiggly",
"lines",
"under",
"misspelled",
"words",
"show",
"an",
"icon",
"in",
"the",
"gutter",
"to",
"indicate",
"a",
"breakpoint",
"keeping",
"track",
"of",
"snippet",
"or",
"auto",
"-",
"completion",
"fields",
"highlight",
"code",
"compilation",
"warnings",
"etc",
".",
"The",
"regions",
"will",
"be",
"automatically",
"adjusted",
"as",
"appropriate",
"when",
"the",
"underlying",
"buffer",
"is",
"changed",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/view.go#L629-L636
|
15,080 |
limetext/backend
|
view.go
|
GetRegions
|
func (v *View) GetRegions(key string) (ret []text.Region) {
v.lock.Lock()
defer v.lock.Unlock()
vr := v.regions[key]
rs := vr.Regions.Regions()
ret = make([]text.Region, len(rs))
copy(ret, rs)
return
}
|
go
|
func (v *View) GetRegions(key string) (ret []text.Region) {
v.lock.Lock()
defer v.lock.Unlock()
vr := v.regions[key]
rs := vr.Regions.Regions()
ret = make([]text.Region, len(rs))
copy(ret, rs)
return
}
|
[
"func",
"(",
"v",
"*",
"View",
")",
"GetRegions",
"(",
"key",
"string",
")",
"(",
"ret",
"[",
"]",
"text",
".",
"Region",
")",
"{",
"v",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"v",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"vr",
":=",
"v",
".",
"regions",
"[",
"key",
"]",
"\n",
"rs",
":=",
"vr",
".",
"Regions",
".",
"Regions",
"(",
")",
"\n",
"ret",
"=",
"make",
"(",
"[",
"]",
"text",
".",
"Region",
",",
"len",
"(",
"rs",
")",
")",
"\n",
"copy",
"(",
"ret",
",",
"rs",
")",
"\n",
"return",
"\n",
"}"
] |
// Returns the Regions associated with the given key.
|
[
"Returns",
"the",
"Regions",
"associated",
"with",
"the",
"given",
"key",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/view.go#L639-L647
|
15,081 |
limetext/backend
|
view.go
|
EraseRegions
|
func (v *View) EraseRegions(key string) {
v.lock.Lock()
defer v.lock.Unlock()
delete(v.regions, key)
}
|
go
|
func (v *View) EraseRegions(key string) {
v.lock.Lock()
defer v.lock.Unlock()
delete(v.regions, key)
}
|
[
"func",
"(",
"v",
"*",
"View",
")",
"EraseRegions",
"(",
"key",
"string",
")",
"{",
"v",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"v",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"delete",
"(",
"v",
".",
"regions",
",",
"key",
")",
"\n",
"}"
] |
// Removes the Regions associated with the given key from the view.
|
[
"Removes",
"the",
"Regions",
"associated",
"with",
"the",
"given",
"key",
"from",
"the",
"view",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/view.go#L650-L654
|
15,082 |
limetext/backend
|
view.go
|
Close
|
func (v *View) Close() bool {
OnPreClose.Call(v)
if v.IsDirty() {
close_anyway := GetEditor().Frontend().OkCancelDialog("File has been modified since last save, close anyway?", "Close")
if !close_anyway {
return false
}
}
if n := v.FileName(); n != "" {
GetEditor().UnWatch(n, v)
}
// Call the event first while there's still access possible to the underlying
// buffer
OnClose.Call(v)
v.window.remove(v)
// Closing the reparseChan, and setting to nil will eventually clean up other resources
// when the parseThread exits
v.lock.Lock()
defer v.lock.Unlock()
close(v.reparseChan)
v.reparseChan = nil
return true
}
|
go
|
func (v *View) Close() bool {
OnPreClose.Call(v)
if v.IsDirty() {
close_anyway := GetEditor().Frontend().OkCancelDialog("File has been modified since last save, close anyway?", "Close")
if !close_anyway {
return false
}
}
if n := v.FileName(); n != "" {
GetEditor().UnWatch(n, v)
}
// Call the event first while there's still access possible to the underlying
// buffer
OnClose.Call(v)
v.window.remove(v)
// Closing the reparseChan, and setting to nil will eventually clean up other resources
// when the parseThread exits
v.lock.Lock()
defer v.lock.Unlock()
close(v.reparseChan)
v.reparseChan = nil
return true
}
|
[
"func",
"(",
"v",
"*",
"View",
")",
"Close",
"(",
")",
"bool",
"{",
"OnPreClose",
".",
"Call",
"(",
"v",
")",
"\n",
"if",
"v",
".",
"IsDirty",
"(",
")",
"{",
"close_anyway",
":=",
"GetEditor",
"(",
")",
".",
"Frontend",
"(",
")",
".",
"OkCancelDialog",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"if",
"!",
"close_anyway",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"n",
":=",
"v",
".",
"FileName",
"(",
")",
";",
"n",
"!=",
"\"",
"\"",
"{",
"GetEditor",
"(",
")",
".",
"UnWatch",
"(",
"n",
",",
"v",
")",
"\n",
"}",
"\n\n",
"// Call the event first while there's still access possible to the underlying",
"// buffer",
"OnClose",
".",
"Call",
"(",
"v",
")",
"\n\n",
"v",
".",
"window",
".",
"remove",
"(",
"v",
")",
"\n\n",
"// Closing the reparseChan, and setting to nil will eventually clean up other resources",
"// when the parseThread exits",
"v",
".",
"lock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"v",
".",
"lock",
".",
"Unlock",
"(",
")",
"\n",
"close",
"(",
"v",
".",
"reparseChan",
")",
"\n",
"v",
".",
"reparseChan",
"=",
"nil",
"\n\n",
"return",
"true",
"\n",
"}"
] |
// Initiate the "close" operation of this view.
// Returns "true" if the view was closed. Otherwise returns "false".
|
[
"Initiate",
"the",
"close",
"operation",
"of",
"this",
"view",
".",
"Returns",
"true",
"if",
"the",
"view",
"was",
"closed",
".",
"Otherwise",
"returns",
"false",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/view.go#L701-L727
|
15,083 |
limetext/backend
|
view.go
|
FindByClass
|
func (v *View) FindByClass(point int, forward bool, classes int) int {
i := -1
if forward {
i = 1
}
size := v.Size()
// Sublime doesn't consider initial point even if it matches.
for p := point + i; ; p += i {
if p <= 0 {
return 0
}
if p >= size {
return size
}
if v.Classify(p)&classes != 0 {
return p
}
}
}
|
go
|
func (v *View) FindByClass(point int, forward bool, classes int) int {
i := -1
if forward {
i = 1
}
size := v.Size()
// Sublime doesn't consider initial point even if it matches.
for p := point + i; ; p += i {
if p <= 0 {
return 0
}
if p >= size {
return size
}
if v.Classify(p)&classes != 0 {
return p
}
}
}
|
[
"func",
"(",
"v",
"*",
"View",
")",
"FindByClass",
"(",
"point",
"int",
",",
"forward",
"bool",
",",
"classes",
"int",
")",
"int",
"{",
"i",
":=",
"-",
"1",
"\n",
"if",
"forward",
"{",
"i",
"=",
"1",
"\n",
"}",
"\n",
"size",
":=",
"v",
".",
"Size",
"(",
")",
"\n",
"// Sublime doesn't consider initial point even if it matches.",
"for",
"p",
":=",
"point",
"+",
"i",
";",
";",
"p",
"+=",
"i",
"{",
"if",
"p",
"<=",
"0",
"{",
"return",
"0",
"\n",
"}",
"\n",
"if",
"p",
">=",
"size",
"{",
"return",
"size",
"\n",
"}",
"\n",
"if",
"v",
".",
"Classify",
"(",
"p",
")",
"&",
"classes",
"!=",
"0",
"{",
"return",
"p",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// Finds the next location after point that matches the given classes
// Searches backward if forward is false
|
[
"Finds",
"the",
"next",
"location",
"after",
"point",
"that",
"matches",
"the",
"given",
"classes",
"Searches",
"backward",
"if",
"forward",
"is",
"false"
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/view.go#L880-L898
|
15,084 |
limetext/backend
|
view.go
|
ExpandByClass
|
func (v *View) ExpandByClass(r text.Region, classes int) text.Region {
// Sublime doesn't consider the points the region starts on.
// If not already on edge of buffer, expand by 1 in both directions.
a := r.A
if a > 0 {
a -= 1
} else if a < 0 {
a = 0
}
b := r.B
size := v.Size()
if b < size {
b += 1
} else if b > size {
b = size
}
for ; a > 0 && (v.Classify(a)&classes == 0); a -= 1 {
}
for ; b < size && (v.Classify(b)&classes == 0); b += 1 {
}
return text.Region{a, b}
}
|
go
|
func (v *View) ExpandByClass(r text.Region, classes int) text.Region {
// Sublime doesn't consider the points the region starts on.
// If not already on edge of buffer, expand by 1 in both directions.
a := r.A
if a > 0 {
a -= 1
} else if a < 0 {
a = 0
}
b := r.B
size := v.Size()
if b < size {
b += 1
} else if b > size {
b = size
}
for ; a > 0 && (v.Classify(a)&classes == 0); a -= 1 {
}
for ; b < size && (v.Classify(b)&classes == 0); b += 1 {
}
return text.Region{a, b}
}
|
[
"func",
"(",
"v",
"*",
"View",
")",
"ExpandByClass",
"(",
"r",
"text",
".",
"Region",
",",
"classes",
"int",
")",
"text",
".",
"Region",
"{",
"// Sublime doesn't consider the points the region starts on.",
"// If not already on edge of buffer, expand by 1 in both directions.",
"a",
":=",
"r",
".",
"A",
"\n",
"if",
"a",
">",
"0",
"{",
"a",
"-=",
"1",
"\n",
"}",
"else",
"if",
"a",
"<",
"0",
"{",
"a",
"=",
"0",
"\n",
"}",
"\n\n",
"b",
":=",
"r",
".",
"B",
"\n",
"size",
":=",
"v",
".",
"Size",
"(",
")",
"\n",
"if",
"b",
"<",
"size",
"{",
"b",
"+=",
"1",
"\n",
"}",
"else",
"if",
"b",
">",
"size",
"{",
"b",
"=",
"size",
"\n",
"}",
"\n\n",
"for",
";",
"a",
">",
"0",
"&&",
"(",
"v",
".",
"Classify",
"(",
"a",
")",
"&",
"classes",
"==",
"0",
")",
";",
"a",
"-=",
"1",
"{",
"}",
"\n",
"for",
";",
"b",
"<",
"size",
"&&",
"(",
"v",
".",
"Classify",
"(",
"b",
")",
"&",
"classes",
"==",
"0",
")",
";",
"b",
"+=",
"1",
"{",
"}",
"\n",
"return",
"text",
".",
"Region",
"{",
"a",
",",
"b",
"}",
"\n",
"}"
] |
// Expands the selection until the point on each side matches the given classes
|
[
"Expands",
"the",
"selection",
"until",
"the",
"point",
"on",
"each",
"side",
"matches",
"the",
"given",
"classes"
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/view.go#L901-L924
|
15,085 |
limetext/backend
|
commandhandler.go
|
init
|
func (ch *commandHandler) init(cmd interface{}, args Args) error {
if in, ok := cmd.(CustomInit); ok {
return in.Init(args)
}
v := reflect.ValueOf(cmd).Elem()
t := v.Type()
for i := 0; i < v.NumField(); i++ {
ft := t.Field(i)
f := v.Field(i)
if ft.Anonymous || !f.CanSet() {
continue
}
key := util.PascalCaseToSnakeCase(ft.Name)
fv, ok := args[key]
if !ok {
fv = reflect.Zero(ft.Type).Interface()
if def, ok := cmd.(CustomDefault); ok {
if val := def.Default(key); val != nil {
fv = val
}
}
}
if f.CanAddr() {
if f2, ok := f.Addr().Interface().(CustomSet); ok {
if err := f2.Set(fv); err != nil {
return err
}
continue
}
}
rv := reflect.ValueOf(fv)
rvtype := rv.Type()
ftype := f.Type()
if !rvtype.AssignableTo(ftype) {
if rvtype.ConvertibleTo(ftype) {
rv = rv.Convert(ftype)
} else {
return fmt.Errorf("Command %v arg %v of type %v not assignable or convertable to %v of type %v", t, rv, rvtype, ft.Name, ftype)
}
}
f.Set(rv)
}
return nil
}
|
go
|
func (ch *commandHandler) init(cmd interface{}, args Args) error {
if in, ok := cmd.(CustomInit); ok {
return in.Init(args)
}
v := reflect.ValueOf(cmd).Elem()
t := v.Type()
for i := 0; i < v.NumField(); i++ {
ft := t.Field(i)
f := v.Field(i)
if ft.Anonymous || !f.CanSet() {
continue
}
key := util.PascalCaseToSnakeCase(ft.Name)
fv, ok := args[key]
if !ok {
fv = reflect.Zero(ft.Type).Interface()
if def, ok := cmd.(CustomDefault); ok {
if val := def.Default(key); val != nil {
fv = val
}
}
}
if f.CanAddr() {
if f2, ok := f.Addr().Interface().(CustomSet); ok {
if err := f2.Set(fv); err != nil {
return err
}
continue
}
}
rv := reflect.ValueOf(fv)
rvtype := rv.Type()
ftype := f.Type()
if !rvtype.AssignableTo(ftype) {
if rvtype.ConvertibleTo(ftype) {
rv = rv.Convert(ftype)
} else {
return fmt.Errorf("Command %v arg %v of type %v not assignable or convertable to %v of type %v", t, rv, rvtype, ft.Name, ftype)
}
}
f.Set(rv)
}
return nil
}
|
[
"func",
"(",
"ch",
"*",
"commandHandler",
")",
"init",
"(",
"cmd",
"interface",
"{",
"}",
",",
"args",
"Args",
")",
"error",
"{",
"if",
"in",
",",
"ok",
":=",
"cmd",
".",
"(",
"CustomInit",
")",
";",
"ok",
"{",
"return",
"in",
".",
"Init",
"(",
"args",
")",
"\n",
"}",
"\n",
"v",
":=",
"reflect",
".",
"ValueOf",
"(",
"cmd",
")",
".",
"Elem",
"(",
")",
"\n",
"t",
":=",
"v",
".",
"Type",
"(",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"v",
".",
"NumField",
"(",
")",
";",
"i",
"++",
"{",
"ft",
":=",
"t",
".",
"Field",
"(",
"i",
")",
"\n",
"f",
":=",
"v",
".",
"Field",
"(",
"i",
")",
"\n",
"if",
"ft",
".",
"Anonymous",
"||",
"!",
"f",
".",
"CanSet",
"(",
")",
"{",
"continue",
"\n",
"}",
"\n",
"key",
":=",
"util",
".",
"PascalCaseToSnakeCase",
"(",
"ft",
".",
"Name",
")",
"\n",
"fv",
",",
"ok",
":=",
"args",
"[",
"key",
"]",
"\n",
"if",
"!",
"ok",
"{",
"fv",
"=",
"reflect",
".",
"Zero",
"(",
"ft",
".",
"Type",
")",
".",
"Interface",
"(",
")",
"\n",
"if",
"def",
",",
"ok",
":=",
"cmd",
".",
"(",
"CustomDefault",
")",
";",
"ok",
"{",
"if",
"val",
":=",
"def",
".",
"Default",
"(",
"key",
")",
";",
"val",
"!=",
"nil",
"{",
"fv",
"=",
"val",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"f",
".",
"CanAddr",
"(",
")",
"{",
"if",
"f2",
",",
"ok",
":=",
"f",
".",
"Addr",
"(",
")",
".",
"Interface",
"(",
")",
".",
"(",
"CustomSet",
")",
";",
"ok",
"{",
"if",
"err",
":=",
"f2",
".",
"Set",
"(",
"fv",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"continue",
"\n",
"}",
"\n",
"}",
"\n",
"rv",
":=",
"reflect",
".",
"ValueOf",
"(",
"fv",
")",
"\n",
"rvtype",
":=",
"rv",
".",
"Type",
"(",
")",
"\n",
"ftype",
":=",
"f",
".",
"Type",
"(",
")",
"\n",
"if",
"!",
"rvtype",
".",
"AssignableTo",
"(",
"ftype",
")",
"{",
"if",
"rvtype",
".",
"ConvertibleTo",
"(",
"ftype",
")",
"{",
"rv",
"=",
"rv",
".",
"Convert",
"(",
"ftype",
")",
"\n",
"}",
"else",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"t",
",",
"rv",
",",
"rvtype",
",",
"ft",
".",
"Name",
",",
"ftype",
")",
"\n",
"}",
"\n",
"}",
"\n",
"f",
".",
"Set",
"(",
"rv",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// If the cmd implements the CustomInit interface, its Init function
// is called, otherwise the fields of the cmd's underlying struct type
// will be enumerated and match against the dictionary keys in args,
// or if the key isn't provided in args, the Zero value will be used.
|
[
"If",
"the",
"cmd",
"implements",
"the",
"CustomInit",
"interface",
"its",
"Init",
"function",
"is",
"called",
"otherwise",
"the",
"fields",
"of",
"the",
"cmd",
"s",
"underlying",
"struct",
"type",
"will",
"be",
"enumerated",
"and",
"match",
"against",
"the",
"dictionary",
"keys",
"in",
"args",
"or",
"if",
"the",
"key",
"isn",
"t",
"provided",
"in",
"args",
"the",
"Zero",
"value",
"will",
"be",
"used",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/commandhandler.go#L49-L92
|
15,086 |
limetext/backend
|
window.go
|
Close
|
func (w *Window) Close() bool {
if !w.CloseAllViews() {
return false
}
GetEditor().remove(w)
return true
}
|
go
|
func (w *Window) Close() bool {
if !w.CloseAllViews() {
return false
}
GetEditor().remove(w)
return true
}
|
[
"func",
"(",
"w",
"*",
"Window",
")",
"Close",
"(",
")",
"bool",
"{",
"if",
"!",
"w",
".",
"CloseAllViews",
"(",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"GetEditor",
"(",
")",
".",
"remove",
"(",
"w",
")",
"\n\n",
"return",
"true",
"\n",
"}"
] |
// Closes the Window and all its Views.
// Returns "true" if the Window closed successfully. Otherwise returns "false".
|
[
"Closes",
"the",
"Window",
"and",
"all",
"its",
"Views",
".",
"Returns",
"true",
"if",
"the",
"Window",
"closed",
"successfully",
".",
"Otherwise",
"returns",
"false",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/window.go#L116-L123
|
15,087 |
limetext/backend
|
window.go
|
CloseAllViews
|
func (w *Window) CloseAllViews() bool {
for len(w.views) > 0 {
if !w.views[0].Close() {
return false
}
}
return true
}
|
go
|
func (w *Window) CloseAllViews() bool {
for len(w.views) > 0 {
if !w.views[0].Close() {
return false
}
}
return true
}
|
[
"func",
"(",
"w",
"*",
"Window",
")",
"CloseAllViews",
"(",
")",
"bool",
"{",
"for",
"len",
"(",
"w",
".",
"views",
")",
">",
"0",
"{",
"if",
"!",
"w",
".",
"views",
"[",
"0",
"]",
".",
"Close",
"(",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"true",
"\n",
"}"
] |
// Closes all of the Window's Views.
// Returns "true" if all the Views closed successfully. Otherwise returns "false".
|
[
"Closes",
"all",
"of",
"the",
"Window",
"s",
"Views",
".",
"Returns",
"true",
"if",
"all",
"the",
"Views",
"closed",
"successfully",
".",
"Otherwise",
"returns",
"false",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/window.go#L127-L135
|
15,088 |
limetext/backend
|
project.go
|
SaveAs
|
func (p *Project) SaveAs(name string) error {
log.Fine("Saving project as %s", name)
if data, err := json.Marshal(p); err != nil {
return err
} else if err := ioutil.WriteFile(name, data, 0644); err != nil {
return err
}
if abs, err := filepath.Abs(name); err != nil {
p.SetName(name)
} else {
p.SetName(abs)
}
return nil
}
|
go
|
func (p *Project) SaveAs(name string) error {
log.Fine("Saving project as %s", name)
if data, err := json.Marshal(p); err != nil {
return err
} else if err := ioutil.WriteFile(name, data, 0644); err != nil {
return err
}
if abs, err := filepath.Abs(name); err != nil {
p.SetName(name)
} else {
p.SetName(abs)
}
return nil
}
|
[
"func",
"(",
"p",
"*",
"Project",
")",
"SaveAs",
"(",
"name",
"string",
")",
"error",
"{",
"log",
".",
"Fine",
"(",
"\"",
"\"",
",",
"name",
")",
"\n",
"if",
"data",
",",
"err",
":=",
"json",
".",
"Marshal",
"(",
"p",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"else",
"if",
"err",
":=",
"ioutil",
".",
"WriteFile",
"(",
"name",
",",
"data",
",",
"0644",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"if",
"abs",
",",
"err",
":=",
"filepath",
".",
"Abs",
"(",
"name",
")",
";",
"err",
"!=",
"nil",
"{",
"p",
".",
"SetName",
"(",
"name",
")",
"\n",
"}",
"else",
"{",
"p",
".",
"SetName",
"(",
"abs",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Marshals project struct to json then writes it to a file with given name
|
[
"Marshals",
"project",
"struct",
"to",
"json",
"then",
"writes",
"it",
"to",
"a",
"file",
"with",
"given",
"name"
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/project.go#L53-L66
|
15,089 |
limetext/backend
|
events.go
|
call
|
func (ie *InitEvent) call() {
log.Debug("OnInit callbacks executing")
defer log.Debug("OnInit callbacks finished")
for _, ev := range *ie {
ev()
}
}
|
go
|
func (ie *InitEvent) call() {
log.Debug("OnInit callbacks executing")
defer log.Debug("OnInit callbacks finished")
for _, ev := range *ie {
ev()
}
}
|
[
"func",
"(",
"ie",
"*",
"InitEvent",
")",
"call",
"(",
")",
"{",
"log",
".",
"Debug",
"(",
"\"",
"\"",
")",
"\n",
"defer",
"log",
".",
"Debug",
"(",
"\"",
"\"",
")",
"\n",
"for",
"_",
",",
"ev",
":=",
"range",
"*",
"ie",
"{",
"ev",
"(",
")",
"\n",
"}",
"\n",
"}"
] |
// Execute the InitEvent.
|
[
"Execute",
"the",
"InitEvent",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/events.go#L79-L85
|
15,090 |
limetext/backend
|
watch/watcher.go
|
flushDir
|
func (w *Watcher) flushDir(name string) {
log.Finest("Flusing watched directory %s", name)
w.dirs = append(w.dirs, name)
for _, p := range w.watchers {
if filepath.Dir(p) == name && !util.Exists(w.dirs, p) {
if err := w.removeWatch(p); err != nil {
log.Error("Couldn't unwatch file %s: %s", p, err)
}
}
}
}
|
go
|
func (w *Watcher) flushDir(name string) {
log.Finest("Flusing watched directory %s", name)
w.dirs = append(w.dirs, name)
for _, p := range w.watchers {
if filepath.Dir(p) == name && !util.Exists(w.dirs, p) {
if err := w.removeWatch(p); err != nil {
log.Error("Couldn't unwatch file %s: %s", p, err)
}
}
}
}
|
[
"func",
"(",
"w",
"*",
"Watcher",
")",
"flushDir",
"(",
"name",
"string",
")",
"{",
"log",
".",
"Finest",
"(",
"\"",
"\"",
",",
"name",
")",
"\n",
"w",
".",
"dirs",
"=",
"append",
"(",
"w",
".",
"dirs",
",",
"name",
")",
"\n",
"for",
"_",
",",
"p",
":=",
"range",
"w",
".",
"watchers",
"{",
"if",
"filepath",
".",
"Dir",
"(",
"p",
")",
"==",
"name",
"&&",
"!",
"util",
".",
"Exists",
"(",
"w",
".",
"dirs",
",",
"p",
")",
"{",
"if",
"err",
":=",
"w",
".",
"removeWatch",
"(",
"p",
")",
";",
"err",
"!=",
"nil",
"{",
"log",
".",
"Error",
"(",
"\"",
"\"",
",",
"p",
",",
"err",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// Remove watchers created on files under this directory because
// one watcher on the parent directory is enough for all of them
|
[
"Remove",
"watchers",
"created",
"on",
"files",
"under",
"this",
"directory",
"because",
"one",
"watcher",
"on",
"the",
"parent",
"directory",
"is",
"enough",
"for",
"all",
"of",
"them"
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/watch/watcher.go#L152-L162
|
15,091 |
limetext/backend
|
watch/watcher.go
|
removeDir
|
func (w *Watcher) removeDir(name string) {
for p, _ := range w.watched {
if filepath.Dir(p) == name {
stat, err := os.Stat(p)
if err != nil {
log.Error("Stat error: %s", err)
}
if err := w.watch(p, stat.IsDir()); err != nil {
log.Error("Could not watch: %s", err)
continue
}
}
}
w.dirs = util.Remove(w.dirs, name)
}
|
go
|
func (w *Watcher) removeDir(name string) {
for p, _ := range w.watched {
if filepath.Dir(p) == name {
stat, err := os.Stat(p)
if err != nil {
log.Error("Stat error: %s", err)
}
if err := w.watch(p, stat.IsDir()); err != nil {
log.Error("Could not watch: %s", err)
continue
}
}
}
w.dirs = util.Remove(w.dirs, name)
}
|
[
"func",
"(",
"w",
"*",
"Watcher",
")",
"removeDir",
"(",
"name",
"string",
")",
"{",
"for",
"p",
",",
"_",
":=",
"range",
"w",
".",
"watched",
"{",
"if",
"filepath",
".",
"Dir",
"(",
"p",
")",
"==",
"name",
"{",
"stat",
",",
"err",
":=",
"os",
".",
"Stat",
"(",
"p",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"log",
".",
"Error",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"if",
"err",
":=",
"w",
".",
"watch",
"(",
"p",
",",
"stat",
".",
"IsDir",
"(",
")",
")",
";",
"err",
"!=",
"nil",
"{",
"log",
".",
"Error",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"continue",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"w",
".",
"dirs",
"=",
"util",
".",
"Remove",
"(",
"w",
".",
"dirs",
",",
"name",
")",
"\n",
"}"
] |
// Put back watchers on watching files under the directory
|
[
"Put",
"back",
"watchers",
"on",
"watching",
"files",
"under",
"the",
"directory"
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/watch/watcher.go#L215-L229
|
15,092 |
limetext/backend
|
watch/watcher.go
|
observe
|
func (w *Watcher) observe() {
for {
select {
case ev, ok := <-w.fsEvent:
if !ok {
// We get here only when w.fsEvent is stopped when closing the watcher
w.watched = nil
w.watchers = nil
w.dirs = nil
close(w.fsEvent)
w.fsEvent = nil
return
}
w.parseEv(ev)
}
}
}
|
go
|
func (w *Watcher) observe() {
for {
select {
case ev, ok := <-w.fsEvent:
if !ok {
// We get here only when w.fsEvent is stopped when closing the watcher
w.watched = nil
w.watchers = nil
w.dirs = nil
close(w.fsEvent)
w.fsEvent = nil
return
}
w.parseEv(ev)
}
}
}
|
[
"func",
"(",
"w",
"*",
"Watcher",
")",
"observe",
"(",
")",
"{",
"for",
"{",
"select",
"{",
"case",
"ev",
",",
"ok",
":=",
"<-",
"w",
".",
"fsEvent",
":",
"if",
"!",
"ok",
"{",
"// We get here only when w.fsEvent is stopped when closing the watcher",
"w",
".",
"watched",
"=",
"nil",
"\n",
"w",
".",
"watchers",
"=",
"nil",
"\n",
"w",
".",
"dirs",
"=",
"nil",
"\n",
"close",
"(",
"w",
".",
"fsEvent",
")",
"\n",
"w",
".",
"fsEvent",
"=",
"nil",
"\n",
"return",
"\n",
"}",
"\n",
"w",
".",
"parseEv",
"(",
"ev",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// Observe dispatches notifications received by the watcher. This function will
// return when the watcher is closed.
|
[
"Observe",
"dispatches",
"notifications",
"received",
"by",
"the",
"watcher",
".",
"This",
"function",
"will",
"return",
"when",
"the",
"watcher",
"is",
"closed",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/watch/watcher.go#L233-L249
|
15,093 |
limetext/backend
|
undo.go
|
Redo
|
func (us *UndoStack) Redo(hard bool) {
if us.position >= len(us.actions) {
// No more actions to redo
return
}
to := us.index(1, hard)
if to == -1 {
to = len(us.actions)
}
for us.position < to {
us.actions[us.position].Apply()
us.position++
}
}
|
go
|
func (us *UndoStack) Redo(hard bool) {
if us.position >= len(us.actions) {
// No more actions to redo
return
}
to := us.index(1, hard)
if to == -1 {
to = len(us.actions)
}
for us.position < to {
us.actions[us.position].Apply()
us.position++
}
}
|
[
"func",
"(",
"us",
"*",
"UndoStack",
")",
"Redo",
"(",
"hard",
"bool",
")",
"{",
"if",
"us",
".",
"position",
">=",
"len",
"(",
"us",
".",
"actions",
")",
"{",
"// No more actions to redo",
"return",
"\n",
"}",
"\n",
"to",
":=",
"us",
".",
"index",
"(",
"1",
",",
"hard",
")",
"\n",
"if",
"to",
"==",
"-",
"1",
"{",
"to",
"=",
"len",
"(",
"us",
".",
"actions",
")",
"\n",
"}",
"\n",
"for",
"us",
".",
"position",
"<",
"to",
"{",
"us",
".",
"actions",
"[",
"us",
".",
"position",
"]",
".",
"Apply",
"(",
")",
"\n",
"us",
".",
"position",
"++",
"\n",
"}",
"\n",
"}"
] |
// Re-applies the next action in the undo stack
// if there are any actions on the stack that had
// been undone.
//
// See comment in Undo regarding the use of "hard".
|
[
"Re",
"-",
"applies",
"the",
"next",
"action",
"in",
"the",
"undo",
"stack",
"if",
"there",
"are",
"any",
"actions",
"on",
"the",
"stack",
"that",
"had",
"been",
"undone",
".",
"See",
"comment",
"in",
"Undo",
"regarding",
"the",
"use",
"of",
"hard",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/undo.go#L84-L97
|
15,094 |
limetext/backend
|
undo.go
|
GlueFrom
|
func (us *UndoStack) GlueFrom(mark int) {
if mark >= us.position {
return
}
var e Edit
e.command = "sequence"
type entry struct {
name string
args Args
}
e.v = us.actions[mark].v
e.savedSel.AddAll(us.actions[mark].savedSel.Regions())
entries := make([]entry, us.position-mark)
for i := range entries {
a := us.actions[i+mark]
entries[i].name = a.command
entries[i].args = a.args
e.composite.Add(a)
}
us.position = mark
us.actions = us.actions[:mark+1]
e.args = make(Args)
e.args["commands"] = entries
us.Add(&e)
}
|
go
|
func (us *UndoStack) GlueFrom(mark int) {
if mark >= us.position {
return
}
var e Edit
e.command = "sequence"
type entry struct {
name string
args Args
}
e.v = us.actions[mark].v
e.savedSel.AddAll(us.actions[mark].savedSel.Regions())
entries := make([]entry, us.position-mark)
for i := range entries {
a := us.actions[i+mark]
entries[i].name = a.command
entries[i].args = a.args
e.composite.Add(a)
}
us.position = mark
us.actions = us.actions[:mark+1]
e.args = make(Args)
e.args["commands"] = entries
us.Add(&e)
}
|
[
"func",
"(",
"us",
"*",
"UndoStack",
")",
"GlueFrom",
"(",
"mark",
"int",
")",
"{",
"if",
"mark",
">=",
"us",
".",
"position",
"{",
"return",
"\n",
"}",
"\n",
"var",
"e",
"Edit",
"\n",
"e",
".",
"command",
"=",
"\"",
"\"",
"\n",
"type",
"entry",
"struct",
"{",
"name",
"string",
"\n",
"args",
"Args",
"\n",
"}",
"\n",
"e",
".",
"v",
"=",
"us",
".",
"actions",
"[",
"mark",
"]",
".",
"v",
"\n",
"e",
".",
"savedSel",
".",
"AddAll",
"(",
"us",
".",
"actions",
"[",
"mark",
"]",
".",
"savedSel",
".",
"Regions",
"(",
")",
")",
"\n\n",
"entries",
":=",
"make",
"(",
"[",
"]",
"entry",
",",
"us",
".",
"position",
"-",
"mark",
")",
"\n",
"for",
"i",
":=",
"range",
"entries",
"{",
"a",
":=",
"us",
".",
"actions",
"[",
"i",
"+",
"mark",
"]",
"\n",
"entries",
"[",
"i",
"]",
".",
"name",
"=",
"a",
".",
"command",
"\n",
"entries",
"[",
"i",
"]",
".",
"args",
"=",
"a",
".",
"args",
"\n",
"e",
".",
"composite",
".",
"Add",
"(",
"a",
")",
"\n",
"}",
"\n",
"us",
".",
"position",
"=",
"mark",
"\n",
"us",
".",
"actions",
"=",
"us",
".",
"actions",
"[",
":",
"mark",
"+",
"1",
"]",
"\n",
"e",
".",
"args",
"=",
"make",
"(",
"Args",
")",
"\n",
"e",
".",
"args",
"[",
"\"",
"\"",
"]",
"=",
"entries",
"\n",
"us",
".",
"Add",
"(",
"&",
"e",
")",
"\n",
"}"
] |
// Glues all edits from the position given by mark,
// to the current position in the UndoStack, replacing
// them by a single entry which will now be composite
// of all those other actions.
//
// In other words, after the glue operation
// a single "undo" operation will then undo all of those edits
// and a single redo after that will redo them all again.
|
[
"Glues",
"all",
"edits",
"from",
"the",
"position",
"given",
"by",
"mark",
"to",
"the",
"current",
"position",
"in",
"the",
"UndoStack",
"replacing",
"them",
"by",
"a",
"single",
"entry",
"which",
"will",
"now",
"be",
"composite",
"of",
"all",
"those",
"other",
"actions",
".",
"In",
"other",
"words",
"after",
"the",
"glue",
"operation",
"a",
"single",
"undo",
"operation",
"will",
"then",
"undo",
"all",
"of",
"those",
"edits",
"and",
"a",
"single",
"redo",
"after",
"that",
"will",
"redo",
"them",
"all",
"again",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/undo.go#L112-L137
|
15,095 |
limetext/backend
|
packages/watcher.go
|
watch
|
func watch(pkg Package) {
if err := watcher.Watch(pkg.Path(), pkg); err != nil {
log.Warn("Couldn't watch %s: %s", pkg.Path(), err)
}
}
|
go
|
func watch(pkg Package) {
if err := watcher.Watch(pkg.Path(), pkg); err != nil {
log.Warn("Couldn't watch %s: %s", pkg.Path(), err)
}
}
|
[
"func",
"watch",
"(",
"pkg",
"Package",
")",
"{",
"if",
"err",
":=",
"watcher",
".",
"Watch",
"(",
"pkg",
".",
"Path",
"(",
")",
",",
"pkg",
")",
";",
"err",
"!=",
"nil",
"{",
"log",
".",
"Warn",
"(",
"\"",
"\"",
",",
"pkg",
".",
"Path",
"(",
")",
",",
"err",
")",
"\n",
"}",
"\n",
"}"
] |
// Helper function for watching a package
|
[
"Helper",
"function",
"for",
"watching",
"a",
"package"
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/packages/watcher.go#L16-L20
|
15,096 |
limetext/backend
|
packages/watcher.go
|
watchDir
|
func watchDir(dir string) {
log.Finest("Watching scaned dir: %s", dir)
sd := &scanDir{dir}
if err := watcher.Watch(sd.path, sd); err != nil {
log.Error("Couldn't watch %s: %s", sd.path, err)
}
}
|
go
|
func watchDir(dir string) {
log.Finest("Watching scaned dir: %s", dir)
sd := &scanDir{dir}
if err := watcher.Watch(sd.path, sd); err != nil {
log.Error("Couldn't watch %s: %s", sd.path, err)
}
}
|
[
"func",
"watchDir",
"(",
"dir",
"string",
")",
"{",
"log",
".",
"Finest",
"(",
"\"",
"\"",
",",
"dir",
")",
"\n",
"sd",
":=",
"&",
"scanDir",
"{",
"dir",
"}",
"\n",
"if",
"err",
":=",
"watcher",
".",
"Watch",
"(",
"sd",
".",
"path",
",",
"sd",
")",
";",
"err",
"!=",
"nil",
"{",
"log",
".",
"Error",
"(",
"\"",
"\"",
",",
"sd",
".",
"path",
",",
"err",
")",
"\n",
"}",
"\n",
"}"
] |
// watches scaned directory
|
[
"watches",
"scaned",
"directory"
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/packages/watcher.go#L42-L48
|
15,097 |
limetext/backend
|
render/view.go
|
Cull
|
func (vrm *ViewRegionMap) Cull(viewport text.Region) {
pe := util.Prof.Enter("render.vrm.Cull")
defer pe.Exit()
rm := []string{}
for k, v := range *vrm {
v.Cull(viewport)
if v.Regions.Len() == 0 {
rm = append(rm, k)
} else {
(*vrm)[k] = v
}
}
for _, r := range rm {
delete(*vrm, r)
}
}
|
go
|
func (vrm *ViewRegionMap) Cull(viewport text.Region) {
pe := util.Prof.Enter("render.vrm.Cull")
defer pe.Exit()
rm := []string{}
for k, v := range *vrm {
v.Cull(viewport)
if v.Regions.Len() == 0 {
rm = append(rm, k)
} else {
(*vrm)[k] = v
}
}
for _, r := range rm {
delete(*vrm, r)
}
}
|
[
"func",
"(",
"vrm",
"*",
"ViewRegionMap",
")",
"Cull",
"(",
"viewport",
"text",
".",
"Region",
")",
"{",
"pe",
":=",
"util",
".",
"Prof",
".",
"Enter",
"(",
"\"",
"\"",
")",
"\n",
"defer",
"pe",
".",
"Exit",
"(",
")",
"\n",
"rm",
":=",
"[",
"]",
"string",
"{",
"}",
"\n",
"for",
"k",
",",
"v",
":=",
"range",
"*",
"vrm",
"{",
"v",
".",
"Cull",
"(",
"viewport",
")",
"\n",
"if",
"v",
".",
"Regions",
".",
"Len",
"(",
")",
"==",
"0",
"{",
"rm",
"=",
"append",
"(",
"rm",
",",
"k",
")",
"\n",
"}",
"else",
"{",
"(",
"*",
"vrm",
")",
"[",
"k",
"]",
"=",
"v",
"\n",
"}",
"\n",
"}",
"\n",
"for",
"_",
",",
"r",
":=",
"range",
"rm",
"{",
"delete",
"(",
"*",
"vrm",
",",
"r",
")",
"\n",
"}",
"\n",
"}"
] |
// Calls Cull on each ViewRegions object contained in the map,
// removing all entries that are outside of the viewport.
|
[
"Calls",
"Cull",
"on",
"each",
"ViewRegions",
"object",
"contained",
"in",
"the",
"map",
"removing",
"all",
"entries",
"that",
"are",
"outside",
"of",
"the",
"viewport",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/render/view.go#L59-L74
|
15,098 |
limetext/backend
|
render/view.go
|
Cull
|
func (vr *ViewRegions) Cull(viewport text.Region) {
pe := util.Prof.Enter("render.vr.Cull")
defer pe.Exit()
nr := []text.Region{}
for _, r := range vr.Regions.Regions() {
if viewport.Intersects(r) {
in := viewport.Intersection(r)
if in.Size() != 0 {
nr = append(nr, in)
}
}
}
vr.Regions.Clear()
vr.Regions.AddAll(nr)
}
|
go
|
func (vr *ViewRegions) Cull(viewport text.Region) {
pe := util.Prof.Enter("render.vr.Cull")
defer pe.Exit()
nr := []text.Region{}
for _, r := range vr.Regions.Regions() {
if viewport.Intersects(r) {
in := viewport.Intersection(r)
if in.Size() != 0 {
nr = append(nr, in)
}
}
}
vr.Regions.Clear()
vr.Regions.AddAll(nr)
}
|
[
"func",
"(",
"vr",
"*",
"ViewRegions",
")",
"Cull",
"(",
"viewport",
"text",
".",
"Region",
")",
"{",
"pe",
":=",
"util",
".",
"Prof",
".",
"Enter",
"(",
"\"",
"\"",
")",
"\n",
"defer",
"pe",
".",
"Exit",
"(",
")",
"\n",
"nr",
":=",
"[",
"]",
"text",
".",
"Region",
"{",
"}",
"\n",
"for",
"_",
",",
"r",
":=",
"range",
"vr",
".",
"Regions",
".",
"Regions",
"(",
")",
"{",
"if",
"viewport",
".",
"Intersects",
"(",
"r",
")",
"{",
"in",
":=",
"viewport",
".",
"Intersection",
"(",
"r",
")",
"\n",
"if",
"in",
".",
"Size",
"(",
")",
"!=",
"0",
"{",
"nr",
"=",
"append",
"(",
"nr",
",",
"in",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"vr",
".",
"Regions",
".",
"Clear",
"(",
")",
"\n",
"vr",
".",
"Regions",
".",
"AddAll",
"(",
"nr",
")",
"\n",
"}"
] |
// Removes any regions that are outside of the given viewport,
// and clips the regions that are intersecting it so that
// all regions remaining are fully contained inside of the viewport.
|
[
"Removes",
"any",
"regions",
"that",
"are",
"outside",
"of",
"the",
"given",
"viewport",
"and",
"clips",
"the",
"regions",
"that",
"are",
"intersecting",
"it",
"so",
"that",
"all",
"regions",
"remaining",
"are",
"fully",
"contained",
"inside",
"of",
"the",
"viewport",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/render/view.go#L79-L93
|
15,099 |
limetext/backend
|
render/view.go
|
Clone
|
func (vr *ViewRegions) Clone() *ViewRegions {
ret := ViewRegions{Scope: vr.Scope, Icon: vr.Icon, Flags: vr.Flags}
ret.Regions.AddAll(vr.Regions.Regions())
return &ret
}
|
go
|
func (vr *ViewRegions) Clone() *ViewRegions {
ret := ViewRegions{Scope: vr.Scope, Icon: vr.Icon, Flags: vr.Flags}
ret.Regions.AddAll(vr.Regions.Regions())
return &ret
}
|
[
"func",
"(",
"vr",
"*",
"ViewRegions",
")",
"Clone",
"(",
")",
"*",
"ViewRegions",
"{",
"ret",
":=",
"ViewRegions",
"{",
"Scope",
":",
"vr",
".",
"Scope",
",",
"Icon",
":",
"vr",
".",
"Icon",
",",
"Flags",
":",
"vr",
".",
"Flags",
"}",
"\n",
"ret",
".",
"Regions",
".",
"AddAll",
"(",
"vr",
".",
"Regions",
".",
"Regions",
"(",
")",
")",
"\n",
"return",
"&",
"ret",
"\n",
"}"
] |
// Creates a copy of this ViewRegions object.
|
[
"Creates",
"a",
"copy",
"of",
"this",
"ViewRegions",
"object",
"."
] |
3e883f0efc3c38aa32c11c06e2e044086a4129f5
|
https://github.com/limetext/backend/blob/3e883f0efc3c38aa32c11c06e2e044086a4129f5/render/view.go#L96-L100
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.