id
int32 0
167k
| repo
stringlengths 5
54
| path
stringlengths 4
155
| func_name
stringlengths 1
118
| original_string
stringlengths 52
85.5k
| language
stringclasses 1
value | code
stringlengths 52
85.5k
| code_tokens
listlengths 21
1.41k
| docstring
stringlengths 6
2.61k
| docstring_tokens
listlengths 3
215
| sha
stringlengths 40
40
| url
stringlengths 85
252
|
---|---|---|---|---|---|---|---|---|---|---|---|
14,300 |
go-ozzo/ozzo-routing
|
context.go
|
PostForm
|
func (c *Context) PostForm(key string, defaultValue ...string) string {
r := c.Request
r.ParseMultipartForm(32 << 20)
if vs := r.PostForm[key]; len(vs) > 0 {
return vs[0]
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return ""
}
|
go
|
func (c *Context) PostForm(key string, defaultValue ...string) string {
r := c.Request
r.ParseMultipartForm(32 << 20)
if vs := r.PostForm[key]; len(vs) > 0 {
return vs[0]
}
if len(defaultValue) > 0 {
return defaultValue[0]
}
return ""
}
|
[
"func",
"(",
"c",
"*",
"Context",
")",
"PostForm",
"(",
"key",
"string",
",",
"defaultValue",
"...",
"string",
")",
"string",
"{",
"r",
":=",
"c",
".",
"Request",
"\n",
"r",
".",
"ParseMultipartForm",
"(",
"32",
"<<",
"20",
")",
"\n",
"if",
"vs",
":=",
"r",
".",
"PostForm",
"[",
"key",
"]",
";",
"len",
"(",
"vs",
")",
">",
"0",
"{",
"return",
"vs",
"[",
"0",
"]",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"defaultValue",
")",
">",
"0",
"{",
"return",
"defaultValue",
"[",
"0",
"]",
"\n",
"}",
"\n",
"return",
"\"",
"\"",
"\n",
"}"
] |
// PostForm returns the first value for the named component from POST and PUT body parameters.
// If key is not present, it returns the specified default value or an empty string.
|
[
"PostForm",
"returns",
"the",
"first",
"value",
"for",
"the",
"named",
"component",
"from",
"POST",
"and",
"PUT",
"body",
"parameters",
".",
"If",
"key",
"is",
"not",
"present",
"it",
"returns",
"the",
"specified",
"default",
"value",
"or",
"an",
"empty",
"string",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/context.go#L106-L117
|
14,301 |
go-ozzo/ozzo-routing
|
context.go
|
Next
|
func (c *Context) Next() error {
c.index++
for n := len(c.handlers); c.index < n; c.index++ {
if err := c.handlers[c.index](c); err != nil {
return err
}
}
return nil
}
|
go
|
func (c *Context) Next() error {
c.index++
for n := len(c.handlers); c.index < n; c.index++ {
if err := c.handlers[c.index](c); err != nil {
return err
}
}
return nil
}
|
[
"func",
"(",
"c",
"*",
"Context",
")",
"Next",
"(",
")",
"error",
"{",
"c",
".",
"index",
"++",
"\n",
"for",
"n",
":=",
"len",
"(",
"c",
".",
"handlers",
")",
";",
"c",
".",
"index",
"<",
"n",
";",
"c",
".",
"index",
"++",
"{",
"if",
"err",
":=",
"c",
".",
"handlers",
"[",
"c",
".",
"index",
"]",
"(",
"c",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] |
// Next calls the rest of the handlers associated with the current route.
// If any of these handlers returns an error, Next will return the error and skip the following handlers.
// Next is normally used when a handler needs to do some postprocessing after the rest of the handlers
// are executed.
|
[
"Next",
"calls",
"the",
"rest",
"of",
"the",
"handlers",
"associated",
"with",
"the",
"current",
"route",
".",
"If",
"any",
"of",
"these",
"handlers",
"returns",
"an",
"error",
"Next",
"will",
"return",
"the",
"error",
"and",
"skip",
"the",
"following",
"handlers",
".",
"Next",
"is",
"normally",
"used",
"when",
"a",
"handler",
"needs",
"to",
"do",
"some",
"postprocessing",
"after",
"the",
"rest",
"of",
"the",
"handlers",
"are",
"executed",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/context.go#L123-L131
|
14,302 |
go-ozzo/ozzo-routing
|
context.go
|
Read
|
func (c *Context) Read(data interface{}) error {
if c.Request.Method != "GET" {
t := getContentType(c.Request)
if reader, ok := DataReaders[t]; ok {
return reader.Read(c.Request, data)
}
}
return DefaultFormDataReader.Read(c.Request, data)
}
|
go
|
func (c *Context) Read(data interface{}) error {
if c.Request.Method != "GET" {
t := getContentType(c.Request)
if reader, ok := DataReaders[t]; ok {
return reader.Read(c.Request, data)
}
}
return DefaultFormDataReader.Read(c.Request, data)
}
|
[
"func",
"(",
"c",
"*",
"Context",
")",
"Read",
"(",
"data",
"interface",
"{",
"}",
")",
"error",
"{",
"if",
"c",
".",
"Request",
".",
"Method",
"!=",
"\"",
"\"",
"{",
"t",
":=",
"getContentType",
"(",
"c",
".",
"Request",
")",
"\n",
"if",
"reader",
",",
"ok",
":=",
"DataReaders",
"[",
"t",
"]",
";",
"ok",
"{",
"return",
"reader",
".",
"Read",
"(",
"c",
".",
"Request",
",",
"data",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"DefaultFormDataReader",
".",
"Read",
"(",
"c",
".",
"Request",
",",
"data",
")",
"\n",
"}"
] |
// Read populates the given struct variable with the data from the current request.
// If the request is NOT a GET request, it will check the "Content-Type" header
// and find a matching reader from DataReaders to read the request data.
// If there is no match or if the request is a GET request, it will use DefaultFormDataReader
// to read the request data.
|
[
"Read",
"populates",
"the",
"given",
"struct",
"variable",
"with",
"the",
"data",
"from",
"the",
"current",
"request",
".",
"If",
"the",
"request",
"is",
"NOT",
"a",
"GET",
"request",
"it",
"will",
"check",
"the",
"Content",
"-",
"Type",
"header",
"and",
"find",
"a",
"matching",
"reader",
"from",
"DataReaders",
"to",
"read",
"the",
"request",
"data",
".",
"If",
"there",
"is",
"no",
"match",
"or",
"if",
"the",
"request",
"is",
"a",
"GET",
"request",
"it",
"will",
"use",
"DefaultFormDataReader",
"to",
"read",
"the",
"request",
"data",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/context.go#L157-L166
|
14,303 |
go-ozzo/ozzo-routing
|
content/language.go
|
negotiateLanguage
|
func negotiateLanguage(r *http.Request, offers []string, defaultOffer string) string {
bestOffer := defaultOffer
bestQ := -1.0
specs := header.ParseAccept(r.Header, "Accept-Language")
for _, offer := range offers {
for _, spec := range specs {
if spec.Q > bestQ && (spec.Value == "*" || spec.Value == offer) {
bestQ = spec.Q
bestOffer = offer
}
}
}
if bestQ == 0 {
bestOffer = defaultOffer
}
return bestOffer
}
|
go
|
func negotiateLanguage(r *http.Request, offers []string, defaultOffer string) string {
bestOffer := defaultOffer
bestQ := -1.0
specs := header.ParseAccept(r.Header, "Accept-Language")
for _, offer := range offers {
for _, spec := range specs {
if spec.Q > bestQ && (spec.Value == "*" || spec.Value == offer) {
bestQ = spec.Q
bestOffer = offer
}
}
}
if bestQ == 0 {
bestOffer = defaultOffer
}
return bestOffer
}
|
[
"func",
"negotiateLanguage",
"(",
"r",
"*",
"http",
".",
"Request",
",",
"offers",
"[",
"]",
"string",
",",
"defaultOffer",
"string",
")",
"string",
"{",
"bestOffer",
":=",
"defaultOffer",
"\n",
"bestQ",
":=",
"-",
"1.0",
"\n",
"specs",
":=",
"header",
".",
"ParseAccept",
"(",
"r",
".",
"Header",
",",
"\"",
"\"",
")",
"\n",
"for",
"_",
",",
"offer",
":=",
"range",
"offers",
"{",
"for",
"_",
",",
"spec",
":=",
"range",
"specs",
"{",
"if",
"spec",
".",
"Q",
">",
"bestQ",
"&&",
"(",
"spec",
".",
"Value",
"==",
"\"",
"\"",
"||",
"spec",
".",
"Value",
"==",
"offer",
")",
"{",
"bestQ",
"=",
"spec",
".",
"Q",
"\n",
"bestOffer",
"=",
"offer",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"bestQ",
"==",
"0",
"{",
"bestOffer",
"=",
"defaultOffer",
"\n",
"}",
"\n",
"return",
"bestOffer",
"\n",
"}"
] |
// negotiateLanguage negotiates the acceptable language according to the Accept-Language HTTP header.
|
[
"negotiateLanguage",
"negotiates",
"the",
"acceptable",
"language",
"according",
"to",
"the",
"Accept",
"-",
"Language",
"HTTP",
"header",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/content/language.go#L44-L60
|
14,304 |
go-ozzo/ozzo-routing
|
reader.go
|
ReadFormData
|
func ReadFormData(form map[string][]string, data interface{}) error {
rv := reflect.ValueOf(data)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return errors.New("data must be a pointer")
}
rv = indirect(rv)
if rv.Kind() != reflect.Struct {
return errors.New("data must be a pointer to a struct")
}
return readForm(form, "", rv)
}
|
go
|
func ReadFormData(form map[string][]string, data interface{}) error {
rv := reflect.ValueOf(data)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return errors.New("data must be a pointer")
}
rv = indirect(rv)
if rv.Kind() != reflect.Struct {
return errors.New("data must be a pointer to a struct")
}
return readForm(form, "", rv)
}
|
[
"func",
"ReadFormData",
"(",
"form",
"map",
"[",
"string",
"]",
"[",
"]",
"string",
",",
"data",
"interface",
"{",
"}",
")",
"error",
"{",
"rv",
":=",
"reflect",
".",
"ValueOf",
"(",
"data",
")",
"\n",
"if",
"rv",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Ptr",
"||",
"rv",
".",
"IsNil",
"(",
")",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"rv",
"=",
"indirect",
"(",
"rv",
")",
"\n",
"if",
"rv",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Struct",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"return",
"readForm",
"(",
"form",
",",
"\"",
"\"",
",",
"rv",
")",
"\n",
"}"
] |
// ReadFormData populates the data variable with the data from the given form values.
|
[
"ReadFormData",
"populates",
"the",
"data",
"variable",
"with",
"the",
"data",
"from",
"the",
"given",
"form",
"values",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/reader.go#L76-L87
|
14,305 |
go-ozzo/ozzo-routing
|
auth/handlers.go
|
DefaultJWTTokenHandler
|
func DefaultJWTTokenHandler(c *routing.Context, token *jwt.Token) error {
c.Set("JWT", token)
return nil
}
|
go
|
func DefaultJWTTokenHandler(c *routing.Context, token *jwt.Token) error {
c.Set("JWT", token)
return nil
}
|
[
"func",
"DefaultJWTTokenHandler",
"(",
"c",
"*",
"routing",
".",
"Context",
",",
"token",
"*",
"jwt",
".",
"Token",
")",
"error",
"{",
"c",
".",
"Set",
"(",
"\"",
"\"",
",",
"token",
")",
"\n",
"return",
"nil",
"\n",
"}"
] |
// DefaultJWTTokenHandler stores the parsed JWT token in the routing context with the key named "JWT".
|
[
"DefaultJWTTokenHandler",
"stores",
"the",
"parsed",
"JWT",
"token",
"in",
"the",
"routing",
"context",
"with",
"the",
"key",
"named",
"JWT",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/auth/handlers.go#L208-L211
|
14,306 |
go-ozzo/ozzo-routing
|
auth/handlers.go
|
NewJWT
|
func NewJWT(claims jwt.MapClaims, signingKey string, signingMethod ...jwt.SigningMethod) (string, error) {
var sm jwt.SigningMethod = jwt.SigningMethodHS256
if len(signingMethod) > 0 {
sm = signingMethod[0]
}
return jwt.NewWithClaims(sm, claims).SignedString([]byte(signingKey))
}
|
go
|
func NewJWT(claims jwt.MapClaims, signingKey string, signingMethod ...jwt.SigningMethod) (string, error) {
var sm jwt.SigningMethod = jwt.SigningMethodHS256
if len(signingMethod) > 0 {
sm = signingMethod[0]
}
return jwt.NewWithClaims(sm, claims).SignedString([]byte(signingKey))
}
|
[
"func",
"NewJWT",
"(",
"claims",
"jwt",
".",
"MapClaims",
",",
"signingKey",
"string",
",",
"signingMethod",
"...",
"jwt",
".",
"SigningMethod",
")",
"(",
"string",
",",
"error",
")",
"{",
"var",
"sm",
"jwt",
".",
"SigningMethod",
"=",
"jwt",
".",
"SigningMethodHS256",
"\n",
"if",
"len",
"(",
"signingMethod",
")",
">",
"0",
"{",
"sm",
"=",
"signingMethod",
"[",
"0",
"]",
"\n",
"}",
"\n",
"return",
"jwt",
".",
"NewWithClaims",
"(",
"sm",
",",
"claims",
")",
".",
"SignedString",
"(",
"[",
"]",
"byte",
"(",
"signingKey",
")",
")",
"\n",
"}"
] |
// NewJWT creates a new JWT token and returns it as a signed string that may be sent to the client side.
// The signingMethod parameter is optional. It defaults to the HS256 algorithm.
|
[
"NewJWT",
"creates",
"a",
"new",
"JWT",
"token",
"and",
"returns",
"it",
"as",
"a",
"signed",
"string",
"that",
"may",
"be",
"sent",
"to",
"the",
"client",
"side",
".",
"The",
"signingMethod",
"parameter",
"is",
"optional",
".",
"It",
"defaults",
"to",
"the",
"HS256",
"algorithm",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/auth/handlers.go#L298-L304
|
14,307 |
go-ozzo/ozzo-routing
|
router.go
|
New
|
func New() *Router {
r := &Router{
namedRoutes: make(map[string]*Route),
stores: make(map[string]routeStore),
}
r.RouteGroup = *newRouteGroup("", r, make([]Handler, 0))
r.NotFound(MethodNotAllowedHandler, NotFoundHandler)
r.pool.New = func() interface{} {
return &Context{
pvalues: make([]string, r.maxParams),
router: r,
}
}
return r
}
|
go
|
func New() *Router {
r := &Router{
namedRoutes: make(map[string]*Route),
stores: make(map[string]routeStore),
}
r.RouteGroup = *newRouteGroup("", r, make([]Handler, 0))
r.NotFound(MethodNotAllowedHandler, NotFoundHandler)
r.pool.New = func() interface{} {
return &Context{
pvalues: make([]string, r.maxParams),
router: r,
}
}
return r
}
|
[
"func",
"New",
"(",
")",
"*",
"Router",
"{",
"r",
":=",
"&",
"Router",
"{",
"namedRoutes",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"Route",
")",
",",
"stores",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"routeStore",
")",
",",
"}",
"\n",
"r",
".",
"RouteGroup",
"=",
"*",
"newRouteGroup",
"(",
"\"",
"\"",
",",
"r",
",",
"make",
"(",
"[",
"]",
"Handler",
",",
"0",
")",
")",
"\n",
"r",
".",
"NotFound",
"(",
"MethodNotAllowedHandler",
",",
"NotFoundHandler",
")",
"\n",
"r",
".",
"pool",
".",
"New",
"=",
"func",
"(",
")",
"interface",
"{",
"}",
"{",
"return",
"&",
"Context",
"{",
"pvalues",
":",
"make",
"(",
"[",
"]",
"string",
",",
"r",
".",
"maxParams",
")",
",",
"router",
":",
"r",
",",
"}",
"\n",
"}",
"\n",
"return",
"r",
"\n",
"}"
] |
// New creates a new Router object.
|
[
"New",
"creates",
"a",
"new",
"Router",
"object",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/router.go#L56-L70
|
14,308 |
go-ozzo/ozzo-routing
|
router.go
|
ServeHTTP
|
func (r *Router) ServeHTTP(res http.ResponseWriter, req *http.Request) {
c := r.pool.Get().(*Context)
c.init(res, req)
if r.UseEscapedPath {
c.handlers, c.pnames = r.find(req.Method, r.normalizeRequestPath(req.URL.EscapedPath()), c.pvalues)
for i, v := range c.pvalues {
c.pvalues[i], _ = url.QueryUnescape(v)
}
} else {
c.handlers, c.pnames = r.find(req.Method, r.normalizeRequestPath(req.URL.Path), c.pvalues)
}
if err := c.Next(); err != nil {
r.handleError(c, err)
}
r.pool.Put(c)
}
|
go
|
func (r *Router) ServeHTTP(res http.ResponseWriter, req *http.Request) {
c := r.pool.Get().(*Context)
c.init(res, req)
if r.UseEscapedPath {
c.handlers, c.pnames = r.find(req.Method, r.normalizeRequestPath(req.URL.EscapedPath()), c.pvalues)
for i, v := range c.pvalues {
c.pvalues[i], _ = url.QueryUnescape(v)
}
} else {
c.handlers, c.pnames = r.find(req.Method, r.normalizeRequestPath(req.URL.Path), c.pvalues)
}
if err := c.Next(); err != nil {
r.handleError(c, err)
}
r.pool.Put(c)
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"ServeHTTP",
"(",
"res",
"http",
".",
"ResponseWriter",
",",
"req",
"*",
"http",
".",
"Request",
")",
"{",
"c",
":=",
"r",
".",
"pool",
".",
"Get",
"(",
")",
".",
"(",
"*",
"Context",
")",
"\n",
"c",
".",
"init",
"(",
"res",
",",
"req",
")",
"\n",
"if",
"r",
".",
"UseEscapedPath",
"{",
"c",
".",
"handlers",
",",
"c",
".",
"pnames",
"=",
"r",
".",
"find",
"(",
"req",
".",
"Method",
",",
"r",
".",
"normalizeRequestPath",
"(",
"req",
".",
"URL",
".",
"EscapedPath",
"(",
")",
")",
",",
"c",
".",
"pvalues",
")",
"\n",
"for",
"i",
",",
"v",
":=",
"range",
"c",
".",
"pvalues",
"{",
"c",
".",
"pvalues",
"[",
"i",
"]",
",",
"_",
"=",
"url",
".",
"QueryUnescape",
"(",
"v",
")",
"\n",
"}",
"\n",
"}",
"else",
"{",
"c",
".",
"handlers",
",",
"c",
".",
"pnames",
"=",
"r",
".",
"find",
"(",
"req",
".",
"Method",
",",
"r",
".",
"normalizeRequestPath",
"(",
"req",
".",
"URL",
".",
"Path",
")",
",",
"c",
".",
"pvalues",
")",
"\n",
"}",
"\n",
"if",
"err",
":=",
"c",
".",
"Next",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"r",
".",
"handleError",
"(",
"c",
",",
"err",
")",
"\n",
"}",
"\n",
"r",
".",
"pool",
".",
"Put",
"(",
"c",
")",
"\n",
"}"
] |
// ServeHTTP handles the HTTP request.
// It is required by http.Handler
|
[
"ServeHTTP",
"handles",
"the",
"HTTP",
"request",
".",
"It",
"is",
"required",
"by",
"http",
".",
"Handler"
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/router.go#L74-L89
|
14,309 |
go-ozzo/ozzo-routing
|
router.go
|
Use
|
func (r *Router) Use(handlers ...Handler) {
r.RouteGroup.Use(handlers...)
r.notFoundHandlers = combineHandlers(r.handlers, r.notFound)
}
|
go
|
func (r *Router) Use(handlers ...Handler) {
r.RouteGroup.Use(handlers...)
r.notFoundHandlers = combineHandlers(r.handlers, r.notFound)
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"Use",
"(",
"handlers",
"...",
"Handler",
")",
"{",
"r",
".",
"RouteGroup",
".",
"Use",
"(",
"handlers",
"...",
")",
"\n",
"r",
".",
"notFoundHandlers",
"=",
"combineHandlers",
"(",
"r",
".",
"handlers",
",",
"r",
".",
"notFound",
")",
"\n",
"}"
] |
// Use appends the specified handlers to the router and shares them with all routes.
|
[
"Use",
"appends",
"the",
"specified",
"handlers",
"to",
"the",
"router",
"and",
"shares",
"them",
"with",
"all",
"routes",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/router.go#L103-L106
|
14,310 |
go-ozzo/ozzo-routing
|
router.go
|
NotFound
|
func (r *Router) NotFound(handlers ...Handler) {
r.notFound = handlers
r.notFoundHandlers = combineHandlers(r.handlers, r.notFound)
}
|
go
|
func (r *Router) NotFound(handlers ...Handler) {
r.notFound = handlers
r.notFoundHandlers = combineHandlers(r.handlers, r.notFound)
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"NotFound",
"(",
"handlers",
"...",
"Handler",
")",
"{",
"r",
".",
"notFound",
"=",
"handlers",
"\n",
"r",
".",
"notFoundHandlers",
"=",
"combineHandlers",
"(",
"r",
".",
"handlers",
",",
"r",
".",
"notFound",
")",
"\n",
"}"
] |
// NotFound specifies the handlers that should be invoked when the router cannot find any route matching a request.
// Note that the handlers registered via Use will be invoked first in this case.
|
[
"NotFound",
"specifies",
"the",
"handlers",
"that",
"should",
"be",
"invoked",
"when",
"the",
"router",
"cannot",
"find",
"any",
"route",
"matching",
"a",
"request",
".",
"Note",
"that",
"the",
"handlers",
"registered",
"via",
"Use",
"will",
"be",
"invoked",
"first",
"in",
"this",
"case",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/router.go#L110-L113
|
14,311 |
go-ozzo/ozzo-routing
|
router.go
|
Find
|
func (r *Router) Find(method, path string) (handlers []Handler, params map[string]string) {
pvalues := make([]string, r.maxParams)
handlers, pnames := r.find(method, path, pvalues)
params = make(map[string]string, len(pnames))
for i, n := range pnames {
params[n] = pvalues[i]
}
return handlers, params
}
|
go
|
func (r *Router) Find(method, path string) (handlers []Handler, params map[string]string) {
pvalues := make([]string, r.maxParams)
handlers, pnames := r.find(method, path, pvalues)
params = make(map[string]string, len(pnames))
for i, n := range pnames {
params[n] = pvalues[i]
}
return handlers, params
}
|
[
"func",
"(",
"r",
"*",
"Router",
")",
"Find",
"(",
"method",
",",
"path",
"string",
")",
"(",
"handlers",
"[",
"]",
"Handler",
",",
"params",
"map",
"[",
"string",
"]",
"string",
")",
"{",
"pvalues",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"r",
".",
"maxParams",
")",
"\n",
"handlers",
",",
"pnames",
":=",
"r",
".",
"find",
"(",
"method",
",",
"path",
",",
"pvalues",
")",
"\n",
"params",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"string",
",",
"len",
"(",
"pnames",
")",
")",
"\n",
"for",
"i",
",",
"n",
":=",
"range",
"pnames",
"{",
"params",
"[",
"n",
"]",
"=",
"pvalues",
"[",
"i",
"]",
"\n",
"}",
"\n",
"return",
"handlers",
",",
"params",
"\n",
"}"
] |
// Find determines the handlers and parameters to use for a specified method and path.
|
[
"Find",
"determines",
"the",
"handlers",
"and",
"parameters",
"to",
"use",
"for",
"a",
"specified",
"method",
"and",
"path",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/router.go#L116-L124
|
14,312 |
go-ozzo/ozzo-routing
|
router.go
|
HTTPHandlerFunc
|
func HTTPHandlerFunc(h http.HandlerFunc) Handler {
return func(c *Context) error {
h(c.Response, c.Request)
return nil
}
}
|
go
|
func HTTPHandlerFunc(h http.HandlerFunc) Handler {
return func(c *Context) error {
h(c.Response, c.Request)
return nil
}
}
|
[
"func",
"HTTPHandlerFunc",
"(",
"h",
"http",
".",
"HandlerFunc",
")",
"Handler",
"{",
"return",
"func",
"(",
"c",
"*",
"Context",
")",
"error",
"{",
"h",
"(",
"c",
".",
"Response",
",",
"c",
".",
"Request",
")",
"\n",
"return",
"nil",
"\n",
"}",
"\n",
"}"
] |
// HTTPHandlerFunc adapts a http.HandlerFunc into a routing.Handler.
|
[
"HTTPHandlerFunc",
"adapts",
"a",
"http",
".",
"HandlerFunc",
"into",
"a",
"routing",
".",
"Handler",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/router.go#L220-L225
|
14,313 |
go-ozzo/ozzo-routing
|
store.go
|
newStore
|
func newStore() *store {
return &store{
root: &node{
static: true,
children: make([]*node, 256),
pchildren: make([]*node, 0),
pindex: -1,
pnames: []string{},
},
}
}
|
go
|
func newStore() *store {
return &store{
root: &node{
static: true,
children: make([]*node, 256),
pchildren: make([]*node, 0),
pindex: -1,
pnames: []string{},
},
}
}
|
[
"func",
"newStore",
"(",
")",
"*",
"store",
"{",
"return",
"&",
"store",
"{",
"root",
":",
"&",
"node",
"{",
"static",
":",
"true",
",",
"children",
":",
"make",
"(",
"[",
"]",
"*",
"node",
",",
"256",
")",
",",
"pchildren",
":",
"make",
"(",
"[",
"]",
"*",
"node",
",",
"0",
")",
",",
"pindex",
":",
"-",
"1",
",",
"pnames",
":",
"[",
"]",
"string",
"{",
"}",
",",
"}",
",",
"}",
"\n",
"}"
] |
// newStore creates a new store.
|
[
"newStore",
"creates",
"a",
"new",
"store",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/store.go#L24-L34
|
14,314 |
go-ozzo/ozzo-routing
|
store.go
|
Add
|
func (s *store) Add(key string, data interface{}) int {
s.count++
return s.root.add(key, data, s.count)
}
|
go
|
func (s *store) Add(key string, data interface{}) int {
s.count++
return s.root.add(key, data, s.count)
}
|
[
"func",
"(",
"s",
"*",
"store",
")",
"Add",
"(",
"key",
"string",
",",
"data",
"interface",
"{",
"}",
")",
"int",
"{",
"s",
".",
"count",
"++",
"\n",
"return",
"s",
".",
"root",
".",
"add",
"(",
"key",
",",
"data",
",",
"s",
".",
"count",
")",
"\n",
"}"
] |
// Add adds a new data item with the given parametric key.
// The number of parameters in the key is returned.
|
[
"Add",
"adds",
"a",
"new",
"data",
"item",
"with",
"the",
"given",
"parametric",
"key",
".",
"The",
"number",
"of",
"parameters",
"in",
"the",
"key",
"is",
"returned",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/store.go#L38-L41
|
14,315 |
go-ozzo/ozzo-routing
|
store.go
|
Get
|
func (s *store) Get(path string, pvalues []string) (data interface{}, pnames []string) {
data, pnames, _ = s.root.get(path, pvalues)
return
}
|
go
|
func (s *store) Get(path string, pvalues []string) (data interface{}, pnames []string) {
data, pnames, _ = s.root.get(path, pvalues)
return
}
|
[
"func",
"(",
"s",
"*",
"store",
")",
"Get",
"(",
"path",
"string",
",",
"pvalues",
"[",
"]",
"string",
")",
"(",
"data",
"interface",
"{",
"}",
",",
"pnames",
"[",
"]",
"string",
")",
"{",
"data",
",",
"pnames",
",",
"_",
"=",
"s",
".",
"root",
".",
"get",
"(",
"path",
",",
"pvalues",
")",
"\n",
"return",
"\n",
"}"
] |
// Get returns the data item matching the given concrete key.
// If the data item was added to the store with a parametric key before, the matching
// parameter names and values will be returned as well.
|
[
"Get",
"returns",
"the",
"data",
"item",
"matching",
"the",
"given",
"concrete",
"key",
".",
"If",
"the",
"data",
"item",
"was",
"added",
"to",
"the",
"store",
"with",
"a",
"parametric",
"key",
"before",
"the",
"matching",
"parameter",
"names",
"and",
"values",
"will",
"be",
"returned",
"as",
"well",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/store.go#L46-L49
|
14,316 |
go-ozzo/ozzo-routing
|
store.go
|
add
|
func (n *node) add(key string, data interface{}, order int) int {
matched := 0
// find the common prefix
for ; matched < len(key) && matched < len(n.key); matched++ {
if key[matched] != n.key[matched] {
break
}
}
if matched == len(n.key) {
if matched == len(key) {
// the node key is the same as the key: make the current node as data node
// if the node is already a data node, ignore the new data since we only care the first matched node
if n.data == nil {
n.data = data
n.order = order
}
return n.pindex + 1
}
// the node key is a prefix of the key: create a child node
newKey := key[matched:]
// try adding to a static child
if child := n.children[newKey[0]]; child != nil {
if pn := child.add(newKey, data, order); pn >= 0 {
return pn
}
}
// try adding to a param child
for _, child := range n.pchildren {
if pn := child.add(newKey, data, order); pn >= 0 {
return pn
}
}
return n.addChild(newKey, data, order)
}
if matched == 0 || !n.static {
// no common prefix, or partial common prefix with a non-static node: should skip this node
return -1
}
// the node key shares a partial prefix with the key: split the node key
n1 := &node{
static: true,
key: n.key[matched:],
data: n.data,
order: n.order,
minOrder: n.minOrder,
pchildren: n.pchildren,
children: n.children,
pindex: n.pindex,
pnames: n.pnames,
}
n.key = key[0:matched]
n.data = nil
n.pchildren = make([]*node, 0)
n.children = make([]*node, 256)
n.children[n1.key[0]] = n1
return n.add(key, data, order)
}
|
go
|
func (n *node) add(key string, data interface{}, order int) int {
matched := 0
// find the common prefix
for ; matched < len(key) && matched < len(n.key); matched++ {
if key[matched] != n.key[matched] {
break
}
}
if matched == len(n.key) {
if matched == len(key) {
// the node key is the same as the key: make the current node as data node
// if the node is already a data node, ignore the new data since we only care the first matched node
if n.data == nil {
n.data = data
n.order = order
}
return n.pindex + 1
}
// the node key is a prefix of the key: create a child node
newKey := key[matched:]
// try adding to a static child
if child := n.children[newKey[0]]; child != nil {
if pn := child.add(newKey, data, order); pn >= 0 {
return pn
}
}
// try adding to a param child
for _, child := range n.pchildren {
if pn := child.add(newKey, data, order); pn >= 0 {
return pn
}
}
return n.addChild(newKey, data, order)
}
if matched == 0 || !n.static {
// no common prefix, or partial common prefix with a non-static node: should skip this node
return -1
}
// the node key shares a partial prefix with the key: split the node key
n1 := &node{
static: true,
key: n.key[matched:],
data: n.data,
order: n.order,
minOrder: n.minOrder,
pchildren: n.pchildren,
children: n.children,
pindex: n.pindex,
pnames: n.pnames,
}
n.key = key[0:matched]
n.data = nil
n.pchildren = make([]*node, 0)
n.children = make([]*node, 256)
n.children[n1.key[0]] = n1
return n.add(key, data, order)
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"add",
"(",
"key",
"string",
",",
"data",
"interface",
"{",
"}",
",",
"order",
"int",
")",
"int",
"{",
"matched",
":=",
"0",
"\n\n",
"// find the common prefix",
"for",
";",
"matched",
"<",
"len",
"(",
"key",
")",
"&&",
"matched",
"<",
"len",
"(",
"n",
".",
"key",
")",
";",
"matched",
"++",
"{",
"if",
"key",
"[",
"matched",
"]",
"!=",
"n",
".",
"key",
"[",
"matched",
"]",
"{",
"break",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"matched",
"==",
"len",
"(",
"n",
".",
"key",
")",
"{",
"if",
"matched",
"==",
"len",
"(",
"key",
")",
"{",
"// the node key is the same as the key: make the current node as data node",
"// if the node is already a data node, ignore the new data since we only care the first matched node",
"if",
"n",
".",
"data",
"==",
"nil",
"{",
"n",
".",
"data",
"=",
"data",
"\n",
"n",
".",
"order",
"=",
"order",
"\n",
"}",
"\n",
"return",
"n",
".",
"pindex",
"+",
"1",
"\n",
"}",
"\n\n",
"// the node key is a prefix of the key: create a child node",
"newKey",
":=",
"key",
"[",
"matched",
":",
"]",
"\n\n",
"// try adding to a static child",
"if",
"child",
":=",
"n",
".",
"children",
"[",
"newKey",
"[",
"0",
"]",
"]",
";",
"child",
"!=",
"nil",
"{",
"if",
"pn",
":=",
"child",
".",
"add",
"(",
"newKey",
",",
"data",
",",
"order",
")",
";",
"pn",
">=",
"0",
"{",
"return",
"pn",
"\n",
"}",
"\n",
"}",
"\n",
"// try adding to a param child",
"for",
"_",
",",
"child",
":=",
"range",
"n",
".",
"pchildren",
"{",
"if",
"pn",
":=",
"child",
".",
"add",
"(",
"newKey",
",",
"data",
",",
"order",
")",
";",
"pn",
">=",
"0",
"{",
"return",
"pn",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"n",
".",
"addChild",
"(",
"newKey",
",",
"data",
",",
"order",
")",
"\n",
"}",
"\n\n",
"if",
"matched",
"==",
"0",
"||",
"!",
"n",
".",
"static",
"{",
"// no common prefix, or partial common prefix with a non-static node: should skip this node",
"return",
"-",
"1",
"\n",
"}",
"\n\n",
"// the node key shares a partial prefix with the key: split the node key",
"n1",
":=",
"&",
"node",
"{",
"static",
":",
"true",
",",
"key",
":",
"n",
".",
"key",
"[",
"matched",
":",
"]",
",",
"data",
":",
"n",
".",
"data",
",",
"order",
":",
"n",
".",
"order",
",",
"minOrder",
":",
"n",
".",
"minOrder",
",",
"pchildren",
":",
"n",
".",
"pchildren",
",",
"children",
":",
"n",
".",
"children",
",",
"pindex",
":",
"n",
".",
"pindex",
",",
"pnames",
":",
"n",
".",
"pnames",
",",
"}",
"\n\n",
"n",
".",
"key",
"=",
"key",
"[",
"0",
":",
"matched",
"]",
"\n",
"n",
".",
"data",
"=",
"nil",
"\n",
"n",
".",
"pchildren",
"=",
"make",
"(",
"[",
"]",
"*",
"node",
",",
"0",
")",
"\n",
"n",
".",
"children",
"=",
"make",
"(",
"[",
"]",
"*",
"node",
",",
"256",
")",
"\n",
"n",
".",
"children",
"[",
"n1",
".",
"key",
"[",
"0",
"]",
"]",
"=",
"n1",
"\n\n",
"return",
"n",
".",
"add",
"(",
"key",
",",
"data",
",",
"order",
")",
"\n",
"}"
] |
// add adds a new data item to the tree rooted at the current node.
// The number of parameters in the key is returned.
|
[
"add",
"adds",
"a",
"new",
"data",
"item",
"to",
"the",
"tree",
"rooted",
"at",
"the",
"current",
"node",
".",
"The",
"number",
"of",
"parameters",
"in",
"the",
"key",
"is",
"returned",
"."
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/store.go#L76-L141
|
14,317 |
go-ozzo/ozzo-routing
|
store.go
|
addChild
|
func (n *node) addChild(key string, data interface{}, order int) int {
// find the first occurrence of a param token
p0, p1 := -1, -1
for i := 0; i < len(key); i++ {
if p0 < 0 && key[i] == '<' {
p0 = i
}
if p0 >= 0 && key[i] == '>' {
p1 = i
break
}
}
if p0 > 0 && p1 > 0 || p1 < 0 {
// param token occurs after a static string, or no param token: create a static node
child := &node{
static: true,
key: key,
minOrder: order,
children: make([]*node, 256),
pchildren: make([]*node, 0),
pindex: n.pindex,
pnames: n.pnames,
}
n.children[key[0]] = child
if p1 > 0 {
// param token occurs after a static string
child.key = key[:p0]
n = child
} else {
// no param token: done adding the child
child.data = data
child.order = order
return child.pindex + 1
}
}
// add param node
child := &node{
static: false,
key: key[p0 : p1+1],
minOrder: order,
children: make([]*node, 256),
pchildren: make([]*node, 0),
pindex: n.pindex,
pnames: n.pnames,
}
pattern := ""
pname := key[p0+1 : p1]
for i := p0 + 1; i < p1; i++ {
if key[i] == ':' {
pname = key[p0+1 : i]
pattern = key[i+1 : p1]
break
}
}
if pattern != "" {
// the param token contains a regular expression
child.regex = regexp.MustCompile("^" + pattern)
}
pnames := make([]string, len(n.pnames)+1)
copy(pnames, n.pnames)
pnames[len(n.pnames)] = pname
child.pnames = pnames
child.pindex = len(pnames) - 1
n.pchildren = append(n.pchildren, child)
if p1 == len(key)-1 {
// the param token is at the end of the key
child.data = data
child.order = order
return child.pindex + 1
}
// process the rest of the key
return child.addChild(key[p1+1:], data, order)
}
|
go
|
func (n *node) addChild(key string, data interface{}, order int) int {
// find the first occurrence of a param token
p0, p1 := -1, -1
for i := 0; i < len(key); i++ {
if p0 < 0 && key[i] == '<' {
p0 = i
}
if p0 >= 0 && key[i] == '>' {
p1 = i
break
}
}
if p0 > 0 && p1 > 0 || p1 < 0 {
// param token occurs after a static string, or no param token: create a static node
child := &node{
static: true,
key: key,
minOrder: order,
children: make([]*node, 256),
pchildren: make([]*node, 0),
pindex: n.pindex,
pnames: n.pnames,
}
n.children[key[0]] = child
if p1 > 0 {
// param token occurs after a static string
child.key = key[:p0]
n = child
} else {
// no param token: done adding the child
child.data = data
child.order = order
return child.pindex + 1
}
}
// add param node
child := &node{
static: false,
key: key[p0 : p1+1],
minOrder: order,
children: make([]*node, 256),
pchildren: make([]*node, 0),
pindex: n.pindex,
pnames: n.pnames,
}
pattern := ""
pname := key[p0+1 : p1]
for i := p0 + 1; i < p1; i++ {
if key[i] == ':' {
pname = key[p0+1 : i]
pattern = key[i+1 : p1]
break
}
}
if pattern != "" {
// the param token contains a regular expression
child.regex = regexp.MustCompile("^" + pattern)
}
pnames := make([]string, len(n.pnames)+1)
copy(pnames, n.pnames)
pnames[len(n.pnames)] = pname
child.pnames = pnames
child.pindex = len(pnames) - 1
n.pchildren = append(n.pchildren, child)
if p1 == len(key)-1 {
// the param token is at the end of the key
child.data = data
child.order = order
return child.pindex + 1
}
// process the rest of the key
return child.addChild(key[p1+1:], data, order)
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"addChild",
"(",
"key",
"string",
",",
"data",
"interface",
"{",
"}",
",",
"order",
"int",
")",
"int",
"{",
"// find the first occurrence of a param token",
"p0",
",",
"p1",
":=",
"-",
"1",
",",
"-",
"1",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"len",
"(",
"key",
")",
";",
"i",
"++",
"{",
"if",
"p0",
"<",
"0",
"&&",
"key",
"[",
"i",
"]",
"==",
"'<'",
"{",
"p0",
"=",
"i",
"\n",
"}",
"\n",
"if",
"p0",
">=",
"0",
"&&",
"key",
"[",
"i",
"]",
"==",
"'>'",
"{",
"p1",
"=",
"i",
"\n",
"break",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"p0",
">",
"0",
"&&",
"p1",
">",
"0",
"||",
"p1",
"<",
"0",
"{",
"// param token occurs after a static string, or no param token: create a static node",
"child",
":=",
"&",
"node",
"{",
"static",
":",
"true",
",",
"key",
":",
"key",
",",
"minOrder",
":",
"order",
",",
"children",
":",
"make",
"(",
"[",
"]",
"*",
"node",
",",
"256",
")",
",",
"pchildren",
":",
"make",
"(",
"[",
"]",
"*",
"node",
",",
"0",
")",
",",
"pindex",
":",
"n",
".",
"pindex",
",",
"pnames",
":",
"n",
".",
"pnames",
",",
"}",
"\n",
"n",
".",
"children",
"[",
"key",
"[",
"0",
"]",
"]",
"=",
"child",
"\n",
"if",
"p1",
">",
"0",
"{",
"// param token occurs after a static string",
"child",
".",
"key",
"=",
"key",
"[",
":",
"p0",
"]",
"\n",
"n",
"=",
"child",
"\n",
"}",
"else",
"{",
"// no param token: done adding the child",
"child",
".",
"data",
"=",
"data",
"\n",
"child",
".",
"order",
"=",
"order",
"\n",
"return",
"child",
".",
"pindex",
"+",
"1",
"\n",
"}",
"\n",
"}",
"\n\n",
"// add param node",
"child",
":=",
"&",
"node",
"{",
"static",
":",
"false",
",",
"key",
":",
"key",
"[",
"p0",
":",
"p1",
"+",
"1",
"]",
",",
"minOrder",
":",
"order",
",",
"children",
":",
"make",
"(",
"[",
"]",
"*",
"node",
",",
"256",
")",
",",
"pchildren",
":",
"make",
"(",
"[",
"]",
"*",
"node",
",",
"0",
")",
",",
"pindex",
":",
"n",
".",
"pindex",
",",
"pnames",
":",
"n",
".",
"pnames",
",",
"}",
"\n",
"pattern",
":=",
"\"",
"\"",
"\n",
"pname",
":=",
"key",
"[",
"p0",
"+",
"1",
":",
"p1",
"]",
"\n",
"for",
"i",
":=",
"p0",
"+",
"1",
";",
"i",
"<",
"p1",
";",
"i",
"++",
"{",
"if",
"key",
"[",
"i",
"]",
"==",
"':'",
"{",
"pname",
"=",
"key",
"[",
"p0",
"+",
"1",
":",
"i",
"]",
"\n",
"pattern",
"=",
"key",
"[",
"i",
"+",
"1",
":",
"p1",
"]",
"\n",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"pattern",
"!=",
"\"",
"\"",
"{",
"// the param token contains a regular expression",
"child",
".",
"regex",
"=",
"regexp",
".",
"MustCompile",
"(",
"\"",
"\"",
"+",
"pattern",
")",
"\n",
"}",
"\n",
"pnames",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"n",
".",
"pnames",
")",
"+",
"1",
")",
"\n",
"copy",
"(",
"pnames",
",",
"n",
".",
"pnames",
")",
"\n",
"pnames",
"[",
"len",
"(",
"n",
".",
"pnames",
")",
"]",
"=",
"pname",
"\n",
"child",
".",
"pnames",
"=",
"pnames",
"\n",
"child",
".",
"pindex",
"=",
"len",
"(",
"pnames",
")",
"-",
"1",
"\n",
"n",
".",
"pchildren",
"=",
"append",
"(",
"n",
".",
"pchildren",
",",
"child",
")",
"\n\n",
"if",
"p1",
"==",
"len",
"(",
"key",
")",
"-",
"1",
"{",
"// the param token is at the end of the key",
"child",
".",
"data",
"=",
"data",
"\n",
"child",
".",
"order",
"=",
"order",
"\n",
"return",
"child",
".",
"pindex",
"+",
"1",
"\n",
"}",
"\n\n",
"// process the rest of the key",
"return",
"child",
".",
"addChild",
"(",
"key",
"[",
"p1",
"+",
"1",
":",
"]",
",",
"data",
",",
"order",
")",
"\n",
"}"
] |
// addChild creates static and param nodes to store the given data
|
[
"addChild",
"creates",
"static",
"and",
"param",
"nodes",
"to",
"store",
"the",
"given",
"data"
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/store.go#L144-L220
|
14,318 |
go-ozzo/ozzo-routing
|
store.go
|
get
|
func (n *node) get(key string, pvalues []string) (data interface{}, pnames []string, order int) {
order = math.MaxInt32
repeat:
if n.static {
// check if the node key is a prefix of the given key
// a slightly optimized version of strings.HasPrefix
nkl := len(n.key)
if nkl > len(key) {
return
}
for i := nkl - 1; i >= 0; i-- {
if n.key[i] != key[i] {
return
}
}
key = key[nkl:]
} else if n.regex != nil {
// param node with regular expression
if n.regex.String() == "^.*" {
pvalues[n.pindex] = key
key = ""
} else if match := n.regex.FindStringIndex(key); match != nil {
pvalues[n.pindex] = key[0:match[1]]
key = key[match[1]:]
} else {
return
}
} else {
// param node matching non-"/" characters
i, kl := 0, len(key)
for ; i < kl; i++ {
if key[i] == '/' {
pvalues[n.pindex] = key[0:i]
key = key[i:]
break
}
}
if i == kl {
pvalues[n.pindex] = key
key = ""
}
}
if len(key) > 0 {
// find a static child that can match the rest of the key
if child := n.children[key[0]]; child != nil {
if len(n.pchildren) == 0 {
// use goto to avoid recursion when no param children
n = child
goto repeat
}
data, pnames, order = child.get(key, pvalues)
}
} else if n.data != nil {
// do not return yet: a param node may match an empty string with smaller order
data, pnames, order = n.data, n.pnames, n.order
}
// try matching param children
tvalues := pvalues
allocated := false
for _, child := range n.pchildren {
if child.minOrder >= order {
continue
}
if data != nil && !allocated {
tvalues = make([]string, len(pvalues))
allocated = true
}
if d, p, s := child.get(key, tvalues); d != nil && s < order {
if allocated {
for i := child.pindex; i < len(p); i++ {
pvalues[i] = tvalues[i]
}
}
data, pnames, order = d, p, s
}
}
return
}
|
go
|
func (n *node) get(key string, pvalues []string) (data interface{}, pnames []string, order int) {
order = math.MaxInt32
repeat:
if n.static {
// check if the node key is a prefix of the given key
// a slightly optimized version of strings.HasPrefix
nkl := len(n.key)
if nkl > len(key) {
return
}
for i := nkl - 1; i >= 0; i-- {
if n.key[i] != key[i] {
return
}
}
key = key[nkl:]
} else if n.regex != nil {
// param node with regular expression
if n.regex.String() == "^.*" {
pvalues[n.pindex] = key
key = ""
} else if match := n.regex.FindStringIndex(key); match != nil {
pvalues[n.pindex] = key[0:match[1]]
key = key[match[1]:]
} else {
return
}
} else {
// param node matching non-"/" characters
i, kl := 0, len(key)
for ; i < kl; i++ {
if key[i] == '/' {
pvalues[n.pindex] = key[0:i]
key = key[i:]
break
}
}
if i == kl {
pvalues[n.pindex] = key
key = ""
}
}
if len(key) > 0 {
// find a static child that can match the rest of the key
if child := n.children[key[0]]; child != nil {
if len(n.pchildren) == 0 {
// use goto to avoid recursion when no param children
n = child
goto repeat
}
data, pnames, order = child.get(key, pvalues)
}
} else if n.data != nil {
// do not return yet: a param node may match an empty string with smaller order
data, pnames, order = n.data, n.pnames, n.order
}
// try matching param children
tvalues := pvalues
allocated := false
for _, child := range n.pchildren {
if child.minOrder >= order {
continue
}
if data != nil && !allocated {
tvalues = make([]string, len(pvalues))
allocated = true
}
if d, p, s := child.get(key, tvalues); d != nil && s < order {
if allocated {
for i := child.pindex; i < len(p); i++ {
pvalues[i] = tvalues[i]
}
}
data, pnames, order = d, p, s
}
}
return
}
|
[
"func",
"(",
"n",
"*",
"node",
")",
"get",
"(",
"key",
"string",
",",
"pvalues",
"[",
"]",
"string",
")",
"(",
"data",
"interface",
"{",
"}",
",",
"pnames",
"[",
"]",
"string",
",",
"order",
"int",
")",
"{",
"order",
"=",
"math",
".",
"MaxInt32",
"\n\n",
"repeat",
":",
"if",
"n",
".",
"static",
"{",
"// check if the node key is a prefix of the given key",
"// a slightly optimized version of strings.HasPrefix",
"nkl",
":=",
"len",
"(",
"n",
".",
"key",
")",
"\n",
"if",
"nkl",
">",
"len",
"(",
"key",
")",
"{",
"return",
"\n",
"}",
"\n",
"for",
"i",
":=",
"nkl",
"-",
"1",
";",
"i",
">=",
"0",
";",
"i",
"--",
"{",
"if",
"n",
".",
"key",
"[",
"i",
"]",
"!=",
"key",
"[",
"i",
"]",
"{",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"key",
"=",
"key",
"[",
"nkl",
":",
"]",
"\n",
"}",
"else",
"if",
"n",
".",
"regex",
"!=",
"nil",
"{",
"// param node with regular expression",
"if",
"n",
".",
"regex",
".",
"String",
"(",
")",
"==",
"\"",
"\"",
"{",
"pvalues",
"[",
"n",
".",
"pindex",
"]",
"=",
"key",
"\n",
"key",
"=",
"\"",
"\"",
"\n",
"}",
"else",
"if",
"match",
":=",
"n",
".",
"regex",
".",
"FindStringIndex",
"(",
"key",
")",
";",
"match",
"!=",
"nil",
"{",
"pvalues",
"[",
"n",
".",
"pindex",
"]",
"=",
"key",
"[",
"0",
":",
"match",
"[",
"1",
"]",
"]",
"\n",
"key",
"=",
"key",
"[",
"match",
"[",
"1",
"]",
":",
"]",
"\n",
"}",
"else",
"{",
"return",
"\n",
"}",
"\n",
"}",
"else",
"{",
"// param node matching non-\"/\" characters",
"i",
",",
"kl",
":=",
"0",
",",
"len",
"(",
"key",
")",
"\n",
"for",
";",
"i",
"<",
"kl",
";",
"i",
"++",
"{",
"if",
"key",
"[",
"i",
"]",
"==",
"'/'",
"{",
"pvalues",
"[",
"n",
".",
"pindex",
"]",
"=",
"key",
"[",
"0",
":",
"i",
"]",
"\n",
"key",
"=",
"key",
"[",
"i",
":",
"]",
"\n",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"i",
"==",
"kl",
"{",
"pvalues",
"[",
"n",
".",
"pindex",
"]",
"=",
"key",
"\n",
"key",
"=",
"\"",
"\"",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"key",
")",
">",
"0",
"{",
"// find a static child that can match the rest of the key",
"if",
"child",
":=",
"n",
".",
"children",
"[",
"key",
"[",
"0",
"]",
"]",
";",
"child",
"!=",
"nil",
"{",
"if",
"len",
"(",
"n",
".",
"pchildren",
")",
"==",
"0",
"{",
"// use goto to avoid recursion when no param children",
"n",
"=",
"child",
"\n",
"goto",
"repeat",
"\n",
"}",
"\n",
"data",
",",
"pnames",
",",
"order",
"=",
"child",
".",
"get",
"(",
"key",
",",
"pvalues",
")",
"\n",
"}",
"\n",
"}",
"else",
"if",
"n",
".",
"data",
"!=",
"nil",
"{",
"// do not return yet: a param node may match an empty string with smaller order",
"data",
",",
"pnames",
",",
"order",
"=",
"n",
".",
"data",
",",
"n",
".",
"pnames",
",",
"n",
".",
"order",
"\n",
"}",
"\n\n",
"// try matching param children",
"tvalues",
":=",
"pvalues",
"\n",
"allocated",
":=",
"false",
"\n",
"for",
"_",
",",
"child",
":=",
"range",
"n",
".",
"pchildren",
"{",
"if",
"child",
".",
"minOrder",
">=",
"order",
"{",
"continue",
"\n",
"}",
"\n",
"if",
"data",
"!=",
"nil",
"&&",
"!",
"allocated",
"{",
"tvalues",
"=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"pvalues",
")",
")",
"\n",
"allocated",
"=",
"true",
"\n",
"}",
"\n",
"if",
"d",
",",
"p",
",",
"s",
":=",
"child",
".",
"get",
"(",
"key",
",",
"tvalues",
")",
";",
"d",
"!=",
"nil",
"&&",
"s",
"<",
"order",
"{",
"if",
"allocated",
"{",
"for",
"i",
":=",
"child",
".",
"pindex",
";",
"i",
"<",
"len",
"(",
"p",
")",
";",
"i",
"++",
"{",
"pvalues",
"[",
"i",
"]",
"=",
"tvalues",
"[",
"i",
"]",
"\n",
"}",
"\n",
"}",
"\n",
"data",
",",
"pnames",
",",
"order",
"=",
"d",
",",
"p",
",",
"s",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"\n",
"}"
] |
// get returns the data item with the key matching the tree rooted at the current node
|
[
"get",
"returns",
"the",
"data",
"item",
"with",
"the",
"key",
"matching",
"the",
"tree",
"rooted",
"at",
"the",
"current",
"node"
] |
9dcabc145f0d565b5219e2fd72c3da04876980b7
|
https://github.com/go-ozzo/ozzo-routing/blob/9dcabc145f0d565b5219e2fd72c3da04876980b7/store.go#L223-L304
|
14,319 |
willf/bitset
|
bitset.go
|
safeSet
|
func (b *BitSet) safeSet() []uint64 {
if b.set == nil {
b.set = make([]uint64, wordsNeeded(0))
}
return b.set
}
|
go
|
func (b *BitSet) safeSet() []uint64 {
if b.set == nil {
b.set = make([]uint64, wordsNeeded(0))
}
return b.set
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"safeSet",
"(",
")",
"[",
"]",
"uint64",
"{",
"if",
"b",
".",
"set",
"==",
"nil",
"{",
"b",
".",
"set",
"=",
"make",
"(",
"[",
"]",
"uint64",
",",
"wordsNeeded",
"(",
"0",
")",
")",
"\n",
"}",
"\n",
"return",
"b",
".",
"set",
"\n",
"}"
] |
// safeSet will fixup b.set to be non-nil and return the field value
|
[
"safeSet",
"will",
"fixup",
"b",
".",
"set",
"to",
"be",
"non",
"-",
"nil",
"and",
"return",
"the",
"field",
"value"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L83-L88
|
14,320 |
willf/bitset
|
bitset.go
|
wordsNeeded
|
func wordsNeeded(i uint) int {
if i > (Cap() - wordSize + 1) {
return int(Cap() >> log2WordSize)
}
return int((i + (wordSize - 1)) >> log2WordSize)
}
|
go
|
func wordsNeeded(i uint) int {
if i > (Cap() - wordSize + 1) {
return int(Cap() >> log2WordSize)
}
return int((i + (wordSize - 1)) >> log2WordSize)
}
|
[
"func",
"wordsNeeded",
"(",
"i",
"uint",
")",
"int",
"{",
"if",
"i",
">",
"(",
"Cap",
"(",
")",
"-",
"wordSize",
"+",
"1",
")",
"{",
"return",
"int",
"(",
"Cap",
"(",
")",
">>",
"log2WordSize",
")",
"\n",
"}",
"\n",
"return",
"int",
"(",
"(",
"i",
"+",
"(",
"wordSize",
"-",
"1",
")",
")",
">>",
"log2WordSize",
")",
"\n",
"}"
] |
// wordsNeeded calculates the number of words needed for i bits
|
[
"wordsNeeded",
"calculates",
"the",
"number",
"of",
"words",
"needed",
"for",
"i",
"bits"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L101-L106
|
14,321 |
willf/bitset
|
bitset.go
|
New
|
func New(length uint) (bset *BitSet) {
defer func() {
if r := recover(); r != nil {
bset = &BitSet{
0,
make([]uint64, 0),
}
}
}()
bset = &BitSet{
length,
make([]uint64, wordsNeeded(length)),
}
return bset
}
|
go
|
func New(length uint) (bset *BitSet) {
defer func() {
if r := recover(); r != nil {
bset = &BitSet{
0,
make([]uint64, 0),
}
}
}()
bset = &BitSet{
length,
make([]uint64, wordsNeeded(length)),
}
return bset
}
|
[
"func",
"New",
"(",
"length",
"uint",
")",
"(",
"bset",
"*",
"BitSet",
")",
"{",
"defer",
"func",
"(",
")",
"{",
"if",
"r",
":=",
"recover",
"(",
")",
";",
"r",
"!=",
"nil",
"{",
"bset",
"=",
"&",
"BitSet",
"{",
"0",
",",
"make",
"(",
"[",
"]",
"uint64",
",",
"0",
")",
",",
"}",
"\n",
"}",
"\n",
"}",
"(",
")",
"\n\n",
"bset",
"=",
"&",
"BitSet",
"{",
"length",
",",
"make",
"(",
"[",
"]",
"uint64",
",",
"wordsNeeded",
"(",
"length",
")",
")",
",",
"}",
"\n\n",
"return",
"bset",
"\n",
"}"
] |
// New creates a new BitSet with a hint that length bits will be required
|
[
"New",
"creates",
"a",
"new",
"BitSet",
"with",
"a",
"hint",
"that",
"length",
"bits",
"will",
"be",
"required"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L109-L125
|
14,322 |
willf/bitset
|
bitset.go
|
extendSetMaybe
|
func (b *BitSet) extendSetMaybe(i uint) {
if i >= b.length { // if we need more bits, make 'em
nsize := wordsNeeded(i + 1)
if b.set == nil {
b.set = make([]uint64, nsize)
} else if cap(b.set) >= nsize {
b.set = b.set[:nsize] // fast resize
} else if len(b.set) < nsize {
newset := make([]uint64, nsize, 2*nsize) // increase capacity 2x
copy(newset, b.set)
b.set = newset
}
b.length = i + 1
}
}
|
go
|
func (b *BitSet) extendSetMaybe(i uint) {
if i >= b.length { // if we need more bits, make 'em
nsize := wordsNeeded(i + 1)
if b.set == nil {
b.set = make([]uint64, nsize)
} else if cap(b.set) >= nsize {
b.set = b.set[:nsize] // fast resize
} else if len(b.set) < nsize {
newset := make([]uint64, nsize, 2*nsize) // increase capacity 2x
copy(newset, b.set)
b.set = newset
}
b.length = i + 1
}
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"extendSetMaybe",
"(",
"i",
"uint",
")",
"{",
"if",
"i",
">=",
"b",
".",
"length",
"{",
"// if we need more bits, make 'em",
"nsize",
":=",
"wordsNeeded",
"(",
"i",
"+",
"1",
")",
"\n",
"if",
"b",
".",
"set",
"==",
"nil",
"{",
"b",
".",
"set",
"=",
"make",
"(",
"[",
"]",
"uint64",
",",
"nsize",
")",
"\n",
"}",
"else",
"if",
"cap",
"(",
"b",
".",
"set",
")",
">=",
"nsize",
"{",
"b",
".",
"set",
"=",
"b",
".",
"set",
"[",
":",
"nsize",
"]",
"// fast resize",
"\n",
"}",
"else",
"if",
"len",
"(",
"b",
".",
"set",
")",
"<",
"nsize",
"{",
"newset",
":=",
"make",
"(",
"[",
"]",
"uint64",
",",
"nsize",
",",
"2",
"*",
"nsize",
")",
"// increase capacity 2x",
"\n",
"copy",
"(",
"newset",
",",
"b",
".",
"set",
")",
"\n",
"b",
".",
"set",
"=",
"newset",
"\n",
"}",
"\n",
"b",
".",
"length",
"=",
"i",
"+",
"1",
"\n",
"}",
"\n",
"}"
] |
// extendSetMaybe adds additional words to incorporate new bits if needed
|
[
"extendSetMaybe",
"adds",
"additional",
"words",
"to",
"incorporate",
"new",
"bits",
"if",
"needed"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L138-L152
|
14,323 |
willf/bitset
|
bitset.go
|
Set
|
func (b *BitSet) Set(i uint) *BitSet {
b.extendSetMaybe(i)
b.set[i>>log2WordSize] |= 1 << (i & (wordSize - 1))
return b
}
|
go
|
func (b *BitSet) Set(i uint) *BitSet {
b.extendSetMaybe(i)
b.set[i>>log2WordSize] |= 1 << (i & (wordSize - 1))
return b
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"Set",
"(",
"i",
"uint",
")",
"*",
"BitSet",
"{",
"b",
".",
"extendSetMaybe",
"(",
"i",
")",
"\n",
"b",
".",
"set",
"[",
"i",
">>",
"log2WordSize",
"]",
"|=",
"1",
"<<",
"(",
"i",
"&",
"(",
"wordSize",
"-",
"1",
")",
")",
"\n",
"return",
"b",
"\n",
"}"
] |
// Set bit i to 1
|
[
"Set",
"bit",
"i",
"to",
"1"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L163-L167
|
14,324 |
willf/bitset
|
bitset.go
|
Clear
|
func (b *BitSet) Clear(i uint) *BitSet {
if i >= b.length {
return b
}
b.set[i>>log2WordSize] &^= 1 << (i & (wordSize - 1))
return b
}
|
go
|
func (b *BitSet) Clear(i uint) *BitSet {
if i >= b.length {
return b
}
b.set[i>>log2WordSize] &^= 1 << (i & (wordSize - 1))
return b
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"Clear",
"(",
"i",
"uint",
")",
"*",
"BitSet",
"{",
"if",
"i",
">=",
"b",
".",
"length",
"{",
"return",
"b",
"\n",
"}",
"\n",
"b",
".",
"set",
"[",
"i",
">>",
"log2WordSize",
"]",
"&^=",
"1",
"<<",
"(",
"i",
"&",
"(",
"wordSize",
"-",
"1",
")",
")",
"\n",
"return",
"b",
"\n",
"}"
] |
// Clear bit i to 0
|
[
"Clear",
"bit",
"i",
"to",
"0"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L170-L176
|
14,325 |
willf/bitset
|
bitset.go
|
SetTo
|
func (b *BitSet) SetTo(i uint, value bool) *BitSet {
if value {
return b.Set(i)
}
return b.Clear(i)
}
|
go
|
func (b *BitSet) SetTo(i uint, value bool) *BitSet {
if value {
return b.Set(i)
}
return b.Clear(i)
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"SetTo",
"(",
"i",
"uint",
",",
"value",
"bool",
")",
"*",
"BitSet",
"{",
"if",
"value",
"{",
"return",
"b",
".",
"Set",
"(",
"i",
")",
"\n",
"}",
"\n",
"return",
"b",
".",
"Clear",
"(",
"i",
")",
"\n",
"}"
] |
// SetTo sets bit i to value
|
[
"SetTo",
"sets",
"bit",
"i",
"to",
"value"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L179-L184
|
14,326 |
willf/bitset
|
bitset.go
|
Flip
|
func (b *BitSet) Flip(i uint) *BitSet {
if i >= b.length {
return b.Set(i)
}
b.set[i>>log2WordSize] ^= 1 << (i & (wordSize - 1))
return b
}
|
go
|
func (b *BitSet) Flip(i uint) *BitSet {
if i >= b.length {
return b.Set(i)
}
b.set[i>>log2WordSize] ^= 1 << (i & (wordSize - 1))
return b
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"Flip",
"(",
"i",
"uint",
")",
"*",
"BitSet",
"{",
"if",
"i",
">=",
"b",
".",
"length",
"{",
"return",
"b",
".",
"Set",
"(",
"i",
")",
"\n",
"}",
"\n",
"b",
".",
"set",
"[",
"i",
">>",
"log2WordSize",
"]",
"^=",
"1",
"<<",
"(",
"i",
"&",
"(",
"wordSize",
"-",
"1",
")",
")",
"\n",
"return",
"b",
"\n",
"}"
] |
// Flip bit at i
|
[
"Flip",
"bit",
"at",
"i"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L187-L193
|
14,327 |
willf/bitset
|
bitset.go
|
Shrink
|
func (b *BitSet) Shrink(length uint) *BitSet {
idx := wordsNeeded(length + 1)
if idx > len(b.set) {
return b
}
shrunk := make([]uint64, idx)
copy(shrunk, b.set[:idx])
b.set = shrunk
b.length = length + 1
b.set[idx-1] &= (allBits >> (uint64(64) - uint64(length&(wordSize-1)) - 1))
return b
}
|
go
|
func (b *BitSet) Shrink(length uint) *BitSet {
idx := wordsNeeded(length + 1)
if idx > len(b.set) {
return b
}
shrunk := make([]uint64, idx)
copy(shrunk, b.set[:idx])
b.set = shrunk
b.length = length + 1
b.set[idx-1] &= (allBits >> (uint64(64) - uint64(length&(wordSize-1)) - 1))
return b
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"Shrink",
"(",
"length",
"uint",
")",
"*",
"BitSet",
"{",
"idx",
":=",
"wordsNeeded",
"(",
"length",
"+",
"1",
")",
"\n",
"if",
"idx",
">",
"len",
"(",
"b",
".",
"set",
")",
"{",
"return",
"b",
"\n",
"}",
"\n",
"shrunk",
":=",
"make",
"(",
"[",
"]",
"uint64",
",",
"idx",
")",
"\n",
"copy",
"(",
"shrunk",
",",
"b",
".",
"set",
"[",
":",
"idx",
"]",
")",
"\n",
"b",
".",
"set",
"=",
"shrunk",
"\n",
"b",
".",
"length",
"=",
"length",
"+",
"1",
"\n",
"b",
".",
"set",
"[",
"idx",
"-",
"1",
"]",
"&=",
"(",
"allBits",
">>",
"(",
"uint64",
"(",
"64",
")",
"-",
"uint64",
"(",
"length",
"&",
"(",
"wordSize",
"-",
"1",
")",
")",
"-",
"1",
")",
")",
"\n",
"return",
"b",
"\n",
"}"
] |
// Shrink shrinks BitSet to desired length in bits. It clears all bits > length
// and reduces the size and length of the set.
//
// A new slice is allocated to store the new bits, so you may see an increase in
// memory usage until the GC runs. Normally this should not be a problem, but if you
// have an extremely large BitSet its important to understand that the old BitSet will
// remain in memory until the GC frees it.
|
[
"Shrink",
"shrinks",
"BitSet",
"to",
"desired",
"length",
"in",
"bits",
".",
"It",
"clears",
"all",
"bits",
">",
"length",
"and",
"reduces",
"the",
"size",
"and",
"length",
"of",
"the",
"set",
".",
"A",
"new",
"slice",
"is",
"allocated",
"to",
"store",
"the",
"new",
"bits",
"so",
"you",
"may",
"see",
"an",
"increase",
"in",
"memory",
"usage",
"until",
"the",
"GC",
"runs",
".",
"Normally",
"this",
"should",
"not",
"be",
"a",
"problem",
"but",
"if",
"you",
"have",
"an",
"extremely",
"large",
"BitSet",
"its",
"important",
"to",
"understand",
"that",
"the",
"old",
"BitSet",
"will",
"remain",
"in",
"memory",
"until",
"the",
"GC",
"frees",
"it",
"."
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L202-L213
|
14,328 |
willf/bitset
|
bitset.go
|
InsertAt
|
func (b *BitSet) InsertAt(idx uint) *BitSet {
insertAtElement := (idx >> log2WordSize)
// if length of set is a multiple of wordSize we need to allocate more space first
if b.isLenExactMultiple() {
b.set = append(b.set, uint64(0))
}
var i uint
for i = uint(len(b.set) - 1); i > insertAtElement; i-- {
// all elements above the position where we want to insert can simply by shifted
b.set[i] <<= 1
// we take the most significant bit of the previous element and set it as
// the least significant bit of the current element
b.set[i] |= (b.set[i-1] & 0x8000000000000000) >> 63
}
// generate a mask to extract the data that we need to shift left
// within the element where we insert a bit
dataMask := ^(uint64(1)<<uint64(idx&(wordSize-1)) - 1)
// extract that data that we'll shift
data := b.set[i] & dataMask
// set the positions of the data mask to 0 in the element where we insert
b.set[i] &= ^dataMask
// shift data mask to the left and insert its data to the slice element
b.set[i] |= data << 1
// add 1 to length of BitSet
b.length++
return b
}
|
go
|
func (b *BitSet) InsertAt(idx uint) *BitSet {
insertAtElement := (idx >> log2WordSize)
// if length of set is a multiple of wordSize we need to allocate more space first
if b.isLenExactMultiple() {
b.set = append(b.set, uint64(0))
}
var i uint
for i = uint(len(b.set) - 1); i > insertAtElement; i-- {
// all elements above the position where we want to insert can simply by shifted
b.set[i] <<= 1
// we take the most significant bit of the previous element and set it as
// the least significant bit of the current element
b.set[i] |= (b.set[i-1] & 0x8000000000000000) >> 63
}
// generate a mask to extract the data that we need to shift left
// within the element where we insert a bit
dataMask := ^(uint64(1)<<uint64(idx&(wordSize-1)) - 1)
// extract that data that we'll shift
data := b.set[i] & dataMask
// set the positions of the data mask to 0 in the element where we insert
b.set[i] &= ^dataMask
// shift data mask to the left and insert its data to the slice element
b.set[i] |= data << 1
// add 1 to length of BitSet
b.length++
return b
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"InsertAt",
"(",
"idx",
"uint",
")",
"*",
"BitSet",
"{",
"insertAtElement",
":=",
"(",
"idx",
">>",
"log2WordSize",
")",
"\n\n",
"// if length of set is a multiple of wordSize we need to allocate more space first",
"if",
"b",
".",
"isLenExactMultiple",
"(",
")",
"{",
"b",
".",
"set",
"=",
"append",
"(",
"b",
".",
"set",
",",
"uint64",
"(",
"0",
")",
")",
"\n",
"}",
"\n\n",
"var",
"i",
"uint",
"\n",
"for",
"i",
"=",
"uint",
"(",
"len",
"(",
"b",
".",
"set",
")",
"-",
"1",
")",
";",
"i",
">",
"insertAtElement",
";",
"i",
"--",
"{",
"// all elements above the position where we want to insert can simply by shifted",
"b",
".",
"set",
"[",
"i",
"]",
"<<=",
"1",
"\n\n",
"// we take the most significant bit of the previous element and set it as",
"// the least significant bit of the current element",
"b",
".",
"set",
"[",
"i",
"]",
"|=",
"(",
"b",
".",
"set",
"[",
"i",
"-",
"1",
"]",
"&",
"0x8000000000000000",
")",
">>",
"63",
"\n",
"}",
"\n\n",
"// generate a mask to extract the data that we need to shift left",
"// within the element where we insert a bit",
"dataMask",
":=",
"^",
"(",
"uint64",
"(",
"1",
")",
"<<",
"uint64",
"(",
"idx",
"&",
"(",
"wordSize",
"-",
"1",
")",
")",
"-",
"1",
")",
"\n\n",
"// extract that data that we'll shift",
"data",
":=",
"b",
".",
"set",
"[",
"i",
"]",
"&",
"dataMask",
"\n\n",
"// set the positions of the data mask to 0 in the element where we insert",
"b",
".",
"set",
"[",
"i",
"]",
"&=",
"^",
"dataMask",
"\n\n",
"// shift data mask to the left and insert its data to the slice element",
"b",
".",
"set",
"[",
"i",
"]",
"|=",
"data",
"<<",
"1",
"\n\n",
"// add 1 to length of BitSet",
"b",
".",
"length",
"++",
"\n\n",
"return",
"b",
"\n",
"}"
] |
// InsertAt takes an index which indicates where a bit should be
// inserted. Then it shifts all the bits in the set to the left by 1, starting
// from the given index position, and sets the index position to 0.
//
// Depending on the size of your BitSet, and where you are inserting the new entry,
// this method could be extremely slow and in some cases might cause the entire BitSet
// to be recopied.
|
[
"InsertAt",
"takes",
"an",
"index",
"which",
"indicates",
"where",
"a",
"bit",
"should",
"be",
"inserted",
".",
"Then",
"it",
"shifts",
"all",
"the",
"bits",
"in",
"the",
"set",
"to",
"the",
"left",
"by",
"1",
"starting",
"from",
"the",
"given",
"index",
"position",
"and",
"sets",
"the",
"index",
"position",
"to",
"0",
".",
"Depending",
"on",
"the",
"size",
"of",
"your",
"BitSet",
"and",
"where",
"you",
"are",
"inserting",
"the",
"new",
"entry",
"this",
"method",
"could",
"be",
"extremely",
"slow",
"and",
"in",
"some",
"cases",
"might",
"cause",
"the",
"entire",
"BitSet",
"to",
"be",
"recopied",
"."
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L222-L257
|
14,329 |
willf/bitset
|
bitset.go
|
ClearAll
|
func (b *BitSet) ClearAll() *BitSet {
if b != nil && b.set != nil {
for i := range b.set {
b.set[i] = 0
}
}
return b
}
|
go
|
func (b *BitSet) ClearAll() *BitSet {
if b != nil && b.set != nil {
for i := range b.set {
b.set[i] = 0
}
}
return b
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"ClearAll",
"(",
")",
"*",
"BitSet",
"{",
"if",
"b",
"!=",
"nil",
"&&",
"b",
".",
"set",
"!=",
"nil",
"{",
"for",
"i",
":=",
"range",
"b",
".",
"set",
"{",
"b",
".",
"set",
"[",
"i",
"]",
"=",
"0",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"b",
"\n",
"}"
] |
// ClearAll clears the entire BitSet
|
[
"ClearAll",
"clears",
"the",
"entire",
"BitSet"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L428-L435
|
14,330 |
willf/bitset
|
bitset.go
|
Clone
|
func (b *BitSet) Clone() *BitSet {
c := New(b.length)
if b.set != nil { // Clone should not modify current object
copy(c.set, b.set)
}
return c
}
|
go
|
func (b *BitSet) Clone() *BitSet {
c := New(b.length)
if b.set != nil { // Clone should not modify current object
copy(c.set, b.set)
}
return c
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"Clone",
"(",
")",
"*",
"BitSet",
"{",
"c",
":=",
"New",
"(",
"b",
".",
"length",
")",
"\n",
"if",
"b",
".",
"set",
"!=",
"nil",
"{",
"// Clone should not modify current object",
"copy",
"(",
"c",
".",
"set",
",",
"b",
".",
"set",
")",
"\n",
"}",
"\n",
"return",
"c",
"\n",
"}"
] |
// Clone this BitSet
|
[
"Clone",
"this",
"BitSet"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L443-L449
|
14,331 |
willf/bitset
|
bitset.go
|
Copy
|
func (b *BitSet) Copy(c *BitSet) (count uint) {
if c == nil {
return
}
if b.set != nil { // Copy should not modify current object
copy(c.set, b.set)
}
count = c.length
if b.length < c.length {
count = b.length
}
return
}
|
go
|
func (b *BitSet) Copy(c *BitSet) (count uint) {
if c == nil {
return
}
if b.set != nil { // Copy should not modify current object
copy(c.set, b.set)
}
count = c.length
if b.length < c.length {
count = b.length
}
return
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"Copy",
"(",
"c",
"*",
"BitSet",
")",
"(",
"count",
"uint",
")",
"{",
"if",
"c",
"==",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"if",
"b",
".",
"set",
"!=",
"nil",
"{",
"// Copy should not modify current object",
"copy",
"(",
"c",
".",
"set",
",",
"b",
".",
"set",
")",
"\n",
"}",
"\n",
"count",
"=",
"c",
".",
"length",
"\n",
"if",
"b",
".",
"length",
"<",
"c",
".",
"length",
"{",
"count",
"=",
"b",
".",
"length",
"\n",
"}",
"\n",
"return",
"\n",
"}"
] |
// Copy into a destination BitSet
// Returning the size of the destination BitSet
// like array copy
|
[
"Copy",
"into",
"a",
"destination",
"BitSet",
"Returning",
"the",
"size",
"of",
"the",
"destination",
"BitSet",
"like",
"array",
"copy"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L454-L466
|
14,332 |
willf/bitset
|
bitset.go
|
Equal
|
func (b *BitSet) Equal(c *BitSet) bool {
if c == nil {
return false
}
if b.length != c.length {
return false
}
if b.length == 0 { // if they have both length == 0, then could have nil set
return true
}
// testing for equality shoud not transform the bitset (no call to safeSet)
for p, v := range b.set {
if c.set[p] != v {
return false
}
}
return true
}
|
go
|
func (b *BitSet) Equal(c *BitSet) bool {
if c == nil {
return false
}
if b.length != c.length {
return false
}
if b.length == 0 { // if they have both length == 0, then could have nil set
return true
}
// testing for equality shoud not transform the bitset (no call to safeSet)
for p, v := range b.set {
if c.set[p] != v {
return false
}
}
return true
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"Equal",
"(",
"c",
"*",
"BitSet",
")",
"bool",
"{",
"if",
"c",
"==",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"b",
".",
"length",
"!=",
"c",
".",
"length",
"{",
"return",
"false",
"\n",
"}",
"\n",
"if",
"b",
".",
"length",
"==",
"0",
"{",
"// if they have both length == 0, then could have nil set",
"return",
"true",
"\n",
"}",
"\n",
"// testing for equality shoud not transform the bitset (no call to safeSet)",
"for",
"p",
",",
"v",
":=",
"range",
"b",
".",
"set",
"{",
"if",
"c",
".",
"set",
"[",
"p",
"]",
"!=",
"v",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] |
// Equal tests the equvalence of two BitSets.
// False if they are of different sizes, otherwise true
// only if all the same bits are set
|
[
"Equal",
"tests",
"the",
"equvalence",
"of",
"two",
"BitSets",
".",
"False",
"if",
"they",
"are",
"of",
"different",
"sizes",
"otherwise",
"true",
"only",
"if",
"all",
"the",
"same",
"bits",
"are",
"set"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L479-L497
|
14,333 |
willf/bitset
|
bitset.go
|
DifferenceCardinality
|
func (b *BitSet) DifferenceCardinality(compare *BitSet) uint {
panicIfNull(b)
panicIfNull(compare)
l := int(compare.wordCount())
if l > int(b.wordCount()) {
l = int(b.wordCount())
}
cnt := uint64(0)
cnt += popcntMaskSlice(b.set[:l], compare.set[:l])
cnt += popcntSlice(b.set[l:])
return uint(cnt)
}
|
go
|
func (b *BitSet) DifferenceCardinality(compare *BitSet) uint {
panicIfNull(b)
panicIfNull(compare)
l := int(compare.wordCount())
if l > int(b.wordCount()) {
l = int(b.wordCount())
}
cnt := uint64(0)
cnt += popcntMaskSlice(b.set[:l], compare.set[:l])
cnt += popcntSlice(b.set[l:])
return uint(cnt)
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"DifferenceCardinality",
"(",
"compare",
"*",
"BitSet",
")",
"uint",
"{",
"panicIfNull",
"(",
"b",
")",
"\n",
"panicIfNull",
"(",
"compare",
")",
"\n",
"l",
":=",
"int",
"(",
"compare",
".",
"wordCount",
"(",
")",
")",
"\n",
"if",
"l",
">",
"int",
"(",
"b",
".",
"wordCount",
"(",
")",
")",
"{",
"l",
"=",
"int",
"(",
"b",
".",
"wordCount",
"(",
")",
")",
"\n",
"}",
"\n",
"cnt",
":=",
"uint64",
"(",
"0",
")",
"\n",
"cnt",
"+=",
"popcntMaskSlice",
"(",
"b",
".",
"set",
"[",
":",
"l",
"]",
",",
"compare",
".",
"set",
"[",
":",
"l",
"]",
")",
"\n",
"cnt",
"+=",
"popcntSlice",
"(",
"b",
".",
"set",
"[",
"l",
":",
"]",
")",
"\n",
"return",
"uint",
"(",
"cnt",
")",
"\n",
"}"
] |
// DifferenceCardinality computes the cardinality of the differnce
|
[
"DifferenceCardinality",
"computes",
"the",
"cardinality",
"of",
"the",
"differnce"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L522-L533
|
14,334 |
willf/bitset
|
bitset.go
|
IntersectionCardinality
|
func (b *BitSet) IntersectionCardinality(compare *BitSet) uint {
panicIfNull(b)
panicIfNull(compare)
b, compare = sortByLength(b, compare)
cnt := popcntAndSlice(b.set, compare.set)
return uint(cnt)
}
|
go
|
func (b *BitSet) IntersectionCardinality(compare *BitSet) uint {
panicIfNull(b)
panicIfNull(compare)
b, compare = sortByLength(b, compare)
cnt := popcntAndSlice(b.set, compare.set)
return uint(cnt)
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"IntersectionCardinality",
"(",
"compare",
"*",
"BitSet",
")",
"uint",
"{",
"panicIfNull",
"(",
"b",
")",
"\n",
"panicIfNull",
"(",
"compare",
")",
"\n",
"b",
",",
"compare",
"=",
"sortByLength",
"(",
"b",
",",
"compare",
")",
"\n",
"cnt",
":=",
"popcntAndSlice",
"(",
"b",
".",
"set",
",",
"compare",
".",
"set",
")",
"\n",
"return",
"uint",
"(",
"cnt",
")",
"\n",
"}"
] |
// IntersectionCardinality computes the cardinality of the union
|
[
"IntersectionCardinality",
"computes",
"the",
"cardinality",
"of",
"the",
"union"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L574-L580
|
14,335 |
willf/bitset
|
bitset.go
|
UnionCardinality
|
func (b *BitSet) UnionCardinality(compare *BitSet) uint {
panicIfNull(b)
panicIfNull(compare)
b, compare = sortByLength(b, compare)
cnt := popcntOrSlice(b.set, compare.set)
if len(compare.set) > len(b.set) {
cnt += popcntSlice(compare.set[len(b.set):])
}
return uint(cnt)
}
|
go
|
func (b *BitSet) UnionCardinality(compare *BitSet) uint {
panicIfNull(b)
panicIfNull(compare)
b, compare = sortByLength(b, compare)
cnt := popcntOrSlice(b.set, compare.set)
if len(compare.set) > len(b.set) {
cnt += popcntSlice(compare.set[len(b.set):])
}
return uint(cnt)
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"UnionCardinality",
"(",
"compare",
"*",
"BitSet",
")",
"uint",
"{",
"panicIfNull",
"(",
"b",
")",
"\n",
"panicIfNull",
"(",
"compare",
")",
"\n",
"b",
",",
"compare",
"=",
"sortByLength",
"(",
"b",
",",
"compare",
")",
"\n",
"cnt",
":=",
"popcntOrSlice",
"(",
"b",
".",
"set",
",",
"compare",
".",
"set",
")",
"\n",
"if",
"len",
"(",
"compare",
".",
"set",
")",
">",
"len",
"(",
"b",
".",
"set",
")",
"{",
"cnt",
"+=",
"popcntSlice",
"(",
"compare",
".",
"set",
"[",
"len",
"(",
"b",
".",
"set",
")",
":",
"]",
")",
"\n",
"}",
"\n",
"return",
"uint",
"(",
"cnt",
")",
"\n",
"}"
] |
// UnionCardinality computes the cardinality of the uniton of the base set
// and the compare set.
|
[
"UnionCardinality",
"computes",
"the",
"cardinality",
"of",
"the",
"uniton",
"of",
"the",
"base",
"set",
"and",
"the",
"compare",
"set",
"."
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L618-L627
|
14,336 |
willf/bitset
|
bitset.go
|
SymmetricDifferenceCardinality
|
func (b *BitSet) SymmetricDifferenceCardinality(compare *BitSet) uint {
panicIfNull(b)
panicIfNull(compare)
b, compare = sortByLength(b, compare)
cnt := popcntXorSlice(b.set, compare.set)
if len(compare.set) > len(b.set) {
cnt += popcntSlice(compare.set[len(b.set):])
}
return uint(cnt)
}
|
go
|
func (b *BitSet) SymmetricDifferenceCardinality(compare *BitSet) uint {
panicIfNull(b)
panicIfNull(compare)
b, compare = sortByLength(b, compare)
cnt := popcntXorSlice(b.set, compare.set)
if len(compare.set) > len(b.set) {
cnt += popcntSlice(compare.set[len(b.set):])
}
return uint(cnt)
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"SymmetricDifferenceCardinality",
"(",
"compare",
"*",
"BitSet",
")",
"uint",
"{",
"panicIfNull",
"(",
"b",
")",
"\n",
"panicIfNull",
"(",
"compare",
")",
"\n",
"b",
",",
"compare",
"=",
"sortByLength",
"(",
"b",
",",
"compare",
")",
"\n",
"cnt",
":=",
"popcntXorSlice",
"(",
"b",
".",
"set",
",",
"compare",
".",
"set",
")",
"\n",
"if",
"len",
"(",
"compare",
".",
"set",
")",
">",
"len",
"(",
"b",
".",
"set",
")",
"{",
"cnt",
"+=",
"popcntSlice",
"(",
"compare",
".",
"set",
"[",
"len",
"(",
"b",
".",
"set",
")",
":",
"]",
")",
"\n",
"}",
"\n",
"return",
"uint",
"(",
"cnt",
")",
"\n",
"}"
] |
// SymmetricDifferenceCardinality computes the cardinality of the symmetric difference
|
[
"SymmetricDifferenceCardinality",
"computes",
"the",
"cardinality",
"of",
"the",
"symmetric",
"difference"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L666-L675
|
14,337 |
willf/bitset
|
bitset.go
|
cleanLastWord
|
func (b *BitSet) cleanLastWord() {
if !b.isLenExactMultiple() {
b.set[len(b.set)-1] &= allBits >> (wordSize - b.length%wordSize)
}
}
|
go
|
func (b *BitSet) cleanLastWord() {
if !b.isLenExactMultiple() {
b.set[len(b.set)-1] &= allBits >> (wordSize - b.length%wordSize)
}
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"cleanLastWord",
"(",
")",
"{",
"if",
"!",
"b",
".",
"isLenExactMultiple",
"(",
")",
"{",
"b",
".",
"set",
"[",
"len",
"(",
"b",
".",
"set",
")",
"-",
"1",
"]",
"&=",
"allBits",
">>",
"(",
"wordSize",
"-",
"b",
".",
"length",
"%",
"wordSize",
")",
"\n",
"}",
"\n",
"}"
] |
// Clean last word by setting unused bits to 0
|
[
"Clean",
"last",
"word",
"by",
"setting",
"unused",
"bits",
"to",
"0"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L705-L709
|
14,338 |
willf/bitset
|
bitset.go
|
All
|
func (b *BitSet) All() bool {
panicIfNull(b)
return b.Count() == b.length
}
|
go
|
func (b *BitSet) All() bool {
panicIfNull(b)
return b.Count() == b.length
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"All",
"(",
")",
"bool",
"{",
"panicIfNull",
"(",
"b",
")",
"\n",
"return",
"b",
".",
"Count",
"(",
")",
"==",
"b",
".",
"length",
"\n",
"}"
] |
// All returns true if all bits are set, false otherwise. Returns true for
// empty sets.
|
[
"All",
"returns",
"true",
"if",
"all",
"bits",
"are",
"set",
"false",
"otherwise",
".",
"Returns",
"true",
"for",
"empty",
"sets",
"."
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L724-L727
|
14,339 |
willf/bitset
|
bitset.go
|
None
|
func (b *BitSet) None() bool {
panicIfNull(b)
if b != nil && b.set != nil {
for _, word := range b.set {
if word > 0 {
return false
}
}
return true
}
return true
}
|
go
|
func (b *BitSet) None() bool {
panicIfNull(b)
if b != nil && b.set != nil {
for _, word := range b.set {
if word > 0 {
return false
}
}
return true
}
return true
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"None",
"(",
")",
"bool",
"{",
"panicIfNull",
"(",
"b",
")",
"\n",
"if",
"b",
"!=",
"nil",
"&&",
"b",
".",
"set",
"!=",
"nil",
"{",
"for",
"_",
",",
"word",
":=",
"range",
"b",
".",
"set",
"{",
"if",
"word",
">",
"0",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] |
// None returns true if no bit is set, false otherwise. Retursn true for
// empty sets.
|
[
"None",
"returns",
"true",
"if",
"no",
"bit",
"is",
"set",
"false",
"otherwise",
".",
"Retursn",
"true",
"for",
"empty",
"sets",
"."
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L731-L742
|
14,340 |
willf/bitset
|
bitset.go
|
IsSuperSet
|
func (b *BitSet) IsSuperSet(other *BitSet) bool {
for i, e := other.NextSet(0); e; i, e = other.NextSet(i + 1) {
if !b.Test(i) {
return false
}
}
return true
}
|
go
|
func (b *BitSet) IsSuperSet(other *BitSet) bool {
for i, e := other.NextSet(0); e; i, e = other.NextSet(i + 1) {
if !b.Test(i) {
return false
}
}
return true
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"IsSuperSet",
"(",
"other",
"*",
"BitSet",
")",
"bool",
"{",
"for",
"i",
",",
"e",
":=",
"other",
".",
"NextSet",
"(",
"0",
")",
";",
"e",
";",
"i",
",",
"e",
"=",
"other",
".",
"NextSet",
"(",
"i",
"+",
"1",
")",
"{",
"if",
"!",
"b",
".",
"Test",
"(",
"i",
")",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] |
// IsSuperSet returns true if this is a superset of the other set
|
[
"IsSuperSet",
"returns",
"true",
"if",
"this",
"is",
"a",
"superset",
"of",
"the",
"other",
"set"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L751-L758
|
14,341 |
willf/bitset
|
bitset.go
|
IsStrictSuperSet
|
func (b *BitSet) IsStrictSuperSet(other *BitSet) bool {
return b.Count() > other.Count() && b.IsSuperSet(other)
}
|
go
|
func (b *BitSet) IsStrictSuperSet(other *BitSet) bool {
return b.Count() > other.Count() && b.IsSuperSet(other)
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"IsStrictSuperSet",
"(",
"other",
"*",
"BitSet",
")",
"bool",
"{",
"return",
"b",
".",
"Count",
"(",
")",
">",
"other",
".",
"Count",
"(",
")",
"&&",
"b",
".",
"IsSuperSet",
"(",
"other",
")",
"\n",
"}"
] |
// IsStrictSuperSet returns true if this is a strict superset of the other set
|
[
"IsStrictSuperSet",
"returns",
"true",
"if",
"this",
"is",
"a",
"strict",
"superset",
"of",
"the",
"other",
"set"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L761-L763
|
14,342 |
willf/bitset
|
bitset.go
|
DumpAsBits
|
func (b *BitSet) DumpAsBits() string {
if b.set == nil {
return "."
}
buffer := bytes.NewBufferString("")
i := len(b.set) - 1
for ; i >= 0; i-- {
fmt.Fprintf(buffer, "%064b.", b.set[i])
}
return buffer.String()
}
|
go
|
func (b *BitSet) DumpAsBits() string {
if b.set == nil {
return "."
}
buffer := bytes.NewBufferString("")
i := len(b.set) - 1
for ; i >= 0; i-- {
fmt.Fprintf(buffer, "%064b.", b.set[i])
}
return buffer.String()
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"DumpAsBits",
"(",
")",
"string",
"{",
"if",
"b",
".",
"set",
"==",
"nil",
"{",
"return",
"\"",
"\"",
"\n",
"}",
"\n",
"buffer",
":=",
"bytes",
".",
"NewBufferString",
"(",
"\"",
"\"",
")",
"\n",
"i",
":=",
"len",
"(",
"b",
".",
"set",
")",
"-",
"1",
"\n",
"for",
";",
"i",
">=",
"0",
";",
"i",
"--",
"{",
"fmt",
".",
"Fprintf",
"(",
"buffer",
",",
"\"",
"\"",
",",
"b",
".",
"set",
"[",
"i",
"]",
")",
"\n",
"}",
"\n",
"return",
"buffer",
".",
"String",
"(",
")",
"\n",
"}"
] |
// DumpAsBits dumps a bit set as a string of bits
|
[
"DumpAsBits",
"dumps",
"a",
"bit",
"set",
"as",
"a",
"string",
"of",
"bits"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L766-L776
|
14,343 |
willf/bitset
|
bitset.go
|
BinaryStorageSize
|
func (b *BitSet) BinaryStorageSize() int {
return binary.Size(uint64(0)) + binary.Size(b.set)
}
|
go
|
func (b *BitSet) BinaryStorageSize() int {
return binary.Size(uint64(0)) + binary.Size(b.set)
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"BinaryStorageSize",
"(",
")",
"int",
"{",
"return",
"binary",
".",
"Size",
"(",
"uint64",
"(",
"0",
")",
")",
"+",
"binary",
".",
"Size",
"(",
"b",
".",
"set",
")",
"\n",
"}"
] |
// BinaryStorageSize returns the binary storage requirements
|
[
"BinaryStorageSize",
"returns",
"the",
"binary",
"storage",
"requirements"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L779-L781
|
14,344 |
willf/bitset
|
bitset.go
|
WriteTo
|
func (b *BitSet) WriteTo(stream io.Writer) (int64, error) {
length := uint64(b.length)
// Write length
err := binary.Write(stream, binaryOrder, length)
if err != nil {
return 0, err
}
// Write set
err = binary.Write(stream, binaryOrder, b.set)
return int64(b.BinaryStorageSize()), err
}
|
go
|
func (b *BitSet) WriteTo(stream io.Writer) (int64, error) {
length := uint64(b.length)
// Write length
err := binary.Write(stream, binaryOrder, length)
if err != nil {
return 0, err
}
// Write set
err = binary.Write(stream, binaryOrder, b.set)
return int64(b.BinaryStorageSize()), err
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"WriteTo",
"(",
"stream",
"io",
".",
"Writer",
")",
"(",
"int64",
",",
"error",
")",
"{",
"length",
":=",
"uint64",
"(",
"b",
".",
"length",
")",
"\n\n",
"// Write length",
"err",
":=",
"binary",
".",
"Write",
"(",
"stream",
",",
"binaryOrder",
",",
"length",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"0",
",",
"err",
"\n",
"}",
"\n\n",
"// Write set",
"err",
"=",
"binary",
".",
"Write",
"(",
"stream",
",",
"binaryOrder",
",",
"b",
".",
"set",
")",
"\n",
"return",
"int64",
"(",
"b",
".",
"BinaryStorageSize",
"(",
")",
")",
",",
"err",
"\n",
"}"
] |
// WriteTo writes a BitSet to a stream
|
[
"WriteTo",
"writes",
"a",
"BitSet",
"to",
"a",
"stream"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L784-L796
|
14,345 |
willf/bitset
|
bitset.go
|
ReadFrom
|
func (b *BitSet) ReadFrom(stream io.Reader) (int64, error) {
var length uint64
// Read length first
err := binary.Read(stream, binaryOrder, &length)
if err != nil {
return 0, err
}
newset := New(uint(length))
if uint64(newset.length) != length {
return 0, errors.New("Unmarshalling error: type mismatch")
}
// Read remaining bytes as set
err = binary.Read(stream, binaryOrder, newset.set)
if err != nil {
return 0, err
}
*b = *newset
return int64(b.BinaryStorageSize()), nil
}
|
go
|
func (b *BitSet) ReadFrom(stream io.Reader) (int64, error) {
var length uint64
// Read length first
err := binary.Read(stream, binaryOrder, &length)
if err != nil {
return 0, err
}
newset := New(uint(length))
if uint64(newset.length) != length {
return 0, errors.New("Unmarshalling error: type mismatch")
}
// Read remaining bytes as set
err = binary.Read(stream, binaryOrder, newset.set)
if err != nil {
return 0, err
}
*b = *newset
return int64(b.BinaryStorageSize()), nil
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"ReadFrom",
"(",
"stream",
"io",
".",
"Reader",
")",
"(",
"int64",
",",
"error",
")",
"{",
"var",
"length",
"uint64",
"\n\n",
"// Read length first",
"err",
":=",
"binary",
".",
"Read",
"(",
"stream",
",",
"binaryOrder",
",",
"&",
"length",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"0",
",",
"err",
"\n",
"}",
"\n",
"newset",
":=",
"New",
"(",
"uint",
"(",
"length",
")",
")",
"\n\n",
"if",
"uint64",
"(",
"newset",
".",
"length",
")",
"!=",
"length",
"{",
"return",
"0",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"// Read remaining bytes as set",
"err",
"=",
"binary",
".",
"Read",
"(",
"stream",
",",
"binaryOrder",
",",
"newset",
".",
"set",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"0",
",",
"err",
"\n",
"}",
"\n\n",
"*",
"b",
"=",
"*",
"newset",
"\n",
"return",
"int64",
"(",
"b",
".",
"BinaryStorageSize",
"(",
")",
")",
",",
"nil",
"\n",
"}"
] |
// ReadFrom reads a BitSet from a stream written using WriteTo
|
[
"ReadFrom",
"reads",
"a",
"BitSet",
"from",
"a",
"stream",
"written",
"using",
"WriteTo"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L799-L821
|
14,346 |
willf/bitset
|
bitset.go
|
MarshalBinary
|
func (b *BitSet) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
writer := bufio.NewWriter(&buf)
_, err := b.WriteTo(writer)
if err != nil {
return []byte{}, err
}
err = writer.Flush()
return buf.Bytes(), err
}
|
go
|
func (b *BitSet) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
writer := bufio.NewWriter(&buf)
_, err := b.WriteTo(writer)
if err != nil {
return []byte{}, err
}
err = writer.Flush()
return buf.Bytes(), err
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"MarshalBinary",
"(",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"var",
"buf",
"bytes",
".",
"Buffer",
"\n",
"writer",
":=",
"bufio",
".",
"NewWriter",
"(",
"&",
"buf",
")",
"\n\n",
"_",
",",
"err",
":=",
"b",
".",
"WriteTo",
"(",
"writer",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"[",
"]",
"byte",
"{",
"}",
",",
"err",
"\n",
"}",
"\n\n",
"err",
"=",
"writer",
".",
"Flush",
"(",
")",
"\n\n",
"return",
"buf",
".",
"Bytes",
"(",
")",
",",
"err",
"\n",
"}"
] |
// MarshalBinary encodes a BitSet into a binary form and returns the result.
|
[
"MarshalBinary",
"encodes",
"a",
"BitSet",
"into",
"a",
"binary",
"form",
"and",
"returns",
"the",
"result",
"."
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L824-L836
|
14,347 |
willf/bitset
|
bitset.go
|
UnmarshalBinary
|
func (b *BitSet) UnmarshalBinary(data []byte) error {
buf := bytes.NewReader(data)
reader := bufio.NewReader(buf)
_, err := b.ReadFrom(reader)
return err
}
|
go
|
func (b *BitSet) UnmarshalBinary(data []byte) error {
buf := bytes.NewReader(data)
reader := bufio.NewReader(buf)
_, err := b.ReadFrom(reader)
return err
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"UnmarshalBinary",
"(",
"data",
"[",
"]",
"byte",
")",
"error",
"{",
"buf",
":=",
"bytes",
".",
"NewReader",
"(",
"data",
")",
"\n",
"reader",
":=",
"bufio",
".",
"NewReader",
"(",
"buf",
")",
"\n\n",
"_",
",",
"err",
":=",
"b",
".",
"ReadFrom",
"(",
"reader",
")",
"\n\n",
"return",
"err",
"\n",
"}"
] |
// UnmarshalBinary decodes the binary form generated by MarshalBinary.
|
[
"UnmarshalBinary",
"decodes",
"the",
"binary",
"form",
"generated",
"by",
"MarshalBinary",
"."
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L839-L846
|
14,348 |
willf/bitset
|
bitset.go
|
MarshalJSON
|
func (b *BitSet) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBuffer(make([]byte, 0, b.BinaryStorageSize()))
_, err := b.WriteTo(buffer)
if err != nil {
return nil, err
}
// URLEncode all bytes
return json.Marshal(base64Encoding.EncodeToString(buffer.Bytes()))
}
|
go
|
func (b *BitSet) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBuffer(make([]byte, 0, b.BinaryStorageSize()))
_, err := b.WriteTo(buffer)
if err != nil {
return nil, err
}
// URLEncode all bytes
return json.Marshal(base64Encoding.EncodeToString(buffer.Bytes()))
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"MarshalJSON",
"(",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"buffer",
":=",
"bytes",
".",
"NewBuffer",
"(",
"make",
"(",
"[",
"]",
"byte",
",",
"0",
",",
"b",
".",
"BinaryStorageSize",
"(",
")",
")",
")",
"\n",
"_",
",",
"err",
":=",
"b",
".",
"WriteTo",
"(",
"buffer",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"// URLEncode all bytes",
"return",
"json",
".",
"Marshal",
"(",
"base64Encoding",
".",
"EncodeToString",
"(",
"buffer",
".",
"Bytes",
"(",
")",
")",
")",
"\n",
"}"
] |
// MarshalJSON marshals a BitSet as a JSON structure
|
[
"MarshalJSON",
"marshals",
"a",
"BitSet",
"as",
"a",
"JSON",
"structure"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L849-L858
|
14,349 |
willf/bitset
|
bitset.go
|
UnmarshalJSON
|
func (b *BitSet) UnmarshalJSON(data []byte) error {
// Unmarshal as string
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
// URLDecode string
buf, err := base64Encoding.DecodeString(s)
if err != nil {
return err
}
_, err = b.ReadFrom(bytes.NewReader(buf))
return err
}
|
go
|
func (b *BitSet) UnmarshalJSON(data []byte) error {
// Unmarshal as string
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
// URLDecode string
buf, err := base64Encoding.DecodeString(s)
if err != nil {
return err
}
_, err = b.ReadFrom(bytes.NewReader(buf))
return err
}
|
[
"func",
"(",
"b",
"*",
"BitSet",
")",
"UnmarshalJSON",
"(",
"data",
"[",
"]",
"byte",
")",
"error",
"{",
"// Unmarshal as string",
"var",
"s",
"string",
"\n",
"err",
":=",
"json",
".",
"Unmarshal",
"(",
"data",
",",
"&",
"s",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"// URLDecode string",
"buf",
",",
"err",
":=",
"base64Encoding",
".",
"DecodeString",
"(",
"s",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"_",
",",
"err",
"=",
"b",
".",
"ReadFrom",
"(",
"bytes",
".",
"NewReader",
"(",
"buf",
")",
")",
"\n",
"return",
"err",
"\n",
"}"
] |
// UnmarshalJSON unmarshals a BitSet from JSON created using MarshalJSON
|
[
"UnmarshalJSON",
"unmarshals",
"a",
"BitSet",
"from",
"JSON",
"created",
"using",
"MarshalJSON"
] |
77892cd8d53fa6524772426bc444dd2e3b4fb18f
|
https://github.com/willf/bitset/blob/77892cd8d53fa6524772426bc444dd2e3b4fb18f/bitset.go#L861-L877
|
14,350 |
valyala/bytebufferpool
|
pool.go
|
Get
|
func (p *Pool) Get() *ByteBuffer {
v := p.pool.Get()
if v != nil {
return v.(*ByteBuffer)
}
return &ByteBuffer{
B: make([]byte, 0, atomic.LoadUint64(&p.defaultSize)),
}
}
|
go
|
func (p *Pool) Get() *ByteBuffer {
v := p.pool.Get()
if v != nil {
return v.(*ByteBuffer)
}
return &ByteBuffer{
B: make([]byte, 0, atomic.LoadUint64(&p.defaultSize)),
}
}
|
[
"func",
"(",
"p",
"*",
"Pool",
")",
"Get",
"(",
")",
"*",
"ByteBuffer",
"{",
"v",
":=",
"p",
".",
"pool",
".",
"Get",
"(",
")",
"\n",
"if",
"v",
"!=",
"nil",
"{",
"return",
"v",
".",
"(",
"*",
"ByteBuffer",
")",
"\n",
"}",
"\n",
"return",
"&",
"ByteBuffer",
"{",
"B",
":",
"make",
"(",
"[",
"]",
"byte",
",",
"0",
",",
"atomic",
".",
"LoadUint64",
"(",
"&",
"p",
".",
"defaultSize",
")",
")",
",",
"}",
"\n",
"}"
] |
// Get returns new byte buffer with zero length.
//
// The byte buffer may be returned to the pool via Put after the use
// in order to minimize GC overhead.
|
[
"Get",
"returns",
"new",
"byte",
"buffer",
"with",
"zero",
"length",
".",
"The",
"byte",
"buffer",
"may",
"be",
"returned",
"to",
"the",
"pool",
"via",
"Put",
"after",
"the",
"use",
"in",
"order",
"to",
"minimize",
"GC",
"overhead",
"."
] |
cdfbe9377474227bb42120c1e22fd4433e7f69bf
|
https://github.com/valyala/bytebufferpool/blob/cdfbe9377474227bb42120c1e22fd4433e7f69bf/pool.go#L48-L56
|
14,351 |
valyala/bytebufferpool
|
pool.go
|
Put
|
func (p *Pool) Put(b *ByteBuffer) {
idx := index(len(b.B))
if atomic.AddUint64(&p.calls[idx], 1) > calibrateCallsThreshold {
p.calibrate()
}
maxSize := int(atomic.LoadUint64(&p.maxSize))
if maxSize == 0 || cap(b.B) <= maxSize {
b.Reset()
p.pool.Put(b)
}
}
|
go
|
func (p *Pool) Put(b *ByteBuffer) {
idx := index(len(b.B))
if atomic.AddUint64(&p.calls[idx], 1) > calibrateCallsThreshold {
p.calibrate()
}
maxSize := int(atomic.LoadUint64(&p.maxSize))
if maxSize == 0 || cap(b.B) <= maxSize {
b.Reset()
p.pool.Put(b)
}
}
|
[
"func",
"(",
"p",
"*",
"Pool",
")",
"Put",
"(",
"b",
"*",
"ByteBuffer",
")",
"{",
"idx",
":=",
"index",
"(",
"len",
"(",
"b",
".",
"B",
")",
")",
"\n\n",
"if",
"atomic",
".",
"AddUint64",
"(",
"&",
"p",
".",
"calls",
"[",
"idx",
"]",
",",
"1",
")",
">",
"calibrateCallsThreshold",
"{",
"p",
".",
"calibrate",
"(",
")",
"\n",
"}",
"\n\n",
"maxSize",
":=",
"int",
"(",
"atomic",
".",
"LoadUint64",
"(",
"&",
"p",
".",
"maxSize",
")",
")",
"\n",
"if",
"maxSize",
"==",
"0",
"||",
"cap",
"(",
"b",
".",
"B",
")",
"<=",
"maxSize",
"{",
"b",
".",
"Reset",
"(",
")",
"\n",
"p",
".",
"pool",
".",
"Put",
"(",
"b",
")",
"\n",
"}",
"\n",
"}"
] |
// Put releases byte buffer obtained via Get to the pool.
//
// The buffer mustn't be accessed after returning to the pool.
|
[
"Put",
"releases",
"byte",
"buffer",
"obtained",
"via",
"Get",
"to",
"the",
"pool",
".",
"The",
"buffer",
"mustn",
"t",
"be",
"accessed",
"after",
"returning",
"to",
"the",
"pool",
"."
] |
cdfbe9377474227bb42120c1e22fd4433e7f69bf
|
https://github.com/valyala/bytebufferpool/blob/cdfbe9377474227bb42120c1e22fd4433e7f69bf/pool.go#L67-L79
|
14,352 |
valyala/bytebufferpool
|
bytebuffer.go
|
ReadFrom
|
func (b *ByteBuffer) ReadFrom(r io.Reader) (int64, error) {
p := b.B
nStart := int64(len(p))
nMax := int64(cap(p))
n := nStart
if nMax == 0 {
nMax = 64
p = make([]byte, nMax)
} else {
p = p[:nMax]
}
for {
if n == nMax {
nMax *= 2
bNew := make([]byte, nMax)
copy(bNew, p)
p = bNew
}
nn, err := r.Read(p[n:])
n += int64(nn)
if err != nil {
b.B = p[:n]
n -= nStart
if err == io.EOF {
return n, nil
}
return n, err
}
}
}
|
go
|
func (b *ByteBuffer) ReadFrom(r io.Reader) (int64, error) {
p := b.B
nStart := int64(len(p))
nMax := int64(cap(p))
n := nStart
if nMax == 0 {
nMax = 64
p = make([]byte, nMax)
} else {
p = p[:nMax]
}
for {
if n == nMax {
nMax *= 2
bNew := make([]byte, nMax)
copy(bNew, p)
p = bNew
}
nn, err := r.Read(p[n:])
n += int64(nn)
if err != nil {
b.B = p[:n]
n -= nStart
if err == io.EOF {
return n, nil
}
return n, err
}
}
}
|
[
"func",
"(",
"b",
"*",
"ByteBuffer",
")",
"ReadFrom",
"(",
"r",
"io",
".",
"Reader",
")",
"(",
"int64",
",",
"error",
")",
"{",
"p",
":=",
"b",
".",
"B",
"\n",
"nStart",
":=",
"int64",
"(",
"len",
"(",
"p",
")",
")",
"\n",
"nMax",
":=",
"int64",
"(",
"cap",
"(",
"p",
")",
")",
"\n",
"n",
":=",
"nStart",
"\n",
"if",
"nMax",
"==",
"0",
"{",
"nMax",
"=",
"64",
"\n",
"p",
"=",
"make",
"(",
"[",
"]",
"byte",
",",
"nMax",
")",
"\n",
"}",
"else",
"{",
"p",
"=",
"p",
"[",
":",
"nMax",
"]",
"\n",
"}",
"\n",
"for",
"{",
"if",
"n",
"==",
"nMax",
"{",
"nMax",
"*=",
"2",
"\n",
"bNew",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"nMax",
")",
"\n",
"copy",
"(",
"bNew",
",",
"p",
")",
"\n",
"p",
"=",
"bNew",
"\n",
"}",
"\n",
"nn",
",",
"err",
":=",
"r",
".",
"Read",
"(",
"p",
"[",
"n",
":",
"]",
")",
"\n",
"n",
"+=",
"int64",
"(",
"nn",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"b",
".",
"B",
"=",
"p",
"[",
":",
"n",
"]",
"\n",
"n",
"-=",
"nStart",
"\n",
"if",
"err",
"==",
"io",
".",
"EOF",
"{",
"return",
"n",
",",
"nil",
"\n",
"}",
"\n",
"return",
"n",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] |
// ReadFrom implements io.ReaderFrom.
//
// The function appends all the data read from r to b.
|
[
"ReadFrom",
"implements",
"io",
".",
"ReaderFrom",
".",
"The",
"function",
"appends",
"all",
"the",
"data",
"read",
"from",
"r",
"to",
"b",
"."
] |
cdfbe9377474227bb42120c1e22fd4433e7f69bf
|
https://github.com/valyala/bytebufferpool/blob/cdfbe9377474227bb42120c1e22fd4433e7f69bf/bytebuffer.go#L27-L56
|
14,353 |
valyala/bytebufferpool
|
bytebuffer.go
|
WriteTo
|
func (b *ByteBuffer) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write(b.B)
return int64(n), err
}
|
go
|
func (b *ByteBuffer) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write(b.B)
return int64(n), err
}
|
[
"func",
"(",
"b",
"*",
"ByteBuffer",
")",
"WriteTo",
"(",
"w",
"io",
".",
"Writer",
")",
"(",
"int64",
",",
"error",
")",
"{",
"n",
",",
"err",
":=",
"w",
".",
"Write",
"(",
"b",
".",
"B",
")",
"\n",
"return",
"int64",
"(",
"n",
")",
",",
"err",
"\n",
"}"
] |
// WriteTo implements io.WriterTo.
|
[
"WriteTo",
"implements",
"io",
".",
"WriterTo",
"."
] |
cdfbe9377474227bb42120c1e22fd4433e7f69bf
|
https://github.com/valyala/bytebufferpool/blob/cdfbe9377474227bb42120c1e22fd4433e7f69bf/bytebuffer.go#L59-L62
|
14,354 |
valyala/bytebufferpool
|
bytebuffer.go
|
Write
|
func (b *ByteBuffer) Write(p []byte) (int, error) {
b.B = append(b.B, p...)
return len(p), nil
}
|
go
|
func (b *ByteBuffer) Write(p []byte) (int, error) {
b.B = append(b.B, p...)
return len(p), nil
}
|
[
"func",
"(",
"b",
"*",
"ByteBuffer",
")",
"Write",
"(",
"p",
"[",
"]",
"byte",
")",
"(",
"int",
",",
"error",
")",
"{",
"b",
".",
"B",
"=",
"append",
"(",
"b",
".",
"B",
",",
"p",
"...",
")",
"\n",
"return",
"len",
"(",
"p",
")",
",",
"nil",
"\n",
"}"
] |
// Write implements io.Writer - it appends p to ByteBuffer.B
|
[
"Write",
"implements",
"io",
".",
"Writer",
"-",
"it",
"appends",
"p",
"to",
"ByteBuffer",
".",
"B"
] |
cdfbe9377474227bb42120c1e22fd4433e7f69bf
|
https://github.com/valyala/bytebufferpool/blob/cdfbe9377474227bb42120c1e22fd4433e7f69bf/bytebuffer.go#L72-L75
|
14,355 |
valyala/bytebufferpool
|
bytebuffer.go
|
WriteByte
|
func (b *ByteBuffer) WriteByte(c byte) error {
b.B = append(b.B, c)
return nil
}
|
go
|
func (b *ByteBuffer) WriteByte(c byte) error {
b.B = append(b.B, c)
return nil
}
|
[
"func",
"(",
"b",
"*",
"ByteBuffer",
")",
"WriteByte",
"(",
"c",
"byte",
")",
"error",
"{",
"b",
".",
"B",
"=",
"append",
"(",
"b",
".",
"B",
",",
"c",
")",
"\n",
"return",
"nil",
"\n",
"}"
] |
// WriteByte appends the byte c to the buffer.
//
// The purpose of this function is bytes.Buffer compatibility.
//
// The function always returns nil.
|
[
"WriteByte",
"appends",
"the",
"byte",
"c",
"to",
"the",
"buffer",
".",
"The",
"purpose",
"of",
"this",
"function",
"is",
"bytes",
".",
"Buffer",
"compatibility",
".",
"The",
"function",
"always",
"returns",
"nil",
"."
] |
cdfbe9377474227bb42120c1e22fd4433e7f69bf
|
https://github.com/valyala/bytebufferpool/blob/cdfbe9377474227bb42120c1e22fd4433e7f69bf/bytebuffer.go#L82-L85
|
14,356 |
valyala/bytebufferpool
|
bytebuffer.go
|
WriteString
|
func (b *ByteBuffer) WriteString(s string) (int, error) {
b.B = append(b.B, s...)
return len(s), nil
}
|
go
|
func (b *ByteBuffer) WriteString(s string) (int, error) {
b.B = append(b.B, s...)
return len(s), nil
}
|
[
"func",
"(",
"b",
"*",
"ByteBuffer",
")",
"WriteString",
"(",
"s",
"string",
")",
"(",
"int",
",",
"error",
")",
"{",
"b",
".",
"B",
"=",
"append",
"(",
"b",
".",
"B",
",",
"s",
"...",
")",
"\n",
"return",
"len",
"(",
"s",
")",
",",
"nil",
"\n",
"}"
] |
// WriteString appends s to ByteBuffer.B.
|
[
"WriteString",
"appends",
"s",
"to",
"ByteBuffer",
".",
"B",
"."
] |
cdfbe9377474227bb42120c1e22fd4433e7f69bf
|
https://github.com/valyala/bytebufferpool/blob/cdfbe9377474227bb42120c1e22fd4433e7f69bf/bytebuffer.go#L88-L91
|
14,357 |
valyala/bytebufferpool
|
bytebuffer.go
|
Set
|
func (b *ByteBuffer) Set(p []byte) {
b.B = append(b.B[:0], p...)
}
|
go
|
func (b *ByteBuffer) Set(p []byte) {
b.B = append(b.B[:0], p...)
}
|
[
"func",
"(",
"b",
"*",
"ByteBuffer",
")",
"Set",
"(",
"p",
"[",
"]",
"byte",
")",
"{",
"b",
".",
"B",
"=",
"append",
"(",
"b",
".",
"B",
"[",
":",
"0",
"]",
",",
"p",
"...",
")",
"\n",
"}"
] |
// Set sets ByteBuffer.B to p.
|
[
"Set",
"sets",
"ByteBuffer",
".",
"B",
"to",
"p",
"."
] |
cdfbe9377474227bb42120c1e22fd4433e7f69bf
|
https://github.com/valyala/bytebufferpool/blob/cdfbe9377474227bb42120c1e22fd4433e7f69bf/bytebuffer.go#L94-L96
|
14,358 |
valyala/bytebufferpool
|
bytebuffer.go
|
SetString
|
func (b *ByteBuffer) SetString(s string) {
b.B = append(b.B[:0], s...)
}
|
go
|
func (b *ByteBuffer) SetString(s string) {
b.B = append(b.B[:0], s...)
}
|
[
"func",
"(",
"b",
"*",
"ByteBuffer",
")",
"SetString",
"(",
"s",
"string",
")",
"{",
"b",
".",
"B",
"=",
"append",
"(",
"b",
".",
"B",
"[",
":",
"0",
"]",
",",
"s",
"...",
")",
"\n",
"}"
] |
// SetString sets ByteBuffer.B to s.
|
[
"SetString",
"sets",
"ByteBuffer",
".",
"B",
"to",
"s",
"."
] |
cdfbe9377474227bb42120c1e22fd4433e7f69bf
|
https://github.com/valyala/bytebufferpool/blob/cdfbe9377474227bb42120c1e22fd4433e7f69bf/bytebuffer.go#L99-L101
|
14,359 |
kyokomi/emoji
|
emoji.go
|
Print
|
func Print(a ...interface{}) (int, error) {
return fmt.Print(compile(fmt.Sprint(a...)))
}
|
go
|
func Print(a ...interface{}) (int, error) {
return fmt.Print(compile(fmt.Sprint(a...)))
}
|
[
"func",
"Print",
"(",
"a",
"...",
"interface",
"{",
"}",
")",
"(",
"int",
",",
"error",
")",
"{",
"return",
"fmt",
".",
"Print",
"(",
"compile",
"(",
"fmt",
".",
"Sprint",
"(",
"a",
"...",
")",
")",
")",
"\n",
"}"
] |
// Print is fmt.Print which supports emoji
|
[
"Print",
"is",
"fmt",
".",
"Print",
"which",
"supports",
"emoji"
] |
51762253ec2b5dafce2d33adfb0d49121924ee58
|
https://github.com/kyokomi/emoji/blob/51762253ec2b5dafce2d33adfb0d49121924ee58/emoji.go#L91-L93
|
14,360 |
kyokomi/emoji
|
emoji.go
|
Println
|
func Println(a ...interface{}) (int, error) {
return fmt.Println(compile(fmt.Sprint(a...)))
}
|
go
|
func Println(a ...interface{}) (int, error) {
return fmt.Println(compile(fmt.Sprint(a...)))
}
|
[
"func",
"Println",
"(",
"a",
"...",
"interface",
"{",
"}",
")",
"(",
"int",
",",
"error",
")",
"{",
"return",
"fmt",
".",
"Println",
"(",
"compile",
"(",
"fmt",
".",
"Sprint",
"(",
"a",
"...",
")",
")",
")",
"\n",
"}"
] |
// Println is fmt.Println which supports emoji
|
[
"Println",
"is",
"fmt",
".",
"Println",
"which",
"supports",
"emoji"
] |
51762253ec2b5dafce2d33adfb0d49121924ee58
|
https://github.com/kyokomi/emoji/blob/51762253ec2b5dafce2d33adfb0d49121924ee58/emoji.go#L96-L98
|
14,361 |
kyokomi/emoji
|
emoji.go
|
Fprint
|
func Fprint(w io.Writer, a ...interface{}) (int, error) {
return fmt.Fprint(w, compile(fmt.Sprint(a...)))
}
|
go
|
func Fprint(w io.Writer, a ...interface{}) (int, error) {
return fmt.Fprint(w, compile(fmt.Sprint(a...)))
}
|
[
"func",
"Fprint",
"(",
"w",
"io",
".",
"Writer",
",",
"a",
"...",
"interface",
"{",
"}",
")",
"(",
"int",
",",
"error",
")",
"{",
"return",
"fmt",
".",
"Fprint",
"(",
"w",
",",
"compile",
"(",
"fmt",
".",
"Sprint",
"(",
"a",
"...",
")",
")",
")",
"\n",
"}"
] |
// Fprint is fmt.Fprint which supports emoji
|
[
"Fprint",
"is",
"fmt",
".",
"Fprint",
"which",
"supports",
"emoji"
] |
51762253ec2b5dafce2d33adfb0d49121924ee58
|
https://github.com/kyokomi/emoji/blob/51762253ec2b5dafce2d33adfb0d49121924ee58/emoji.go#L106-L108
|
14,362 |
kyokomi/emoji
|
emoji.go
|
Fprintf
|
func Fprintf(w io.Writer, format string, a ...interface{}) (int, error) {
return fmt.Fprint(w, compile(fmt.Sprintf(format, a...)))
}
|
go
|
func Fprintf(w io.Writer, format string, a ...interface{}) (int, error) {
return fmt.Fprint(w, compile(fmt.Sprintf(format, a...)))
}
|
[
"func",
"Fprintf",
"(",
"w",
"io",
".",
"Writer",
",",
"format",
"string",
",",
"a",
"...",
"interface",
"{",
"}",
")",
"(",
"int",
",",
"error",
")",
"{",
"return",
"fmt",
".",
"Fprint",
"(",
"w",
",",
"compile",
"(",
"fmt",
".",
"Sprintf",
"(",
"format",
",",
"a",
"...",
")",
")",
")",
"\n",
"}"
] |
// Fprintf is fmt.Fprintf which supports emoji
|
[
"Fprintf",
"is",
"fmt",
".",
"Fprintf",
"which",
"supports",
"emoji"
] |
51762253ec2b5dafce2d33adfb0d49121924ee58
|
https://github.com/kyokomi/emoji/blob/51762253ec2b5dafce2d33adfb0d49121924ee58/emoji.go#L116-L118
|
14,363 |
kyokomi/emoji
|
emoji.go
|
Sprintf
|
func Sprintf(format string, a ...interface{}) string {
return compile(fmt.Sprintf(format, a...))
}
|
go
|
func Sprintf(format string, a ...interface{}) string {
return compile(fmt.Sprintf(format, a...))
}
|
[
"func",
"Sprintf",
"(",
"format",
"string",
",",
"a",
"...",
"interface",
"{",
"}",
")",
"string",
"{",
"return",
"compile",
"(",
"fmt",
".",
"Sprintf",
"(",
"format",
",",
"a",
"...",
")",
")",
"\n",
"}"
] |
// Sprintf is fmt.Sprintf which supports emoji
|
[
"Sprintf",
"is",
"fmt",
".",
"Sprintf",
"which",
"supports",
"emoji"
] |
51762253ec2b5dafce2d33adfb0d49121924ee58
|
https://github.com/kyokomi/emoji/blob/51762253ec2b5dafce2d33adfb0d49121924ee58/emoji.go#L126-L128
|
14,364 |
kyokomi/emoji
|
emoji.go
|
Errorf
|
func Errorf(format string, a ...interface{}) error {
return errors.New(compile(Sprintf(format, a...)))
}
|
go
|
func Errorf(format string, a ...interface{}) error {
return errors.New(compile(Sprintf(format, a...)))
}
|
[
"func",
"Errorf",
"(",
"format",
"string",
",",
"a",
"...",
"interface",
"{",
"}",
")",
"error",
"{",
"return",
"errors",
".",
"New",
"(",
"compile",
"(",
"Sprintf",
"(",
"format",
",",
"a",
"...",
")",
")",
")",
"\n",
"}"
] |
// Errorf is fmt.Errorf which supports emoji
|
[
"Errorf",
"is",
"fmt",
".",
"Errorf",
"which",
"supports",
"emoji"
] |
51762253ec2b5dafce2d33adfb0d49121924ee58
|
https://github.com/kyokomi/emoji/blob/51762253ec2b5dafce2d33adfb0d49121924ee58/emoji.go#L131-L133
|
14,365 |
opencontainers/runtime-tools
|
generate/config.go
|
InitConfigLinuxResourcesCPU
|
func (g *Generator) InitConfigLinuxResourcesCPU() {
g.initConfigLinuxResources()
if g.Config.Linux.Resources.CPU == nil {
g.Config.Linux.Resources.CPU = &rspec.LinuxCPU{}
}
}
|
go
|
func (g *Generator) InitConfigLinuxResourcesCPU() {
g.initConfigLinuxResources()
if g.Config.Linux.Resources.CPU == nil {
g.Config.Linux.Resources.CPU = &rspec.LinuxCPU{}
}
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"InitConfigLinuxResourcesCPU",
"(",
")",
"{",
"g",
".",
"initConfigLinuxResources",
"(",
")",
"\n",
"if",
"g",
".",
"Config",
".",
"Linux",
".",
"Resources",
".",
"CPU",
"==",
"nil",
"{",
"g",
".",
"Config",
".",
"Linux",
".",
"Resources",
".",
"CPU",
"=",
"&",
"rspec",
".",
"LinuxCPU",
"{",
"}",
"\n",
"}",
"\n",
"}"
] |
// InitConfigLinuxResourcesCPU initializes CPU of Linux resources
|
[
"InitConfigLinuxResourcesCPU",
"initializes",
"CPU",
"of",
"Linux",
"resources"
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/config.go#L98-L103
|
14,366 |
opencontainers/runtime-tools
|
cgroups/cgroups_v1.go
|
GetPidsData
|
func (cg *CgroupV1) GetPidsData(pid int, cgPath string) (*rspec.LinuxPids, error) {
if filepath.IsAbs(cgPath) {
path := filepath.Join(cg.MountPath, "pids", cgPath)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return nil, specerror.NewError(specerror.CgroupsAbsPathRelToMount, fmt.Errorf("In the case of an absolute path, the runtime MUST take the path to be relative to the cgroups mount point"), rspec.Version)
}
return nil, err
}
}
lp := &rspec.LinuxPids{}
fileName := strings.Join([]string{"pids", "max"}, ".")
filePath := filepath.Join(cg.MountPath, "pids", cgPath, fileName)
if !filepath.IsAbs(cgPath) {
subPath, err := GetSubsystemPath(pid, "pids")
if err != nil {
return nil, err
}
if !strings.Contains(subPath, cgPath) {
return nil, fmt.Errorf("cgroup subsystem %s is not mounted as expected", "pids")
}
filePath = filepath.Join(cg.MountPath, "pids", subPath, fileName)
}
contents, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
res, err := strconv.ParseInt(strings.TrimSpace(string(contents)), 10, 64)
if err != nil {
if os.IsNotExist(err) {
return nil, specerror.NewError(specerror.CgroupsPathAttach, fmt.Errorf("The runtime MUST consistently attach to the same place in the cgroups hierarchy given the same value of `cgroupsPath`"), rspec.Version)
}
return nil, err
}
lp.Limit = res
return lp, nil
}
|
go
|
func (cg *CgroupV1) GetPidsData(pid int, cgPath string) (*rspec.LinuxPids, error) {
if filepath.IsAbs(cgPath) {
path := filepath.Join(cg.MountPath, "pids", cgPath)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return nil, specerror.NewError(specerror.CgroupsAbsPathRelToMount, fmt.Errorf("In the case of an absolute path, the runtime MUST take the path to be relative to the cgroups mount point"), rspec.Version)
}
return nil, err
}
}
lp := &rspec.LinuxPids{}
fileName := strings.Join([]string{"pids", "max"}, ".")
filePath := filepath.Join(cg.MountPath, "pids", cgPath, fileName)
if !filepath.IsAbs(cgPath) {
subPath, err := GetSubsystemPath(pid, "pids")
if err != nil {
return nil, err
}
if !strings.Contains(subPath, cgPath) {
return nil, fmt.Errorf("cgroup subsystem %s is not mounted as expected", "pids")
}
filePath = filepath.Join(cg.MountPath, "pids", subPath, fileName)
}
contents, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
res, err := strconv.ParseInt(strings.TrimSpace(string(contents)), 10, 64)
if err != nil {
if os.IsNotExist(err) {
return nil, specerror.NewError(specerror.CgroupsPathAttach, fmt.Errorf("The runtime MUST consistently attach to the same place in the cgroups hierarchy given the same value of `cgroupsPath`"), rspec.Version)
}
return nil, err
}
lp.Limit = res
return lp, nil
}
|
[
"func",
"(",
"cg",
"*",
"CgroupV1",
")",
"GetPidsData",
"(",
"pid",
"int",
",",
"cgPath",
"string",
")",
"(",
"*",
"rspec",
".",
"LinuxPids",
",",
"error",
")",
"{",
"if",
"filepath",
".",
"IsAbs",
"(",
"cgPath",
")",
"{",
"path",
":=",
"filepath",
".",
"Join",
"(",
"cg",
".",
"MountPath",
",",
"\"",
"\"",
",",
"cgPath",
")",
"\n",
"if",
"_",
",",
"err",
":=",
"os",
".",
"Stat",
"(",
"path",
")",
";",
"err",
"!=",
"nil",
"{",
"if",
"os",
".",
"IsNotExist",
"(",
"err",
")",
"{",
"return",
"nil",
",",
"specerror",
".",
"NewError",
"(",
"specerror",
".",
"CgroupsAbsPathRelToMount",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
",",
"rspec",
".",
"Version",
")",
"\n",
"}",
"\n",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"lp",
":=",
"&",
"rspec",
".",
"LinuxPids",
"{",
"}",
"\n",
"fileName",
":=",
"strings",
".",
"Join",
"(",
"[",
"]",
"string",
"{",
"\"",
"\"",
",",
"\"",
"\"",
"}",
",",
"\"",
"\"",
")",
"\n",
"filePath",
":=",
"filepath",
".",
"Join",
"(",
"cg",
".",
"MountPath",
",",
"\"",
"\"",
",",
"cgPath",
",",
"fileName",
")",
"\n",
"if",
"!",
"filepath",
".",
"IsAbs",
"(",
"cgPath",
")",
"{",
"subPath",
",",
"err",
":=",
"GetSubsystemPath",
"(",
"pid",
",",
"\"",
"\"",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"if",
"!",
"strings",
".",
"Contains",
"(",
"subPath",
",",
"cgPath",
")",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"filePath",
"=",
"filepath",
".",
"Join",
"(",
"cg",
".",
"MountPath",
",",
"\"",
"\"",
",",
"subPath",
",",
"fileName",
")",
"\n",
"}",
"\n",
"contents",
",",
"err",
":=",
"ioutil",
".",
"ReadFile",
"(",
"filePath",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"res",
",",
"err",
":=",
"strconv",
".",
"ParseInt",
"(",
"strings",
".",
"TrimSpace",
"(",
"string",
"(",
"contents",
")",
")",
",",
"10",
",",
"64",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"if",
"os",
".",
"IsNotExist",
"(",
"err",
")",
"{",
"return",
"nil",
",",
"specerror",
".",
"NewError",
"(",
"specerror",
".",
"CgroupsPathAttach",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
",",
"rspec",
".",
"Version",
")",
"\n",
"}",
"\n\n",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"lp",
".",
"Limit",
"=",
"res",
"\n\n",
"return",
"lp",
",",
"nil",
"\n",
"}"
] |
// GetPidsData gets cgroup pids data
|
[
"GetPidsData",
"gets",
"cgroup",
"pids",
"data"
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/cgroups/cgroups_v1.go#L641-L679
|
14,367 |
opencontainers/runtime-tools
|
generate/seccomp/syscall_compare.go
|
decideCourseOfAction
|
func decideCourseOfAction(newSyscall *rspec.LinuxSyscall, syscalls []rspec.LinuxSyscall) (string, error) {
ruleForSyscallAlreadyExists := false
var sliceOfDeterminedActions []string
for i, syscall := range syscalls {
if sameName(&syscall, newSyscall) {
ruleForSyscallAlreadyExists = true
if identical(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, nothing)
}
if sameAction(newSyscall, &syscall) {
if bothHaveArgs(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, seccompAppend)
}
if onlyOneHasArgs(newSyscall, &syscall) {
if firstParamOnlyHasArgs(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, "overwrite:"+strconv.Itoa(i))
} else {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, nothing)
}
}
}
if !sameAction(newSyscall, &syscall) {
if bothHaveArgs(newSyscall, &syscall) {
if sameArgs(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, "overwrite:"+strconv.Itoa(i))
}
if !sameArgs(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, seccompAppend)
}
}
if onlyOneHasArgs(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, seccompAppend)
}
if neitherHasArgs(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, "overwrite:"+strconv.Itoa(i))
}
}
}
}
if !ruleForSyscallAlreadyExists {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, seccompAppend)
}
// Nothing has highest priority
for _, determinedAction := range sliceOfDeterminedActions {
if determinedAction == nothing {
return determinedAction, nil
}
}
// Overwrite has second highest priority
for _, determinedAction := range sliceOfDeterminedActions {
if strings.Contains(determinedAction, seccompOverwrite) {
return determinedAction, nil
}
}
// Append has the lowest priority
for _, determinedAction := range sliceOfDeterminedActions {
if determinedAction == seccompAppend {
return determinedAction, nil
}
}
return "", fmt.Errorf("Trouble determining action: %s", sliceOfDeterminedActions)
}
|
go
|
func decideCourseOfAction(newSyscall *rspec.LinuxSyscall, syscalls []rspec.LinuxSyscall) (string, error) {
ruleForSyscallAlreadyExists := false
var sliceOfDeterminedActions []string
for i, syscall := range syscalls {
if sameName(&syscall, newSyscall) {
ruleForSyscallAlreadyExists = true
if identical(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, nothing)
}
if sameAction(newSyscall, &syscall) {
if bothHaveArgs(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, seccompAppend)
}
if onlyOneHasArgs(newSyscall, &syscall) {
if firstParamOnlyHasArgs(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, "overwrite:"+strconv.Itoa(i))
} else {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, nothing)
}
}
}
if !sameAction(newSyscall, &syscall) {
if bothHaveArgs(newSyscall, &syscall) {
if sameArgs(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, "overwrite:"+strconv.Itoa(i))
}
if !sameArgs(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, seccompAppend)
}
}
if onlyOneHasArgs(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, seccompAppend)
}
if neitherHasArgs(newSyscall, &syscall) {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, "overwrite:"+strconv.Itoa(i))
}
}
}
}
if !ruleForSyscallAlreadyExists {
sliceOfDeterminedActions = append(sliceOfDeterminedActions, seccompAppend)
}
// Nothing has highest priority
for _, determinedAction := range sliceOfDeterminedActions {
if determinedAction == nothing {
return determinedAction, nil
}
}
// Overwrite has second highest priority
for _, determinedAction := range sliceOfDeterminedActions {
if strings.Contains(determinedAction, seccompOverwrite) {
return determinedAction, nil
}
}
// Append has the lowest priority
for _, determinedAction := range sliceOfDeterminedActions {
if determinedAction == seccompAppend {
return determinedAction, nil
}
}
return "", fmt.Errorf("Trouble determining action: %s", sliceOfDeterminedActions)
}
|
[
"func",
"decideCourseOfAction",
"(",
"newSyscall",
"*",
"rspec",
".",
"LinuxSyscall",
",",
"syscalls",
"[",
"]",
"rspec",
".",
"LinuxSyscall",
")",
"(",
"string",
",",
"error",
")",
"{",
"ruleForSyscallAlreadyExists",
":=",
"false",
"\n\n",
"var",
"sliceOfDeterminedActions",
"[",
"]",
"string",
"\n",
"for",
"i",
",",
"syscall",
":=",
"range",
"syscalls",
"{",
"if",
"sameName",
"(",
"&",
"syscall",
",",
"newSyscall",
")",
"{",
"ruleForSyscallAlreadyExists",
"=",
"true",
"\n\n",
"if",
"identical",
"(",
"newSyscall",
",",
"&",
"syscall",
")",
"{",
"sliceOfDeterminedActions",
"=",
"append",
"(",
"sliceOfDeterminedActions",
",",
"nothing",
")",
"\n",
"}",
"\n\n",
"if",
"sameAction",
"(",
"newSyscall",
",",
"&",
"syscall",
")",
"{",
"if",
"bothHaveArgs",
"(",
"newSyscall",
",",
"&",
"syscall",
")",
"{",
"sliceOfDeterminedActions",
"=",
"append",
"(",
"sliceOfDeterminedActions",
",",
"seccompAppend",
")",
"\n",
"}",
"\n",
"if",
"onlyOneHasArgs",
"(",
"newSyscall",
",",
"&",
"syscall",
")",
"{",
"if",
"firstParamOnlyHasArgs",
"(",
"newSyscall",
",",
"&",
"syscall",
")",
"{",
"sliceOfDeterminedActions",
"=",
"append",
"(",
"sliceOfDeterminedActions",
",",
"\"",
"\"",
"+",
"strconv",
".",
"Itoa",
"(",
"i",
")",
")",
"\n",
"}",
"else",
"{",
"sliceOfDeterminedActions",
"=",
"append",
"(",
"sliceOfDeterminedActions",
",",
"nothing",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"!",
"sameAction",
"(",
"newSyscall",
",",
"&",
"syscall",
")",
"{",
"if",
"bothHaveArgs",
"(",
"newSyscall",
",",
"&",
"syscall",
")",
"{",
"if",
"sameArgs",
"(",
"newSyscall",
",",
"&",
"syscall",
")",
"{",
"sliceOfDeterminedActions",
"=",
"append",
"(",
"sliceOfDeterminedActions",
",",
"\"",
"\"",
"+",
"strconv",
".",
"Itoa",
"(",
"i",
")",
")",
"\n",
"}",
"\n",
"if",
"!",
"sameArgs",
"(",
"newSyscall",
",",
"&",
"syscall",
")",
"{",
"sliceOfDeterminedActions",
"=",
"append",
"(",
"sliceOfDeterminedActions",
",",
"seccompAppend",
")",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"onlyOneHasArgs",
"(",
"newSyscall",
",",
"&",
"syscall",
")",
"{",
"sliceOfDeterminedActions",
"=",
"append",
"(",
"sliceOfDeterminedActions",
",",
"seccompAppend",
")",
"\n",
"}",
"\n",
"if",
"neitherHasArgs",
"(",
"newSyscall",
",",
"&",
"syscall",
")",
"{",
"sliceOfDeterminedActions",
"=",
"append",
"(",
"sliceOfDeterminedActions",
",",
"\"",
"\"",
"+",
"strconv",
".",
"Itoa",
"(",
"i",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"!",
"ruleForSyscallAlreadyExists",
"{",
"sliceOfDeterminedActions",
"=",
"append",
"(",
"sliceOfDeterminedActions",
",",
"seccompAppend",
")",
"\n",
"}",
"\n\n",
"// Nothing has highest priority",
"for",
"_",
",",
"determinedAction",
":=",
"range",
"sliceOfDeterminedActions",
"{",
"if",
"determinedAction",
"==",
"nothing",
"{",
"return",
"determinedAction",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"// Overwrite has second highest priority",
"for",
"_",
",",
"determinedAction",
":=",
"range",
"sliceOfDeterminedActions",
"{",
"if",
"strings",
".",
"Contains",
"(",
"determinedAction",
",",
"seccompOverwrite",
")",
"{",
"return",
"determinedAction",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"// Append has the lowest priority",
"for",
"_",
",",
"determinedAction",
":=",
"range",
"sliceOfDeterminedActions",
"{",
"if",
"determinedAction",
"==",
"seccompAppend",
"{",
"return",
"determinedAction",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"\"",
"\"",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"sliceOfDeterminedActions",
")",
"\n",
"}"
] |
// Determine if a new syscall rule should be appended, overwrite an existing rule
// or if no action should be taken at all
|
[
"Determine",
"if",
"a",
"new",
"syscall",
"rule",
"should",
"be",
"appended",
"overwrite",
"an",
"existing",
"rule",
"or",
"if",
"no",
"action",
"should",
"be",
"taken",
"at",
"all"
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/seccomp/syscall_compare.go#L14-L84
|
14,368 |
opencontainers/runtime-tools
|
cgroups/cgroups_v2.go
|
GetPidsData
|
func GetPidsData(pid int, cgPath string) (*rspec.LinuxPids, error) {
return nil, fmt.Errorf("unimplemented yet")
}
|
go
|
func GetPidsData(pid int, cgPath string) (*rspec.LinuxPids, error) {
return nil, fmt.Errorf("unimplemented yet")
}
|
[
"func",
"GetPidsData",
"(",
"pid",
"int",
",",
"cgPath",
"string",
")",
"(",
"*",
"rspec",
".",
"LinuxPids",
",",
"error",
")",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}"
] |
// GetPidsData gets cgroup pid ints data
|
[
"GetPidsData",
"gets",
"cgroup",
"pid",
"ints",
"data"
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/cgroups/cgroups_v2.go#L45-L47
|
14,369 |
opencontainers/runtime-tools
|
validate/validate_linux.go
|
LastCap
|
func LastCap() capability.Cap {
last := capability.CAP_LAST_CAP
// hack for RHEL6 which has no /proc/sys/kernel/cap_last_cap
if last == capability.Cap(63) {
last = capability.CAP_BLOCK_SUSPEND
}
return last
}
|
go
|
func LastCap() capability.Cap {
last := capability.CAP_LAST_CAP
// hack for RHEL6 which has no /proc/sys/kernel/cap_last_cap
if last == capability.Cap(63) {
last = capability.CAP_BLOCK_SUSPEND
}
return last
}
|
[
"func",
"LastCap",
"(",
")",
"capability",
".",
"Cap",
"{",
"last",
":=",
"capability",
".",
"CAP_LAST_CAP",
"\n",
"// hack for RHEL6 which has no /proc/sys/kernel/cap_last_cap",
"if",
"last",
"==",
"capability",
".",
"Cap",
"(",
"63",
")",
"{",
"last",
"=",
"capability",
".",
"CAP_BLOCK_SUSPEND",
"\n",
"}",
"\n\n",
"return",
"last",
"\n",
"}"
] |
// LastCap return last cap of system
|
[
"LastCap",
"return",
"last",
"cap",
"of",
"system"
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/validate/validate_linux.go#L24-L32
|
14,370 |
opencontainers/runtime-tools
|
generate/seccomp/parse_remove.go
|
RemoveAllSeccompRules
|
func RemoveAllSeccompRules(config *rspec.LinuxSeccomp) error {
if config == nil {
return fmt.Errorf("Cannot remove action from nil Seccomp pointer")
}
newSyscallSlice := []rspec.LinuxSyscall{}
config.Syscalls = newSyscallSlice
return nil
}
|
go
|
func RemoveAllSeccompRules(config *rspec.LinuxSeccomp) error {
if config == nil {
return fmt.Errorf("Cannot remove action from nil Seccomp pointer")
}
newSyscallSlice := []rspec.LinuxSyscall{}
config.Syscalls = newSyscallSlice
return nil
}
|
[
"func",
"RemoveAllSeccompRules",
"(",
"config",
"*",
"rspec",
".",
"LinuxSeccomp",
")",
"error",
"{",
"if",
"config",
"==",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"newSyscallSlice",
":=",
"[",
"]",
"rspec",
".",
"LinuxSyscall",
"{",
"}",
"\n",
"config",
".",
"Syscalls",
"=",
"newSyscallSlice",
"\n",
"return",
"nil",
"\n",
"}"
] |
// RemoveAllSeccompRules removes all seccomp syscall rules
|
[
"RemoveAllSeccompRules",
"removes",
"all",
"seccomp",
"syscall",
"rules"
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/seccomp/parse_remove.go#L30-L37
|
14,371 |
opencontainers/runtime-tools
|
generate/seccomp/parse_remove.go
|
RemoveAllMatchingRules
|
func RemoveAllMatchingRules(config *rspec.LinuxSeccomp, seccompAction rspec.LinuxSeccompAction) error {
if config == nil {
return fmt.Errorf("Cannot remove action from nil Seccomp pointer")
}
for _, syscall := range config.Syscalls {
if reflect.DeepEqual(syscall.Action, seccompAction) {
RemoveAction(strings.Join(syscall.Names, ","), config)
}
}
return nil
}
|
go
|
func RemoveAllMatchingRules(config *rspec.LinuxSeccomp, seccompAction rspec.LinuxSeccompAction) error {
if config == nil {
return fmt.Errorf("Cannot remove action from nil Seccomp pointer")
}
for _, syscall := range config.Syscalls {
if reflect.DeepEqual(syscall.Action, seccompAction) {
RemoveAction(strings.Join(syscall.Names, ","), config)
}
}
return nil
}
|
[
"func",
"RemoveAllMatchingRules",
"(",
"config",
"*",
"rspec",
".",
"LinuxSeccomp",
",",
"seccompAction",
"rspec",
".",
"LinuxSeccompAction",
")",
"error",
"{",
"if",
"config",
"==",
"nil",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"syscall",
":=",
"range",
"config",
".",
"Syscalls",
"{",
"if",
"reflect",
".",
"DeepEqual",
"(",
"syscall",
".",
"Action",
",",
"seccompAction",
")",
"{",
"RemoveAction",
"(",
"strings",
".",
"Join",
"(",
"syscall",
".",
"Names",
",",
"\"",
"\"",
")",
",",
"config",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] |
// RemoveAllMatchingRules will remove any syscall rules that match the specified action
|
[
"RemoveAllMatchingRules",
"will",
"remove",
"any",
"syscall",
"rules",
"that",
"match",
"the",
"specified",
"action"
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/seccomp/parse_remove.go#L40-L52
|
14,372 |
opencontainers/runtime-tools
|
cgroups/cgroups.go
|
FindCgroup
|
func FindCgroup() (Cgroup, error) {
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
return nil, err
}
defer f.Close()
cgroupv2 := false
scanner := bufio.NewScanner(f)
for scanner.Scan() {
text := scanner.Text()
fields := strings.Split(text, " ")
// Safe as mountinfo encodes mountpoints with spaces as \040.
index := strings.Index(text, " - ")
postSeparatorFields := strings.Split(text[index+3:], " ")
numPostFields := len(postSeparatorFields)
// This is an error as we can't detect if the mount is for "cgroup"
if numPostFields == 0 {
return nil, fmt.Errorf("Found no fields post '-' in %q", text)
}
if postSeparatorFields[0] == "cgroup" {
// No need to parse the rest of the postSeparatorFields
cg := &CgroupV1{
MountPath: filepath.Dir(fields[4]),
}
return cg, nil
} else if postSeparatorFields[0] == "cgroup2" {
cgroupv2 = true
continue
//TODO cgroupv2 unimplemented
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
if cgroupv2 {
return nil, fmt.Errorf("cgroupv2 is not supported yet")
}
return nil, fmt.Errorf("cgroup is not found")
}
|
go
|
func FindCgroup() (Cgroup, error) {
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
return nil, err
}
defer f.Close()
cgroupv2 := false
scanner := bufio.NewScanner(f)
for scanner.Scan() {
text := scanner.Text()
fields := strings.Split(text, " ")
// Safe as mountinfo encodes mountpoints with spaces as \040.
index := strings.Index(text, " - ")
postSeparatorFields := strings.Split(text[index+3:], " ")
numPostFields := len(postSeparatorFields)
// This is an error as we can't detect if the mount is for "cgroup"
if numPostFields == 0 {
return nil, fmt.Errorf("Found no fields post '-' in %q", text)
}
if postSeparatorFields[0] == "cgroup" {
// No need to parse the rest of the postSeparatorFields
cg := &CgroupV1{
MountPath: filepath.Dir(fields[4]),
}
return cg, nil
} else if postSeparatorFields[0] == "cgroup2" {
cgroupv2 = true
continue
//TODO cgroupv2 unimplemented
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
if cgroupv2 {
return nil, fmt.Errorf("cgroupv2 is not supported yet")
}
return nil, fmt.Errorf("cgroup is not found")
}
|
[
"func",
"FindCgroup",
"(",
")",
"(",
"Cgroup",
",",
"error",
")",
"{",
"f",
",",
"err",
":=",
"os",
".",
"Open",
"(",
"\"",
"\"",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"defer",
"f",
".",
"Close",
"(",
")",
"\n\n",
"cgroupv2",
":=",
"false",
"\n",
"scanner",
":=",
"bufio",
".",
"NewScanner",
"(",
"f",
")",
"\n",
"for",
"scanner",
".",
"Scan",
"(",
")",
"{",
"text",
":=",
"scanner",
".",
"Text",
"(",
")",
"\n",
"fields",
":=",
"strings",
".",
"Split",
"(",
"text",
",",
"\"",
"\"",
")",
"\n",
"// Safe as mountinfo encodes mountpoints with spaces as \\040.",
"index",
":=",
"strings",
".",
"Index",
"(",
"text",
",",
"\"",
"\"",
")",
"\n",
"postSeparatorFields",
":=",
"strings",
".",
"Split",
"(",
"text",
"[",
"index",
"+",
"3",
":",
"]",
",",
"\"",
"\"",
")",
"\n",
"numPostFields",
":=",
"len",
"(",
"postSeparatorFields",
")",
"\n\n",
"// This is an error as we can't detect if the mount is for \"cgroup\"",
"if",
"numPostFields",
"==",
"0",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"text",
")",
"\n",
"}",
"\n\n",
"if",
"postSeparatorFields",
"[",
"0",
"]",
"==",
"\"",
"\"",
"{",
"// No need to parse the rest of the postSeparatorFields",
"cg",
":=",
"&",
"CgroupV1",
"{",
"MountPath",
":",
"filepath",
".",
"Dir",
"(",
"fields",
"[",
"4",
"]",
")",
",",
"}",
"\n",
"return",
"cg",
",",
"nil",
"\n",
"}",
"else",
"if",
"postSeparatorFields",
"[",
"0",
"]",
"==",
"\"",
"\"",
"{",
"cgroupv2",
"=",
"true",
"\n",
"continue",
"\n",
"//TODO cgroupv2 unimplemented",
"}",
"\n",
"}",
"\n\n",
"if",
"err",
":=",
"scanner",
".",
"Err",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"cgroupv2",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
")",
"\n",
"}"
] |
// FindCgroup gets cgroup root mountpoint
|
[
"FindCgroup",
"gets",
"cgroup",
"root",
"mountpoint"
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/cgroups/cgroups.go#L33-L77
|
14,373 |
opencontainers/runtime-tools
|
cgroups/cgroups.go
|
GetSubsystemPath
|
func GetSubsystemPath(pid int, subsystem string) (string, error) {
contents, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cgroup", pid))
if err != nil {
return "", err
}
parts := strings.Split(strings.TrimSpace(string(contents)), "\n")
for _, part := range parts {
elem := strings.SplitN(part, ":", 3)
if len(elem) < 3 {
continue
}
subelems := strings.Split(elem[1], ",")
for _, subelem := range subelems {
if subelem == subsystem {
return elem[2], nil
}
}
}
return "", fmt.Errorf("subsystem %s not found", subsystem)
}
|
go
|
func GetSubsystemPath(pid int, subsystem string) (string, error) {
contents, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cgroup", pid))
if err != nil {
return "", err
}
parts := strings.Split(strings.TrimSpace(string(contents)), "\n")
for _, part := range parts {
elem := strings.SplitN(part, ":", 3)
if len(elem) < 3 {
continue
}
subelems := strings.Split(elem[1], ",")
for _, subelem := range subelems {
if subelem == subsystem {
return elem[2], nil
}
}
}
return "", fmt.Errorf("subsystem %s not found", subsystem)
}
|
[
"func",
"GetSubsystemPath",
"(",
"pid",
"int",
",",
"subsystem",
"string",
")",
"(",
"string",
",",
"error",
")",
"{",
"contents",
",",
"err",
":=",
"ioutil",
".",
"ReadFile",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"pid",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"parts",
":=",
"strings",
".",
"Split",
"(",
"strings",
".",
"TrimSpace",
"(",
"string",
"(",
"contents",
")",
")",
",",
"\"",
"\\n",
"\"",
")",
"\n",
"for",
"_",
",",
"part",
":=",
"range",
"parts",
"{",
"elem",
":=",
"strings",
".",
"SplitN",
"(",
"part",
",",
"\"",
"\"",
",",
"3",
")",
"\n",
"if",
"len",
"(",
"elem",
")",
"<",
"3",
"{",
"continue",
"\n",
"}",
"\n",
"subelems",
":=",
"strings",
".",
"Split",
"(",
"elem",
"[",
"1",
"]",
",",
"\"",
"\"",
")",
"\n",
"for",
"_",
",",
"subelem",
":=",
"range",
"subelems",
"{",
"if",
"subelem",
"==",
"subsystem",
"{",
"return",
"elem",
"[",
"2",
"]",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"\"",
"\"",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"subsystem",
")",
"\n",
"}"
] |
// GetSubsystemPath gets path of subsystem
|
[
"GetSubsystemPath",
"gets",
"path",
"of",
"subsystem"
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/cgroups/cgroups.go#L80-L101
|
14,374 |
opencontainers/runtime-tools
|
generate/generate.go
|
NewFromFile
|
func NewFromFile(path string) (Generator, error) {
cf, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return Generator{}, fmt.Errorf("template configuration at %s not found", path)
}
return Generator{}, err
}
defer cf.Close()
return NewFromTemplate(cf)
}
|
go
|
func NewFromFile(path string) (Generator, error) {
cf, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return Generator{}, fmt.Errorf("template configuration at %s not found", path)
}
return Generator{}, err
}
defer cf.Close()
return NewFromTemplate(cf)
}
|
[
"func",
"NewFromFile",
"(",
"path",
"string",
")",
"(",
"Generator",
",",
"error",
")",
"{",
"cf",
",",
"err",
":=",
"os",
".",
"Open",
"(",
"path",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"if",
"os",
".",
"IsNotExist",
"(",
"err",
")",
"{",
"return",
"Generator",
"{",
"}",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"path",
")",
"\n",
"}",
"\n",
"return",
"Generator",
"{",
"}",
",",
"err",
"\n",
"}",
"\n",
"defer",
"cf",
".",
"Close",
"(",
")",
"\n\n",
"return",
"NewFromTemplate",
"(",
"cf",
")",
"\n",
"}"
] |
// NewFromFile loads the template specified in a file into a
// configuration Generator.
|
[
"NewFromFile",
"loads",
"the",
"template",
"specified",
"in",
"a",
"file",
"into",
"a",
"configuration",
"Generator",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L270-L281
|
14,375 |
opencontainers/runtime-tools
|
generate/generate.go
|
NewFromTemplate
|
func NewFromTemplate(r io.Reader) (Generator, error) {
var config rspec.Spec
if err := json.NewDecoder(r).Decode(&config); err != nil {
return Generator{}, err
}
envCache := map[string]int{}
if config.Process != nil {
envCache = createEnvCacheMap(config.Process.Env)
}
return Generator{
Config: &config,
envMap: envCache,
}, nil
}
|
go
|
func NewFromTemplate(r io.Reader) (Generator, error) {
var config rspec.Spec
if err := json.NewDecoder(r).Decode(&config); err != nil {
return Generator{}, err
}
envCache := map[string]int{}
if config.Process != nil {
envCache = createEnvCacheMap(config.Process.Env)
}
return Generator{
Config: &config,
envMap: envCache,
}, nil
}
|
[
"func",
"NewFromTemplate",
"(",
"r",
"io",
".",
"Reader",
")",
"(",
"Generator",
",",
"error",
")",
"{",
"var",
"config",
"rspec",
".",
"Spec",
"\n",
"if",
"err",
":=",
"json",
".",
"NewDecoder",
"(",
"r",
")",
".",
"Decode",
"(",
"&",
"config",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"Generator",
"{",
"}",
",",
"err",
"\n",
"}",
"\n\n",
"envCache",
":=",
"map",
"[",
"string",
"]",
"int",
"{",
"}",
"\n",
"if",
"config",
".",
"Process",
"!=",
"nil",
"{",
"envCache",
"=",
"createEnvCacheMap",
"(",
"config",
".",
"Process",
".",
"Env",
")",
"\n",
"}",
"\n\n",
"return",
"Generator",
"{",
"Config",
":",
"&",
"config",
",",
"envMap",
":",
"envCache",
",",
"}",
",",
"nil",
"\n",
"}"
] |
// NewFromTemplate loads the template from io.Reader into a
// configuration Generator.
|
[
"NewFromTemplate",
"loads",
"the",
"template",
"from",
"io",
".",
"Reader",
"into",
"a",
"configuration",
"Generator",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L285-L300
|
14,376 |
opencontainers/runtime-tools
|
generate/generate.go
|
createEnvCacheMap
|
func createEnvCacheMap(env []string) map[string]int {
envMap := make(map[string]int, len(env))
for i, val := range env {
envMap[val] = i
}
return envMap
}
|
go
|
func createEnvCacheMap(env []string) map[string]int {
envMap := make(map[string]int, len(env))
for i, val := range env {
envMap[val] = i
}
return envMap
}
|
[
"func",
"createEnvCacheMap",
"(",
"env",
"[",
"]",
"string",
")",
"map",
"[",
"string",
"]",
"int",
"{",
"envMap",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"int",
",",
"len",
"(",
"env",
")",
")",
"\n",
"for",
"i",
",",
"val",
":=",
"range",
"env",
"{",
"envMap",
"[",
"val",
"]",
"=",
"i",
"\n",
"}",
"\n",
"return",
"envMap",
"\n",
"}"
] |
// createEnvCacheMap creates a hash map with the ENV variables given by the config
|
[
"createEnvCacheMap",
"creates",
"a",
"hash",
"map",
"with",
"the",
"ENV",
"variables",
"given",
"by",
"the",
"config"
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L303-L309
|
14,377 |
opencontainers/runtime-tools
|
generate/generate.go
|
Save
|
func (g *Generator) Save(w io.Writer, exportOpts ExportOptions) (err error) {
var data []byte
if g.Config.Linux != nil {
buf, err := json.Marshal(g.Config.Linux)
if err != nil {
return err
}
if string(buf) == "{}" {
g.Config.Linux = nil
}
}
if exportOpts.Seccomp {
data, err = json.MarshalIndent(g.Config.Linux.Seccomp, "", "\t")
} else {
data, err = json.MarshalIndent(g.Config, "", "\t")
}
if err != nil {
return err
}
_, err = w.Write(data)
if err != nil {
return err
}
return nil
}
|
go
|
func (g *Generator) Save(w io.Writer, exportOpts ExportOptions) (err error) {
var data []byte
if g.Config.Linux != nil {
buf, err := json.Marshal(g.Config.Linux)
if err != nil {
return err
}
if string(buf) == "{}" {
g.Config.Linux = nil
}
}
if exportOpts.Seccomp {
data, err = json.MarshalIndent(g.Config.Linux.Seccomp, "", "\t")
} else {
data, err = json.MarshalIndent(g.Config, "", "\t")
}
if err != nil {
return err
}
_, err = w.Write(data)
if err != nil {
return err
}
return nil
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"Save",
"(",
"w",
"io",
".",
"Writer",
",",
"exportOpts",
"ExportOptions",
")",
"(",
"err",
"error",
")",
"{",
"var",
"data",
"[",
"]",
"byte",
"\n\n",
"if",
"g",
".",
"Config",
".",
"Linux",
"!=",
"nil",
"{",
"buf",
",",
"err",
":=",
"json",
".",
"Marshal",
"(",
"g",
".",
"Config",
".",
"Linux",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"if",
"string",
"(",
"buf",
")",
"==",
"\"",
"\"",
"{",
"g",
".",
"Config",
".",
"Linux",
"=",
"nil",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"exportOpts",
".",
"Seccomp",
"{",
"data",
",",
"err",
"=",
"json",
".",
"MarshalIndent",
"(",
"g",
".",
"Config",
".",
"Linux",
".",
"Seccomp",
",",
"\"",
"\"",
",",
"\"",
"\\t",
"\"",
")",
"\n",
"}",
"else",
"{",
"data",
",",
"err",
"=",
"json",
".",
"MarshalIndent",
"(",
"g",
".",
"Config",
",",
"\"",
"\"",
",",
"\"",
"\\t",
"\"",
")",
"\n",
"}",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"_",
",",
"err",
"=",
"w",
".",
"Write",
"(",
"data",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] |
// Save writes the configuration into w.
|
[
"Save",
"writes",
"the",
"configuration",
"into",
"w",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L328-L356
|
14,378 |
opencontainers/runtime-tools
|
generate/generate.go
|
SaveToFile
|
func (g *Generator) SaveToFile(path string, exportOpts ExportOptions) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return g.Save(f, exportOpts)
}
|
go
|
func (g *Generator) SaveToFile(path string, exportOpts ExportOptions) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return g.Save(f, exportOpts)
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SaveToFile",
"(",
"path",
"string",
",",
"exportOpts",
"ExportOptions",
")",
"error",
"{",
"f",
",",
"err",
":=",
"os",
".",
"Create",
"(",
"path",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"defer",
"f",
".",
"Close",
"(",
")",
"\n",
"return",
"g",
".",
"Save",
"(",
"f",
",",
"exportOpts",
")",
"\n",
"}"
] |
// SaveToFile writes the configuration into a file.
|
[
"SaveToFile",
"writes",
"the",
"configuration",
"into",
"a",
"file",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L359-L366
|
14,379 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetVersion
|
func (g *Generator) SetVersion(version string) {
g.initConfig()
g.Config.Version = version
}
|
go
|
func (g *Generator) SetVersion(version string) {
g.initConfig()
g.Config.Version = version
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetVersion",
"(",
"version",
"string",
")",
"{",
"g",
".",
"initConfig",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Version",
"=",
"version",
"\n",
"}"
] |
// SetVersion sets g.Config.Version.
|
[
"SetVersion",
"sets",
"g",
".",
"Config",
".",
"Version",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L369-L372
|
14,380 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetRootPath
|
func (g *Generator) SetRootPath(path string) {
g.initConfigRoot()
g.Config.Root.Path = path
}
|
go
|
func (g *Generator) SetRootPath(path string) {
g.initConfigRoot()
g.Config.Root.Path = path
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetRootPath",
"(",
"path",
"string",
")",
"{",
"g",
".",
"initConfigRoot",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Root",
".",
"Path",
"=",
"path",
"\n",
"}"
] |
// SetRootPath sets g.Config.Root.Path.
|
[
"SetRootPath",
"sets",
"g",
".",
"Config",
".",
"Root",
".",
"Path",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L375-L378
|
14,381 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetRootReadonly
|
func (g *Generator) SetRootReadonly(b bool) {
g.initConfigRoot()
g.Config.Root.Readonly = b
}
|
go
|
func (g *Generator) SetRootReadonly(b bool) {
g.initConfigRoot()
g.Config.Root.Readonly = b
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetRootReadonly",
"(",
"b",
"bool",
")",
"{",
"g",
".",
"initConfigRoot",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Root",
".",
"Readonly",
"=",
"b",
"\n",
"}"
] |
// SetRootReadonly sets g.Config.Root.Readonly.
|
[
"SetRootReadonly",
"sets",
"g",
".",
"Config",
".",
"Root",
".",
"Readonly",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L381-L384
|
14,382 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetHostname
|
func (g *Generator) SetHostname(s string) {
g.initConfig()
g.Config.Hostname = s
}
|
go
|
func (g *Generator) SetHostname(s string) {
g.initConfig()
g.Config.Hostname = s
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetHostname",
"(",
"s",
"string",
")",
"{",
"g",
".",
"initConfig",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Hostname",
"=",
"s",
"\n",
"}"
] |
// SetHostname sets g.Config.Hostname.
|
[
"SetHostname",
"sets",
"g",
".",
"Config",
".",
"Hostname",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L387-L390
|
14,383 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetOCIVersion
|
func (g *Generator) SetOCIVersion(s string) {
g.initConfig()
g.Config.Version = s
}
|
go
|
func (g *Generator) SetOCIVersion(s string) {
g.initConfig()
g.Config.Version = s
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetOCIVersion",
"(",
"s",
"string",
")",
"{",
"g",
".",
"initConfig",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Version",
"=",
"s",
"\n",
"}"
] |
// SetOCIVersion sets g.Config.Version.
|
[
"SetOCIVersion",
"sets",
"g",
".",
"Config",
".",
"Version",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L393-L396
|
14,384 |
opencontainers/runtime-tools
|
generate/generate.go
|
ClearAnnotations
|
func (g *Generator) ClearAnnotations() {
if g.Config == nil {
return
}
g.Config.Annotations = make(map[string]string)
}
|
go
|
func (g *Generator) ClearAnnotations() {
if g.Config == nil {
return
}
g.Config.Annotations = make(map[string]string)
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"ClearAnnotations",
"(",
")",
"{",
"if",
"g",
".",
"Config",
"==",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"g",
".",
"Config",
".",
"Annotations",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"string",
")",
"\n",
"}"
] |
// ClearAnnotations clears g.Config.Annotations.
|
[
"ClearAnnotations",
"clears",
"g",
".",
"Config",
".",
"Annotations",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L399-L404
|
14,385 |
opencontainers/runtime-tools
|
generate/generate.go
|
AddAnnotation
|
func (g *Generator) AddAnnotation(key, value string) {
g.initConfigAnnotations()
g.Config.Annotations[key] = value
}
|
go
|
func (g *Generator) AddAnnotation(key, value string) {
g.initConfigAnnotations()
g.Config.Annotations[key] = value
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"AddAnnotation",
"(",
"key",
",",
"value",
"string",
")",
"{",
"g",
".",
"initConfigAnnotations",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Annotations",
"[",
"key",
"]",
"=",
"value",
"\n",
"}"
] |
// AddAnnotation adds an annotation into g.Config.Annotations.
|
[
"AddAnnotation",
"adds",
"an",
"annotation",
"into",
"g",
".",
"Config",
".",
"Annotations",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L407-L410
|
14,386 |
opencontainers/runtime-tools
|
generate/generate.go
|
RemoveAnnotation
|
func (g *Generator) RemoveAnnotation(key string) {
if g.Config == nil || g.Config.Annotations == nil {
return
}
delete(g.Config.Annotations, key)
}
|
go
|
func (g *Generator) RemoveAnnotation(key string) {
if g.Config == nil || g.Config.Annotations == nil {
return
}
delete(g.Config.Annotations, key)
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"RemoveAnnotation",
"(",
"key",
"string",
")",
"{",
"if",
"g",
".",
"Config",
"==",
"nil",
"||",
"g",
".",
"Config",
".",
"Annotations",
"==",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"delete",
"(",
"g",
".",
"Config",
".",
"Annotations",
",",
"key",
")",
"\n",
"}"
] |
// RemoveAnnotation remove an annotation from g.Config.Annotations.
|
[
"RemoveAnnotation",
"remove",
"an",
"annotation",
"from",
"g",
".",
"Config",
".",
"Annotations",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L413-L418
|
14,387 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetProcessConsoleSize
|
func (g *Generator) SetProcessConsoleSize(width, height uint) {
g.initConfigProcessConsoleSize()
g.Config.Process.ConsoleSize.Width = width
g.Config.Process.ConsoleSize.Height = height
}
|
go
|
func (g *Generator) SetProcessConsoleSize(width, height uint) {
g.initConfigProcessConsoleSize()
g.Config.Process.ConsoleSize.Width = width
g.Config.Process.ConsoleSize.Height = height
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetProcessConsoleSize",
"(",
"width",
",",
"height",
"uint",
")",
"{",
"g",
".",
"initConfigProcessConsoleSize",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Process",
".",
"ConsoleSize",
".",
"Width",
"=",
"width",
"\n",
"g",
".",
"Config",
".",
"Process",
".",
"ConsoleSize",
".",
"Height",
"=",
"height",
"\n",
"}"
] |
// SetProcessConsoleSize sets g.Config.Process.ConsoleSize.
|
[
"SetProcessConsoleSize",
"sets",
"g",
".",
"Config",
".",
"Process",
".",
"ConsoleSize",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L429-L433
|
14,388 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetProcessUID
|
func (g *Generator) SetProcessUID(uid uint32) {
g.initConfigProcess()
g.Config.Process.User.UID = uid
}
|
go
|
func (g *Generator) SetProcessUID(uid uint32) {
g.initConfigProcess()
g.Config.Process.User.UID = uid
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetProcessUID",
"(",
"uid",
"uint32",
")",
"{",
"g",
".",
"initConfigProcess",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Process",
".",
"User",
".",
"UID",
"=",
"uid",
"\n",
"}"
] |
// SetProcessUID sets g.Config.Process.User.UID.
|
[
"SetProcessUID",
"sets",
"g",
".",
"Config",
".",
"Process",
".",
"User",
".",
"UID",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L436-L439
|
14,389 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetProcessUsername
|
func (g *Generator) SetProcessUsername(username string) {
g.initConfigProcess()
g.Config.Process.User.Username = username
}
|
go
|
func (g *Generator) SetProcessUsername(username string) {
g.initConfigProcess()
g.Config.Process.User.Username = username
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetProcessUsername",
"(",
"username",
"string",
")",
"{",
"g",
".",
"initConfigProcess",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Process",
".",
"User",
".",
"Username",
"=",
"username",
"\n",
"}"
] |
// SetProcessUsername sets g.Config.Process.User.Username.
|
[
"SetProcessUsername",
"sets",
"g",
".",
"Config",
".",
"Process",
".",
"User",
".",
"Username",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L442-L445
|
14,390 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetProcessGID
|
func (g *Generator) SetProcessGID(gid uint32) {
g.initConfigProcess()
g.Config.Process.User.GID = gid
}
|
go
|
func (g *Generator) SetProcessGID(gid uint32) {
g.initConfigProcess()
g.Config.Process.User.GID = gid
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetProcessGID",
"(",
"gid",
"uint32",
")",
"{",
"g",
".",
"initConfigProcess",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Process",
".",
"User",
".",
"GID",
"=",
"gid",
"\n",
"}"
] |
// SetProcessGID sets g.Config.Process.User.GID.
|
[
"SetProcessGID",
"sets",
"g",
".",
"Config",
".",
"Process",
".",
"User",
".",
"GID",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L448-L451
|
14,391 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetProcessCwd
|
func (g *Generator) SetProcessCwd(cwd string) {
g.initConfigProcess()
g.Config.Process.Cwd = cwd
}
|
go
|
func (g *Generator) SetProcessCwd(cwd string) {
g.initConfigProcess()
g.Config.Process.Cwd = cwd
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetProcessCwd",
"(",
"cwd",
"string",
")",
"{",
"g",
".",
"initConfigProcess",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Process",
".",
"Cwd",
"=",
"cwd",
"\n",
"}"
] |
// SetProcessCwd sets g.Config.Process.Cwd.
|
[
"SetProcessCwd",
"sets",
"g",
".",
"Config",
".",
"Process",
".",
"Cwd",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L454-L457
|
14,392 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetProcessNoNewPrivileges
|
func (g *Generator) SetProcessNoNewPrivileges(b bool) {
g.initConfigProcess()
g.Config.Process.NoNewPrivileges = b
}
|
go
|
func (g *Generator) SetProcessNoNewPrivileges(b bool) {
g.initConfigProcess()
g.Config.Process.NoNewPrivileges = b
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetProcessNoNewPrivileges",
"(",
"b",
"bool",
")",
"{",
"g",
".",
"initConfigProcess",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Process",
".",
"NoNewPrivileges",
"=",
"b",
"\n",
"}"
] |
// SetProcessNoNewPrivileges sets g.Config.Process.NoNewPrivileges.
|
[
"SetProcessNoNewPrivileges",
"sets",
"g",
".",
"Config",
".",
"Process",
".",
"NoNewPrivileges",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L460-L463
|
14,393 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetProcessTerminal
|
func (g *Generator) SetProcessTerminal(b bool) {
g.initConfigProcess()
g.Config.Process.Terminal = b
}
|
go
|
func (g *Generator) SetProcessTerminal(b bool) {
g.initConfigProcess()
g.Config.Process.Terminal = b
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetProcessTerminal",
"(",
"b",
"bool",
")",
"{",
"g",
".",
"initConfigProcess",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Process",
".",
"Terminal",
"=",
"b",
"\n",
"}"
] |
// SetProcessTerminal sets g.Config.Process.Terminal.
|
[
"SetProcessTerminal",
"sets",
"g",
".",
"Config",
".",
"Process",
".",
"Terminal",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L466-L469
|
14,394 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetProcessApparmorProfile
|
func (g *Generator) SetProcessApparmorProfile(prof string) {
g.initConfigProcess()
g.Config.Process.ApparmorProfile = prof
}
|
go
|
func (g *Generator) SetProcessApparmorProfile(prof string) {
g.initConfigProcess()
g.Config.Process.ApparmorProfile = prof
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetProcessApparmorProfile",
"(",
"prof",
"string",
")",
"{",
"g",
".",
"initConfigProcess",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Process",
".",
"ApparmorProfile",
"=",
"prof",
"\n",
"}"
] |
// SetProcessApparmorProfile sets g.Config.Process.ApparmorProfile.
|
[
"SetProcessApparmorProfile",
"sets",
"g",
".",
"Config",
".",
"Process",
".",
"ApparmorProfile",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L472-L475
|
14,395 |
opencontainers/runtime-tools
|
generate/generate.go
|
SetProcessArgs
|
func (g *Generator) SetProcessArgs(args []string) {
g.initConfigProcess()
g.Config.Process.Args = args
}
|
go
|
func (g *Generator) SetProcessArgs(args []string) {
g.initConfigProcess()
g.Config.Process.Args = args
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"SetProcessArgs",
"(",
"args",
"[",
"]",
"string",
")",
"{",
"g",
".",
"initConfigProcess",
"(",
")",
"\n",
"g",
".",
"Config",
".",
"Process",
".",
"Args",
"=",
"args",
"\n",
"}"
] |
// SetProcessArgs sets g.Config.Process.Args.
|
[
"SetProcessArgs",
"sets",
"g",
".",
"Config",
".",
"Process",
".",
"Args",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L478-L481
|
14,396 |
opencontainers/runtime-tools
|
generate/generate.go
|
ClearProcessEnv
|
func (g *Generator) ClearProcessEnv() {
if g.Config == nil || g.Config.Process == nil {
return
}
g.Config.Process.Env = []string{}
// Clear out the env cache map as well
g.envMap = map[string]int{}
}
|
go
|
func (g *Generator) ClearProcessEnv() {
if g.Config == nil || g.Config.Process == nil {
return
}
g.Config.Process.Env = []string{}
// Clear out the env cache map as well
g.envMap = map[string]int{}
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"ClearProcessEnv",
"(",
")",
"{",
"if",
"g",
".",
"Config",
"==",
"nil",
"||",
"g",
".",
"Config",
".",
"Process",
"==",
"nil",
"{",
"return",
"\n",
"}",
"\n",
"g",
".",
"Config",
".",
"Process",
".",
"Env",
"=",
"[",
"]",
"string",
"{",
"}",
"\n",
"// Clear out the env cache map as well",
"g",
".",
"envMap",
"=",
"map",
"[",
"string",
"]",
"int",
"{",
"}",
"\n",
"}"
] |
// ClearProcessEnv clears g.Config.Process.Env.
|
[
"ClearProcessEnv",
"clears",
"g",
".",
"Config",
".",
"Process",
".",
"Env",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L484-L491
|
14,397 |
opencontainers/runtime-tools
|
generate/generate.go
|
AddProcessEnv
|
func (g *Generator) AddProcessEnv(name, value string) {
if name == "" {
return
}
g.initConfigProcess()
g.addEnv(fmt.Sprintf("%s=%s", name, value), name)
}
|
go
|
func (g *Generator) AddProcessEnv(name, value string) {
if name == "" {
return
}
g.initConfigProcess()
g.addEnv(fmt.Sprintf("%s=%s", name, value), name)
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"AddProcessEnv",
"(",
"name",
",",
"value",
"string",
")",
"{",
"if",
"name",
"==",
"\"",
"\"",
"{",
"return",
"\n",
"}",
"\n\n",
"g",
".",
"initConfigProcess",
"(",
")",
"\n",
"g",
".",
"addEnv",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"name",
",",
"value",
")",
",",
"name",
")",
"\n",
"}"
] |
// AddProcessEnv adds name=value into g.Config.Process.Env, or replaces an
// existing entry with the given name.
|
[
"AddProcessEnv",
"adds",
"name",
"=",
"value",
"into",
"g",
".",
"Config",
".",
"Process",
".",
"Env",
"or",
"replaces",
"an",
"existing",
"entry",
"with",
"the",
"given",
"name",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L495-L502
|
14,398 |
opencontainers/runtime-tools
|
generate/generate.go
|
AddMultipleProcessEnv
|
func (g *Generator) AddMultipleProcessEnv(envs []string) {
g.initConfigProcess()
for _, val := range envs {
split := strings.SplitN(val, "=", 2)
g.addEnv(val, split[0])
}
}
|
go
|
func (g *Generator) AddMultipleProcessEnv(envs []string) {
g.initConfigProcess()
for _, val := range envs {
split := strings.SplitN(val, "=", 2)
g.addEnv(val, split[0])
}
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"AddMultipleProcessEnv",
"(",
"envs",
"[",
"]",
"string",
")",
"{",
"g",
".",
"initConfigProcess",
"(",
")",
"\n\n",
"for",
"_",
",",
"val",
":=",
"range",
"envs",
"{",
"split",
":=",
"strings",
".",
"SplitN",
"(",
"val",
",",
"\"",
"\"",
",",
"2",
")",
"\n",
"g",
".",
"addEnv",
"(",
"val",
",",
"split",
"[",
"0",
"]",
")",
"\n",
"}",
"\n",
"}"
] |
// AddMultipleProcessEnv adds multiple name=value into g.Config.Process.Env, or replaces
// existing entries with the given name.
|
[
"AddMultipleProcessEnv",
"adds",
"multiple",
"name",
"=",
"value",
"into",
"g",
".",
"Config",
".",
"Process",
".",
"Env",
"or",
"replaces",
"existing",
"entries",
"with",
"the",
"given",
"name",
"."
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L506-L513
|
14,399 |
opencontainers/runtime-tools
|
generate/generate.go
|
addEnv
|
func (g *Generator) addEnv(env, key string) {
if idx, ok := g.envMap[key]; ok {
// The ENV exists in the cache, so change its value in g.Config.Process.Env
g.Config.Process.Env[idx] = env
} else {
// else the env doesn't exist, so add it and add it's index to g.envMap
g.Config.Process.Env = append(g.Config.Process.Env, env)
g.envMap[key] = len(g.Config.Process.Env) - 1
}
}
|
go
|
func (g *Generator) addEnv(env, key string) {
if idx, ok := g.envMap[key]; ok {
// The ENV exists in the cache, so change its value in g.Config.Process.Env
g.Config.Process.Env[idx] = env
} else {
// else the env doesn't exist, so add it and add it's index to g.envMap
g.Config.Process.Env = append(g.Config.Process.Env, env)
g.envMap[key] = len(g.Config.Process.Env) - 1
}
}
|
[
"func",
"(",
"g",
"*",
"Generator",
")",
"addEnv",
"(",
"env",
",",
"key",
"string",
")",
"{",
"if",
"idx",
",",
"ok",
":=",
"g",
".",
"envMap",
"[",
"key",
"]",
";",
"ok",
"{",
"// The ENV exists in the cache, so change its value in g.Config.Process.Env",
"g",
".",
"Config",
".",
"Process",
".",
"Env",
"[",
"idx",
"]",
"=",
"env",
"\n",
"}",
"else",
"{",
"// else the env doesn't exist, so add it and add it's index to g.envMap",
"g",
".",
"Config",
".",
"Process",
".",
"Env",
"=",
"append",
"(",
"g",
".",
"Config",
".",
"Process",
".",
"Env",
",",
"env",
")",
"\n",
"g",
".",
"envMap",
"[",
"key",
"]",
"=",
"len",
"(",
"g",
".",
"Config",
".",
"Process",
".",
"Env",
")",
"-",
"1",
"\n",
"}",
"\n",
"}"
] |
// addEnv looks through adds ENV to the Process and checks envMap for
// any duplicates
// This is called by both AddMultipleProcessEnv and AddProcessEnv
|
[
"addEnv",
"looks",
"through",
"adds",
"ENV",
"to",
"the",
"Process",
"and",
"checks",
"envMap",
"for",
"any",
"duplicates",
"This",
"is",
"called",
"by",
"both",
"AddMultipleProcessEnv",
"and",
"AddProcessEnv"
] |
095789df6c2bc53a9dd4464cda8c22616d66e0d6
|
https://github.com/opencontainers/runtime-tools/blob/095789df6c2bc53a9dd4464cda8c22616d66e0d6/generate/generate.go#L518-L527
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.