repo
stringlengths 5
67
| path
stringlengths 4
218
| func_name
stringlengths 0
151
| original_string
stringlengths 52
373k
| language
stringclasses 6
values | code
stringlengths 52
373k
| code_tokens
listlengths 10
512
| docstring
stringlengths 3
47.2k
| docstring_tokens
listlengths 3
234
| sha
stringlengths 40
40
| url
stringlengths 85
339
| partition
stringclasses 3
values |
|---|---|---|---|---|---|---|---|---|---|---|---|
golang/debug
|
internal/gocore/dominator.go
|
calculate
|
func (d *ltDom) calculate() {
// name -> bucket (a name), per Georgiadis.
buckets := make([]vName, d.nVertices)
for i := range buckets {
buckets[i] = vName(i)
}
for i := vNumber(len(d.vertices)) - 1; i > 0; i-- {
w := d.vertices[i]
// Step 3. Implicitly define the immediate dominator of each node.
for v := buckets[w]; v != w; v = buckets[v] {
u := d.eval(v)
if d.semis[u] < d.semis[v] {
d.idom[v] = u
} else {
d.idom[v] = w
}
}
// Step 2. Compute the semidominators of all nodes.
root, obj := d.findVertexByName(w)
// This loop never visits the pseudo-root.
if root != nil {
u := d.eval(pseudoRoot)
if d.semis[u] < d.semis[w] {
d.semis[w] = d.semis[u]
}
} else {
d.p.ForEachReversePtr(obj, func(x Object, r *Root, _, _ int64) bool {
var v int
if r != nil {
v = d.p.findRootIndex(r) + 1
} else {
v, _ = d.p.findObjectIndex(d.p.Addr(x))
v += d.nRoots + 1
}
u := d.eval(vName(v))
if d.semis[u] < d.semis[w] {
d.semis[w] = d.semis[u]
}
return true
})
}
d.link(d.parents[w], w)
if d.parents[w] == d.vertices[d.semis[w]] {
d.idom[w] = d.parents[w]
} else {
buckets[w] = buckets[d.vertices[d.semis[w]]]
buckets[d.vertices[d.semis[w]]] = w
}
}
// The final 'Step 3' is now outside the loop.
for v := buckets[pseudoRoot]; v != pseudoRoot; v = buckets[v] {
d.idom[v] = pseudoRoot
}
// Step 4. Explicitly define the immediate dominator of each
// node, in preorder.
for _, w := range d.vertices[1:] {
if d.idom[w] != d.vertices[d.semis[w]] {
d.idom[w] = d.idom[d.idom[w]]
}
}
}
|
go
|
func (d *ltDom) calculate() {
// name -> bucket (a name), per Georgiadis.
buckets := make([]vName, d.nVertices)
for i := range buckets {
buckets[i] = vName(i)
}
for i := vNumber(len(d.vertices)) - 1; i > 0; i-- {
w := d.vertices[i]
// Step 3. Implicitly define the immediate dominator of each node.
for v := buckets[w]; v != w; v = buckets[v] {
u := d.eval(v)
if d.semis[u] < d.semis[v] {
d.idom[v] = u
} else {
d.idom[v] = w
}
}
// Step 2. Compute the semidominators of all nodes.
root, obj := d.findVertexByName(w)
// This loop never visits the pseudo-root.
if root != nil {
u := d.eval(pseudoRoot)
if d.semis[u] < d.semis[w] {
d.semis[w] = d.semis[u]
}
} else {
d.p.ForEachReversePtr(obj, func(x Object, r *Root, _, _ int64) bool {
var v int
if r != nil {
v = d.p.findRootIndex(r) + 1
} else {
v, _ = d.p.findObjectIndex(d.p.Addr(x))
v += d.nRoots + 1
}
u := d.eval(vName(v))
if d.semis[u] < d.semis[w] {
d.semis[w] = d.semis[u]
}
return true
})
}
d.link(d.parents[w], w)
if d.parents[w] == d.vertices[d.semis[w]] {
d.idom[w] = d.parents[w]
} else {
buckets[w] = buckets[d.vertices[d.semis[w]]]
buckets[d.vertices[d.semis[w]]] = w
}
}
// The final 'Step 3' is now outside the loop.
for v := buckets[pseudoRoot]; v != pseudoRoot; v = buckets[v] {
d.idom[v] = pseudoRoot
}
// Step 4. Explicitly define the immediate dominator of each
// node, in preorder.
for _, w := range d.vertices[1:] {
if d.idom[w] != d.vertices[d.semis[w]] {
d.idom[w] = d.idom[d.idom[w]]
}
}
}
|
[
"func",
"(",
"d",
"*",
"ltDom",
")",
"calculate",
"(",
")",
"{",
"buckets",
":=",
"make",
"(",
"[",
"]",
"vName",
",",
"d",
".",
"nVertices",
")",
"\n",
"for",
"i",
":=",
"range",
"buckets",
"{",
"buckets",
"[",
"i",
"]",
"=",
"vName",
"(",
"i",
")",
"\n",
"}",
"\n",
"for",
"i",
":=",
"vNumber",
"(",
"len",
"(",
"d",
".",
"vertices",
")",
")",
"-",
"1",
";",
"i",
">",
"0",
";",
"i",
"--",
"{",
"w",
":=",
"d",
".",
"vertices",
"[",
"i",
"]",
"\n",
"for",
"v",
":=",
"buckets",
"[",
"w",
"]",
";",
"v",
"!=",
"w",
";",
"v",
"=",
"buckets",
"[",
"v",
"]",
"{",
"u",
":=",
"d",
".",
"eval",
"(",
"v",
")",
"\n",
"if",
"d",
".",
"semis",
"[",
"u",
"]",
"<",
"d",
".",
"semis",
"[",
"v",
"]",
"{",
"d",
".",
"idom",
"[",
"v",
"]",
"=",
"u",
"\n",
"}",
"else",
"{",
"d",
".",
"idom",
"[",
"v",
"]",
"=",
"w",
"\n",
"}",
"\n",
"}",
"\n",
"root",
",",
"obj",
":=",
"d",
".",
"findVertexByName",
"(",
"w",
")",
"\n",
"if",
"root",
"!=",
"nil",
"{",
"u",
":=",
"d",
".",
"eval",
"(",
"pseudoRoot",
")",
"\n",
"if",
"d",
".",
"semis",
"[",
"u",
"]",
"<",
"d",
".",
"semis",
"[",
"w",
"]",
"{",
"d",
".",
"semis",
"[",
"w",
"]",
"=",
"d",
".",
"semis",
"[",
"u",
"]",
"\n",
"}",
"\n",
"}",
"else",
"{",
"d",
".",
"p",
".",
"ForEachReversePtr",
"(",
"obj",
",",
"func",
"(",
"x",
"Object",
",",
"r",
"*",
"Root",
",",
"_",
",",
"_",
"int64",
")",
"bool",
"{",
"var",
"v",
"int",
"\n",
"if",
"r",
"!=",
"nil",
"{",
"v",
"=",
"d",
".",
"p",
".",
"findRootIndex",
"(",
"r",
")",
"+",
"1",
"\n",
"}",
"else",
"{",
"v",
",",
"_",
"=",
"d",
".",
"p",
".",
"findObjectIndex",
"(",
"d",
".",
"p",
".",
"Addr",
"(",
"x",
")",
")",
"\n",
"v",
"+=",
"d",
".",
"nRoots",
"+",
"1",
"\n",
"}",
"\n",
"u",
":=",
"d",
".",
"eval",
"(",
"vName",
"(",
"v",
")",
")",
"\n",
"if",
"d",
".",
"semis",
"[",
"u",
"]",
"<",
"d",
".",
"semis",
"[",
"w",
"]",
"{",
"d",
".",
"semis",
"[",
"w",
"]",
"=",
"d",
".",
"semis",
"[",
"u",
"]",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}",
")",
"\n",
"}",
"\n",
"d",
".",
"link",
"(",
"d",
".",
"parents",
"[",
"w",
"]",
",",
"w",
")",
"\n",
"if",
"d",
".",
"parents",
"[",
"w",
"]",
"==",
"d",
".",
"vertices",
"[",
"d",
".",
"semis",
"[",
"w",
"]",
"]",
"{",
"d",
".",
"idom",
"[",
"w",
"]",
"=",
"d",
".",
"parents",
"[",
"w",
"]",
"\n",
"}",
"else",
"{",
"buckets",
"[",
"w",
"]",
"=",
"buckets",
"[",
"d",
".",
"vertices",
"[",
"d",
".",
"semis",
"[",
"w",
"]",
"]",
"]",
"\n",
"buckets",
"[",
"d",
".",
"vertices",
"[",
"d",
".",
"semis",
"[",
"w",
"]",
"]",
"]",
"=",
"w",
"\n",
"}",
"\n",
"}",
"\n",
"for",
"v",
":=",
"buckets",
"[",
"pseudoRoot",
"]",
";",
"v",
"!=",
"pseudoRoot",
";",
"v",
"=",
"buckets",
"[",
"v",
"]",
"{",
"d",
".",
"idom",
"[",
"v",
"]",
"=",
"pseudoRoot",
"\n",
"}",
"\n",
"for",
"_",
",",
"w",
":=",
"range",
"d",
".",
"vertices",
"[",
"1",
":",
"]",
"{",
"if",
"d",
".",
"idom",
"[",
"w",
"]",
"!=",
"d",
".",
"vertices",
"[",
"d",
".",
"semis",
"[",
"w",
"]",
"]",
"{",
"d",
".",
"idom",
"[",
"w",
"]",
"=",
"d",
".",
"idom",
"[",
"d",
".",
"idom",
"[",
"w",
"]",
"]",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// calculate runs the main part of LT.
|
[
"calculate",
"runs",
"the",
"main",
"part",
"of",
"LT",
"."
] |
19561fee47cf8cd0400d1b094c5898002f97cf90
|
https://github.com/golang/debug/blob/19561fee47cf8cd0400d1b094c5898002f97cf90/internal/gocore/dominator.go#L199-L266
|
test
|
golang/debug
|
internal/gocore/dominator.go
|
eval
|
func (d *ltDom) eval(v vName) vName {
if d.ancestor[v] == -1 {
return v
}
d.compress(v)
return d.labels[v]
}
|
go
|
func (d *ltDom) eval(v vName) vName {
if d.ancestor[v] == -1 {
return v
}
d.compress(v)
return d.labels[v]
}
|
[
"func",
"(",
"d",
"*",
"ltDom",
")",
"eval",
"(",
"v",
"vName",
")",
"vName",
"{",
"if",
"d",
".",
"ancestor",
"[",
"v",
"]",
"==",
"-",
"1",
"{",
"return",
"v",
"\n",
"}",
"\n",
"d",
".",
"compress",
"(",
"v",
")",
"\n",
"return",
"d",
".",
"labels",
"[",
"v",
"]",
"\n",
"}"
] |
// eval is EVAL from the papers.
|
[
"eval",
"is",
"EVAL",
"from",
"the",
"papers",
"."
] |
19561fee47cf8cd0400d1b094c5898002f97cf90
|
https://github.com/golang/debug/blob/19561fee47cf8cd0400d1b094c5898002f97cf90/internal/gocore/dominator.go#L269-L275
|
test
|
golang/debug
|
internal/gocore/dominator.go
|
compress
|
func (d *ltDom) compress(v vName) {
var stackBuf [20]vName
stack := stackBuf[:0]
for d.ancestor[d.ancestor[v]] != -1 {
stack = append(stack, v)
v = d.ancestor[v]
}
for len(stack) != 0 {
v := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if d.semis[d.labels[d.ancestor[v]]] < d.semis[d.labels[v]] {
d.labels[v] = d.labels[d.ancestor[v]]
}
d.ancestor[v] = d.ancestor[d.ancestor[v]]
}
}
|
go
|
func (d *ltDom) compress(v vName) {
var stackBuf [20]vName
stack := stackBuf[:0]
for d.ancestor[d.ancestor[v]] != -1 {
stack = append(stack, v)
v = d.ancestor[v]
}
for len(stack) != 0 {
v := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if d.semis[d.labels[d.ancestor[v]]] < d.semis[d.labels[v]] {
d.labels[v] = d.labels[d.ancestor[v]]
}
d.ancestor[v] = d.ancestor[d.ancestor[v]]
}
}
|
[
"func",
"(",
"d",
"*",
"ltDom",
")",
"compress",
"(",
"v",
"vName",
")",
"{",
"var",
"stackBuf",
"[",
"20",
"]",
"vName",
"\n",
"stack",
":=",
"stackBuf",
"[",
":",
"0",
"]",
"\n",
"for",
"d",
".",
"ancestor",
"[",
"d",
".",
"ancestor",
"[",
"v",
"]",
"]",
"!=",
"-",
"1",
"{",
"stack",
"=",
"append",
"(",
"stack",
",",
"v",
")",
"\n",
"v",
"=",
"d",
".",
"ancestor",
"[",
"v",
"]",
"\n",
"}",
"\n",
"for",
"len",
"(",
"stack",
")",
"!=",
"0",
"{",
"v",
":=",
"stack",
"[",
"len",
"(",
"stack",
")",
"-",
"1",
"]",
"\n",
"stack",
"=",
"stack",
"[",
":",
"len",
"(",
"stack",
")",
"-",
"1",
"]",
"\n",
"if",
"d",
".",
"semis",
"[",
"d",
".",
"labels",
"[",
"d",
".",
"ancestor",
"[",
"v",
"]",
"]",
"]",
"<",
"d",
".",
"semis",
"[",
"d",
".",
"labels",
"[",
"v",
"]",
"]",
"{",
"d",
".",
"labels",
"[",
"v",
"]",
"=",
"d",
".",
"labels",
"[",
"d",
".",
"ancestor",
"[",
"v",
"]",
"]",
"\n",
"}",
"\n",
"d",
".",
"ancestor",
"[",
"v",
"]",
"=",
"d",
".",
"ancestor",
"[",
"d",
".",
"ancestor",
"[",
"v",
"]",
"]",
"\n",
"}",
"\n",
"}"
] |
// compress is COMPRESS from the papers.
|
[
"compress",
"is",
"COMPRESS",
"from",
"the",
"papers",
"."
] |
19561fee47cf8cd0400d1b094c5898002f97cf90
|
https://github.com/golang/debug/blob/19561fee47cf8cd0400d1b094c5898002f97cf90/internal/gocore/dominator.go#L278-L295
|
test
|
golang/debug
|
internal/gocore/dominator.go
|
link
|
func (d *ltDom) link(v, w vName) {
d.ancestor[w] = v
}
|
go
|
func (d *ltDom) link(v, w vName) {
d.ancestor[w] = v
}
|
[
"func",
"(",
"d",
"*",
"ltDom",
")",
"link",
"(",
"v",
",",
"w",
"vName",
")",
"{",
"d",
".",
"ancestor",
"[",
"w",
"]",
"=",
"v",
"\n",
"}"
] |
// link is LINK from the papers.
|
[
"link",
"is",
"LINK",
"from",
"the",
"papers",
"."
] |
19561fee47cf8cd0400d1b094c5898002f97cf90
|
https://github.com/golang/debug/blob/19561fee47cf8cd0400d1b094c5898002f97cf90/internal/gocore/dominator.go#L298-L300
|
test
|
golang/debug
|
internal/gocore/dominator.go
|
reverse
|
func (d *dominators) reverse() {
// One inbound edge per vertex. Then we need an extra so that you can
// always look at ridx[i+1], and another for working storage while
// populating redge.
cnt := make([]int, len(d.idom)+2)
// Fill cnt[2:] with the number of outbound edges for each vertex.
tmp := cnt[2:]
for _, idom := range d.idom {
tmp[idom]++
}
// Make tmp cumulative. After this step, cnt[1:] is what we want for
// ridx, but the next step messes it up.
var n int
for idx, c := range tmp {
n += c
tmp[idx] = n
}
// Store outbound edges in redge, using cnt[1:] as the index to store
// the next edge for each vertex. After we're done, everything's been
// shifted over one, and cnt is ridx.
redge := make([]vName, len(d.idom))
tmp = cnt[1:]
for i, idom := range d.idom {
redge[tmp[idom]] = vName(i)
tmp[idom]++
}
d.redge, d.ridx = redge, cnt[:len(cnt)-1]
}
|
go
|
func (d *dominators) reverse() {
// One inbound edge per vertex. Then we need an extra so that you can
// always look at ridx[i+1], and another for working storage while
// populating redge.
cnt := make([]int, len(d.idom)+2)
// Fill cnt[2:] with the number of outbound edges for each vertex.
tmp := cnt[2:]
for _, idom := range d.idom {
tmp[idom]++
}
// Make tmp cumulative. After this step, cnt[1:] is what we want for
// ridx, but the next step messes it up.
var n int
for idx, c := range tmp {
n += c
tmp[idx] = n
}
// Store outbound edges in redge, using cnt[1:] as the index to store
// the next edge for each vertex. After we're done, everything's been
// shifted over one, and cnt is ridx.
redge := make([]vName, len(d.idom))
tmp = cnt[1:]
for i, idom := range d.idom {
redge[tmp[idom]] = vName(i)
tmp[idom]++
}
d.redge, d.ridx = redge, cnt[:len(cnt)-1]
}
|
[
"func",
"(",
"d",
"*",
"dominators",
")",
"reverse",
"(",
")",
"{",
"cnt",
":=",
"make",
"(",
"[",
"]",
"int",
",",
"len",
"(",
"d",
".",
"idom",
")",
"+",
"2",
")",
"\n",
"tmp",
":=",
"cnt",
"[",
"2",
":",
"]",
"\n",
"for",
"_",
",",
"idom",
":=",
"range",
"d",
".",
"idom",
"{",
"tmp",
"[",
"idom",
"]",
"++",
"\n",
"}",
"\n",
"var",
"n",
"int",
"\n",
"for",
"idx",
",",
"c",
":=",
"range",
"tmp",
"{",
"n",
"+=",
"c",
"\n",
"tmp",
"[",
"idx",
"]",
"=",
"n",
"\n",
"}",
"\n",
"redge",
":=",
"make",
"(",
"[",
"]",
"vName",
",",
"len",
"(",
"d",
".",
"idom",
")",
")",
"\n",
"tmp",
"=",
"cnt",
"[",
"1",
":",
"]",
"\n",
"for",
"i",
",",
"idom",
":=",
"range",
"d",
".",
"idom",
"{",
"redge",
"[",
"tmp",
"[",
"idom",
"]",
"]",
"=",
"vName",
"(",
"i",
")",
"\n",
"tmp",
"[",
"idom",
"]",
"++",
"\n",
"}",
"\n",
"d",
".",
"redge",
",",
"d",
".",
"ridx",
"=",
"redge",
",",
"cnt",
"[",
":",
"len",
"(",
"cnt",
")",
"-",
"1",
"]",
"\n",
"}"
] |
// reverse computes and stores reverse edges for each vertex.
|
[
"reverse",
"computes",
"and",
"stores",
"reverse",
"edges",
"for",
"each",
"vertex",
"."
] |
19561fee47cf8cd0400d1b094c5898002f97cf90
|
https://github.com/golang/debug/blob/19561fee47cf8cd0400d1b094c5898002f97cf90/internal/gocore/dominator.go#L303-L333
|
test
|
golang/debug
|
internal/gocore/dominator.go
|
calcSize
|
func (d *dominators) calcSize(p *Process) {
d.size = make([]int64, len(d.idom))
type workItem struct {
v vName
mode dfsMode
}
work := []workItem{{pseudoRoot, down}}
for len(work) > 0 {
item := &work[len(work)-1]
kids := d.redge[d.ridx[item.v]:d.ridx[item.v+1]]
if item.mode == down && len(kids) != 0 {
item.mode = up
for _, w := range kids {
if w == 0 {
// bogus self-edge. Ignore.
continue
}
work = append(work, workItem{w, down})
}
continue
}
work = work[:len(work)-1]
root, obj := d.findVertexByName(item.v)
var size int64
switch {
case item.v == pseudoRoot:
break
case root != nil:
size += root.Type.Size
default:
size += p.Size(obj)
}
for _, w := range kids {
size += d.size[w]
}
d.size[item.v] = size
}
}
|
go
|
func (d *dominators) calcSize(p *Process) {
d.size = make([]int64, len(d.idom))
type workItem struct {
v vName
mode dfsMode
}
work := []workItem{{pseudoRoot, down}}
for len(work) > 0 {
item := &work[len(work)-1]
kids := d.redge[d.ridx[item.v]:d.ridx[item.v+1]]
if item.mode == down && len(kids) != 0 {
item.mode = up
for _, w := range kids {
if w == 0 {
// bogus self-edge. Ignore.
continue
}
work = append(work, workItem{w, down})
}
continue
}
work = work[:len(work)-1]
root, obj := d.findVertexByName(item.v)
var size int64
switch {
case item.v == pseudoRoot:
break
case root != nil:
size += root.Type.Size
default:
size += p.Size(obj)
}
for _, w := range kids {
size += d.size[w]
}
d.size[item.v] = size
}
}
|
[
"func",
"(",
"d",
"*",
"dominators",
")",
"calcSize",
"(",
"p",
"*",
"Process",
")",
"{",
"d",
".",
"size",
"=",
"make",
"(",
"[",
"]",
"int64",
",",
"len",
"(",
"d",
".",
"idom",
")",
")",
"\n",
"type",
"workItem",
"struct",
"{",
"v",
"vName",
"\n",
"mode",
"dfsMode",
"\n",
"}",
"\n",
"work",
":=",
"[",
"]",
"workItem",
"{",
"{",
"pseudoRoot",
",",
"down",
"}",
"}",
"\n",
"for",
"len",
"(",
"work",
")",
">",
"0",
"{",
"item",
":=",
"&",
"work",
"[",
"len",
"(",
"work",
")",
"-",
"1",
"]",
"\n",
"kids",
":=",
"d",
".",
"redge",
"[",
"d",
".",
"ridx",
"[",
"item",
".",
"v",
"]",
":",
"d",
".",
"ridx",
"[",
"item",
".",
"v",
"+",
"1",
"]",
"]",
"\n",
"if",
"item",
".",
"mode",
"==",
"down",
"&&",
"len",
"(",
"kids",
")",
"!=",
"0",
"{",
"item",
".",
"mode",
"=",
"up",
"\n",
"for",
"_",
",",
"w",
":=",
"range",
"kids",
"{",
"if",
"w",
"==",
"0",
"{",
"continue",
"\n",
"}",
"\n",
"work",
"=",
"append",
"(",
"work",
",",
"workItem",
"{",
"w",
",",
"down",
"}",
")",
"\n",
"}",
"\n",
"continue",
"\n",
"}",
"\n",
"work",
"=",
"work",
"[",
":",
"len",
"(",
"work",
")",
"-",
"1",
"]",
"\n",
"root",
",",
"obj",
":=",
"d",
".",
"findVertexByName",
"(",
"item",
".",
"v",
")",
"\n",
"var",
"size",
"int64",
"\n",
"switch",
"{",
"case",
"item",
".",
"v",
"==",
"pseudoRoot",
":",
"break",
"\n",
"case",
"root",
"!=",
"nil",
":",
"size",
"+=",
"root",
".",
"Type",
".",
"Size",
"\n",
"default",
":",
"size",
"+=",
"p",
".",
"Size",
"(",
"obj",
")",
"\n",
"}",
"\n",
"for",
"_",
",",
"w",
":=",
"range",
"kids",
"{",
"size",
"+=",
"d",
".",
"size",
"[",
"w",
"]",
"\n",
"}",
"\n",
"d",
".",
"size",
"[",
"item",
".",
"v",
"]",
"=",
"size",
"\n",
"}",
"\n",
"}"
] |
// calcSize calculates the total retained size for each vertex.
|
[
"calcSize",
"calculates",
"the",
"total",
"retained",
"size",
"for",
"each",
"vertex",
"."
] |
19561fee47cf8cd0400d1b094c5898002f97cf90
|
https://github.com/golang/debug/blob/19561fee47cf8cd0400d1b094c5898002f97cf90/internal/gocore/dominator.go#L343-L384
|
test
|
golang/debug
|
cmd/viewcore/html.go
|
objField
|
func objField(c *gocore.Process, x gocore.Object, off int64) string {
t, r := c.Type(x)
if t == nil {
return fmt.Sprintf("f%d", off)
}
s := ""
if r > 1 {
s = fmt.Sprintf("[%d]", off/t.Size)
off %= t.Size
}
return s + typeFieldName(t, off)
}
|
go
|
func objField(c *gocore.Process, x gocore.Object, off int64) string {
t, r := c.Type(x)
if t == nil {
return fmt.Sprintf("f%d", off)
}
s := ""
if r > 1 {
s = fmt.Sprintf("[%d]", off/t.Size)
off %= t.Size
}
return s + typeFieldName(t, off)
}
|
[
"func",
"objField",
"(",
"c",
"*",
"gocore",
".",
"Process",
",",
"x",
"gocore",
".",
"Object",
",",
"off",
"int64",
")",
"string",
"{",
"t",
",",
"r",
":=",
"c",
".",
"Type",
"(",
"x",
")",
"\n",
"if",
"t",
"==",
"nil",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"f%d\"",
",",
"off",
")",
"\n",
"}",
"\n",
"s",
":=",
"\"\"",
"\n",
"if",
"r",
">",
"1",
"{",
"s",
"=",
"fmt",
".",
"Sprintf",
"(",
"\"[%d]\"",
",",
"off",
"/",
"t",
".",
"Size",
")",
"\n",
"off",
"%=",
"t",
".",
"Size",
"\n",
"}",
"\n",
"return",
"s",
"+",
"typeFieldName",
"(",
"t",
",",
"off",
")",
"\n",
"}"
] |
// Returns the name of the field at offset off in x.
|
[
"Returns",
"the",
"name",
"of",
"the",
"field",
"at",
"offset",
"off",
"in",
"x",
"."
] |
19561fee47cf8cd0400d1b094c5898002f97cf90
|
https://github.com/golang/debug/blob/19561fee47cf8cd0400d1b094c5898002f97cf90/cmd/viewcore/html.go#L393-L404
|
test
|
golang/debug
|
internal/core/process.go
|
Readable
|
func (p *Process) Readable(a Address) bool {
return p.findMapping(a) != nil
}
|
go
|
func (p *Process) Readable(a Address) bool {
return p.findMapping(a) != nil
}
|
[
"func",
"(",
"p",
"*",
"Process",
")",
"Readable",
"(",
"a",
"Address",
")",
"bool",
"{",
"return",
"p",
".",
"findMapping",
"(",
"a",
")",
"!=",
"nil",
"\n",
"}"
] |
// Readable reports whether the address a is readable.
|
[
"Readable",
"reports",
"whether",
"the",
"address",
"a",
"is",
"readable",
"."
] |
19561fee47cf8cd0400d1b094c5898002f97cf90
|
https://github.com/golang/debug/blob/19561fee47cf8cd0400d1b094c5898002f97cf90/internal/core/process.go#L70-L72
|
test
|
golang/debug
|
internal/core/process.go
|
ReadableN
|
func (p *Process) ReadableN(a Address, n int64) bool {
for {
m := p.findMapping(a)
if m == nil || m.perm&Read == 0 {
return false
}
c := m.max.Sub(a)
if n <= c {
return true
}
n -= c
a = a.Add(c)
}
}
|
go
|
func (p *Process) ReadableN(a Address, n int64) bool {
for {
m := p.findMapping(a)
if m == nil || m.perm&Read == 0 {
return false
}
c := m.max.Sub(a)
if n <= c {
return true
}
n -= c
a = a.Add(c)
}
}
|
[
"func",
"(",
"p",
"*",
"Process",
")",
"ReadableN",
"(",
"a",
"Address",
",",
"n",
"int64",
")",
"bool",
"{",
"for",
"{",
"m",
":=",
"p",
".",
"findMapping",
"(",
"a",
")",
"\n",
"if",
"m",
"==",
"nil",
"||",
"m",
".",
"perm",
"&",
"Read",
"==",
"0",
"{",
"return",
"false",
"\n",
"}",
"\n",
"c",
":=",
"m",
".",
"max",
".",
"Sub",
"(",
"a",
")",
"\n",
"if",
"n",
"<=",
"c",
"{",
"return",
"true",
"\n",
"}",
"\n",
"n",
"-=",
"c",
"\n",
"a",
"=",
"a",
".",
"Add",
"(",
"c",
")",
"\n",
"}",
"\n",
"}"
] |
// ReadableN reports whether the n bytes starting at address a are readable.
|
[
"ReadableN",
"reports",
"whether",
"the",
"n",
"bytes",
"starting",
"at",
"address",
"a",
"are",
"readable",
"."
] |
19561fee47cf8cd0400d1b094c5898002f97cf90
|
https://github.com/golang/debug/blob/19561fee47cf8cd0400d1b094c5898002f97cf90/internal/core/process.go#L75-L88
|
test
|
golang/debug
|
internal/core/process.go
|
splitMappingsAt
|
func (p *Process) splitMappingsAt(a Address) {
for _, m := range p.memory.mappings {
if a < m.min || a > m.max {
continue
}
if a == m.min || a == m.max {
return
}
// Split this mapping at a.
m2 := new(Mapping)
*m2 = *m
m.max = a
m2.min = a
if m2.f != nil {
m2.off += m.Size()
}
if m2.origF != nil {
m2.origOff += m.Size()
}
p.memory.mappings = append(p.memory.mappings, m2)
return
}
}
|
go
|
func (p *Process) splitMappingsAt(a Address) {
for _, m := range p.memory.mappings {
if a < m.min || a > m.max {
continue
}
if a == m.min || a == m.max {
return
}
// Split this mapping at a.
m2 := new(Mapping)
*m2 = *m
m.max = a
m2.min = a
if m2.f != nil {
m2.off += m.Size()
}
if m2.origF != nil {
m2.origOff += m.Size()
}
p.memory.mappings = append(p.memory.mappings, m2)
return
}
}
|
[
"func",
"(",
"p",
"*",
"Process",
")",
"splitMappingsAt",
"(",
"a",
"Address",
")",
"{",
"for",
"_",
",",
"m",
":=",
"range",
"p",
".",
"memory",
".",
"mappings",
"{",
"if",
"a",
"<",
"m",
".",
"min",
"||",
"a",
">",
"m",
".",
"max",
"{",
"continue",
"\n",
"}",
"\n",
"if",
"a",
"==",
"m",
".",
"min",
"||",
"a",
"==",
"m",
".",
"max",
"{",
"return",
"\n",
"}",
"\n",
"m2",
":=",
"new",
"(",
"Mapping",
")",
"\n",
"*",
"m2",
"=",
"*",
"m",
"\n",
"m",
".",
"max",
"=",
"a",
"\n",
"m2",
".",
"min",
"=",
"a",
"\n",
"if",
"m2",
".",
"f",
"!=",
"nil",
"{",
"m2",
".",
"off",
"+=",
"m",
".",
"Size",
"(",
")",
"\n",
"}",
"\n",
"if",
"m2",
".",
"origF",
"!=",
"nil",
"{",
"m2",
".",
"origOff",
"+=",
"m",
".",
"Size",
"(",
")",
"\n",
"}",
"\n",
"p",
".",
"memory",
".",
"mappings",
"=",
"append",
"(",
"p",
".",
"memory",
".",
"mappings",
",",
"m2",
")",
"\n",
"return",
"\n",
"}",
"\n",
"}"
] |
// splitMappingsAt ensures that a is not in the middle of any mapping.
// Splits mappings as necessary.
|
[
"splitMappingsAt",
"ensures",
"that",
"a",
"is",
"not",
"in",
"the",
"middle",
"of",
"any",
"mapping",
".",
"Splits",
"mappings",
"as",
"necessary",
"."
] |
19561fee47cf8cd0400d1b094c5898002f97cf90
|
https://github.com/golang/debug/blob/19561fee47cf8cd0400d1b094c5898002f97cf90/internal/core/process.go#L533-L555
|
test
|
golang/debug
|
internal/gocore/type.go
|
DynamicType
|
func (p *Process) DynamicType(t *Type, a core.Address) *Type {
switch t.Kind {
default:
panic("asking for the dynamic type of a non-interface")
case KindEface:
x := p.proc.ReadPtr(a)
if x == 0 {
return nil
}
return p.runtimeType2Type(x)
case KindIface:
x := p.proc.ReadPtr(a)
if x == 0 {
return nil
}
// Read type out of itab.
x = p.proc.ReadPtr(x.Add(p.proc.PtrSize()))
return p.runtimeType2Type(x)
}
}
|
go
|
func (p *Process) DynamicType(t *Type, a core.Address) *Type {
switch t.Kind {
default:
panic("asking for the dynamic type of a non-interface")
case KindEface:
x := p.proc.ReadPtr(a)
if x == 0 {
return nil
}
return p.runtimeType2Type(x)
case KindIface:
x := p.proc.ReadPtr(a)
if x == 0 {
return nil
}
// Read type out of itab.
x = p.proc.ReadPtr(x.Add(p.proc.PtrSize()))
return p.runtimeType2Type(x)
}
}
|
[
"func",
"(",
"p",
"*",
"Process",
")",
"DynamicType",
"(",
"t",
"*",
"Type",
",",
"a",
"core",
".",
"Address",
")",
"*",
"Type",
"{",
"switch",
"t",
".",
"Kind",
"{",
"default",
":",
"panic",
"(",
"\"asking for the dynamic type of a non-interface\"",
")",
"\n",
"case",
"KindEface",
":",
"x",
":=",
"p",
".",
"proc",
".",
"ReadPtr",
"(",
"a",
")",
"\n",
"if",
"x",
"==",
"0",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"return",
"p",
".",
"runtimeType2Type",
"(",
"x",
")",
"\n",
"case",
"KindIface",
":",
"x",
":=",
"p",
".",
"proc",
".",
"ReadPtr",
"(",
"a",
")",
"\n",
"if",
"x",
"==",
"0",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"x",
"=",
"p",
".",
"proc",
".",
"ReadPtr",
"(",
"x",
".",
"Add",
"(",
"p",
".",
"proc",
".",
"PtrSize",
"(",
")",
")",
")",
"\n",
"return",
"p",
".",
"runtimeType2Type",
"(",
"x",
")",
"\n",
"}",
"\n",
"}"
] |
// DynamicType returns the concrete type stored in the interface type t at address a.
// If the interface is nil, returns nil.
|
[
"DynamicType",
"returns",
"the",
"concrete",
"type",
"stored",
"in",
"the",
"interface",
"type",
"t",
"at",
"address",
"a",
".",
"If",
"the",
"interface",
"is",
"nil",
"returns",
"nil",
"."
] |
19561fee47cf8cd0400d1b094c5898002f97cf90
|
https://github.com/golang/debug/blob/19561fee47cf8cd0400d1b094c5898002f97cf90/internal/gocore/type.go#L92-L111
|
test
|
concourse/baggageclaim
|
fs/btrfs.go
|
Create
|
func (fs *BtrfsFilesystem) Create(bytes uint64) error {
// significantly
idempotent := exec.Command("bash", "-e", "-x", "-c", `
if [ ! -e $IMAGE_PATH ] || [ "$(stat --printf="%s" $IMAGE_PATH)" != "$SIZE_IN_BYTES" ]; then
touch $IMAGE_PATH
truncate -s ${SIZE_IN_BYTES} $IMAGE_PATH
fi
lo="$(losetup -j $IMAGE_PATH | cut -d':' -f1)"
if [ -z "$lo" ]; then
lo="$(losetup -f --show $IMAGE_PATH)"
fi
if ! file $IMAGE_PATH | grep BTRFS; then
`+fs.mkfsBin+` --nodiscard $IMAGE_PATH
fi
mkdir -p $MOUNT_PATH
if ! mountpoint -q $MOUNT_PATH; then
mount -t btrfs $lo $MOUNT_PATH
fi
`)
idempotent.Env = []string{
"PATH=" + os.Getenv("PATH"),
"MOUNT_PATH=" + fs.mountPath,
"IMAGE_PATH=" + fs.imagePath,
fmt.Sprintf("SIZE_IN_BYTES=%d", bytes),
}
_, err := fs.run(idempotent)
return err
}
|
go
|
func (fs *BtrfsFilesystem) Create(bytes uint64) error {
// significantly
idempotent := exec.Command("bash", "-e", "-x", "-c", `
if [ ! -e $IMAGE_PATH ] || [ "$(stat --printf="%s" $IMAGE_PATH)" != "$SIZE_IN_BYTES" ]; then
touch $IMAGE_PATH
truncate -s ${SIZE_IN_BYTES} $IMAGE_PATH
fi
lo="$(losetup -j $IMAGE_PATH | cut -d':' -f1)"
if [ -z "$lo" ]; then
lo="$(losetup -f --show $IMAGE_PATH)"
fi
if ! file $IMAGE_PATH | grep BTRFS; then
`+fs.mkfsBin+` --nodiscard $IMAGE_PATH
fi
mkdir -p $MOUNT_PATH
if ! mountpoint -q $MOUNT_PATH; then
mount -t btrfs $lo $MOUNT_PATH
fi
`)
idempotent.Env = []string{
"PATH=" + os.Getenv("PATH"),
"MOUNT_PATH=" + fs.mountPath,
"IMAGE_PATH=" + fs.imagePath,
fmt.Sprintf("SIZE_IN_BYTES=%d", bytes),
}
_, err := fs.run(idempotent)
return err
}
|
[
"func",
"(",
"fs",
"*",
"BtrfsFilesystem",
")",
"Create",
"(",
"bytes",
"uint64",
")",
"error",
"{",
"idempotent",
":=",
"exec",
".",
"Command",
"(",
"\"bash\"",
",",
"\"-e\"",
",",
"\"-x\"",
",",
"\"-c\"",
",",
"`\t\tif [ ! -e $IMAGE_PATH ] || [ \"$(stat --printf=\"%s\" $IMAGE_PATH)\" != \"$SIZE_IN_BYTES\" ]; then\t\t\ttouch $IMAGE_PATH\t\t\ttruncate -s ${SIZE_IN_BYTES} $IMAGE_PATH\t\tfi\t\tlo=\"$(losetup -j $IMAGE_PATH | cut -d':' -f1)\"\t\tif [ -z \"$lo\" ]; then\t\t\tlo=\"$(losetup -f --show $IMAGE_PATH)\"\t\tfi\t\tif ! file $IMAGE_PATH | grep BTRFS; then\t\t\t`",
"+",
"fs",
".",
"mkfsBin",
"+",
"` --nodiscard $IMAGE_PATH\t\tfi\t\tmkdir -p $MOUNT_PATH\t\tif ! mountpoint -q $MOUNT_PATH; then\t\t\tmount -t btrfs $lo $MOUNT_PATH\t\tfi\t`",
")",
"\n",
"idempotent",
".",
"Env",
"=",
"[",
"]",
"string",
"{",
"\"PATH=\"",
"+",
"os",
".",
"Getenv",
"(",
"\"PATH\"",
")",
",",
"\"MOUNT_PATH=\"",
"+",
"fs",
".",
"mountPath",
",",
"\"IMAGE_PATH=\"",
"+",
"fs",
".",
"imagePath",
",",
"fmt",
".",
"Sprintf",
"(",
"\"SIZE_IN_BYTES=%d\"",
",",
"bytes",
")",
",",
"}",
"\n",
"_",
",",
"err",
":=",
"fs",
".",
"run",
"(",
"idempotent",
")",
"\n",
"return",
"err",
"\n",
"}"
] |
// lower your expectations
|
[
"lower",
"your",
"expectations"
] |
6fee2c8029decd1325b5e9d6d4069ea4a7c4578d
|
https://github.com/concourse/baggageclaim/blob/6fee2c8029decd1325b5e9d6d4069ea4a7c4578d/fs/btrfs.go#L31-L65
|
test
|
goware/urlx
|
urlx.go
|
Resolve
|
func Resolve(u *url.URL) (*net.IPAddr, error) {
host, _, err := SplitHostPort(u)
if err != nil {
return nil, err
}
addr, err := net.ResolveIPAddr("ip", host)
if err != nil {
return nil, err
}
return addr, nil
}
|
go
|
func Resolve(u *url.URL) (*net.IPAddr, error) {
host, _, err := SplitHostPort(u)
if err != nil {
return nil, err
}
addr, err := net.ResolveIPAddr("ip", host)
if err != nil {
return nil, err
}
return addr, nil
}
|
[
"func",
"Resolve",
"(",
"u",
"*",
"url",
".",
"URL",
")",
"(",
"*",
"net",
".",
"IPAddr",
",",
"error",
")",
"{",
"host",
",",
"_",
",",
"err",
":=",
"SplitHostPort",
"(",
"u",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"addr",
",",
"err",
":=",
"net",
".",
"ResolveIPAddr",
"(",
"\"ip\"",
",",
"host",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"addr",
",",
"nil",
"\n",
"}"
] |
// Resolve resolves the URL host to its IP address.
|
[
"Resolve",
"resolves",
"the",
"URL",
"host",
"to",
"its",
"IP",
"address",
"."
] |
4fc201f7f862af2abfaca0c2904be15ea0037a34
|
https://github.com/goware/urlx/blob/4fc201f7f862af2abfaca0c2904be15ea0037a34/urlx.go#L175-L187
|
test
|
google/shlex
|
shlex.go
|
Equal
|
func (a *Token) Equal(b *Token) bool {
if a == nil || b == nil {
return false
}
if a.tokenType != b.tokenType {
return false
}
return a.value == b.value
}
|
go
|
func (a *Token) Equal(b *Token) bool {
if a == nil || b == nil {
return false
}
if a.tokenType != b.tokenType {
return false
}
return a.value == b.value
}
|
[
"func",
"(",
"a",
"*",
"Token",
")",
"Equal",
"(",
"b",
"*",
"Token",
")",
"bool",
"{",
"if",
"a",
"==",
"nil",
"||",
"b",
"==",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"a",
".",
"tokenType",
"!=",
"b",
".",
"tokenType",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"a",
".",
"value",
"==",
"b",
".",
"value",
"\n",
"}"
] |
// Equal reports whether tokens a, and b, are equal.
// Two tokens are equal if both their types and values are equal. A nil token can
// never be equal to another token.
|
[
"Equal",
"reports",
"whether",
"tokens",
"a",
"and",
"b",
"are",
"equal",
".",
"Two",
"tokens",
"are",
"equal",
"if",
"both",
"their",
"types",
"and",
"values",
"are",
"equal",
".",
"A",
"nil",
"token",
"can",
"never",
"be",
"equal",
"to",
"another",
"token",
"."
] |
c34317bd91bf98fab745d77b03933cf8769299fe
|
https://github.com/google/shlex/blob/c34317bd91bf98fab745d77b03933cf8769299fe/shlex.go#L67-L75
|
test
|
google/shlex
|
shlex.go
|
newDefaultClassifier
|
func newDefaultClassifier() tokenClassifier {
t := tokenClassifier{}
t.addRuneClass(spaceRunes, spaceRuneClass)
t.addRuneClass(escapingQuoteRunes, escapingQuoteRuneClass)
t.addRuneClass(nonEscapingQuoteRunes, nonEscapingQuoteRuneClass)
t.addRuneClass(escapeRunes, escapeRuneClass)
t.addRuneClass(commentRunes, commentRuneClass)
return t
}
|
go
|
func newDefaultClassifier() tokenClassifier {
t := tokenClassifier{}
t.addRuneClass(spaceRunes, spaceRuneClass)
t.addRuneClass(escapingQuoteRunes, escapingQuoteRuneClass)
t.addRuneClass(nonEscapingQuoteRunes, nonEscapingQuoteRuneClass)
t.addRuneClass(escapeRunes, escapeRuneClass)
t.addRuneClass(commentRunes, commentRuneClass)
return t
}
|
[
"func",
"newDefaultClassifier",
"(",
")",
"tokenClassifier",
"{",
"t",
":=",
"tokenClassifier",
"{",
"}",
"\n",
"t",
".",
"addRuneClass",
"(",
"spaceRunes",
",",
"spaceRuneClass",
")",
"\n",
"t",
".",
"addRuneClass",
"(",
"escapingQuoteRunes",
",",
"escapingQuoteRuneClass",
")",
"\n",
"t",
".",
"addRuneClass",
"(",
"nonEscapingQuoteRunes",
",",
"nonEscapingQuoteRuneClass",
")",
"\n",
"t",
".",
"addRuneClass",
"(",
"escapeRunes",
",",
"escapeRuneClass",
")",
"\n",
"t",
".",
"addRuneClass",
"(",
"commentRunes",
",",
"commentRuneClass",
")",
"\n",
"return",
"t",
"\n",
"}"
] |
// newDefaultClassifier creates a new classifier for ASCII characters.
|
[
"newDefaultClassifier",
"creates",
"a",
"new",
"classifier",
"for",
"ASCII",
"characters",
"."
] |
c34317bd91bf98fab745d77b03933cf8769299fe
|
https://github.com/google/shlex/blob/c34317bd91bf98fab745d77b03933cf8769299fe/shlex.go#L126-L134
|
test
|
google/shlex
|
shlex.go
|
Next
|
func (l *Lexer) Next() (string, error) {
for {
token, err := (*Tokenizer)(l).Next()
if err != nil {
return "", err
}
switch token.tokenType {
case WordToken:
return token.value, nil
case CommentToken:
// skip comments
default:
return "", fmt.Errorf("Unknown token type: %v", token.tokenType)
}
}
}
|
go
|
func (l *Lexer) Next() (string, error) {
for {
token, err := (*Tokenizer)(l).Next()
if err != nil {
return "", err
}
switch token.tokenType {
case WordToken:
return token.value, nil
case CommentToken:
// skip comments
default:
return "", fmt.Errorf("Unknown token type: %v", token.tokenType)
}
}
}
|
[
"func",
"(",
"l",
"*",
"Lexer",
")",
"Next",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"for",
"{",
"token",
",",
"err",
":=",
"(",
"*",
"Tokenizer",
")",
"(",
"l",
")",
".",
"Next",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"\"",
",",
"err",
"\n",
"}",
"\n",
"switch",
"token",
".",
"tokenType",
"{",
"case",
"WordToken",
":",
"return",
"token",
".",
"value",
",",
"nil",
"\n",
"case",
"CommentToken",
":",
"default",
":",
"return",
"\"\"",
",",
"fmt",
".",
"Errorf",
"(",
"\"Unknown token type: %v\"",
",",
"token",
".",
"tokenType",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// Next returns the next word, or an error. If there are no more words,
// the error will be io.EOF.
|
[
"Next",
"returns",
"the",
"next",
"word",
"or",
"an",
"error",
".",
"If",
"there",
"are",
"no",
"more",
"words",
"the",
"error",
"will",
"be",
"io",
".",
"EOF",
"."
] |
c34317bd91bf98fab745d77b03933cf8769299fe
|
https://github.com/google/shlex/blob/c34317bd91bf98fab745d77b03933cf8769299fe/shlex.go#L152-L167
|
test
|
google/shlex
|
shlex.go
|
NewTokenizer
|
func NewTokenizer(r io.Reader) *Tokenizer {
input := bufio.NewReader(r)
classifier := newDefaultClassifier()
return &Tokenizer{
input: *input,
classifier: classifier}
}
|
go
|
func NewTokenizer(r io.Reader) *Tokenizer {
input := bufio.NewReader(r)
classifier := newDefaultClassifier()
return &Tokenizer{
input: *input,
classifier: classifier}
}
|
[
"func",
"NewTokenizer",
"(",
"r",
"io",
".",
"Reader",
")",
"*",
"Tokenizer",
"{",
"input",
":=",
"bufio",
".",
"NewReader",
"(",
"r",
")",
"\n",
"classifier",
":=",
"newDefaultClassifier",
"(",
")",
"\n",
"return",
"&",
"Tokenizer",
"{",
"input",
":",
"*",
"input",
",",
"classifier",
":",
"classifier",
"}",
"\n",
"}"
] |
// NewTokenizer creates a new tokenizer from an input stream.
|
[
"NewTokenizer",
"creates",
"a",
"new",
"tokenizer",
"from",
"an",
"input",
"stream",
"."
] |
c34317bd91bf98fab745d77b03933cf8769299fe
|
https://github.com/google/shlex/blob/c34317bd91bf98fab745d77b03933cf8769299fe/shlex.go#L176-L182
|
test
|
google/shlex
|
shlex.go
|
Split
|
func Split(s string) ([]string, error) {
l := NewLexer(strings.NewReader(s))
subStrings := make([]string, 0)
for {
word, err := l.Next()
if err != nil {
if err == io.EOF {
return subStrings, nil
}
return subStrings, err
}
subStrings = append(subStrings, word)
}
}
|
go
|
func Split(s string) ([]string, error) {
l := NewLexer(strings.NewReader(s))
subStrings := make([]string, 0)
for {
word, err := l.Next()
if err != nil {
if err == io.EOF {
return subStrings, nil
}
return subStrings, err
}
subStrings = append(subStrings, word)
}
}
|
[
"func",
"Split",
"(",
"s",
"string",
")",
"(",
"[",
"]",
"string",
",",
"error",
")",
"{",
"l",
":=",
"NewLexer",
"(",
"strings",
".",
"NewReader",
"(",
"s",
")",
")",
"\n",
"subStrings",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"0",
")",
"\n",
"for",
"{",
"word",
",",
"err",
":=",
"l",
".",
"Next",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"if",
"err",
"==",
"io",
".",
"EOF",
"{",
"return",
"subStrings",
",",
"nil",
"\n",
"}",
"\n",
"return",
"subStrings",
",",
"err",
"\n",
"}",
"\n",
"subStrings",
"=",
"append",
"(",
"subStrings",
",",
"word",
")",
"\n",
"}",
"\n",
"}"
] |
// Split partitions a string into a slice of strings.
|
[
"Split",
"partitions",
"a",
"string",
"into",
"a",
"slice",
"of",
"strings",
"."
] |
c34317bd91bf98fab745d77b03933cf8769299fe
|
https://github.com/google/shlex/blob/c34317bd91bf98fab745d77b03933cf8769299fe/shlex.go#L403-L416
|
test
|
mgutz/str
|
funcsAO.go
|
Between
|
func Between(s, left, right string) string {
l := len(left)
startPos := strings.Index(s, left)
if startPos < 0 {
return ""
}
endPos := IndexOf(s, right, startPos+l)
//log.Printf("%s: left %s right %s start %d end %d", s, left, right, startPos+l, endPos)
if endPos < 0 {
return ""
} else if right == "" {
return s[endPos:]
} else {
return s[startPos+l : endPos]
}
}
|
go
|
func Between(s, left, right string) string {
l := len(left)
startPos := strings.Index(s, left)
if startPos < 0 {
return ""
}
endPos := IndexOf(s, right, startPos+l)
//log.Printf("%s: left %s right %s start %d end %d", s, left, right, startPos+l, endPos)
if endPos < 0 {
return ""
} else if right == "" {
return s[endPos:]
} else {
return s[startPos+l : endPos]
}
}
|
[
"func",
"Between",
"(",
"s",
",",
"left",
",",
"right",
"string",
")",
"string",
"{",
"l",
":=",
"len",
"(",
"left",
")",
"\n",
"startPos",
":=",
"strings",
".",
"Index",
"(",
"s",
",",
"left",
")",
"\n",
"if",
"startPos",
"<",
"0",
"{",
"return",
"\"\"",
"\n",
"}",
"\n",
"endPos",
":=",
"IndexOf",
"(",
"s",
",",
"right",
",",
"startPos",
"+",
"l",
")",
"\n",
"if",
"endPos",
"<",
"0",
"{",
"return",
"\"\"",
"\n",
"}",
"else",
"if",
"right",
"==",
"\"\"",
"{",
"return",
"s",
"[",
"endPos",
":",
"]",
"\n",
"}",
"else",
"{",
"return",
"s",
"[",
"startPos",
"+",
"l",
":",
"endPos",
"]",
"\n",
"}",
"\n",
"}"
] |
// Between extracts a string between left and right strings.
|
[
"Between",
"extracts",
"a",
"string",
"between",
"left",
"and",
"right",
"strings",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L51-L66
|
test
|
mgutz/str
|
funcsAO.go
|
BetweenF
|
func BetweenF(left, right string) func(string) string {
return func(s string) string {
return Between(s, left, right)
}
}
|
go
|
func BetweenF(left, right string) func(string) string {
return func(s string) string {
return Between(s, left, right)
}
}
|
[
"func",
"BetweenF",
"(",
"left",
",",
"right",
"string",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"Between",
"(",
"s",
",",
"left",
",",
"right",
")",
"\n",
"}",
"\n",
"}"
] |
// BetweenF is the filter form for Between.
|
[
"BetweenF",
"is",
"the",
"filter",
"form",
"for",
"Between",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L69-L73
|
test
|
mgutz/str
|
funcsAO.go
|
Camelize
|
func Camelize(s string) string {
return camelizeRe.ReplaceAllStringFunc(s, func(val string) string {
val = strings.ToUpper(val)
val = camelizeRe2.ReplaceAllString(val, "")
return val
})
}
|
go
|
func Camelize(s string) string {
return camelizeRe.ReplaceAllStringFunc(s, func(val string) string {
val = strings.ToUpper(val)
val = camelizeRe2.ReplaceAllString(val, "")
return val
})
}
|
[
"func",
"Camelize",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"camelizeRe",
".",
"ReplaceAllStringFunc",
"(",
"s",
",",
"func",
"(",
"val",
"string",
")",
"string",
"{",
"val",
"=",
"strings",
".",
"ToUpper",
"(",
"val",
")",
"\n",
"val",
"=",
"camelizeRe2",
".",
"ReplaceAllString",
"(",
"val",
",",
"\"\"",
")",
"\n",
"return",
"val",
"\n",
"}",
")",
"\n",
"}"
] |
// Camelize return new string which removes any underscores or dashes and convert a string into camel casing.
|
[
"Camelize",
"return",
"new",
"string",
"which",
"removes",
"any",
"underscores",
"or",
"dashes",
"and",
"convert",
"a",
"string",
"into",
"camel",
"casing",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L76-L82
|
test
|
mgutz/str
|
funcsAO.go
|
Capitalize
|
func Capitalize(s string) string {
return strings.ToUpper(s[0:1]) + strings.ToLower(s[1:])
}
|
go
|
func Capitalize(s string) string {
return strings.ToUpper(s[0:1]) + strings.ToLower(s[1:])
}
|
[
"func",
"Capitalize",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"strings",
".",
"ToUpper",
"(",
"s",
"[",
"0",
":",
"1",
"]",
")",
"+",
"strings",
".",
"ToLower",
"(",
"s",
"[",
"1",
":",
"]",
")",
"\n",
"}"
] |
// Capitalize uppercases the first char of s and lowercases the rest.
|
[
"Capitalize",
"uppercases",
"the",
"first",
"char",
"of",
"s",
"and",
"lowercases",
"the",
"rest",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L85-L87
|
test
|
mgutz/str
|
funcsAO.go
|
CharAt
|
func CharAt(s string, index int) string {
l := len(s)
shortcut := index < 0 || index > l-1 || l == 0
if shortcut {
return ""
}
return s[index : index+1]
}
|
go
|
func CharAt(s string, index int) string {
l := len(s)
shortcut := index < 0 || index > l-1 || l == 0
if shortcut {
return ""
}
return s[index : index+1]
}
|
[
"func",
"CharAt",
"(",
"s",
"string",
",",
"index",
"int",
")",
"string",
"{",
"l",
":=",
"len",
"(",
"s",
")",
"\n",
"shortcut",
":=",
"index",
"<",
"0",
"||",
"index",
">",
"l",
"-",
"1",
"||",
"l",
"==",
"0",
"\n",
"if",
"shortcut",
"{",
"return",
"\"\"",
"\n",
"}",
"\n",
"return",
"s",
"[",
"index",
":",
"index",
"+",
"1",
"]",
"\n",
"}"
] |
// CharAt returns a string from the character at the specified position.
|
[
"CharAt",
"returns",
"a",
"string",
"from",
"the",
"character",
"at",
"the",
"specified",
"position",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L90-L97
|
test
|
mgutz/str
|
funcsAO.go
|
CharAtF
|
func CharAtF(index int) func(string) string {
return func(s string) string {
return CharAt(s, index)
}
}
|
go
|
func CharAtF(index int) func(string) string {
return func(s string) string {
return CharAt(s, index)
}
}
|
[
"func",
"CharAtF",
"(",
"index",
"int",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"CharAt",
"(",
"s",
",",
"index",
")",
"\n",
"}",
"\n",
"}"
] |
// CharAtF is the filter form of CharAt.
|
[
"CharAtF",
"is",
"the",
"filter",
"form",
"of",
"CharAt",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L100-L104
|
test
|
mgutz/str
|
funcsAO.go
|
ChompLeft
|
func ChompLeft(s, prefix string) string {
if strings.HasPrefix(s, prefix) {
return s[len(prefix):]
}
return s
}
|
go
|
func ChompLeft(s, prefix string) string {
if strings.HasPrefix(s, prefix) {
return s[len(prefix):]
}
return s
}
|
[
"func",
"ChompLeft",
"(",
"s",
",",
"prefix",
"string",
")",
"string",
"{",
"if",
"strings",
".",
"HasPrefix",
"(",
"s",
",",
"prefix",
")",
"{",
"return",
"s",
"[",
"len",
"(",
"prefix",
")",
":",
"]",
"\n",
"}",
"\n",
"return",
"s",
"\n",
"}"
] |
// ChompLeft removes prefix at the start of a string.
|
[
"ChompLeft",
"removes",
"prefix",
"at",
"the",
"start",
"of",
"a",
"string",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L107-L112
|
test
|
mgutz/str
|
funcsAO.go
|
ChompLeftF
|
func ChompLeftF(prefix string) func(string) string {
return func(s string) string {
return ChompLeft(s, prefix)
}
}
|
go
|
func ChompLeftF(prefix string) func(string) string {
return func(s string) string {
return ChompLeft(s, prefix)
}
}
|
[
"func",
"ChompLeftF",
"(",
"prefix",
"string",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"ChompLeft",
"(",
"s",
",",
"prefix",
")",
"\n",
"}",
"\n",
"}"
] |
// ChompLeftF is the filter form of ChompLeft.
|
[
"ChompLeftF",
"is",
"the",
"filter",
"form",
"of",
"ChompLeft",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L115-L119
|
test
|
mgutz/str
|
funcsAO.go
|
ChompRight
|
func ChompRight(s, suffix string) string {
if strings.HasSuffix(s, suffix) {
return s[:len(s)-len(suffix)]
}
return s
}
|
go
|
func ChompRight(s, suffix string) string {
if strings.HasSuffix(s, suffix) {
return s[:len(s)-len(suffix)]
}
return s
}
|
[
"func",
"ChompRight",
"(",
"s",
",",
"suffix",
"string",
")",
"string",
"{",
"if",
"strings",
".",
"HasSuffix",
"(",
"s",
",",
"suffix",
")",
"{",
"return",
"s",
"[",
":",
"len",
"(",
"s",
")",
"-",
"len",
"(",
"suffix",
")",
"]",
"\n",
"}",
"\n",
"return",
"s",
"\n",
"}"
] |
// ChompRight removes suffix from end of s.
|
[
"ChompRight",
"removes",
"suffix",
"from",
"end",
"of",
"s",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L122-L127
|
test
|
mgutz/str
|
funcsAO.go
|
ChompRightF
|
func ChompRightF(suffix string) func(string) string {
return func(s string) string {
return ChompRight(s, suffix)
}
}
|
go
|
func ChompRightF(suffix string) func(string) string {
return func(s string) string {
return ChompRight(s, suffix)
}
}
|
[
"func",
"ChompRightF",
"(",
"suffix",
"string",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"ChompRight",
"(",
"s",
",",
"suffix",
")",
"\n",
"}",
"\n",
"}"
] |
// ChompRightF is the filter form of ChompRight.
|
[
"ChompRightF",
"is",
"the",
"filter",
"form",
"of",
"ChompRight",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L130-L134
|
test
|
mgutz/str
|
funcsAO.go
|
ClassifyF
|
func ClassifyF(s string) func(string) string {
return func(s string) string {
return Classify(s)
}
}
|
go
|
func ClassifyF(s string) func(string) string {
return func(s string) string {
return Classify(s)
}
}
|
[
"func",
"ClassifyF",
"(",
"s",
"string",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"Classify",
"(",
"s",
")",
"\n",
"}",
"\n",
"}"
] |
// ClassifyF is the filter form of Classify.
|
[
"ClassifyF",
"is",
"the",
"filter",
"form",
"of",
"Classify",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L142-L146
|
test
|
mgutz/str
|
funcsAO.go
|
Clean
|
func Clean(s string) string {
s = spacesRe.ReplaceAllString(s, " ")
s = beginEndSpacesRe.ReplaceAllString(s, "")
return s
}
|
go
|
func Clean(s string) string {
s = spacesRe.ReplaceAllString(s, " ")
s = beginEndSpacesRe.ReplaceAllString(s, "")
return s
}
|
[
"func",
"Clean",
"(",
"s",
"string",
")",
"string",
"{",
"s",
"=",
"spacesRe",
".",
"ReplaceAllString",
"(",
"s",
",",
"\" \"",
")",
"\n",
"s",
"=",
"beginEndSpacesRe",
".",
"ReplaceAllString",
"(",
"s",
",",
"\"\"",
")",
"\n",
"return",
"s",
"\n",
"}"
] |
// Clean compresses all adjacent whitespace to a single space and trims s.
|
[
"Clean",
"compresses",
"all",
"adjacent",
"whitespace",
"to",
"a",
"single",
"space",
"and",
"trims",
"s",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L149-L153
|
test
|
mgutz/str
|
funcsAO.go
|
Dasherize
|
func Dasherize(s string) string {
s = strings.TrimSpace(s)
s = spaceUnderscoreRe.ReplaceAllString(s, "-")
s = capitalsRe.ReplaceAllString(s, "-$1")
s = dashesRe.ReplaceAllString(s, "-")
s = strings.ToLower(s)
return s
}
|
go
|
func Dasherize(s string) string {
s = strings.TrimSpace(s)
s = spaceUnderscoreRe.ReplaceAllString(s, "-")
s = capitalsRe.ReplaceAllString(s, "-$1")
s = dashesRe.ReplaceAllString(s, "-")
s = strings.ToLower(s)
return s
}
|
[
"func",
"Dasherize",
"(",
"s",
"string",
")",
"string",
"{",
"s",
"=",
"strings",
".",
"TrimSpace",
"(",
"s",
")",
"\n",
"s",
"=",
"spaceUnderscoreRe",
".",
"ReplaceAllString",
"(",
"s",
",",
"\"-\"",
")",
"\n",
"s",
"=",
"capitalsRe",
".",
"ReplaceAllString",
"(",
"s",
",",
"\"-$1\"",
")",
"\n",
"s",
"=",
"dashesRe",
".",
"ReplaceAllString",
"(",
"s",
",",
"\"-\"",
")",
"\n",
"s",
"=",
"strings",
".",
"ToLower",
"(",
"s",
")",
"\n",
"return",
"s",
"\n",
"}"
] |
// Dasherize converts a camel cased string into a string delimited by dashes.
|
[
"Dasherize",
"converts",
"a",
"camel",
"cased",
"string",
"into",
"a",
"string",
"delimited",
"by",
"dashes",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L156-L163
|
test
|
mgutz/str
|
funcsAO.go
|
EscapeHTML
|
func EscapeHTML(s string) string {
if Verbose {
fmt.Println("Use html.EscapeString instead of EscapeHTML")
}
return html.EscapeString(s)
}
|
go
|
func EscapeHTML(s string) string {
if Verbose {
fmt.Println("Use html.EscapeString instead of EscapeHTML")
}
return html.EscapeString(s)
}
|
[
"func",
"EscapeHTML",
"(",
"s",
"string",
")",
"string",
"{",
"if",
"Verbose",
"{",
"fmt",
".",
"Println",
"(",
"\"Use html.EscapeString instead of EscapeHTML\"",
")",
"\n",
"}",
"\n",
"return",
"html",
".",
"EscapeString",
"(",
"s",
")",
"\n",
"}"
] |
// EscapeHTML is alias for html.EscapeString.
|
[
"EscapeHTML",
"is",
"alias",
"for",
"html",
".",
"EscapeString",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L166-L171
|
test
|
mgutz/str
|
funcsAO.go
|
DecodeHTMLEntities
|
func DecodeHTMLEntities(s string) string {
if Verbose {
fmt.Println("Use html.UnescapeString instead of DecodeHTMLEntities")
}
return html.UnescapeString(s)
}
|
go
|
func DecodeHTMLEntities(s string) string {
if Verbose {
fmt.Println("Use html.UnescapeString instead of DecodeHTMLEntities")
}
return html.UnescapeString(s)
}
|
[
"func",
"DecodeHTMLEntities",
"(",
"s",
"string",
")",
"string",
"{",
"if",
"Verbose",
"{",
"fmt",
".",
"Println",
"(",
"\"Use html.UnescapeString instead of DecodeHTMLEntities\"",
")",
"\n",
"}",
"\n",
"return",
"html",
".",
"UnescapeString",
"(",
"s",
")",
"\n",
"}"
] |
// DecodeHTMLEntities decodes HTML entities into their proper string representation.
// DecodeHTMLEntities is an alias for html.UnescapeString
|
[
"DecodeHTMLEntities",
"decodes",
"HTML",
"entities",
"into",
"their",
"proper",
"string",
"representation",
".",
"DecodeHTMLEntities",
"is",
"an",
"alias",
"for",
"html",
".",
"UnescapeString"
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L175-L180
|
test
|
mgutz/str
|
funcsAO.go
|
EnsurePrefixF
|
func EnsurePrefixF(prefix string) func(string) string {
return func(s string) string {
return EnsurePrefix(s, prefix)
}
}
|
go
|
func EnsurePrefixF(prefix string) func(string) string {
return func(s string) string {
return EnsurePrefix(s, prefix)
}
}
|
[
"func",
"EnsurePrefixF",
"(",
"prefix",
"string",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"EnsurePrefix",
"(",
"s",
",",
"prefix",
")",
"\n",
"}",
"\n",
"}"
] |
// EnsurePrefixF is the filter form of EnsurePrefix.
|
[
"EnsurePrefixF",
"is",
"the",
"filter",
"form",
"of",
"EnsurePrefix",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L191-L195
|
test
|
mgutz/str
|
funcsAO.go
|
EnsureSuffixF
|
func EnsureSuffixF(suffix string) func(string) string {
return func(s string) string {
return EnsureSuffix(s, suffix)
}
}
|
go
|
func EnsureSuffixF(suffix string) func(string) string {
return func(s string) string {
return EnsureSuffix(s, suffix)
}
}
|
[
"func",
"EnsureSuffixF",
"(",
"suffix",
"string",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"EnsureSuffix",
"(",
"s",
",",
"suffix",
")",
"\n",
"}",
"\n",
"}"
] |
// EnsureSuffixF is the filter form of EnsureSuffix.
|
[
"EnsureSuffixF",
"is",
"the",
"filter",
"form",
"of",
"EnsureSuffix",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L206-L210
|
test
|
mgutz/str
|
funcsAO.go
|
Humanize
|
func Humanize(s string) string {
if s == "" {
return s
}
s = Underscore(s)
var humanizeRe = regexp.MustCompile(`_id$`)
s = humanizeRe.ReplaceAllString(s, "")
s = strings.Replace(s, "_", " ", -1)
s = strings.TrimSpace(s)
s = Capitalize(s)
return s
}
|
go
|
func Humanize(s string) string {
if s == "" {
return s
}
s = Underscore(s)
var humanizeRe = regexp.MustCompile(`_id$`)
s = humanizeRe.ReplaceAllString(s, "")
s = strings.Replace(s, "_", " ", -1)
s = strings.TrimSpace(s)
s = Capitalize(s)
return s
}
|
[
"func",
"Humanize",
"(",
"s",
"string",
")",
"string",
"{",
"if",
"s",
"==",
"\"\"",
"{",
"return",
"s",
"\n",
"}",
"\n",
"s",
"=",
"Underscore",
"(",
"s",
")",
"\n",
"var",
"humanizeRe",
"=",
"regexp",
".",
"MustCompile",
"(",
"`_id$`",
")",
"\n",
"s",
"=",
"humanizeRe",
".",
"ReplaceAllString",
"(",
"s",
",",
"\"\"",
")",
"\n",
"s",
"=",
"strings",
".",
"Replace",
"(",
"s",
",",
"\"_\"",
",",
"\" \"",
",",
"-",
"1",
")",
"\n",
"s",
"=",
"strings",
".",
"TrimSpace",
"(",
"s",
")",
"\n",
"s",
"=",
"Capitalize",
"(",
"s",
")",
"\n",
"return",
"s",
"\n",
"}"
] |
// Humanize transforms s into a human friendly form.
|
[
"Humanize",
"transforms",
"s",
"into",
"a",
"human",
"friendly",
"form",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L213-L224
|
test
|
mgutz/str
|
funcsAO.go
|
Iif
|
func Iif(condition bool, truthy string, falsey string) string {
if condition {
return truthy
}
return falsey
}
|
go
|
func Iif(condition bool, truthy string, falsey string) string {
if condition {
return truthy
}
return falsey
}
|
[
"func",
"Iif",
"(",
"condition",
"bool",
",",
"truthy",
"string",
",",
"falsey",
"string",
")",
"string",
"{",
"if",
"condition",
"{",
"return",
"truthy",
"\n",
"}",
"\n",
"return",
"falsey",
"\n",
"}"
] |
// Iif is short for immediate if. If condition is true return truthy else falsey.
|
[
"Iif",
"is",
"short",
"for",
"immediate",
"if",
".",
"If",
"condition",
"is",
"true",
"return",
"truthy",
"else",
"falsey",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L227-L232
|
test
|
mgutz/str
|
funcsAO.go
|
IndexOf
|
func IndexOf(s string, needle string, start int) int {
l := len(s)
if needle == "" {
if start < 0 {
return 0
} else if start < l {
return start
} else {
return l
}
}
if start < 0 || start > l-1 {
return -1
}
pos := strings.Index(s[start:], needle)
if pos == -1 {
return -1
}
return start + pos
}
|
go
|
func IndexOf(s string, needle string, start int) int {
l := len(s)
if needle == "" {
if start < 0 {
return 0
} else if start < l {
return start
} else {
return l
}
}
if start < 0 || start > l-1 {
return -1
}
pos := strings.Index(s[start:], needle)
if pos == -1 {
return -1
}
return start + pos
}
|
[
"func",
"IndexOf",
"(",
"s",
"string",
",",
"needle",
"string",
",",
"start",
"int",
")",
"int",
"{",
"l",
":=",
"len",
"(",
"s",
")",
"\n",
"if",
"needle",
"==",
"\"\"",
"{",
"if",
"start",
"<",
"0",
"{",
"return",
"0",
"\n",
"}",
"else",
"if",
"start",
"<",
"l",
"{",
"return",
"start",
"\n",
"}",
"else",
"{",
"return",
"l",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"start",
"<",
"0",
"||",
"start",
">",
"l",
"-",
"1",
"{",
"return",
"-",
"1",
"\n",
"}",
"\n",
"pos",
":=",
"strings",
".",
"Index",
"(",
"s",
"[",
"start",
":",
"]",
",",
"needle",
")",
"\n",
"if",
"pos",
"==",
"-",
"1",
"{",
"return",
"-",
"1",
"\n",
"}",
"\n",
"return",
"start",
"+",
"pos",
"\n",
"}"
] |
// IndexOf finds the index of needle in s starting from start.
|
[
"IndexOf",
"finds",
"the",
"index",
"of",
"needle",
"in",
"s",
"starting",
"from",
"start",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L235-L254
|
test
|
mgutz/str
|
funcsAO.go
|
IsLower
|
func IsLower(s string) bool {
return IsAlpha(s) && s == strings.ToLower(s)
}
|
go
|
func IsLower(s string) bool {
return IsAlpha(s) && s == strings.ToLower(s)
}
|
[
"func",
"IsLower",
"(",
"s",
"string",
")",
"bool",
"{",
"return",
"IsAlpha",
"(",
"s",
")",
"&&",
"s",
"==",
"strings",
".",
"ToLower",
"(",
"s",
")",
"\n",
"}"
] |
// IsLower returns true if s comprised of all lower case characters.
|
[
"IsLower",
"returns",
"true",
"if",
"s",
"comprised",
"of",
"all",
"lower",
"case",
"characters",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L267-L269
|
test
|
mgutz/str
|
funcsAO.go
|
IsUpper
|
func IsUpper(s string) bool {
return IsAlpha(s) && s == strings.ToUpper(s)
}
|
go
|
func IsUpper(s string) bool {
return IsAlpha(s) && s == strings.ToUpper(s)
}
|
[
"func",
"IsUpper",
"(",
"s",
"string",
")",
"bool",
"{",
"return",
"IsAlpha",
"(",
"s",
")",
"&&",
"s",
"==",
"strings",
".",
"ToUpper",
"(",
"s",
")",
"\n",
"}"
] |
// IsUpper returns true if s contains all upper case chracters.
|
[
"IsUpper",
"returns",
"true",
"if",
"s",
"contains",
"all",
"upper",
"case",
"chracters",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L277-L279
|
test
|
mgutz/str
|
funcsAO.go
|
Left
|
func Left(s string, n int) string {
if n < 0 {
return Right(s, -n)
}
return Substr(s, 0, n)
}
|
go
|
func Left(s string, n int) string {
if n < 0 {
return Right(s, -n)
}
return Substr(s, 0, n)
}
|
[
"func",
"Left",
"(",
"s",
"string",
",",
"n",
"int",
")",
"string",
"{",
"if",
"n",
"<",
"0",
"{",
"return",
"Right",
"(",
"s",
",",
"-",
"n",
")",
"\n",
"}",
"\n",
"return",
"Substr",
"(",
"s",
",",
"0",
",",
"n",
")",
"\n",
"}"
] |
// Left returns the left substring of length n.
|
[
"Left",
"returns",
"the",
"left",
"substring",
"of",
"length",
"n",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L290-L295
|
test
|
mgutz/str
|
funcsAO.go
|
LeftF
|
func LeftF(n int) func(string) string {
return func(s string) string {
return Left(s, n)
}
}
|
go
|
func LeftF(n int) func(string) string {
return func(s string) string {
return Left(s, n)
}
}
|
[
"func",
"LeftF",
"(",
"n",
"int",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"Left",
"(",
"s",
",",
"n",
")",
"\n",
"}",
"\n",
"}"
] |
// LeftF is the filter form of Left.
|
[
"LeftF",
"is",
"the",
"filter",
"form",
"of",
"Left",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L298-L302
|
test
|
mgutz/str
|
funcsAO.go
|
Letters
|
func Letters(s string) []string {
result := []string{}
for _, r := range s {
result = append(result, string(r))
}
return result
}
|
go
|
func Letters(s string) []string {
result := []string{}
for _, r := range s {
result = append(result, string(r))
}
return result
}
|
[
"func",
"Letters",
"(",
"s",
"string",
")",
"[",
"]",
"string",
"{",
"result",
":=",
"[",
"]",
"string",
"{",
"}",
"\n",
"for",
"_",
",",
"r",
":=",
"range",
"s",
"{",
"result",
"=",
"append",
"(",
"result",
",",
"string",
"(",
"r",
")",
")",
"\n",
"}",
"\n",
"return",
"result",
"\n",
"}"
] |
// Letters returns an array of runes as strings so it can be indexed into.
|
[
"Letters",
"returns",
"an",
"array",
"of",
"runes",
"as",
"strings",
"so",
"it",
"can",
"be",
"indexed",
"into",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L310-L316
|
test
|
mgutz/str
|
funcsAO.go
|
Lines
|
func Lines(s string) []string {
s = strings.Replace(s, "\r\n", "\n", -1)
return strings.Split(s, "\n")
}
|
go
|
func Lines(s string) []string {
s = strings.Replace(s, "\r\n", "\n", -1)
return strings.Split(s, "\n")
}
|
[
"func",
"Lines",
"(",
"s",
"string",
")",
"[",
"]",
"string",
"{",
"s",
"=",
"strings",
".",
"Replace",
"(",
"s",
",",
"\"\\r\\n\"",
",",
"\\r",
",",
"\\n",
")",
"\n",
"\"\\n\"",
"\n",
"}"
] |
// Lines convert windows newlines to unix newlines then convert to an Array of lines.
|
[
"Lines",
"convert",
"windows",
"newlines",
"to",
"unix",
"newlines",
"then",
"convert",
"to",
"an",
"Array",
"of",
"lines",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L319-L322
|
test
|
mgutz/str
|
funcsAO.go
|
Map
|
func Map(arr []string, iterator func(string) string) []string {
r := []string{}
for _, item := range arr {
r = append(r, iterator(item))
}
return r
}
|
go
|
func Map(arr []string, iterator func(string) string) []string {
r := []string{}
for _, item := range arr {
r = append(r, iterator(item))
}
return r
}
|
[
"func",
"Map",
"(",
"arr",
"[",
"]",
"string",
",",
"iterator",
"func",
"(",
"string",
")",
"string",
")",
"[",
"]",
"string",
"{",
"r",
":=",
"[",
"]",
"string",
"{",
"}",
"\n",
"for",
"_",
",",
"item",
":=",
"range",
"arr",
"{",
"r",
"=",
"append",
"(",
"r",
",",
"iterator",
"(",
"item",
")",
")",
"\n",
"}",
"\n",
"return",
"r",
"\n",
"}"
] |
// Map maps an array's iitem through an iterator.
|
[
"Map",
"maps",
"an",
"array",
"s",
"iitem",
"through",
"an",
"iterator",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L325-L331
|
test
|
mgutz/str
|
funcsAO.go
|
Match
|
func Match(s, pattern string) bool {
r := regexp.MustCompile(pattern)
return r.MatchString(s)
}
|
go
|
func Match(s, pattern string) bool {
r := regexp.MustCompile(pattern)
return r.MatchString(s)
}
|
[
"func",
"Match",
"(",
"s",
",",
"pattern",
"string",
")",
"bool",
"{",
"r",
":=",
"regexp",
".",
"MustCompile",
"(",
"pattern",
")",
"\n",
"return",
"r",
".",
"MatchString",
"(",
"s",
")",
"\n",
"}"
] |
// Match returns true if patterns matches the string
|
[
"Match",
"returns",
"true",
"if",
"patterns",
"matches",
"the",
"string"
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsAO.go#L334-L337
|
test
|
mgutz/str
|
Gododir/main.go
|
tasks
|
func tasks(p *do.Project) {
p.Task("default", do.S{"readme"}, nil)
p.Task("install", nil, func(c *do.Context) {
c.Run("go get github.com/robertkrimen/godocdown/godocdown")
})
p.Task("lint", nil, func(c *do.Context) {
c.Run("golint .")
c.Run("gofmt -w -s .")
c.Run("go vet .")
c.Run("go test")
})
p.Task("readme", nil, func(c *do.Context) {
c.Run("godocdown -output README.md")
packageName, _ := util.PackageName("doc.go")
// add godoc link
goa.Pipe(
f.Load("./README.md"),
f.Str(str.ReplaceF("--", "\n[godoc](https://godoc.org/"+packageName+")\n", 1)),
f.Write(),
)
}).Src("**/*.go")
p.Task("test", nil, func(c *do.Context) {
c.Run("go test")
})
}
|
go
|
func tasks(p *do.Project) {
p.Task("default", do.S{"readme"}, nil)
p.Task("install", nil, func(c *do.Context) {
c.Run("go get github.com/robertkrimen/godocdown/godocdown")
})
p.Task("lint", nil, func(c *do.Context) {
c.Run("golint .")
c.Run("gofmt -w -s .")
c.Run("go vet .")
c.Run("go test")
})
p.Task("readme", nil, func(c *do.Context) {
c.Run("godocdown -output README.md")
packageName, _ := util.PackageName("doc.go")
// add godoc link
goa.Pipe(
f.Load("./README.md"),
f.Str(str.ReplaceF("--", "\n[godoc](https://godoc.org/"+packageName+")\n", 1)),
f.Write(),
)
}).Src("**/*.go")
p.Task("test", nil, func(c *do.Context) {
c.Run("go test")
})
}
|
[
"func",
"tasks",
"(",
"p",
"*",
"do",
".",
"Project",
")",
"{",
"p",
".",
"Task",
"(",
"\"default\"",
",",
"do",
".",
"S",
"{",
"\"readme\"",
"}",
",",
"nil",
")",
"\n",
"p",
".",
"Task",
"(",
"\"install\"",
",",
"nil",
",",
"func",
"(",
"c",
"*",
"do",
".",
"Context",
")",
"{",
"c",
".",
"Run",
"(",
"\"go get github.com/robertkrimen/godocdown/godocdown\"",
")",
"\n",
"}",
")",
"\n",
"p",
".",
"Task",
"(",
"\"lint\"",
",",
"nil",
",",
"func",
"(",
"c",
"*",
"do",
".",
"Context",
")",
"{",
"c",
".",
"Run",
"(",
"\"golint .\"",
")",
"\n",
"c",
".",
"Run",
"(",
"\"gofmt -w -s .\"",
")",
"\n",
"c",
".",
"Run",
"(",
"\"go vet .\"",
")",
"\n",
"c",
".",
"Run",
"(",
"\"go test\"",
")",
"\n",
"}",
")",
"\n",
"p",
".",
"Task",
"(",
"\"readme\"",
",",
"nil",
",",
"func",
"(",
"c",
"*",
"do",
".",
"Context",
")",
"{",
"c",
".",
"Run",
"(",
"\"godocdown -output README.md\"",
")",
"\n",
"packageName",
",",
"_",
":=",
"util",
".",
"PackageName",
"(",
"\"doc.go\"",
")",
"\n",
"goa",
".",
"Pipe",
"(",
"f",
".",
"Load",
"(",
"\"./README.md\"",
")",
",",
"f",
".",
"Str",
"(",
"str",
".",
"ReplaceF",
"(",
"\"--\"",
",",
"\"\\n[godoc](https://godoc.org/\"",
"+",
"\\n",
"+",
"packageName",
",",
"\")\\n\"",
")",
")",
",",
"\\n",
",",
")",
"\n",
"}",
")",
".",
"1",
"f",
".",
"Write",
"(",
")",
"\n",
"Src",
"\n",
"}"
] |
// Project is local project.
|
[
"Project",
"is",
"local",
"project",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/Gododir/main.go#L13-L43
|
test
|
mgutz/str
|
funcsPZ.go
|
Pad
|
func Pad(s, c string, n int) string {
L := len(s)
if L >= n {
return s
}
n -= L
left := strings.Repeat(c, int(math.Ceil(float64(n)/2)))
right := strings.Repeat(c, int(math.Floor(float64(n)/2)))
return left + s + right
}
|
go
|
func Pad(s, c string, n int) string {
L := len(s)
if L >= n {
return s
}
n -= L
left := strings.Repeat(c, int(math.Ceil(float64(n)/2)))
right := strings.Repeat(c, int(math.Floor(float64(n)/2)))
return left + s + right
}
|
[
"func",
"Pad",
"(",
"s",
",",
"c",
"string",
",",
"n",
"int",
")",
"string",
"{",
"L",
":=",
"len",
"(",
"s",
")",
"\n",
"if",
"L",
">=",
"n",
"{",
"return",
"s",
"\n",
"}",
"\n",
"n",
"-=",
"L",
"\n",
"left",
":=",
"strings",
".",
"Repeat",
"(",
"c",
",",
"int",
"(",
"math",
".",
"Ceil",
"(",
"float64",
"(",
"n",
")",
"/",
"2",
")",
")",
")",
"\n",
"right",
":=",
"strings",
".",
"Repeat",
"(",
"c",
",",
"int",
"(",
"math",
".",
"Floor",
"(",
"float64",
"(",
"n",
")",
"/",
"2",
")",
")",
")",
"\n",
"return",
"left",
"+",
"s",
"+",
"right",
"\n",
"}"
] |
// Pad pads string s on both sides with c until it has length of n.
|
[
"Pad",
"pads",
"string",
"s",
"on",
"both",
"sides",
"with",
"c",
"until",
"it",
"has",
"length",
"of",
"n",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L16-L26
|
test
|
mgutz/str
|
funcsPZ.go
|
PadF
|
func PadF(c string, n int) func(string) string {
return func(s string) string {
return Pad(s, c, n)
}
}
|
go
|
func PadF(c string, n int) func(string) string {
return func(s string) string {
return Pad(s, c, n)
}
}
|
[
"func",
"PadF",
"(",
"c",
"string",
",",
"n",
"int",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"Pad",
"(",
"s",
",",
"c",
",",
"n",
")",
"\n",
"}",
"\n",
"}"
] |
// PadF is the filter form of Pad.
|
[
"PadF",
"is",
"the",
"filter",
"form",
"of",
"Pad",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L29-L33
|
test
|
mgutz/str
|
funcsPZ.go
|
PadLeft
|
func PadLeft(s, c string, n int) string {
L := len(s)
if L > n {
return s
}
return strings.Repeat(c, (n-L)) + s
}
|
go
|
func PadLeft(s, c string, n int) string {
L := len(s)
if L > n {
return s
}
return strings.Repeat(c, (n-L)) + s
}
|
[
"func",
"PadLeft",
"(",
"s",
",",
"c",
"string",
",",
"n",
"int",
")",
"string",
"{",
"L",
":=",
"len",
"(",
"s",
")",
"\n",
"if",
"L",
">",
"n",
"{",
"return",
"s",
"\n",
"}",
"\n",
"return",
"strings",
".",
"Repeat",
"(",
"c",
",",
"(",
"n",
"-",
"L",
")",
")",
"+",
"s",
"\n",
"}"
] |
// PadLeft pads s on left side with c until it has length of n.
|
[
"PadLeft",
"pads",
"s",
"on",
"left",
"side",
"with",
"c",
"until",
"it",
"has",
"length",
"of",
"n",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L36-L42
|
test
|
mgutz/str
|
funcsPZ.go
|
PadLeftF
|
func PadLeftF(c string, n int) func(string) string {
return func(s string) string {
return PadLeft(s, c, n)
}
}
|
go
|
func PadLeftF(c string, n int) func(string) string {
return func(s string) string {
return PadLeft(s, c, n)
}
}
|
[
"func",
"PadLeftF",
"(",
"c",
"string",
",",
"n",
"int",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"PadLeft",
"(",
"s",
",",
"c",
",",
"n",
")",
"\n",
"}",
"\n",
"}"
] |
// PadLeftF is the filter form of PadLeft.
|
[
"PadLeftF",
"is",
"the",
"filter",
"form",
"of",
"PadLeft",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L45-L49
|
test
|
mgutz/str
|
funcsPZ.go
|
PadRightF
|
func PadRightF(c string, n int) func(string) string {
return func(s string) string {
return PadRight(s, c, n)
}
}
|
go
|
func PadRightF(c string, n int) func(string) string {
return func(s string) string {
return PadRight(s, c, n)
}
}
|
[
"func",
"PadRightF",
"(",
"c",
"string",
",",
"n",
"int",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"PadRight",
"(",
"s",
",",
"c",
",",
"n",
")",
"\n",
"}",
"\n",
"}"
] |
// PadRightF is the filter form of Padright
|
[
"PadRightF",
"is",
"the",
"filter",
"form",
"of",
"Padright"
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L61-L65
|
test
|
mgutz/str
|
funcsPZ.go
|
Pipe
|
func Pipe(s string, funcs ...func(string) string) string {
for _, fn := range funcs {
s = fn(s)
}
return s
}
|
go
|
func Pipe(s string, funcs ...func(string) string) string {
for _, fn := range funcs {
s = fn(s)
}
return s
}
|
[
"func",
"Pipe",
"(",
"s",
"string",
",",
"funcs",
"...",
"func",
"(",
"string",
")",
"string",
")",
"string",
"{",
"for",
"_",
",",
"fn",
":=",
"range",
"funcs",
"{",
"s",
"=",
"fn",
"(",
"s",
")",
"\n",
"}",
"\n",
"return",
"s",
"\n",
"}"
] |
// Pipe pipes s through one or more string filters.
|
[
"Pipe",
"pipes",
"s",
"through",
"one",
"or",
"more",
"string",
"filters",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L68-L73
|
test
|
mgutz/str
|
funcsPZ.go
|
QuoteItems
|
func QuoteItems(arr []string) []string {
return Map(arr, func(s string) string {
return strconv.Quote(s)
})
}
|
go
|
func QuoteItems(arr []string) []string {
return Map(arr, func(s string) string {
return strconv.Quote(s)
})
}
|
[
"func",
"QuoteItems",
"(",
"arr",
"[",
"]",
"string",
")",
"[",
"]",
"string",
"{",
"return",
"Map",
"(",
"arr",
",",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"strconv",
".",
"Quote",
"(",
"s",
")",
"\n",
"}",
")",
"\n",
"}"
] |
// QuoteItems quotes all items in array, mostly for debugging.
|
[
"QuoteItems",
"quotes",
"all",
"items",
"in",
"array",
"mostly",
"for",
"debugging",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L76-L80
|
test
|
mgutz/str
|
funcsPZ.go
|
ReplaceF
|
func ReplaceF(old, new string, n int) func(string) string {
return func(s string) string {
return strings.Replace(s, old, new, n)
}
}
|
go
|
func ReplaceF(old, new string, n int) func(string) string {
return func(s string) string {
return strings.Replace(s, old, new, n)
}
}
|
[
"func",
"ReplaceF",
"(",
"old",
",",
"new",
"string",
",",
"n",
"int",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"strings",
".",
"Replace",
"(",
"s",
",",
"old",
",",
"new",
",",
"n",
")",
"\n",
"}",
"\n",
"}"
] |
// ReplaceF is the filter form of strings.Replace.
|
[
"ReplaceF",
"is",
"the",
"filter",
"form",
"of",
"strings",
".",
"Replace",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L83-L87
|
test
|
mgutz/str
|
funcsPZ.go
|
ReplacePatternF
|
func ReplacePatternF(pattern, repl string) func(string) string {
return func(s string) string {
return ReplacePattern(s, pattern, repl)
}
}
|
go
|
func ReplacePatternF(pattern, repl string) func(string) string {
return func(s string) string {
return ReplacePattern(s, pattern, repl)
}
}
|
[
"func",
"ReplacePatternF",
"(",
"pattern",
",",
"repl",
"string",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"ReplacePattern",
"(",
"s",
",",
"pattern",
",",
"repl",
")",
"\n",
"}",
"\n",
"}"
] |
// ReplacePatternF is the filter form of ReplaceRegexp.
|
[
"ReplacePatternF",
"is",
"the",
"filter",
"form",
"of",
"ReplaceRegexp",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L97-L101
|
test
|
mgutz/str
|
funcsPZ.go
|
Reverse
|
func Reverse(s string) string {
cs := make([]rune, utf8.RuneCountInString(s))
i := len(cs)
for _, c := range s {
i--
cs[i] = c
}
return string(cs)
}
|
go
|
func Reverse(s string) string {
cs := make([]rune, utf8.RuneCountInString(s))
i := len(cs)
for _, c := range s {
i--
cs[i] = c
}
return string(cs)
}
|
[
"func",
"Reverse",
"(",
"s",
"string",
")",
"string",
"{",
"cs",
":=",
"make",
"(",
"[",
"]",
"rune",
",",
"utf8",
".",
"RuneCountInString",
"(",
"s",
")",
")",
"\n",
"i",
":=",
"len",
"(",
"cs",
")",
"\n",
"for",
"_",
",",
"c",
":=",
"range",
"s",
"{",
"i",
"--",
"\n",
"cs",
"[",
"i",
"]",
"=",
"c",
"\n",
"}",
"\n",
"return",
"string",
"(",
"cs",
")",
"\n",
"}"
] |
// Reverse a string
|
[
"Reverse",
"a",
"string"
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L104-L112
|
test
|
mgutz/str
|
funcsPZ.go
|
RightF
|
func RightF(n int) func(string) string {
return func(s string) string {
return Right(s, n)
}
}
|
go
|
func RightF(n int) func(string) string {
return func(s string) string {
return Right(s, n)
}
}
|
[
"func",
"RightF",
"(",
"n",
"int",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"Right",
"(",
"s",
",",
"n",
")",
"\n",
"}",
"\n",
"}"
] |
// RightF is the Filter version of Right.
|
[
"RightF",
"is",
"the",
"Filter",
"version",
"of",
"Right",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L123-L127
|
test
|
mgutz/str
|
funcsPZ.go
|
Slice
|
func Slice(s string, start, end int) string {
if end > -1 {
return s[start:end]
}
L := len(s)
if L+end > 0 {
return s[start : L-end]
}
return s[start:]
}
|
go
|
func Slice(s string, start, end int) string {
if end > -1 {
return s[start:end]
}
L := len(s)
if L+end > 0 {
return s[start : L-end]
}
return s[start:]
}
|
[
"func",
"Slice",
"(",
"s",
"string",
",",
"start",
",",
"end",
"int",
")",
"string",
"{",
"if",
"end",
">",
"-",
"1",
"{",
"return",
"s",
"[",
"start",
":",
"end",
"]",
"\n",
"}",
"\n",
"L",
":=",
"len",
"(",
"s",
")",
"\n",
"if",
"L",
"+",
"end",
">",
"0",
"{",
"return",
"s",
"[",
"start",
":",
"L",
"-",
"end",
"]",
"\n",
"}",
"\n",
"return",
"s",
"[",
"start",
":",
"]",
"\n",
"}"
] |
// Slice slices a string. If end is negative then it is the from the end
// of the string.
|
[
"Slice",
"slices",
"a",
"string",
".",
"If",
"end",
"is",
"negative",
"then",
"it",
"is",
"the",
"from",
"the",
"end",
"of",
"the",
"string",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L142-L151
|
test
|
mgutz/str
|
funcsPZ.go
|
SliceF
|
func SliceF(start, end int) func(string) string {
return func(s string) string {
return Slice(s, start, end)
}
}
|
go
|
func SliceF(start, end int) func(string) string {
return func(s string) string {
return Slice(s, start, end)
}
}
|
[
"func",
"SliceF",
"(",
"start",
",",
"end",
"int",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"Slice",
"(",
"s",
",",
"start",
",",
"end",
")",
"\n",
"}",
"\n",
"}"
] |
// SliceF is the filter for Slice.
|
[
"SliceF",
"is",
"the",
"filter",
"for",
"Slice",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L154-L158
|
test
|
mgutz/str
|
funcsPZ.go
|
SliceContains
|
func SliceContains(slice []string, val string) bool {
if slice == nil {
return false
}
for _, it := range slice {
if it == val {
return true
}
}
return false
}
|
go
|
func SliceContains(slice []string, val string) bool {
if slice == nil {
return false
}
for _, it := range slice {
if it == val {
return true
}
}
return false
}
|
[
"func",
"SliceContains",
"(",
"slice",
"[",
"]",
"string",
",",
"val",
"string",
")",
"bool",
"{",
"if",
"slice",
"==",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n",
"for",
"_",
",",
"it",
":=",
"range",
"slice",
"{",
"if",
"it",
"==",
"val",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] |
// SliceContains determines whether val is an element in slice.
|
[
"SliceContains",
"determines",
"whether",
"val",
"is",
"an",
"element",
"in",
"slice",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L161-L172
|
test
|
mgutz/str
|
funcsPZ.go
|
SliceIndexOf
|
func SliceIndexOf(slice []string, val string) int {
if slice == nil {
return -1
}
for i, it := range slice {
if it == val {
return i
}
}
return -1
}
|
go
|
func SliceIndexOf(slice []string, val string) int {
if slice == nil {
return -1
}
for i, it := range slice {
if it == val {
return i
}
}
return -1
}
|
[
"func",
"SliceIndexOf",
"(",
"slice",
"[",
"]",
"string",
",",
"val",
"string",
")",
"int",
"{",
"if",
"slice",
"==",
"nil",
"{",
"return",
"-",
"1",
"\n",
"}",
"\n",
"for",
"i",
",",
"it",
":=",
"range",
"slice",
"{",
"if",
"it",
"==",
"val",
"{",
"return",
"i",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"-",
"1",
"\n",
"}"
] |
// SliceIndexOf gets the indx of val in slice. Returns -1 if not found.
|
[
"SliceIndexOf",
"gets",
"the",
"indx",
"of",
"val",
"in",
"slice",
".",
"Returns",
"-",
"1",
"if",
"not",
"found",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L175-L186
|
test
|
mgutz/str
|
funcsPZ.go
|
Slugify
|
func Slugify(s string) string {
sl := slugifyRe.ReplaceAllString(s, "")
sl = strings.ToLower(sl)
sl = Dasherize(sl)
return sl
}
|
go
|
func Slugify(s string) string {
sl := slugifyRe.ReplaceAllString(s, "")
sl = strings.ToLower(sl)
sl = Dasherize(sl)
return sl
}
|
[
"func",
"Slugify",
"(",
"s",
"string",
")",
"string",
"{",
"sl",
":=",
"slugifyRe",
".",
"ReplaceAllString",
"(",
"s",
",",
"\"\"",
")",
"\n",
"sl",
"=",
"strings",
".",
"ToLower",
"(",
"sl",
")",
"\n",
"sl",
"=",
"Dasherize",
"(",
"sl",
")",
"\n",
"return",
"sl",
"\n",
"}"
] |
// Slugify converts s into a dasherized string suitable for URL segment.
|
[
"Slugify",
"converts",
"s",
"into",
"a",
"dasherized",
"string",
"suitable",
"for",
"URL",
"segment",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L189-L194
|
test
|
mgutz/str
|
funcsPZ.go
|
StripPunctuation
|
func StripPunctuation(s string) string {
s = stripPuncRe.ReplaceAllString(s, "")
s = nWhitespaceRe.ReplaceAllString(s, " ")
return s
}
|
go
|
func StripPunctuation(s string) string {
s = stripPuncRe.ReplaceAllString(s, "")
s = nWhitespaceRe.ReplaceAllString(s, " ")
return s
}
|
[
"func",
"StripPunctuation",
"(",
"s",
"string",
")",
"string",
"{",
"s",
"=",
"stripPuncRe",
".",
"ReplaceAllString",
"(",
"s",
",",
"\"\"",
")",
"\n",
"s",
"=",
"nWhitespaceRe",
".",
"ReplaceAllString",
"(",
"s",
",",
"\" \"",
")",
"\n",
"return",
"s",
"\n",
"}"
] |
// StripPunctuation strips puncation from string.
|
[
"StripPunctuation",
"strips",
"puncation",
"from",
"string",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L197-L201
|
test
|
mgutz/str
|
funcsPZ.go
|
StripTags
|
func StripTags(s string, tags ...string) string {
if len(tags) == 0 {
tags = append(tags, "")
}
for _, tag := range tags {
stripTagsRe := regexp.MustCompile(`(?i)<\/?` + tag + `[^<>]*>`)
s = stripTagsRe.ReplaceAllString(s, "")
}
return s
}
|
go
|
func StripTags(s string, tags ...string) string {
if len(tags) == 0 {
tags = append(tags, "")
}
for _, tag := range tags {
stripTagsRe := regexp.MustCompile(`(?i)<\/?` + tag + `[^<>]*>`)
s = stripTagsRe.ReplaceAllString(s, "")
}
return s
}
|
[
"func",
"StripTags",
"(",
"s",
"string",
",",
"tags",
"...",
"string",
")",
"string",
"{",
"if",
"len",
"(",
"tags",
")",
"==",
"0",
"{",
"tags",
"=",
"append",
"(",
"tags",
",",
"\"\"",
")",
"\n",
"}",
"\n",
"for",
"_",
",",
"tag",
":=",
"range",
"tags",
"{",
"stripTagsRe",
":=",
"regexp",
".",
"MustCompile",
"(",
"`(?i)<\\/?`",
"+",
"tag",
"+",
"`[^<>]*>`",
")",
"\n",
"s",
"=",
"stripTagsRe",
".",
"ReplaceAllString",
"(",
"s",
",",
"\"\"",
")",
"\n",
"}",
"\n",
"return",
"s",
"\n",
"}"
] |
// StripTags strips all of the html tags or tags specified by the parameters
|
[
"StripTags",
"strips",
"all",
"of",
"the",
"html",
"tags",
"or",
"tags",
"specified",
"by",
"the",
"parameters"
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L204-L213
|
test
|
mgutz/str
|
funcsPZ.go
|
Substr
|
func Substr(s string, index int, n int) string {
L := len(s)
if index < 0 || index >= L || s == "" {
return ""
}
end := index + n
if end >= L {
end = L
}
if end <= index {
return ""
}
return s[index:end]
}
|
go
|
func Substr(s string, index int, n int) string {
L := len(s)
if index < 0 || index >= L || s == "" {
return ""
}
end := index + n
if end >= L {
end = L
}
if end <= index {
return ""
}
return s[index:end]
}
|
[
"func",
"Substr",
"(",
"s",
"string",
",",
"index",
"int",
",",
"n",
"int",
")",
"string",
"{",
"L",
":=",
"len",
"(",
"s",
")",
"\n",
"if",
"index",
"<",
"0",
"||",
"index",
">=",
"L",
"||",
"s",
"==",
"\"\"",
"{",
"return",
"\"\"",
"\n",
"}",
"\n",
"end",
":=",
"index",
"+",
"n",
"\n",
"if",
"end",
">=",
"L",
"{",
"end",
"=",
"L",
"\n",
"}",
"\n",
"if",
"end",
"<=",
"index",
"{",
"return",
"\"\"",
"\n",
"}",
"\n",
"return",
"s",
"[",
"index",
":",
"end",
"]",
"\n",
"}"
] |
// Substr returns a substring of s starting at index of length n.
|
[
"Substr",
"returns",
"a",
"substring",
"of",
"s",
"starting",
"at",
"index",
"of",
"length",
"n",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L216-L229
|
test
|
mgutz/str
|
funcsPZ.go
|
SubstrF
|
func SubstrF(index, n int) func(string) string {
return func(s string) string {
return Substr(s, index, n)
}
}
|
go
|
func SubstrF(index, n int) func(string) string {
return func(s string) string {
return Substr(s, index, n)
}
}
|
[
"func",
"SubstrF",
"(",
"index",
",",
"n",
"int",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"Substr",
"(",
"s",
",",
"index",
",",
"n",
")",
"\n",
"}",
"\n",
"}"
] |
// SubstrF is the filter form of Substr.
|
[
"SubstrF",
"is",
"the",
"filter",
"form",
"of",
"Substr",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L232-L236
|
test
|
mgutz/str
|
funcsPZ.go
|
TemplateWithDelimiters
|
func TemplateWithDelimiters(s string, values map[string]interface{}, opening, closing string) string {
escapeDelimiter := func(delim string) string {
result := templateRe.ReplaceAllString(delim, "\\$1")
return templateRe2.ReplaceAllString(result, "\\$")
}
openingDelim := escapeDelimiter(opening)
closingDelim := escapeDelimiter(closing)
r := regexp.MustCompile(openingDelim + `(.+?)` + closingDelim)
matches := r.FindAllStringSubmatch(s, -1)
for _, submatches := range matches {
match := submatches[0]
key := submatches[1]
//log.Printf("match %s key %s\n", match, key)
if values[key] != nil {
v := fmt.Sprintf("%v", values[key])
s = strings.Replace(s, match, v, -1)
}
}
return s
}
|
go
|
func TemplateWithDelimiters(s string, values map[string]interface{}, opening, closing string) string {
escapeDelimiter := func(delim string) string {
result := templateRe.ReplaceAllString(delim, "\\$1")
return templateRe2.ReplaceAllString(result, "\\$")
}
openingDelim := escapeDelimiter(opening)
closingDelim := escapeDelimiter(closing)
r := regexp.MustCompile(openingDelim + `(.+?)` + closingDelim)
matches := r.FindAllStringSubmatch(s, -1)
for _, submatches := range matches {
match := submatches[0]
key := submatches[1]
//log.Printf("match %s key %s\n", match, key)
if values[key] != nil {
v := fmt.Sprintf("%v", values[key])
s = strings.Replace(s, match, v, -1)
}
}
return s
}
|
[
"func",
"TemplateWithDelimiters",
"(",
"s",
"string",
",",
"values",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
",",
"opening",
",",
"closing",
"string",
")",
"string",
"{",
"escapeDelimiter",
":=",
"func",
"(",
"delim",
"string",
")",
"string",
"{",
"result",
":=",
"templateRe",
".",
"ReplaceAllString",
"(",
"delim",
",",
"\"\\\\$1\"",
")",
"\n",
"\\\\",
"\n",
"}",
"\n",
"return",
"templateRe2",
".",
"ReplaceAllString",
"(",
"result",
",",
"\"\\\\$\"",
")",
"\n",
"\\\\",
"\n",
"openingDelim",
":=",
"escapeDelimiter",
"(",
"opening",
")",
"\n",
"closingDelim",
":=",
"escapeDelimiter",
"(",
"closing",
")",
"\n",
"r",
":=",
"regexp",
".",
"MustCompile",
"(",
"openingDelim",
"+",
"`(.+?)`",
"+",
"closingDelim",
")",
"\n",
"matches",
":=",
"r",
".",
"FindAllStringSubmatch",
"(",
"s",
",",
"-",
"1",
")",
"\n",
"}"
] |
// TemplateWithDelimiters is string template with user-defineable opening and closing delimiters.
|
[
"TemplateWithDelimiters",
"is",
"string",
"template",
"with",
"user",
"-",
"defineable",
"opening",
"and",
"closing",
"delimiters",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L251-L272
|
test
|
mgutz/str
|
funcsPZ.go
|
ToArgv
|
func ToArgv(s string) []string {
const (
InArg = iota
InArgQuote
OutOfArg
)
currentState := OutOfArg
currentQuoteChar := "\x00" // to distinguish between ' and " quotations
// this allows to use "foo'bar"
currentArg := ""
argv := []string{}
isQuote := func(c string) bool {
return c == `"` || c == `'`
}
isEscape := func(c string) bool {
return c == `\`
}
isWhitespace := func(c string) bool {
return c == " " || c == "\t"
}
L := len(s)
for i := 0; i < L; i++ {
c := s[i : i+1]
//fmt.Printf("c %s state %v arg %s argv %v i %d\n", c, currentState, currentArg, args, i)
if isQuote(c) {
switch currentState {
case OutOfArg:
currentArg = ""
fallthrough
case InArg:
currentState = InArgQuote
currentQuoteChar = c
case InArgQuote:
if c == currentQuoteChar {
currentState = InArg
} else {
currentArg += c
}
}
} else if isWhitespace(c) {
switch currentState {
case InArg:
argv = append(argv, currentArg)
currentState = OutOfArg
case InArgQuote:
currentArg += c
case OutOfArg:
// nothing
}
} else if isEscape(c) {
switch currentState {
case OutOfArg:
currentArg = ""
currentState = InArg
fallthrough
case InArg:
fallthrough
case InArgQuote:
if i == L-1 {
if runtime.GOOS == "windows" {
// just add \ to end for windows
currentArg += c
} else {
panic("Escape character at end string")
}
} else {
if runtime.GOOS == "windows" {
peek := s[i+1 : i+2]
if peek != `"` {
currentArg += c
}
} else {
i++
c = s[i : i+1]
currentArg += c
}
}
}
} else {
switch currentState {
case InArg, InArgQuote:
currentArg += c
case OutOfArg:
currentArg = ""
currentArg += c
currentState = InArg
}
}
}
if currentState == InArg {
argv = append(argv, currentArg)
} else if currentState == InArgQuote {
panic("Starting quote has no ending quote.")
}
return argv
}
|
go
|
func ToArgv(s string) []string {
const (
InArg = iota
InArgQuote
OutOfArg
)
currentState := OutOfArg
currentQuoteChar := "\x00" // to distinguish between ' and " quotations
// this allows to use "foo'bar"
currentArg := ""
argv := []string{}
isQuote := func(c string) bool {
return c == `"` || c == `'`
}
isEscape := func(c string) bool {
return c == `\`
}
isWhitespace := func(c string) bool {
return c == " " || c == "\t"
}
L := len(s)
for i := 0; i < L; i++ {
c := s[i : i+1]
//fmt.Printf("c %s state %v arg %s argv %v i %d\n", c, currentState, currentArg, args, i)
if isQuote(c) {
switch currentState {
case OutOfArg:
currentArg = ""
fallthrough
case InArg:
currentState = InArgQuote
currentQuoteChar = c
case InArgQuote:
if c == currentQuoteChar {
currentState = InArg
} else {
currentArg += c
}
}
} else if isWhitespace(c) {
switch currentState {
case InArg:
argv = append(argv, currentArg)
currentState = OutOfArg
case InArgQuote:
currentArg += c
case OutOfArg:
// nothing
}
} else if isEscape(c) {
switch currentState {
case OutOfArg:
currentArg = ""
currentState = InArg
fallthrough
case InArg:
fallthrough
case InArgQuote:
if i == L-1 {
if runtime.GOOS == "windows" {
// just add \ to end for windows
currentArg += c
} else {
panic("Escape character at end string")
}
} else {
if runtime.GOOS == "windows" {
peek := s[i+1 : i+2]
if peek != `"` {
currentArg += c
}
} else {
i++
c = s[i : i+1]
currentArg += c
}
}
}
} else {
switch currentState {
case InArg, InArgQuote:
currentArg += c
case OutOfArg:
currentArg = ""
currentArg += c
currentState = InArg
}
}
}
if currentState == InArg {
argv = append(argv, currentArg)
} else if currentState == InArgQuote {
panic("Starting quote has no ending quote.")
}
return argv
}
|
[
"func",
"ToArgv",
"(",
"s",
"string",
")",
"[",
"]",
"string",
"{",
"const",
"(",
"InArg",
"=",
"iota",
"\n",
"InArgQuote",
"\n",
"OutOfArg",
"\n",
")",
"\n",
"currentState",
":=",
"OutOfArg",
"\n",
"currentQuoteChar",
":=",
"\"\\x00\"",
"\n",
"\\x00",
"\n",
"currentArg",
":=",
"\"\"",
"\n",
"argv",
":=",
"[",
"]",
"string",
"{",
"}",
"\n",
"isQuote",
":=",
"func",
"(",
"c",
"string",
")",
"bool",
"{",
"return",
"c",
"==",
"`\"`",
"||",
"c",
"==",
"`'`",
"\n",
"}",
"\n",
"isEscape",
":=",
"func",
"(",
"c",
"string",
")",
"bool",
"{",
"return",
"c",
"==",
"`\\`",
"\n",
"}",
"\n",
"isWhitespace",
":=",
"func",
"(",
"c",
"string",
")",
"bool",
"{",
"return",
"c",
"==",
"\" \"",
"||",
"c",
"==",
"\"\\t\"",
"\n",
"}",
"\n",
"\\t",
"\n",
"L",
":=",
"len",
"(",
"s",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"L",
";",
"i",
"++",
"{",
"c",
":=",
"s",
"[",
"i",
":",
"i",
"+",
"1",
"]",
"\n",
"if",
"isQuote",
"(",
"c",
")",
"{",
"switch",
"currentState",
"{",
"case",
"OutOfArg",
":",
"currentArg",
"=",
"\"\"",
"\n",
"fallthrough",
"\n",
"case",
"InArg",
":",
"currentState",
"=",
"InArgQuote",
"\n",
"currentQuoteChar",
"=",
"c",
"\n",
"case",
"InArgQuote",
":",
"if",
"c",
"==",
"currentQuoteChar",
"{",
"currentState",
"=",
"InArg",
"\n",
"}",
"else",
"{",
"currentArg",
"+=",
"c",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"else",
"if",
"isWhitespace",
"(",
"c",
")",
"{",
"switch",
"currentState",
"{",
"case",
"InArg",
":",
"argv",
"=",
"append",
"(",
"argv",
",",
"currentArg",
")",
"\n",
"currentState",
"=",
"OutOfArg",
"\n",
"case",
"InArgQuote",
":",
"currentArg",
"+=",
"c",
"\n",
"case",
"OutOfArg",
":",
"}",
"\n",
"}",
"else",
"if",
"isEscape",
"(",
"c",
")",
"{",
"switch",
"currentState",
"{",
"case",
"OutOfArg",
":",
"currentArg",
"=",
"\"\"",
"\n",
"currentState",
"=",
"InArg",
"\n",
"fallthrough",
"\n",
"case",
"InArg",
":",
"fallthrough",
"\n",
"case",
"InArgQuote",
":",
"if",
"i",
"==",
"L",
"-",
"1",
"{",
"if",
"runtime",
".",
"GOOS",
"==",
"\"windows\"",
"{",
"currentArg",
"+=",
"c",
"\n",
"}",
"else",
"{",
"panic",
"(",
"\"Escape character at end string\"",
")",
"\n",
"}",
"\n",
"}",
"else",
"{",
"if",
"runtime",
".",
"GOOS",
"==",
"\"windows\"",
"{",
"peek",
":=",
"s",
"[",
"i",
"+",
"1",
":",
"i",
"+",
"2",
"]",
"\n",
"if",
"peek",
"!=",
"`\"`",
"{",
"currentArg",
"+=",
"c",
"\n",
"}",
"\n",
"}",
"else",
"{",
"i",
"++",
"\n",
"c",
"=",
"s",
"[",
"i",
":",
"i",
"+",
"1",
"]",
"\n",
"currentArg",
"+=",
"c",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"else",
"{",
"switch",
"currentState",
"{",
"case",
"InArg",
",",
"InArgQuote",
":",
"currentArg",
"+=",
"c",
"\n",
"case",
"OutOfArg",
":",
"currentArg",
"=",
"\"\"",
"\n",
"currentArg",
"+=",
"c",
"\n",
"currentState",
"=",
"InArg",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// ToArgv converts string s into an argv for exec.
|
[
"ToArgv",
"converts",
"string",
"s",
"into",
"an",
"argv",
"for",
"exec",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L275-L381
|
test
|
mgutz/str
|
funcsPZ.go
|
ToBool
|
func ToBool(s string) bool {
s = strings.ToLower(s)
return s == "true" || s == "yes" || s == "on" || s == "1"
}
|
go
|
func ToBool(s string) bool {
s = strings.ToLower(s)
return s == "true" || s == "yes" || s == "on" || s == "1"
}
|
[
"func",
"ToBool",
"(",
"s",
"string",
")",
"bool",
"{",
"s",
"=",
"strings",
".",
"ToLower",
"(",
"s",
")",
"\n",
"return",
"s",
"==",
"\"true\"",
"||",
"s",
"==",
"\"yes\"",
"||",
"s",
"==",
"\"on\"",
"||",
"s",
"==",
"\"1\"",
"\n",
"}"
] |
// ToBool fuzzily converts truthy values.
|
[
"ToBool",
"fuzzily",
"converts",
"truthy",
"values",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L384-L387
|
test
|
mgutz/str
|
funcsPZ.go
|
ToBoolOr
|
func ToBoolOr(s string, defaultValue bool) bool {
b, err := strconv.ParseBool(s)
if err != nil {
return defaultValue
}
return b
}
|
go
|
func ToBoolOr(s string, defaultValue bool) bool {
b, err := strconv.ParseBool(s)
if err != nil {
return defaultValue
}
return b
}
|
[
"func",
"ToBoolOr",
"(",
"s",
"string",
",",
"defaultValue",
"bool",
")",
"bool",
"{",
"b",
",",
"err",
":=",
"strconv",
".",
"ParseBool",
"(",
"s",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"defaultValue",
"\n",
"}",
"\n",
"return",
"b",
"\n",
"}"
] |
// ToBoolOr parses s as a bool or returns defaultValue.
|
[
"ToBoolOr",
"parses",
"s",
"as",
"a",
"bool",
"or",
"returns",
"defaultValue",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L390-L396
|
test
|
mgutz/str
|
funcsPZ.go
|
ToIntOr
|
func ToIntOr(s string, defaultValue int) int {
n, err := strconv.Atoi(s)
if err != nil {
return defaultValue
}
return n
}
|
go
|
func ToIntOr(s string, defaultValue int) int {
n, err := strconv.Atoi(s)
if err != nil {
return defaultValue
}
return n
}
|
[
"func",
"ToIntOr",
"(",
"s",
"string",
",",
"defaultValue",
"int",
")",
"int",
"{",
"n",
",",
"err",
":=",
"strconv",
".",
"Atoi",
"(",
"s",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"defaultValue",
"\n",
"}",
"\n",
"return",
"n",
"\n",
"}"
] |
// ToIntOr parses s as an int or returns defaultValue.
|
[
"ToIntOr",
"parses",
"s",
"as",
"an",
"int",
"or",
"returns",
"defaultValue",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L399-L405
|
test
|
mgutz/str
|
funcsPZ.go
|
ToFloat32Or
|
func ToFloat32Or(s string, defaultValue float32) float32 {
f, err := strconv.ParseFloat(s, 32)
if err != nil {
return defaultValue
}
return float32(f)
}
|
go
|
func ToFloat32Or(s string, defaultValue float32) float32 {
f, err := strconv.ParseFloat(s, 32)
if err != nil {
return defaultValue
}
return float32(f)
}
|
[
"func",
"ToFloat32Or",
"(",
"s",
"string",
",",
"defaultValue",
"float32",
")",
"float32",
"{",
"f",
",",
"err",
":=",
"strconv",
".",
"ParseFloat",
"(",
"s",
",",
"32",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"defaultValue",
"\n",
"}",
"\n",
"return",
"float32",
"(",
"f",
")",
"\n",
"}"
] |
// ToFloat32Or parses as a float32 or returns defaultValue on error.
|
[
"ToFloat32Or",
"parses",
"as",
"a",
"float32",
"or",
"returns",
"defaultValue",
"on",
"error",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L408-L414
|
test
|
mgutz/str
|
funcsPZ.go
|
ToFloat64Or
|
func ToFloat64Or(s string, defaultValue float64) float64 {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return defaultValue
}
return f
}
|
go
|
func ToFloat64Or(s string, defaultValue float64) float64 {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return defaultValue
}
return f
}
|
[
"func",
"ToFloat64Or",
"(",
"s",
"string",
",",
"defaultValue",
"float64",
")",
"float64",
"{",
"f",
",",
"err",
":=",
"strconv",
".",
"ParseFloat",
"(",
"s",
",",
"64",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"defaultValue",
"\n",
"}",
"\n",
"return",
"f",
"\n",
"}"
] |
// ToFloat64Or parses s as a float64 or returns defaultValue.
|
[
"ToFloat64Or",
"parses",
"s",
"as",
"a",
"float64",
"or",
"returns",
"defaultValue",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L417-L423
|
test
|
mgutz/str
|
funcsPZ.go
|
UnescapeHTML
|
func UnescapeHTML(s string) string {
if Verbose {
fmt.Println("Use html.UnescapeString instead of UnescapeHTML")
}
return html.UnescapeString(s)
}
|
go
|
func UnescapeHTML(s string) string {
if Verbose {
fmt.Println("Use html.UnescapeString instead of UnescapeHTML")
}
return html.UnescapeString(s)
}
|
[
"func",
"UnescapeHTML",
"(",
"s",
"string",
")",
"string",
"{",
"if",
"Verbose",
"{",
"fmt",
".",
"Println",
"(",
"\"Use html.UnescapeString instead of UnescapeHTML\"",
")",
"\n",
"}",
"\n",
"return",
"html",
".",
"UnescapeString",
"(",
"s",
")",
"\n",
"}"
] |
// UnescapeHTML is an alias for html.UnescapeString.
|
[
"UnescapeHTML",
"is",
"an",
"alias",
"for",
"html",
".",
"UnescapeString",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L502-L507
|
test
|
mgutz/str
|
funcsPZ.go
|
WrapHTML
|
func WrapHTML(s string, tag string, attrs map[string]string) string {
escapeHTMLAttributeQuotes := func(v string) string {
v = strings.Replace(v, "<", "<", -1)
v = strings.Replace(v, "&", "&", -1)
v = strings.Replace(v, "\"", """, -1)
return v
}
if tag == "" {
tag = "div"
}
el := "<" + tag
for name, val := range attrs {
el += " " + name + "=\"" + escapeHTMLAttributeQuotes(val) + "\""
}
el += ">" + s + "</" + tag + ">"
return el
}
|
go
|
func WrapHTML(s string, tag string, attrs map[string]string) string {
escapeHTMLAttributeQuotes := func(v string) string {
v = strings.Replace(v, "<", "<", -1)
v = strings.Replace(v, "&", "&", -1)
v = strings.Replace(v, "\"", """, -1)
return v
}
if tag == "" {
tag = "div"
}
el := "<" + tag
for name, val := range attrs {
el += " " + name + "=\"" + escapeHTMLAttributeQuotes(val) + "\""
}
el += ">" + s + "</" + tag + ">"
return el
}
|
[
"func",
"WrapHTML",
"(",
"s",
"string",
",",
"tag",
"string",
",",
"attrs",
"map",
"[",
"string",
"]",
"string",
")",
"string",
"{",
"escapeHTMLAttributeQuotes",
":=",
"func",
"(",
"v",
"string",
")",
"string",
"{",
"v",
"=",
"strings",
".",
"Replace",
"(",
"v",
",",
"\"<\"",
",",
"\"<\"",
",",
"-",
"1",
")",
"\n",
"v",
"=",
"strings",
".",
"Replace",
"(",
"v",
",",
"\"&\"",
",",
"\"&\"",
",",
"-",
"1",
")",
"\n",
"v",
"=",
"strings",
".",
"Replace",
"(",
"v",
",",
"\"\\\"\"",
",",
"\\\"",
",",
"\""\"",
")",
"\n",
"-",
"1",
"\n",
"}",
"\n",
"return",
"v",
"\n",
"if",
"tag",
"==",
"\"\"",
"{",
"tag",
"=",
"\"div\"",
"\n",
"}",
"\n",
"el",
":=",
"\"<\"",
"+",
"tag",
"\n",
"for",
"name",
",",
"val",
":=",
"range",
"attrs",
"{",
"el",
"+=",
"\" \"",
"+",
"name",
"+",
"\"=\\\"\"",
"+",
"\\\"",
"+",
"escapeHTMLAttributeQuotes",
"(",
"val",
")",
"\n",
"}",
"\n",
"\"\\\"\"",
"\n",
"}"
] |
// WrapHTML wraps s within HTML tag having attributes attrs. Note,
// WrapHTML does not escape s value.
|
[
"WrapHTML",
"wraps",
"s",
"within",
"HTML",
"tag",
"having",
"attributes",
"attrs",
".",
"Note",
"WrapHTML",
"does",
"not",
"escape",
"s",
"value",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L511-L527
|
test
|
mgutz/str
|
funcsPZ.go
|
WrapHTMLF
|
func WrapHTMLF(tag string, attrs map[string]string) func(string) string {
return func(s string) string {
return WrapHTML(s, tag, attrs)
}
}
|
go
|
func WrapHTMLF(tag string, attrs map[string]string) func(string) string {
return func(s string) string {
return WrapHTML(s, tag, attrs)
}
}
|
[
"func",
"WrapHTMLF",
"(",
"tag",
"string",
",",
"attrs",
"map",
"[",
"string",
"]",
"string",
")",
"func",
"(",
"string",
")",
"string",
"{",
"return",
"func",
"(",
"s",
"string",
")",
"string",
"{",
"return",
"WrapHTML",
"(",
"s",
",",
"tag",
",",
"attrs",
")",
"\n",
"}",
"\n",
"}"
] |
// WrapHTMLF is the filter form of WrapHTML.
|
[
"WrapHTMLF",
"is",
"the",
"filter",
"form",
"of",
"WrapHTML",
"."
] |
968bf66e3da857419e4f6e71b2d5c9ae95682dc4
|
https://github.com/mgutz/str/blob/968bf66e3da857419e4f6e71b2d5c9ae95682dc4/funcsPZ.go#L530-L534
|
test
|
hashicorp/go-reap
|
reap_unix.go
|
ReapChildren
|
func ReapChildren(pids PidCh, errors ErrorCh, done chan struct{}, reapLock *sync.RWMutex) {
c := make(chan os.Signal, 1)
signal.Notify(c, unix.SIGCHLD)
for {
// Block for an incoming signal that a child has exited.
select {
case <-c:
// Got a child signal, drop out and reap.
case <-done:
return
}
// Attempt to reap all abandoned child processes after getting
// the reap lock, which makes sure the application isn't doing
// any waiting of its own. Note that we do the full write lock
// here.
func() {
if reapLock != nil {
reapLock.Lock()
defer reapLock.Unlock()
}
POLL:
// Try to reap children until there aren't any more. We
// never block in here so that we are always responsive
// to signals, at the expense of possibly leaving a
// child behind if we get here too quickly. Any
// stragglers should get reaped the next time we see a
// signal, so we won't leak in the long run.
var status unix.WaitStatus
pid, err := unix.Wait4(-1, &status, unix.WNOHANG, nil)
switch err {
case nil:
// Got a child, clean this up and poll again.
if pid > 0 {
if pids != nil {
pids <- pid
}
goto POLL
}
return
case unix.ECHILD:
// No more children, we are done.
return
case unix.EINTR:
// We got interrupted, try again. This likely
// can't happen since we are calling Wait4 in a
// non-blocking fashion, but it's good to be
// complete and handle this case rather than
// fail.
goto POLL
default:
// We got some other error we didn't expect.
// Wait for another SIGCHLD so we don't
// potentially spam in here and chew up CPU.
if errors != nil {
errors <- err
}
return
}
}()
}
}
|
go
|
func ReapChildren(pids PidCh, errors ErrorCh, done chan struct{}, reapLock *sync.RWMutex) {
c := make(chan os.Signal, 1)
signal.Notify(c, unix.SIGCHLD)
for {
// Block for an incoming signal that a child has exited.
select {
case <-c:
// Got a child signal, drop out and reap.
case <-done:
return
}
// Attempt to reap all abandoned child processes after getting
// the reap lock, which makes sure the application isn't doing
// any waiting of its own. Note that we do the full write lock
// here.
func() {
if reapLock != nil {
reapLock.Lock()
defer reapLock.Unlock()
}
POLL:
// Try to reap children until there aren't any more. We
// never block in here so that we are always responsive
// to signals, at the expense of possibly leaving a
// child behind if we get here too quickly. Any
// stragglers should get reaped the next time we see a
// signal, so we won't leak in the long run.
var status unix.WaitStatus
pid, err := unix.Wait4(-1, &status, unix.WNOHANG, nil)
switch err {
case nil:
// Got a child, clean this up and poll again.
if pid > 0 {
if pids != nil {
pids <- pid
}
goto POLL
}
return
case unix.ECHILD:
// No more children, we are done.
return
case unix.EINTR:
// We got interrupted, try again. This likely
// can't happen since we are calling Wait4 in a
// non-blocking fashion, but it's good to be
// complete and handle this case rather than
// fail.
goto POLL
default:
// We got some other error we didn't expect.
// Wait for another SIGCHLD so we don't
// potentially spam in here and chew up CPU.
if errors != nil {
errors <- err
}
return
}
}()
}
}
|
[
"func",
"ReapChildren",
"(",
"pids",
"PidCh",
",",
"errors",
"ErrorCh",
",",
"done",
"chan",
"struct",
"{",
"}",
",",
"reapLock",
"*",
"sync",
".",
"RWMutex",
")",
"{",
"c",
":=",
"make",
"(",
"chan",
"os",
".",
"Signal",
",",
"1",
")",
"\n",
"signal",
".",
"Notify",
"(",
"c",
",",
"unix",
".",
"SIGCHLD",
")",
"\n",
"for",
"{",
"select",
"{",
"case",
"<-",
"c",
":",
"case",
"<-",
"done",
":",
"return",
"\n",
"}",
"\n",
"func",
"(",
")",
"{",
"if",
"reapLock",
"!=",
"nil",
"{",
"reapLock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"reapLock",
".",
"Unlock",
"(",
")",
"\n",
"}",
"\n",
"POLL",
":",
"var",
"status",
"unix",
".",
"WaitStatus",
"\n",
"pid",
",",
"err",
":=",
"unix",
".",
"Wait4",
"(",
"-",
"1",
",",
"&",
"status",
",",
"unix",
".",
"WNOHANG",
",",
"nil",
")",
"\n",
"switch",
"err",
"{",
"case",
"nil",
":",
"if",
"pid",
">",
"0",
"{",
"if",
"pids",
"!=",
"nil",
"{",
"pids",
"<-",
"pid",
"\n",
"}",
"\n",
"goto",
"POLL",
"\n",
"}",
"\n",
"return",
"\n",
"case",
"unix",
".",
"ECHILD",
":",
"return",
"\n",
"case",
"unix",
".",
"EINTR",
":",
"goto",
"POLL",
"\n",
"default",
":",
"if",
"errors",
"!=",
"nil",
"{",
"errors",
"<-",
"err",
"\n",
"}",
"\n",
"return",
"\n",
"}",
"\n",
"}",
"(",
")",
"\n",
"}",
"\n",
"}"
] |
// ReapChildren is a long-running routine that blocks waiting for child
// processes to exit and reaps them, reporting reaped process IDs to the
// optional pids channel and any errors to the optional errors channel.
//
// The optional reapLock will be used to prevent reaping during periods
// when you know your application is waiting for subprocesses to return.
// You need to use care in order to prevent the reaper from stealing your
// return values from uses of packages like Go's exec. We use an RWMutex
// so that we don't serialize all of the application's execution of sub
// processes with each other, but we do serialize them with reaping. The
// application should get a read lock when it wants to do a wait.
|
[
"ReapChildren",
"is",
"a",
"long",
"-",
"running",
"routine",
"that",
"blocks",
"waiting",
"for",
"child",
"processes",
"to",
"exit",
"and",
"reaps",
"them",
"reporting",
"reaped",
"process",
"IDs",
"to",
"the",
"optional",
"pids",
"channel",
"and",
"any",
"errors",
"to",
"the",
"optional",
"errors",
"channel",
".",
"The",
"optional",
"reapLock",
"will",
"be",
"used",
"to",
"prevent",
"reaping",
"during",
"periods",
"when",
"you",
"know",
"your",
"application",
"is",
"waiting",
"for",
"subprocesses",
"to",
"return",
".",
"You",
"need",
"to",
"use",
"care",
"in",
"order",
"to",
"prevent",
"the",
"reaper",
"from",
"stealing",
"your",
"return",
"values",
"from",
"uses",
"of",
"packages",
"like",
"Go",
"s",
"exec",
".",
"We",
"use",
"an",
"RWMutex",
"so",
"that",
"we",
"don",
"t",
"serialize",
"all",
"of",
"the",
"application",
"s",
"execution",
"of",
"sub",
"processes",
"with",
"each",
"other",
"but",
"we",
"do",
"serialize",
"them",
"with",
"reaping",
".",
"The",
"application",
"should",
"get",
"a",
"read",
"lock",
"when",
"it",
"wants",
"to",
"do",
"a",
"wait",
"."
] |
bf58d8a43e7b6bf026d99d6295c4de703b67b374
|
https://github.com/hashicorp/go-reap/blob/bf58d8a43e7b6bf026d99d6295c4de703b67b374/reap_unix.go#L30-L96
|
test
|
okzk/sdnotify
|
notify_linux.go
|
SdNotify
|
func SdNotify(state string) error {
name := os.Getenv("NOTIFY_SOCKET")
if name == "" {
return ErrSdNotifyNoSocket
}
conn, err := net.DialUnix("unixgram", nil, &net.UnixAddr{Name: name, Net: "unixgram"})
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Write([]byte(state))
return err
}
|
go
|
func SdNotify(state string) error {
name := os.Getenv("NOTIFY_SOCKET")
if name == "" {
return ErrSdNotifyNoSocket
}
conn, err := net.DialUnix("unixgram", nil, &net.UnixAddr{Name: name, Net: "unixgram"})
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Write([]byte(state))
return err
}
|
[
"func",
"SdNotify",
"(",
"state",
"string",
")",
"error",
"{",
"name",
":=",
"os",
".",
"Getenv",
"(",
"\"NOTIFY_SOCKET\"",
")",
"\n",
"if",
"name",
"==",
"\"\"",
"{",
"return",
"ErrSdNotifyNoSocket",
"\n",
"}",
"\n",
"conn",
",",
"err",
":=",
"net",
".",
"DialUnix",
"(",
"\"unixgram\"",
",",
"nil",
",",
"&",
"net",
".",
"UnixAddr",
"{",
"Name",
":",
"name",
",",
"Net",
":",
"\"unixgram\"",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"defer",
"conn",
".",
"Close",
"(",
")",
"\n",
"_",
",",
"err",
"=",
"conn",
".",
"Write",
"(",
"[",
"]",
"byte",
"(",
"state",
")",
")",
"\n",
"return",
"err",
"\n",
"}"
] |
// SdNotify sends a specified string to the systemd notification socket.
|
[
"SdNotify",
"sends",
"a",
"specified",
"string",
"to",
"the",
"systemd",
"notification",
"socket",
"."
] |
d9becc38acbd785892af7637319e2c5e101057f7
|
https://github.com/okzk/sdnotify/blob/d9becc38acbd785892af7637319e2c5e101057f7/notify_linux.go#L9-L23
|
test
|
256dpi/fire
|
flame/authenticator.go
|
NewAuthenticator
|
func NewAuthenticator(store *coal.Store, policy *Policy) *Authenticator {
// initialize token
coal.Init(policy.Token)
// initialize clients
for _, model := range policy.Clients {
coal.Init(model)
}
return &Authenticator{
store: store,
policy: policy,
}
}
|
go
|
func NewAuthenticator(store *coal.Store, policy *Policy) *Authenticator {
// initialize token
coal.Init(policy.Token)
// initialize clients
for _, model := range policy.Clients {
coal.Init(model)
}
return &Authenticator{
store: store,
policy: policy,
}
}
|
[
"func",
"NewAuthenticator",
"(",
"store",
"*",
"coal",
".",
"Store",
",",
"policy",
"*",
"Policy",
")",
"*",
"Authenticator",
"{",
"coal",
".",
"Init",
"(",
"policy",
".",
"Token",
")",
"\n",
"for",
"_",
",",
"model",
":=",
"range",
"policy",
".",
"Clients",
"{",
"coal",
".",
"Init",
"(",
"model",
")",
"\n",
"}",
"\n",
"return",
"&",
"Authenticator",
"{",
"store",
":",
"store",
",",
"policy",
":",
"policy",
",",
"}",
"\n",
"}"
] |
// NewAuthenticator constructs a new Authenticator from a store and policy.
|
[
"NewAuthenticator",
"constructs",
"a",
"new",
"Authenticator",
"from",
"a",
"store",
"and",
"policy",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/flame/authenticator.go#L55-L68
|
test
|
256dpi/fire
|
flame/authenticator.go
|
Endpoint
|
func (a *Authenticator) Endpoint(prefix string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// create tracer
tracer := fire.NewTracerFromRequest(r, "flame/Authenticator.Endpoint")
tracer.Tag("prefix", prefix)
defer tracer.Finish(true)
// continue any previous aborts
defer stack.Resume(func(err error) {
// directly write oauth2 errors
if oauth2Error, ok := err.(*oauth2.Error); ok {
_ = oauth2.WriteError(w, oauth2Error)
return
}
// set critical error on last span
tracer.Tag("error", true)
tracer.Log("error", err.Error())
tracer.Log("stack", stack.Trace())
// otherwise report critical errors
if a.Reporter != nil {
a.Reporter(err)
}
// ignore errors caused by writing critical errors
_ = oauth2.WriteError(w, oauth2.ServerError(""))
})
// trim and split path
s := strings.Split(strings.Trim(strings.TrimPrefix(r.URL.Path, prefix), "/"), "/")
if len(s) != 1 || (s[0] != "authorize" && s[0] != "token" && s[0] != "revoke") {
w.WriteHeader(http.StatusNotFound)
return
}
// copy store
store := a.store.Copy()
defer store.Close()
// create
state := &state{
request: r,
writer: w,
store: store,
tracer: tracer,
}
// call endpoints
switch s[0] {
case "authorize":
a.authorizationEndpoint(state)
case "token":
a.tokenEndpoint(state)
case "revoke":
a.revocationEndpoint(state)
}
})
}
|
go
|
func (a *Authenticator) Endpoint(prefix string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// create tracer
tracer := fire.NewTracerFromRequest(r, "flame/Authenticator.Endpoint")
tracer.Tag("prefix", prefix)
defer tracer.Finish(true)
// continue any previous aborts
defer stack.Resume(func(err error) {
// directly write oauth2 errors
if oauth2Error, ok := err.(*oauth2.Error); ok {
_ = oauth2.WriteError(w, oauth2Error)
return
}
// set critical error on last span
tracer.Tag("error", true)
tracer.Log("error", err.Error())
tracer.Log("stack", stack.Trace())
// otherwise report critical errors
if a.Reporter != nil {
a.Reporter(err)
}
// ignore errors caused by writing critical errors
_ = oauth2.WriteError(w, oauth2.ServerError(""))
})
// trim and split path
s := strings.Split(strings.Trim(strings.TrimPrefix(r.URL.Path, prefix), "/"), "/")
if len(s) != 1 || (s[0] != "authorize" && s[0] != "token" && s[0] != "revoke") {
w.WriteHeader(http.StatusNotFound)
return
}
// copy store
store := a.store.Copy()
defer store.Close()
// create
state := &state{
request: r,
writer: w,
store: store,
tracer: tracer,
}
// call endpoints
switch s[0] {
case "authorize":
a.authorizationEndpoint(state)
case "token":
a.tokenEndpoint(state)
case "revoke":
a.revocationEndpoint(state)
}
})
}
|
[
"func",
"(",
"a",
"*",
"Authenticator",
")",
"Endpoint",
"(",
"prefix",
"string",
")",
"http",
".",
"Handler",
"{",
"return",
"http",
".",
"HandlerFunc",
"(",
"func",
"(",
"w",
"http",
".",
"ResponseWriter",
",",
"r",
"*",
"http",
".",
"Request",
")",
"{",
"tracer",
":=",
"fire",
".",
"NewTracerFromRequest",
"(",
"r",
",",
"\"flame/Authenticator.Endpoint\"",
")",
"\n",
"tracer",
".",
"Tag",
"(",
"\"prefix\"",
",",
"prefix",
")",
"\n",
"defer",
"tracer",
".",
"Finish",
"(",
"true",
")",
"\n",
"defer",
"stack",
".",
"Resume",
"(",
"func",
"(",
"err",
"error",
")",
"{",
"if",
"oauth2Error",
",",
"ok",
":=",
"err",
".",
"(",
"*",
"oauth2",
".",
"Error",
")",
";",
"ok",
"{",
"_",
"=",
"oauth2",
".",
"WriteError",
"(",
"w",
",",
"oauth2Error",
")",
"\n",
"return",
"\n",
"}",
"\n",
"tracer",
".",
"Tag",
"(",
"\"error\"",
",",
"true",
")",
"\n",
"tracer",
".",
"Log",
"(",
"\"error\"",
",",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"tracer",
".",
"Log",
"(",
"\"stack\"",
",",
"stack",
".",
"Trace",
"(",
")",
")",
"\n",
"if",
"a",
".",
"Reporter",
"!=",
"nil",
"{",
"a",
".",
"Reporter",
"(",
"err",
")",
"\n",
"}",
"\n",
"_",
"=",
"oauth2",
".",
"WriteError",
"(",
"w",
",",
"oauth2",
".",
"ServerError",
"(",
"\"\"",
")",
")",
"\n",
"}",
")",
"\n",
"s",
":=",
"strings",
".",
"Split",
"(",
"strings",
".",
"Trim",
"(",
"strings",
".",
"TrimPrefix",
"(",
"r",
".",
"URL",
".",
"Path",
",",
"prefix",
")",
",",
"\"/\"",
")",
",",
"\"/\"",
")",
"\n",
"if",
"len",
"(",
"s",
")",
"!=",
"1",
"||",
"(",
"s",
"[",
"0",
"]",
"!=",
"\"authorize\"",
"&&",
"s",
"[",
"0",
"]",
"!=",
"\"token\"",
"&&",
"s",
"[",
"0",
"]",
"!=",
"\"revoke\"",
")",
"{",
"w",
".",
"WriteHeader",
"(",
"http",
".",
"StatusNotFound",
")",
"\n",
"return",
"\n",
"}",
"\n",
"store",
":=",
"a",
".",
"store",
".",
"Copy",
"(",
")",
"\n",
"defer",
"store",
".",
"Close",
"(",
")",
"\n",
"state",
":=",
"&",
"state",
"{",
"request",
":",
"r",
",",
"writer",
":",
"w",
",",
"store",
":",
"store",
",",
"tracer",
":",
"tracer",
",",
"}",
"\n",
"switch",
"s",
"[",
"0",
"]",
"{",
"case",
"\"authorize\"",
":",
"a",
".",
"authorizationEndpoint",
"(",
"state",
")",
"\n",
"case",
"\"token\"",
":",
"a",
".",
"tokenEndpoint",
"(",
"state",
")",
"\n",
"case",
"\"revoke\"",
":",
"a",
".",
"revocationEndpoint",
"(",
"state",
")",
"\n",
"}",
"\n",
"}",
")",
"\n",
"}"
] |
// Endpoint returns a handler for the common token and authorize endpoint.
|
[
"Endpoint",
"returns",
"a",
"handler",
"for",
"the",
"common",
"token",
"and",
"authorize",
"endpoint",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/flame/authenticator.go#L71-L129
|
test
|
256dpi/fire
|
coal/helpers.go
|
Unique
|
func Unique(ids []bson.ObjectId) []bson.ObjectId {
// prepare map
m := make(map[bson.ObjectId]bool)
l := make([]bson.ObjectId, 0, len(ids))
for _, id := range ids {
if _, ok := m[id]; !ok {
m[id] = true
l = append(l, id)
}
}
return l
}
|
go
|
func Unique(ids []bson.ObjectId) []bson.ObjectId {
// prepare map
m := make(map[bson.ObjectId]bool)
l := make([]bson.ObjectId, 0, len(ids))
for _, id := range ids {
if _, ok := m[id]; !ok {
m[id] = true
l = append(l, id)
}
}
return l
}
|
[
"func",
"Unique",
"(",
"ids",
"[",
"]",
"bson",
".",
"ObjectId",
")",
"[",
"]",
"bson",
".",
"ObjectId",
"{",
"m",
":=",
"make",
"(",
"map",
"[",
"bson",
".",
"ObjectId",
"]",
"bool",
")",
"\n",
"l",
":=",
"make",
"(",
"[",
"]",
"bson",
".",
"ObjectId",
",",
"0",
",",
"len",
"(",
"ids",
")",
")",
"\n",
"for",
"_",
",",
"id",
":=",
"range",
"ids",
"{",
"if",
"_",
",",
"ok",
":=",
"m",
"[",
"id",
"]",
";",
"!",
"ok",
"{",
"m",
"[",
"id",
"]",
"=",
"true",
"\n",
"l",
"=",
"append",
"(",
"l",
",",
"id",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"l",
"\n",
"}"
] |
// Unique is a helper to get a unique list of object ids.
|
[
"Unique",
"is",
"a",
"helper",
"to",
"get",
"a",
"unique",
"list",
"of",
"object",
"ids",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/coal/helpers.go#L107-L120
|
test
|
256dpi/fire
|
coal/helpers.go
|
Contains
|
func Contains(list []bson.ObjectId, id bson.ObjectId) bool {
for _, item := range list {
if item == id {
return true
}
}
return false
}
|
go
|
func Contains(list []bson.ObjectId, id bson.ObjectId) bool {
for _, item := range list {
if item == id {
return true
}
}
return false
}
|
[
"func",
"Contains",
"(",
"list",
"[",
"]",
"bson",
".",
"ObjectId",
",",
"id",
"bson",
".",
"ObjectId",
")",
"bool",
"{",
"for",
"_",
",",
"item",
":=",
"range",
"list",
"{",
"if",
"item",
"==",
"id",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] |
// Contains returns true if a list of object ids contains the specified id.
|
[
"Contains",
"returns",
"true",
"if",
"a",
"list",
"of",
"object",
"ids",
"contains",
"the",
"specified",
"id",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/coal/helpers.go#L123-L131
|
test
|
256dpi/fire
|
coal/helpers.go
|
Includes
|
func Includes(all, subset []bson.ObjectId) bool {
for _, item := range subset {
if !Contains(all, item) {
return false
}
}
return true
}
|
go
|
func Includes(all, subset []bson.ObjectId) bool {
for _, item := range subset {
if !Contains(all, item) {
return false
}
}
return true
}
|
[
"func",
"Includes",
"(",
"all",
",",
"subset",
"[",
"]",
"bson",
".",
"ObjectId",
")",
"bool",
"{",
"for",
"_",
",",
"item",
":=",
"range",
"subset",
"{",
"if",
"!",
"Contains",
"(",
"all",
",",
"item",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] |
// Includes returns true if a list of object ids includes another list of object
// ids.
|
[
"Includes",
"returns",
"true",
"if",
"a",
"list",
"of",
"object",
"ids",
"includes",
"another",
"list",
"of",
"object",
"ids",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/coal/helpers.go#L135-L143
|
test
|
256dpi/fire
|
coal/helpers.go
|
Require
|
func Require(m Model, flags ...string) {
// check all flags
for _, f := range flags {
L(m, f, true)
}
}
|
go
|
func Require(m Model, flags ...string) {
// check all flags
for _, f := range flags {
L(m, f, true)
}
}
|
[
"func",
"Require",
"(",
"m",
"Model",
",",
"flags",
"...",
"string",
")",
"{",
"for",
"_",
",",
"f",
":=",
"range",
"flags",
"{",
"L",
"(",
"m",
",",
"f",
",",
"true",
")",
"\n",
"}",
"\n",
"}"
] |
// Require will check if the specified flags are set on the specified model and
// panic if one is missing.
|
[
"Require",
"will",
"check",
"if",
"the",
"specified",
"flags",
"are",
"set",
"on",
"the",
"specified",
"model",
"and",
"panic",
"if",
"one",
"is",
"missing",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/coal/helpers.go#L147-L152
|
test
|
256dpi/fire
|
axe/job.go
|
Enqueue
|
func Enqueue(store *coal.SubStore, name string, data Model, delay time.Duration) (*Job, error) {
// set default data
if data == nil {
data = bson.M{}
}
// get time
now := time.Now()
// prepare job
job := coal.Init(&Job{
Name: name,
Status: StatusEnqueued,
Created: now,
Available: now.Add(delay),
}).(*Job)
// marshall data
raw, err := bson.Marshal(data)
if err != nil {
return nil, err
}
// marshall into job
err = bson.Unmarshal(raw, &job.Data)
if err != nil {
return nil, err
}
// insert job
err = store.C(job).Insert(job)
if err != nil {
return nil, err
}
return job, nil
}
|
go
|
func Enqueue(store *coal.SubStore, name string, data Model, delay time.Duration) (*Job, error) {
// set default data
if data == nil {
data = bson.M{}
}
// get time
now := time.Now()
// prepare job
job := coal.Init(&Job{
Name: name,
Status: StatusEnqueued,
Created: now,
Available: now.Add(delay),
}).(*Job)
// marshall data
raw, err := bson.Marshal(data)
if err != nil {
return nil, err
}
// marshall into job
err = bson.Unmarshal(raw, &job.Data)
if err != nil {
return nil, err
}
// insert job
err = store.C(job).Insert(job)
if err != nil {
return nil, err
}
return job, nil
}
|
[
"func",
"Enqueue",
"(",
"store",
"*",
"coal",
".",
"SubStore",
",",
"name",
"string",
",",
"data",
"Model",
",",
"delay",
"time",
".",
"Duration",
")",
"(",
"*",
"Job",
",",
"error",
")",
"{",
"if",
"data",
"==",
"nil",
"{",
"data",
"=",
"bson",
".",
"M",
"{",
"}",
"\n",
"}",
"\n",
"now",
":=",
"time",
".",
"Now",
"(",
")",
"\n",
"job",
":=",
"coal",
".",
"Init",
"(",
"&",
"Job",
"{",
"Name",
":",
"name",
",",
"Status",
":",
"StatusEnqueued",
",",
"Created",
":",
"now",
",",
"Available",
":",
"now",
".",
"Add",
"(",
"delay",
")",
",",
"}",
")",
".",
"(",
"*",
"Job",
")",
"\n",
"raw",
",",
"err",
":=",
"bson",
".",
"Marshal",
"(",
"data",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"err",
"=",
"bson",
".",
"Unmarshal",
"(",
"raw",
",",
"&",
"job",
".",
"Data",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"err",
"=",
"store",
".",
"C",
"(",
"job",
")",
".",
"Insert",
"(",
"job",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"job",
",",
"nil",
"\n",
"}"
] |
// Enqueue will enqueue a job using the specified name and data. If a delay
// is specified the job will not be dequeued until the specified time has passed.
|
[
"Enqueue",
"will",
"enqueue",
"a",
"job",
"using",
"the",
"specified",
"name",
"and",
"data",
".",
"If",
"a",
"delay",
"is",
"specified",
"the",
"job",
"will",
"not",
"be",
"dequeued",
"until",
"the",
"specified",
"time",
"has",
"passed",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/axe/job.go#L93-L129
|
test
|
256dpi/fire
|
ash/authorizer.go
|
A
|
func A(name string, m fire.Matcher, h Handler) *Authorizer {
// panic if matcher or handler is not set
if m == nil || h == nil {
panic("ash: missing matcher or handler")
}
// construct and return authorizer
return &Authorizer{
Matcher: m,
Handler: func(ctx *fire.Context) ([]*Enforcer, error) {
// begin trace
ctx.Tracer.Push(name)
// call handler
enforcers, err := h(ctx)
if err != nil {
return nil, err
}
// finish trace
ctx.Tracer.Pop()
return enforcers, nil
},
}
}
|
go
|
func A(name string, m fire.Matcher, h Handler) *Authorizer {
// panic if matcher or handler is not set
if m == nil || h == nil {
panic("ash: missing matcher or handler")
}
// construct and return authorizer
return &Authorizer{
Matcher: m,
Handler: func(ctx *fire.Context) ([]*Enforcer, error) {
// begin trace
ctx.Tracer.Push(name)
// call handler
enforcers, err := h(ctx)
if err != nil {
return nil, err
}
// finish trace
ctx.Tracer.Pop()
return enforcers, nil
},
}
}
|
[
"func",
"A",
"(",
"name",
"string",
",",
"m",
"fire",
".",
"Matcher",
",",
"h",
"Handler",
")",
"*",
"Authorizer",
"{",
"if",
"m",
"==",
"nil",
"||",
"h",
"==",
"nil",
"{",
"panic",
"(",
"\"ash: missing matcher or handler\"",
")",
"\n",
"}",
"\n",
"return",
"&",
"Authorizer",
"{",
"Matcher",
":",
"m",
",",
"Handler",
":",
"func",
"(",
"ctx",
"*",
"fire",
".",
"Context",
")",
"(",
"[",
"]",
"*",
"Enforcer",
",",
"error",
")",
"{",
"ctx",
".",
"Tracer",
".",
"Push",
"(",
"name",
")",
"\n",
"enforcers",
",",
"err",
":=",
"h",
"(",
"ctx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"ctx",
".",
"Tracer",
".",
"Pop",
"(",
")",
"\n",
"return",
"enforcers",
",",
"nil",
"\n",
"}",
",",
"}",
"\n",
"}"
] |
// A is a short-hand function to construct an authorizer. It will also add tracing
// code around the execution of the authorizer.
|
[
"A",
"is",
"a",
"short",
"-",
"hand",
"function",
"to",
"construct",
"an",
"authorizer",
".",
"It",
"will",
"also",
"add",
"tracing",
"code",
"around",
"the",
"execution",
"of",
"the",
"authorizer",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/ash/authorizer.go#L7-L32
|
test
|
256dpi/fire
|
ash/authorizer.go
|
Run
|
func Run(enforcers ...*Enforcer) *Authorizer {
return A("ash/Run", fire.All(), func(ctx *fire.Context) ([]*Enforcer, error) {
return enforcers, nil
})
}
|
go
|
func Run(enforcers ...*Enforcer) *Authorizer {
return A("ash/Run", fire.All(), func(ctx *fire.Context) ([]*Enforcer, error) {
return enforcers, nil
})
}
|
[
"func",
"Run",
"(",
"enforcers",
"...",
"*",
"Enforcer",
")",
"*",
"Authorizer",
"{",
"return",
"A",
"(",
"\"ash/Run\"",
",",
"fire",
".",
"All",
"(",
")",
",",
"func",
"(",
"ctx",
"*",
"fire",
".",
"Context",
")",
"(",
"[",
"]",
"*",
"Enforcer",
",",
"error",
")",
"{",
"return",
"enforcers",
",",
"nil",
"\n",
"}",
")",
"\n",
"}"
] |
// Run will authorize immediately and return the provided list of enforcers.
|
[
"Run",
"will",
"authorize",
"immediately",
"and",
"return",
"the",
"provided",
"list",
"of",
"enforcers",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/ash/authorizer.go#L53-L57
|
test
|
256dpi/fire
|
ash/authorizer.go
|
And
|
func And(a, b *Authorizer) *Authorizer {
return A("ash/And", func(ctx *fire.Context) bool {
return a.Matcher(ctx) && b.Matcher(ctx)
}, func(ctx *fire.Context) ([]*Enforcer, error) {
// run first callback
enforcers1, err := a.Handler(ctx)
if err != nil {
return nil, err
} else if enforcers1 == nil {
return nil, nil
}
// run second callback
enforcers2, err := b.Handler(ctx)
if err != nil {
return nil, err
} else if enforcers2 == nil {
return nil, nil
}
// merge both sets
enforcers := append(S{}, enforcers1...)
enforcers = append(enforcers, enforcers2...)
return enforcers, nil
})
}
|
go
|
func And(a, b *Authorizer) *Authorizer {
return A("ash/And", func(ctx *fire.Context) bool {
return a.Matcher(ctx) && b.Matcher(ctx)
}, func(ctx *fire.Context) ([]*Enforcer, error) {
// run first callback
enforcers1, err := a.Handler(ctx)
if err != nil {
return nil, err
} else if enforcers1 == nil {
return nil, nil
}
// run second callback
enforcers2, err := b.Handler(ctx)
if err != nil {
return nil, err
} else if enforcers2 == nil {
return nil, nil
}
// merge both sets
enforcers := append(S{}, enforcers1...)
enforcers = append(enforcers, enforcers2...)
return enforcers, nil
})
}
|
[
"func",
"And",
"(",
"a",
",",
"b",
"*",
"Authorizer",
")",
"*",
"Authorizer",
"{",
"return",
"A",
"(",
"\"ash/And\"",
",",
"func",
"(",
"ctx",
"*",
"fire",
".",
"Context",
")",
"bool",
"{",
"return",
"a",
".",
"Matcher",
"(",
"ctx",
")",
"&&",
"b",
".",
"Matcher",
"(",
"ctx",
")",
"\n",
"}",
",",
"func",
"(",
"ctx",
"*",
"fire",
".",
"Context",
")",
"(",
"[",
"]",
"*",
"Enforcer",
",",
"error",
")",
"{",
"enforcers1",
",",
"err",
":=",
"a",
".",
"Handler",
"(",
"ctx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"else",
"if",
"enforcers1",
"==",
"nil",
"{",
"return",
"nil",
",",
"nil",
"\n",
"}",
"\n",
"enforcers2",
",",
"err",
":=",
"b",
".",
"Handler",
"(",
"ctx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"else",
"if",
"enforcers2",
"==",
"nil",
"{",
"return",
"nil",
",",
"nil",
"\n",
"}",
"\n",
"enforcers",
":=",
"append",
"(",
"S",
"{",
"}",
",",
"enforcers1",
"...",
")",
"\n",
"enforcers",
"=",
"append",
"(",
"enforcers",
",",
"enforcers2",
"...",
")",
"\n",
"return",
"enforcers",
",",
"nil",
"\n",
"}",
")",
"\n",
"}"
] |
// And will match and run both authorizers and return immediately if one does not
// return a set of enforcers. The two successfully returned enforcer sets are
// merged into one and returned.
|
[
"And",
"will",
"match",
"and",
"run",
"both",
"authorizers",
"and",
"return",
"immediately",
"if",
"one",
"does",
"not",
"return",
"a",
"set",
"of",
"enforcers",
".",
"The",
"two",
"successfully",
"returned",
"enforcer",
"sets",
"are",
"merged",
"into",
"one",
"and",
"returned",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/ash/authorizer.go#L62-L88
|
test
|
256dpi/fire
|
ash/authorizer.go
|
Or
|
func Or(a, b *Authorizer) *Authorizer {
return A("ash/Or", func(ctx *fire.Context) bool {
return a.Matcher(ctx) || b.Matcher(ctx)
}, func(ctx *fire.Context) ([]*Enforcer, error) {
// check first authorizer
if a.Matcher(ctx) {
// run callback
enforcers, err := a.Handler(ctx)
if err != nil {
return nil, err
}
// return on success
if enforcers != nil {
return enforcers, nil
}
}
// check second authorizer
if b.Matcher(ctx) {
// run callback
enforcers, err := b.Handler(ctx)
if err != nil {
return nil, err
}
// return on success
if enforcers != nil {
return enforcers, nil
}
}
return nil, nil
})
}
|
go
|
func Or(a, b *Authorizer) *Authorizer {
return A("ash/Or", func(ctx *fire.Context) bool {
return a.Matcher(ctx) || b.Matcher(ctx)
}, func(ctx *fire.Context) ([]*Enforcer, error) {
// check first authorizer
if a.Matcher(ctx) {
// run callback
enforcers, err := a.Handler(ctx)
if err != nil {
return nil, err
}
// return on success
if enforcers != nil {
return enforcers, nil
}
}
// check second authorizer
if b.Matcher(ctx) {
// run callback
enforcers, err := b.Handler(ctx)
if err != nil {
return nil, err
}
// return on success
if enforcers != nil {
return enforcers, nil
}
}
return nil, nil
})
}
|
[
"func",
"Or",
"(",
"a",
",",
"b",
"*",
"Authorizer",
")",
"*",
"Authorizer",
"{",
"return",
"A",
"(",
"\"ash/Or\"",
",",
"func",
"(",
"ctx",
"*",
"fire",
".",
"Context",
")",
"bool",
"{",
"return",
"a",
".",
"Matcher",
"(",
"ctx",
")",
"||",
"b",
".",
"Matcher",
"(",
"ctx",
")",
"\n",
"}",
",",
"func",
"(",
"ctx",
"*",
"fire",
".",
"Context",
")",
"(",
"[",
"]",
"*",
"Enforcer",
",",
"error",
")",
"{",
"if",
"a",
".",
"Matcher",
"(",
"ctx",
")",
"{",
"enforcers",
",",
"err",
":=",
"a",
".",
"Handler",
"(",
"ctx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"if",
"enforcers",
"!=",
"nil",
"{",
"return",
"enforcers",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"b",
".",
"Matcher",
"(",
"ctx",
")",
"{",
"enforcers",
",",
"err",
":=",
"b",
".",
"Handler",
"(",
"ctx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"if",
"enforcers",
"!=",
"nil",
"{",
"return",
"enforcers",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
",",
"nil",
"\n",
"}",
")",
"\n",
"}"
] |
// Or will match and run the first authorizer and return its enforcers on success.
// If no enforcers are returned it will match and run the second authorizer and
// return its enforcers.
|
[
"Or",
"will",
"match",
"and",
"run",
"the",
"first",
"authorizer",
"and",
"return",
"its",
"enforcers",
"on",
"success",
".",
"If",
"no",
"enforcers",
"are",
"returned",
"it",
"will",
"match",
"and",
"run",
"the",
"second",
"authorizer",
"and",
"return",
"its",
"enforcers",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/ash/authorizer.go#L98-L132
|
test
|
256dpi/fire
|
axe/queue.go
|
Enqueue
|
func (q *Queue) Enqueue(name string, data Model, delay time.Duration) (*Job, error) {
// copy store
store := q.store.Copy()
defer store.Close()
// enqueue job
job, err := Enqueue(store, name, data, delay)
if err != nil {
return nil, err
}
return job, nil
}
|
go
|
func (q *Queue) Enqueue(name string, data Model, delay time.Duration) (*Job, error) {
// copy store
store := q.store.Copy()
defer store.Close()
// enqueue job
job, err := Enqueue(store, name, data, delay)
if err != nil {
return nil, err
}
return job, nil
}
|
[
"func",
"(",
"q",
"*",
"Queue",
")",
"Enqueue",
"(",
"name",
"string",
",",
"data",
"Model",
",",
"delay",
"time",
".",
"Duration",
")",
"(",
"*",
"Job",
",",
"error",
")",
"{",
"store",
":=",
"q",
".",
"store",
".",
"Copy",
"(",
")",
"\n",
"defer",
"store",
".",
"Close",
"(",
")",
"\n",
"job",
",",
"err",
":=",
"Enqueue",
"(",
"store",
",",
"name",
",",
"data",
",",
"delay",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"job",
",",
"nil",
"\n",
"}"
] |
// Enqueue will enqueue a job using the specified name and data. If a delay
// is specified the job will not dequeued until the specified time has passed.
|
[
"Enqueue",
"will",
"enqueue",
"a",
"job",
"using",
"the",
"specified",
"name",
"and",
"data",
".",
"If",
"a",
"delay",
"is",
"specified",
"the",
"job",
"will",
"not",
"dequeued",
"until",
"the",
"specified",
"time",
"has",
"passed",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/axe/queue.go#L46-L58
|
test
|
256dpi/fire
|
axe/queue.go
|
Callback
|
func (q *Queue) Callback(name string, delay time.Duration, matcher fire.Matcher, cb func(ctx *fire.Context) Model) *fire.Callback {
return fire.C("axe/Queue.Callback", matcher, func(ctx *fire.Context) error {
// set task tag
ctx.Tracer.Tag("task", name)
// get data
var data Model
if cb != nil {
data = cb(ctx)
}
// check if controller uses same store
if q.store == ctx.Controller.Store {
// enqueue job using context store
_, err := Enqueue(ctx.Store, name, data, delay)
if err != nil {
return err
}
} else {
// enqueue job using queue store
_, err := q.Enqueue(name, data, delay)
if err != nil {
return err
}
}
// respond with an empty object
if ctx.Operation.Action() {
err := ctx.Respond(fire.Map{})
if err != nil {
return err
}
}
return nil
})
}
|
go
|
func (q *Queue) Callback(name string, delay time.Duration, matcher fire.Matcher, cb func(ctx *fire.Context) Model) *fire.Callback {
return fire.C("axe/Queue.Callback", matcher, func(ctx *fire.Context) error {
// set task tag
ctx.Tracer.Tag("task", name)
// get data
var data Model
if cb != nil {
data = cb(ctx)
}
// check if controller uses same store
if q.store == ctx.Controller.Store {
// enqueue job using context store
_, err := Enqueue(ctx.Store, name, data, delay)
if err != nil {
return err
}
} else {
// enqueue job using queue store
_, err := q.Enqueue(name, data, delay)
if err != nil {
return err
}
}
// respond with an empty object
if ctx.Operation.Action() {
err := ctx.Respond(fire.Map{})
if err != nil {
return err
}
}
return nil
})
}
|
[
"func",
"(",
"q",
"*",
"Queue",
")",
"Callback",
"(",
"name",
"string",
",",
"delay",
"time",
".",
"Duration",
",",
"matcher",
"fire",
".",
"Matcher",
",",
"cb",
"func",
"(",
"ctx",
"*",
"fire",
".",
"Context",
")",
"Model",
")",
"*",
"fire",
".",
"Callback",
"{",
"return",
"fire",
".",
"C",
"(",
"\"axe/Queue.Callback\"",
",",
"matcher",
",",
"func",
"(",
"ctx",
"*",
"fire",
".",
"Context",
")",
"error",
"{",
"ctx",
".",
"Tracer",
".",
"Tag",
"(",
"\"task\"",
",",
"name",
")",
"\n",
"var",
"data",
"Model",
"\n",
"if",
"cb",
"!=",
"nil",
"{",
"data",
"=",
"cb",
"(",
"ctx",
")",
"\n",
"}",
"\n",
"if",
"q",
".",
"store",
"==",
"ctx",
".",
"Controller",
".",
"Store",
"{",
"_",
",",
"err",
":=",
"Enqueue",
"(",
"ctx",
".",
"Store",
",",
"name",
",",
"data",
",",
"delay",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"else",
"{",
"_",
",",
"err",
":=",
"q",
".",
"Enqueue",
"(",
"name",
",",
"data",
",",
"delay",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"ctx",
".",
"Operation",
".",
"Action",
"(",
")",
"{",
"err",
":=",
"ctx",
".",
"Respond",
"(",
"fire",
".",
"Map",
"{",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}",
")",
"\n",
"}"
] |
// Callback is a factory to create callbacks that can be used to enqueue jobs
// during request processing.
|
[
"Callback",
"is",
"a",
"factory",
"to",
"create",
"callbacks",
"that",
"can",
"be",
"used",
"to",
"enqueue",
"jobs",
"during",
"request",
"processing",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/axe/queue.go#L62-L98
|
test
|
256dpi/fire
|
spark/watcher.go
|
NewWatcher
|
func NewWatcher() *Watcher {
// prepare watcher
w := &Watcher{
streams: make(map[string]*Stream),
}
// create and add manager
w.manager = newManager(w)
return w
}
|
go
|
func NewWatcher() *Watcher {
// prepare watcher
w := &Watcher{
streams: make(map[string]*Stream),
}
// create and add manager
w.manager = newManager(w)
return w
}
|
[
"func",
"NewWatcher",
"(",
")",
"*",
"Watcher",
"{",
"w",
":=",
"&",
"Watcher",
"{",
"streams",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"Stream",
")",
",",
"}",
"\n",
"w",
".",
"manager",
"=",
"newManager",
"(",
"w",
")",
"\n",
"return",
"w",
"\n",
"}"
] |
// NewWatcher creates and returns a new watcher.
|
[
"NewWatcher",
"creates",
"and",
"returns",
"a",
"new",
"watcher",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/spark/watcher.go#L25-L35
|
test
|
256dpi/fire
|
spark/watcher.go
|
Add
|
func (w *Watcher) Add(stream *Stream) {
// initialize model
coal.Init(stream.Model)
// check existence
if w.streams[stream.Name()] != nil {
panic(fmt.Sprintf(`spark: stream with name "%s" already exists`, stream.Name()))
}
// save stream
w.streams[stream.Name()] = stream
// open stream
coal.OpenStream(stream.Store, stream.Model, nil, func(e coal.Event, id bson.ObjectId, m coal.Model, token []byte) {
// ignore real deleted events when soft delete has been enabled
if stream.SoftDelete && e == coal.Deleted {
return
}
// handle soft deleted documents
if stream.SoftDelete && e == coal.Updated {
// get soft delete field
softDeleteField := coal.L(stream.Model, "fire-soft-delete", true)
// get deleted time
t := m.MustGet(softDeleteField).(*time.Time)
// change type if document has been soft deleted
if t != nil && !t.IsZero() {
e = coal.Deleted
}
}
// create event
evt := &Event{
Type: e,
ID: id,
Model: m,
Stream: stream,
}
// broadcast event
w.manager.broadcast(evt)
}, nil, func(err error) bool {
// report error
w.Reporter(err)
return true
})
}
|
go
|
func (w *Watcher) Add(stream *Stream) {
// initialize model
coal.Init(stream.Model)
// check existence
if w.streams[stream.Name()] != nil {
panic(fmt.Sprintf(`spark: stream with name "%s" already exists`, stream.Name()))
}
// save stream
w.streams[stream.Name()] = stream
// open stream
coal.OpenStream(stream.Store, stream.Model, nil, func(e coal.Event, id bson.ObjectId, m coal.Model, token []byte) {
// ignore real deleted events when soft delete has been enabled
if stream.SoftDelete && e == coal.Deleted {
return
}
// handle soft deleted documents
if stream.SoftDelete && e == coal.Updated {
// get soft delete field
softDeleteField := coal.L(stream.Model, "fire-soft-delete", true)
// get deleted time
t := m.MustGet(softDeleteField).(*time.Time)
// change type if document has been soft deleted
if t != nil && !t.IsZero() {
e = coal.Deleted
}
}
// create event
evt := &Event{
Type: e,
ID: id,
Model: m,
Stream: stream,
}
// broadcast event
w.manager.broadcast(evt)
}, nil, func(err error) bool {
// report error
w.Reporter(err)
return true
})
}
|
[
"func",
"(",
"w",
"*",
"Watcher",
")",
"Add",
"(",
"stream",
"*",
"Stream",
")",
"{",
"coal",
".",
"Init",
"(",
"stream",
".",
"Model",
")",
"\n",
"if",
"w",
".",
"streams",
"[",
"stream",
".",
"Name",
"(",
")",
"]",
"!=",
"nil",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"`spark: stream with name \"%s\" already exists`",
",",
"stream",
".",
"Name",
"(",
")",
")",
")",
"\n",
"}",
"\n",
"w",
".",
"streams",
"[",
"stream",
".",
"Name",
"(",
")",
"]",
"=",
"stream",
"\n",
"coal",
".",
"OpenStream",
"(",
"stream",
".",
"Store",
",",
"stream",
".",
"Model",
",",
"nil",
",",
"func",
"(",
"e",
"coal",
".",
"Event",
",",
"id",
"bson",
".",
"ObjectId",
",",
"m",
"coal",
".",
"Model",
",",
"token",
"[",
"]",
"byte",
")",
"{",
"if",
"stream",
".",
"SoftDelete",
"&&",
"e",
"==",
"coal",
".",
"Deleted",
"{",
"return",
"\n",
"}",
"\n",
"if",
"stream",
".",
"SoftDelete",
"&&",
"e",
"==",
"coal",
".",
"Updated",
"{",
"softDeleteField",
":=",
"coal",
".",
"L",
"(",
"stream",
".",
"Model",
",",
"\"fire-soft-delete\"",
",",
"true",
")",
"\n",
"t",
":=",
"m",
".",
"MustGet",
"(",
"softDeleteField",
")",
".",
"(",
"*",
"time",
".",
"Time",
")",
"\n",
"if",
"t",
"!=",
"nil",
"&&",
"!",
"t",
".",
"IsZero",
"(",
")",
"{",
"e",
"=",
"coal",
".",
"Deleted",
"\n",
"}",
"\n",
"}",
"\n",
"evt",
":=",
"&",
"Event",
"{",
"Type",
":",
"e",
",",
"ID",
":",
"id",
",",
"Model",
":",
"m",
",",
"Stream",
":",
"stream",
",",
"}",
"\n",
"w",
".",
"manager",
".",
"broadcast",
"(",
"evt",
")",
"\n",
"}",
",",
"nil",
",",
"func",
"(",
"err",
"error",
")",
"bool",
"{",
"w",
".",
"Reporter",
"(",
"err",
")",
"\n",
"return",
"true",
"\n",
"}",
")",
"\n",
"}"
] |
// Add will add a stream to the watcher.
|
[
"Add",
"will",
"add",
"a",
"stream",
"to",
"the",
"watcher",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/spark/watcher.go#L38-L87
|
test
|
256dpi/fire
|
spark/watcher.go
|
Action
|
func (w *Watcher) Action() *fire.Action {
return &fire.Action{
Methods: []string{"GET"},
Callback: fire.C("spark/Watcher.Action", fire.All(), func(ctx *fire.Context) error {
// handle connection
w.manager.handle(ctx)
return nil
}),
}
}
|
go
|
func (w *Watcher) Action() *fire.Action {
return &fire.Action{
Methods: []string{"GET"},
Callback: fire.C("spark/Watcher.Action", fire.All(), func(ctx *fire.Context) error {
// handle connection
w.manager.handle(ctx)
return nil
}),
}
}
|
[
"func",
"(",
"w",
"*",
"Watcher",
")",
"Action",
"(",
")",
"*",
"fire",
".",
"Action",
"{",
"return",
"&",
"fire",
".",
"Action",
"{",
"Methods",
":",
"[",
"]",
"string",
"{",
"\"GET\"",
"}",
",",
"Callback",
":",
"fire",
".",
"C",
"(",
"\"spark/Watcher.Action\"",
",",
"fire",
".",
"All",
"(",
")",
",",
"func",
"(",
"ctx",
"*",
"fire",
".",
"Context",
")",
"error",
"{",
"w",
".",
"manager",
".",
"handle",
"(",
"ctx",
")",
"\n",
"return",
"nil",
"\n",
"}",
")",
",",
"}",
"\n",
"}"
] |
// Action returns an action that should be registered in the group under
// the "watch" name.
|
[
"Action",
"returns",
"an",
"action",
"that",
"should",
"be",
"registered",
"in",
"the",
"group",
"under",
"the",
"watch",
"name",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/spark/watcher.go#L91-L101
|
test
|
256dpi/fire
|
context.go
|
Write
|
func (o Operation) Write() bool {
return o == Create || o == Update || o == Delete
}
|
go
|
func (o Operation) Write() bool {
return o == Create || o == Update || o == Delete
}
|
[
"func",
"(",
"o",
"Operation",
")",
"Write",
"(",
")",
"bool",
"{",
"return",
"o",
"==",
"Create",
"||",
"o",
"==",
"Update",
"||",
"o",
"==",
"Delete",
"\n",
"}"
] |
// Write will return true when this operation does write data.
|
[
"Write",
"will",
"return",
"true",
"when",
"this",
"operation",
"does",
"write",
"data",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/context.go#L63-L65
|
test
|
256dpi/fire
|
context.go
|
String
|
func (o Operation) String() string {
switch o {
case List:
return "List"
case Find:
return "Find"
case Create:
return "Create"
case Update:
return "Update"
case Delete:
return "Delete"
case CollectionAction:
return "CollectionAction"
case ResourceAction:
return "ResourceAction"
}
return ""
}
|
go
|
func (o Operation) String() string {
switch o {
case List:
return "List"
case Find:
return "Find"
case Create:
return "Create"
case Update:
return "Update"
case Delete:
return "Delete"
case CollectionAction:
return "CollectionAction"
case ResourceAction:
return "ResourceAction"
}
return ""
}
|
[
"func",
"(",
"o",
"Operation",
")",
"String",
"(",
")",
"string",
"{",
"switch",
"o",
"{",
"case",
"List",
":",
"return",
"\"List\"",
"\n",
"case",
"Find",
":",
"return",
"\"Find\"",
"\n",
"case",
"Create",
":",
"return",
"\"Create\"",
"\n",
"case",
"Update",
":",
"return",
"\"Update\"",
"\n",
"case",
"Delete",
":",
"return",
"\"Delete\"",
"\n",
"case",
"CollectionAction",
":",
"return",
"\"CollectionAction\"",
"\n",
"case",
"ResourceAction",
":",
"return",
"\"ResourceAction\"",
"\n",
"}",
"\n",
"return",
"\"\"",
"\n",
"}"
] |
// String returns the name of the operation.
|
[
"String",
"returns",
"the",
"name",
"of",
"the",
"operation",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/context.go#L73-L92
|
test
|
256dpi/fire
|
context.go
|
Query
|
func (c *Context) Query() bson.M {
return bson.M{"$and": append([]bson.M{c.Selector}, c.Filters...)}
}
|
go
|
func (c *Context) Query() bson.M {
return bson.M{"$and": append([]bson.M{c.Selector}, c.Filters...)}
}
|
[
"func",
"(",
"c",
"*",
"Context",
")",
"Query",
"(",
")",
"bson",
".",
"M",
"{",
"return",
"bson",
".",
"M",
"{",
"\"$and\"",
":",
"append",
"(",
"[",
"]",
"bson",
".",
"M",
"{",
"c",
".",
"Selector",
"}",
",",
"c",
".",
"Filters",
"...",
")",
"}",
"\n",
"}"
] |
// Query returns the composite query of Selector and Filter.
|
[
"Query",
"returns",
"the",
"composite",
"query",
"of",
"Selector",
"and",
"Filter",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/context.go#L206-L208
|
test
|
256dpi/fire
|
context.go
|
Respond
|
func (c *Context) Respond(value interface{}) error {
// encode response
bytes, err := json.Marshal(value)
if err != nil {
return err
}
// write token
_, err = c.ResponseWriter.Write(bytes)
if err != nil {
return err
}
return nil
}
|
go
|
func (c *Context) Respond(value interface{}) error {
// encode response
bytes, err := json.Marshal(value)
if err != nil {
return err
}
// write token
_, err = c.ResponseWriter.Write(bytes)
if err != nil {
return err
}
return nil
}
|
[
"func",
"(",
"c",
"*",
"Context",
")",
"Respond",
"(",
"value",
"interface",
"{",
"}",
")",
"error",
"{",
"bytes",
",",
"err",
":=",
"json",
".",
"Marshal",
"(",
"value",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"_",
",",
"err",
"=",
"c",
".",
"ResponseWriter",
".",
"Write",
"(",
"bytes",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Respond will encode the provided value as JSON and write it to the client.
|
[
"Respond",
"will",
"encode",
"the",
"provided",
"value",
"as",
"JSON",
"and",
"write",
"it",
"to",
"the",
"client",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/context.go#L253-L267
|
test
|
256dpi/fire
|
flame/tools.go
|
EnsureApplication
|
func EnsureApplication(store *coal.Store, name, key, secret string) (string, error) {
// copy store
s := store.Copy()
defer s.Close()
// count main applications
var apps []Application
err := s.C(&Application{}).Find(bson.M{
coal.F(&Application{}, "Name"): name,
}).All(&apps)
if err != nil {
return "", err
}
// check existence
if len(apps) > 1 {
return "", errors.New("to many applications with that name")
} else if len(apps) == 1 {
return apps[0].Key, nil
}
// application is missing
// create application
app := coal.Init(&Application{}).(*Application)
app.Key = key
app.Name = name
app.Secret = secret
// validate model
err = app.Validate()
if err != nil {
return "", err
}
// save application
err = s.C(app).Insert(app)
if err != nil {
return "", err
}
return app.Key, nil
}
|
go
|
func EnsureApplication(store *coal.Store, name, key, secret string) (string, error) {
// copy store
s := store.Copy()
defer s.Close()
// count main applications
var apps []Application
err := s.C(&Application{}).Find(bson.M{
coal.F(&Application{}, "Name"): name,
}).All(&apps)
if err != nil {
return "", err
}
// check existence
if len(apps) > 1 {
return "", errors.New("to many applications with that name")
} else if len(apps) == 1 {
return apps[0].Key, nil
}
// application is missing
// create application
app := coal.Init(&Application{}).(*Application)
app.Key = key
app.Name = name
app.Secret = secret
// validate model
err = app.Validate()
if err != nil {
return "", err
}
// save application
err = s.C(app).Insert(app)
if err != nil {
return "", err
}
return app.Key, nil
}
|
[
"func",
"EnsureApplication",
"(",
"store",
"*",
"coal",
".",
"Store",
",",
"name",
",",
"key",
",",
"secret",
"string",
")",
"(",
"string",
",",
"error",
")",
"{",
"s",
":=",
"store",
".",
"Copy",
"(",
")",
"\n",
"defer",
"s",
".",
"Close",
"(",
")",
"\n",
"var",
"apps",
"[",
"]",
"Application",
"\n",
"err",
":=",
"s",
".",
"C",
"(",
"&",
"Application",
"{",
"}",
")",
".",
"Find",
"(",
"bson",
".",
"M",
"{",
"coal",
".",
"F",
"(",
"&",
"Application",
"{",
"}",
",",
"\"Name\"",
")",
":",
"name",
",",
"}",
")",
".",
"All",
"(",
"&",
"apps",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"\"",
",",
"err",
"\n",
"}",
"\n",
"if",
"len",
"(",
"apps",
")",
">",
"1",
"{",
"return",
"\"\"",
",",
"errors",
".",
"New",
"(",
"\"to many applications with that name\"",
")",
"\n",
"}",
"else",
"if",
"len",
"(",
"apps",
")",
"==",
"1",
"{",
"return",
"apps",
"[",
"0",
"]",
".",
"Key",
",",
"nil",
"\n",
"}",
"\n",
"app",
":=",
"coal",
".",
"Init",
"(",
"&",
"Application",
"{",
"}",
")",
".",
"(",
"*",
"Application",
")",
"\n",
"app",
".",
"Key",
"=",
"key",
"\n",
"app",
".",
"Name",
"=",
"name",
"\n",
"app",
".",
"Secret",
"=",
"secret",
"\n",
"err",
"=",
"app",
".",
"Validate",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"\"",
",",
"err",
"\n",
"}",
"\n",
"err",
"=",
"s",
".",
"C",
"(",
"app",
")",
".",
"Insert",
"(",
"app",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"\"",
",",
"err",
"\n",
"}",
"\n",
"return",
"app",
".",
"Key",
",",
"nil",
"\n",
"}"
] |
// EnsureApplication will ensure that an application with the provided name
// exists and returns its key.
|
[
"EnsureApplication",
"will",
"ensure",
"that",
"an",
"application",
"with",
"the",
"provided",
"name",
"exists",
"and",
"returns",
"its",
"key",
"."
] |
fa66e74352b30b9a4c730f7b8dc773302941b0fb
|
https://github.com/256dpi/fire/blob/fa66e74352b30b9a4c730f7b8dc773302941b0fb/flame/tools.go#L47-L89
|
test
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.