作者:li-an
项目:telegra
// snmpTranslate resolves the given OID.
// The contents of the oid parameter will be replaced with the numeric oid value.
// If name is empty, the textual OID value is stored in it. If the textual OID cannot be translated, the numeric OID is stored instead.
// If mibPrefix is non-nil, the MIB in which the OID was found is stored, with a suffix of "::".
func snmpTranslate(mibPrefix *string, oid *string, name *string) error {
if strings.ContainsAny(*oid, ":abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") {
out, err := execCmd("snmptranslate", "-m", "all", "-On", *oid)
if err != nil {
return Errorf(err, "translating %s", *oid)
}
*oid = string(bytes.TrimSuffix(out, []byte{'\n'}))
}
if *name == "" {
out, err := execCmd("snmptranslate", "-m", "all", *oid)
if err != nil {
//TODO debug message
*name = *oid
} else {
if i := bytes.Index(out, []byte("::")); i != -1 {
if mibPrefix != nil {
*mibPrefix = string(out[:i+2])
}
out = out[i+2:]
}
*name = string(bytes.TrimSuffix(out, []byte{'\n'}))
}
}
return nil
}
作者:Maksadbe
项目:telegra
// statsPerNUMA gathers hugepages statistics from each NUMA node
func statsPerNUMA(path string) (map[string]HugepagesNUMAStats, error) {
var hugepagesStats = make(map[string]HugepagesNUMAStats)
dirs, err := ioutil.ReadDir(path)
if err != nil {
return hugepagesStats, err
}
for _, d := range dirs {
if !(d.IsDir() && strings.HasPrefix(d.Name(), "node")) {
continue
}
hugepagesFree := fmt.Sprintf("%s/%s/hugepages/hugepages-2048kB/free_hugepages", path, d.Name())
hugepagesNR := fmt.Sprintf("%s/%s/hugepages/hugepages-2048kB/nr_hugepages", path, d.Name())
free, err := ioutil.ReadFile(hugepagesFree)
if err != nil {
return hugepagesStats, err
}
nr, err := ioutil.ReadFile(hugepagesNR)
if err != nil {
return hugepagesStats, err
}
f, _ := strconv.Atoi(string(bytes.TrimSuffix(free, newlineByte)))
n, _ := strconv.Atoi(string(bytes.TrimSuffix(nr, newlineByte)))
hugepagesStats[d.Name()] = HugepagesNUMAStats{Free: f, NR: n}
}
return hugepagesStats, nil
}
作者:alexsavelie
项目:go-vc
func (r *Repository) execAndParseCols(subcmd string) ([][2]string, error) {
cmd := exec.Command("hg", "-v", "--debug", subcmd)
cmd.Dir = r.Dir
out, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("exec `hg -v --debug %s` failed: %s. Output was:\n\n%s", subcmd, err, out)
}
out = bytes.TrimSuffix(out, []byte("\n")) // remove trailing newline
lines := bytes.Split(out, []byte("\n"))
sort.Sort(byteSlices(lines)) // sort for consistency
refs := make([][2]string, len(lines))
for i, line := range lines {
line = bytes.TrimSuffix(line, []byte(" (inactive)"))
// format: "NAME SEQUENCE:ID" (arbitrary amount of whitespace between NAME and SEQUENCE)
if len(line) <= 41 {
return nil, fmt.Errorf("unexpectedly short (<=41 bytes) line in `hg -v --debug %s` output", subcmd)
}
id := line[len(line)-40:]
// find where the SEQUENCE begins
seqIdx := bytes.LastIndex(line, []byte(" "))
if seqIdx == -1 {
return nil, fmt.Errorf("unexpectedly no whitespace in line in `hg -v --debug %s` output", subcmd)
}
name := bytes.TrimRight(line[:seqIdx], " ")
refs[i] = [2]string{string(id), string(name)}
}
return refs, nil
}
作者:gosur
项目:dotfile
func (p *docPrinter) adjustedOutputPosition() int64 {
b := p.buf.Bytes()
b = bytes.TrimSuffix(b, []byte{'*'})
b = bytes.TrimSuffix(b, []byte{'[', ']'})
b = bytes.TrimSuffix(b, []byte{'*'})
b = bytes.TrimSuffix(b, []byte{'&'})
return p.outputPosition() - int64(p.buf.Len()-len(b))
}
作者:achand
项目:g
func ExampleTrimSuffix() {
var b = []byte("Hello, goodbye, etc!")
b = bytes.TrimSuffix(b, []byte("goodbye, etc!"))
b = bytes.TrimSuffix(b, []byte("gopher"))
b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...)
os.Stdout.Write(b)
// Output: Hello, world!
}
作者:JuanCarlos
项目:flee
func (l *lexer) toEOL() ([]byte, error) {
line, err := l.buf.ReadBytes('\n')
// ignore EOF here since it's roughly equivalent to EOL
if err != nil && err != io.EOF {
return nil, err
}
line = bytes.TrimSuffix(line, []byte{'\r'})
line = bytes.TrimSuffix(line, []byte{'\n'})
return line, nil
}
作者:GeertJoha
项目:qml-ki
// Parses output of `qmake -v` like
//
// Using Qt version 5.2.1 in /usr/lib/x86_64-linux-gnu
//
// And `qtpaths --plugin-dir` like
//
// /usr/local/Cellar/qt5/5.3.0/plugins
func getQtInfo() (info qtInfo, err error) {
errFmt := fmt.Errorf("qt info: qmake unexpected output")
buf, err := exec.Command("qmake", "-v").Output()
if err != nil {
err = fmt.Errorf("qt info: qmake: %v", err)
return
}
idx := bytes.Index(buf, []byte("Qt version"))
if idx < 0 {
err = errFmt
return
}
buf = buf[idx:]
n, err := fmt.Sscanf(string(buf), "Qt version %s in %s", &info.Version, &info.LibPath)
if err != nil || n != 2 {
err = errFmt
}
// qtpaths
buf, err = exec.Command("qtpaths", "--plugin-dir").Output()
if err != nil {
err = fmt.Errorf("qt info: qtpaths: %v", err)
return
}
buf = bytes.TrimSpace(buf)
if len(buf) < 1 || !bytes.HasSuffix(buf, []byte("plugins")) {
err = fmt.Errorf("qt info: qtpaths unexpected output")
return
}
buf = bytes.TrimSuffix(buf, []byte("plugins"))
info.BasePath = string(buf[:len(buf)-1]) // drop sep
return
}
作者:ah
项目:go-flynn-exampl
func (r *Reader) Read() ([]byte, error) {
buf := []byte{}
var isErr bool
for {
line, err := r.ReadBytes('\n')
if err != nil {
return nil, err
}
if bytes.HasPrefix(line, []byte("event: error")) {
isErr = true
}
if bytes.HasPrefix(line, []byte("data: ")) {
data := bytes.TrimSuffix(bytes.TrimPrefix(line, []byte("data: ")), []byte("\n"))
buf = append(buf, data...)
}
// peek ahead one byte to see if we have a double newline (terminator)
if peek, err := r.Peek(1); err == nil && string(peek) == "\n" {
break
}
}
if isErr {
return nil, Error(string(buf))
}
return buf, nil
}
作者:alexbrainma
项目:delv
func (dbp *Process) loadProcessInformation(wg *sync.WaitGroup) {
defer wg.Done()
comm, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/comm", dbp.Pid))
if err == nil {
// removes newline character
comm = bytes.TrimSuffix(comm, []byte("\n"))
}
if comm == nil || len(comm) <= 0 {
stat, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/stat", dbp.Pid))
if err != nil {
fmt.Printf("Could not read proc stat: %v\n", err)
os.Exit(1)
}
expr := fmt.Sprintf("%d\\s*\\((.*)\\)", dbp.Pid)
rexp, err := regexp.Compile(expr)
if err != nil {
fmt.Printf("Regexp compile error: %v\n", err)
os.Exit(1)
}
match := rexp.FindSubmatch(stat)
if match == nil {
fmt.Printf("No match found using regexp '%s' in /proc/%d/stat\n", expr, dbp.Pid)
os.Exit(1)
}
comm = match[1]
}
dbp.os.comm = strings.Replace(string(comm), "%", "%%", -1)
}
作者:peidachan
项目:go
func matchRoute(path []byte, itype int) *RouteMatched {
var route *Route
var items []*Route
switch itype {
case 3:
items = wsRoutes
default:
items = routes
}
for _, route = range items {
if route.Pattern == nil {
// fix route
if bytes.HasSuffix(path, B_SLASH) {
path = bytes.TrimSuffix(path, B_SLASH)
}
if bytes.Equal(path, route.Rule) {
return &RouteMatched{ClassType: route.ClassType, Params: nil}
}
} else {
// regexp route
if matched := route.Pattern.FindAllSubmatch(path, -1); matched != nil {
params := make(map[string]string)
i := 0
for _, value := range route.Keys {
i++
params[value] = html.EscapeString(string(matched[0][i]))
}
return &RouteMatched{ClassType: route.ClassType, Params: params}
}
}
}
return nil
}
作者:cmar
项目:o
// indentVal writes the given YAML-formatted value x to w and prefixing
// the second and subsequent lines with the given ident.
func indentVal(w io.Writer, x interface{}, indentStr string) {
data, err := yaml.Marshal(x)
if err != nil {
panic(fmt.Errorf("cannot marshal YAML", err))
}
if len(data) == 0 {
panic("YAML cannot marshal to empty string")
}
indent := []byte(indentStr + " ")
if canUseSameLine(x) {
w.Write(space)
} else {
w.Write(nl)
w.Write(indent)
}
data = bytes.TrimSuffix(data, nl)
lines := bytes.Split(data, nl)
for i, line := range lines {
if i > 0 {
w.Write(indent)
}
w.Write(line)
w.Write(nl)
}
}
作者:DaveDaCod
项目:docke
func (c *Copier) copySrc(name string, src io.Reader) {
defer c.copyJobs.Done()
reader := bufio.NewReader(src)
for {
line, err := reader.ReadBytes('\n')
line = bytes.TrimSuffix(line, []byte{'\n'})
// ReadBytes can return full or partial output even when it failed.
// e.g. it can return a full entry and EOF.
if err == nil || len(line) > 0 {
if logErr := c.dst.Log(&Message{ContainerID: c.cid, Line: line, Source: name, Timestamp: time.Now().UTC()}); logErr != nil {
logrus.Errorf("Failed to log msg %q for logger %s: %s", line, c.dst.Name(), logErr)
}
}
if err != nil {
if err != io.EOF {
logrus.Errorf("Error scanning log stream: %s", err)
}
return
}
}
}
作者:joltd
项目:jol
func fmtToJsonArr(s []byte) []byte {
s = bytes.Replace(s, []byte("{"), []byte("[{"), 1)
s = bytes.Replace(s, []byte("}"), []byte("},"), -1)
s = bytes.TrimSuffix(s, []byte(","))
s = append(s, []byte("]")...)
return s
}
作者:emil2
项目:go-vc
func (r *Repository) showRef(arg string) ([][2]string, error) {
cmd := exec.Command("git", "show-ref", arg)
cmd.Dir = r.Dir
out, err := cmd.CombinedOutput()
if err != nil {
// Exit status of 1 and no output means there were no
// results. This is not a fatal error.
if exitStatus(err) == 1 && len(out) == 0 {
return nil, nil
}
return nil, fmt.Errorf("exec `git show-ref %s` in %s failed: %s. Output was:\n\n%s", arg, r.Dir, err, out)
}
out = bytes.TrimSuffix(out, []byte("\n")) // remove trailing newline
lines := bytes.Split(out, []byte("\n"))
sort.Sort(byteSlices(lines)) // sort for consistency
refs := make([][2]string, len(lines))
for i, line := range lines {
if len(line) <= 41 {
return nil, errors.New("unexpectedly short (<=41 bytes) line in `git show-ref ...` output")
}
id := line[:40]
name := line[41:]
refs[i] = [2]string{string(id), string(name)}
}
return refs, nil
}
作者:williballenthi
项目:Lancelo
func readAscii(buf []byte) (string, error) {
br := bufio.NewReader(bytes.NewReader(buf))
bytez, e := br.ReadBytes(byte(0x00))
check(e)
bytez = bytes.TrimSuffix(bytez, []byte{0x00})
return string(bytez), nil
}
作者:camunda-third-part
项目:hug
// markdownify renders a given string from Markdown to HTML.
func markdownify(in interface{}) template.HTML {
text := cast.ToString(in)
m := helpers.RenderBytes(&helpers.RenderingContext{Content: []byte(text), PageFmt: "markdown"})
m = bytes.TrimPrefix(m, markdownTrimPrefix)
m = bytes.TrimSuffix(m, markdownTrimSuffix)
return template.HTML(m)
}
作者:tonistiig
项目:continuit
func listxattrAll(path string, listFunc listxattrFunc) ([]string, error) {
var p []byte // nil on first execution
for {
n, err := listFunc(path, p) // first call gets buffer size.
if err != nil {
return nil, err
}
if n > len(p) {
p = make([]byte, n)
continue
}
p = p[:n]
ps := bytes.Split(bytes.TrimSuffix(p, []byte{0}), []byte{0})
var entries []string
for _, p := range ps {
s := string(p)
if s != "" {
entries = append(entries, s)
}
}
return entries, nil
}
}
作者:alcortes
项目:go-gi
// The refs are either tips (obj-id SP refname) or a peeled (obj-id SP refname^{}).
// If there are no refs, then there might be a shallow or flush-ptk.
func decodeOtherRefs(p *Decoder) decoderStateFn {
if ok := p.nextLine(); !ok {
return nil
}
if bytes.HasPrefix(p.line, shallow) {
return decodeShallow
}
if len(p.line) == 0 {
return nil
}
saveTo := p.data.References
if bytes.HasSuffix(p.line, peeled) {
p.line = bytes.TrimSuffix(p.line, peeled)
saveTo = p.data.Peeled
}
ref, hash, err := readRef(p.line)
if err != nil {
p.error("%s", err)
return nil
}
saveTo[ref] = hash
return decodeOtherRefs
}
作者:francoismissli
项目:bloomcache
// The request should be of format
// ADD|ElementToAdd\n
// TEST|AmIHere
// writes back 200|true if the element is probably in the bloom filter
// writes back 200|false is the element is definitely not in the bloom filter
func (c *ClientConn) writeResponse(request []byte) {
message := bytes.Split(request, []byte{'|'})
var response []byte
if len(message) != 2 {
response = []byte("400|Malformed request\n Usage: ADD|ElementToAdd or TEST|ElementToTest\n")
c.conn.Write(response)
return
}
verb := string(message[0])
item := bytes.TrimSuffix(message[1], []byte("\n"))
if verb == "TEST" {
if c.f.Test(item) {
response = []byte("200|true\n")
} else {
response = []byte("200|false\n")
}
} else if verb == "ADD" {
c.f.Add(item)
response = []byte("201\n")
} else {
response = []byte("400|Unknown Verb\n")
}
c.conn.Write(response)
fmt.Println("Request:", string(request), "Response: ", string(response))
}
作者:j
项目:kat
func substPatternBytes(pat, repl, str []byte) (pre, subst, post []byte) {
i := bytes.IndexByte(pat, '%')
if i < 0 {
if bytes.Equal(str, pat) {
return repl, nil, nil
}
return str, nil, nil
}
in := str
trimed := str
if i > 0 {
trimed = bytes.TrimPrefix(in, pat[:i])
if bytes.Equal(trimed, in) {
return str, nil, nil
}
}
in = trimed
if i < len(pat)-1 {
trimed = bytes.TrimSuffix(in, pat[i+1:])
if bytes.Equal(trimed, in) {
return str, nil, nil
}
}
i = bytes.IndexByte(repl, '%')
if i < 0 {
return repl, nil, nil
}
return repl[:i], trimed, repl[i+1:]
}