作者:alash3a
项目:ao
// Scan the datafile in reverse order using a custom separator and function.
// The provided function has two params, data and whether we at the end or not .
// This function will lock the whole file till it ends .
func (this *AOF) ReverseScan(sep []byte, fn func(data []byte, atEOF bool) bool) {
this.Lock()
defer this.Unlock()
pos := int64(0)
done := int64(0)
data := []byte{}
for {
this.file.Seek(pos, 2)
tmp := make([]byte, len(sep))
n, _ := this.file.Read(tmp)
pos -= int64(len(sep))
if n > 0 {
done += int64(n)
data = append(tmp, data...)
}
if bytes.Equal(sep, tmp) {
if !fn(bytes.Trim(data, string(sep)), false) {
break
}
data = []byte{}
}
if done >= this.size {
fn(bytes.Trim(data, string(sep)), true)
break
}
}
data = []byte{}
}
作者:alash3a
项目:ao
// Scan the datafile using a custom separator and function.
// The provided function has two params, data and whether we at the end or not .
// This function will lock the whole file till it ends .
func (this *AOF) Scan(sep []byte, fn func(data []byte, atEOF bool) bool) {
this.Lock()
defer this.Unlock()
this.file.Seek(0, 0)
data := []byte{}
for {
tmp := make([]byte, len(sep))
n, e := this.file.Read(tmp)
if n > 0 {
data = append(data, tmp[0:n]...)
}
if e != nil || n == 0 {
if len(data) > 0 {
fn(bytes.Trim(data, string(sep)), true)
}
break
}
if bytes.Equal(sep, tmp) {
if !fn(bytes.Trim(data, string(sep)), false) {
break
}
data = []byte{}
}
}
data = []byte{}
}
作者:y8
项目:OWL-v
func WiteToTSDB(channel chan []byte, cfg *Config) {
START:
tcpAddr, err := net.ResolveTCPAddr("tcp4", cfg.OPENTSDB_ADDR)
if err != nil {
panic(fmt.Sprintf("error opentsdb address(%s)", cfg.OPENTSDB_ADDR))
}
conn, err := net.DialTCP("tcp4", nil, tcpAddr)
if err != nil {
slog.Error("connect tsdb server(%s) error(%s)", tcpAddr, err)
time.Sleep(time.Second * 30)
goto START
}
slog.Info("connected to opentsdb server %s", cfg.OPENTSDB_ADDR)
conn.SetKeepAlive(true)
defer conn.Close()
for {
select {
case data, ok := <-channel:
if ok {
data = append(data, '\n')
length, err := conn.Write(data)
if err != nil {
slog.Error("write opentsdb error %s", err)
channel <- bytes.Trim(data, "\n")
time.Sleep(time.Second * 30)
goto START
}
dlog.Info("write opentsdb %d bytes, data:(%s)", length, string(bytes.Trim(data, "\n")))
}
}
}
}
作者:sbine
项目:gu
func parseMeta(buf *bytes.Buffer, repo string) {
var name string
pack := map[string]string{}
pack["REPO"] = repo
_, _ = buf.ReadByte()
for {
key, err := buf.ReadBytes('%')
if err == io.EOF {
break
}
key = bytes.Trim(key, "%")
values, _ := buf.ReadBytes('%')
values = bytes.Trim(values, "%")
values = bytes.Replace(values, []byte("\n"), []byte(" "), -1)
values = bytes.Trim(values, " ")
if string(key) == "NAME" {
name = string(values)
}
pack[string(key)] = string(values)
}
v, _ := packages[name]
if v != nil {
printf("%s exists\n", name)
}
packages[name] = pack
}
作者:minikom
项目:ModRipper.g
func ProtrackerParse(rawData *bytes.Buffer) (samples []Sample) {
modTitle := string(bytes.Trim(rawData.Next(20), zerostring))
for i := 0; i < 31; i++ {
sampleTitle := modTitle + " - " + string(bytes.Trim(rawData.Next(22), zerostring))
sampleLength := BigEndianBytesToInt(rawData.Next(2)) * 2
if sampleLength >= uint16(2) &&
len(sampleTitle) > 0 {
samples = append(samples, Sample{
Title: sampleTitle,
Length: int(sampleLength),
})
}
//discard finetune (1 byte), volume (1 byte), repeat info (4 bytes)
_ = rawData.Next(6)
}
songLength := rawData.Next(2)[0] //discard unused 127 byte.
patternOrder := rawData.Next(133)[:songLength]
// discard pattern data
for i := 0; i < int(biggest(patternOrder))+1; i++ { // patterns start at 00, so add 1
_ = rawData.Next(1024)
}
for i, s := range samples {
samples[i].Data = rawData.Next(s.Length)
}
fmt.Println("Title:", modTitle, "Samples:", len(samples))
return
}
作者:handong89
项目:mobil
// checkVersionMatch makes sure that the go command in the path matches
// the GOROOT that will be used for building the cross compiler.
//
// This is typically not a problem when using the a release version, but
// it is easy for development environments to drift, causing unexpected
// errors.
//
// checkVersionMatch is run after the tmpGoroot is built, so the dist
// command is available to call.
func checkVersionMatch(tmpGoroot string, version []byte) error {
if buildN {
return nil
}
version = bytes.TrimPrefix(version, []byte("go version "))
version = bytes.Trim(version, "\n")
dist := filepath.Join(tmpGoroot, "pkg/tool/"+goEnv("GOOS")+"_"+goEnv("GOARCH")+"/dist")
if goos == "windows" {
dist += ".exe"
}
cmd := exec.Command(dist, "version")
cmd.Dir = tmpGoroot
cmd.Env = []string{
"GOROOT=" + tmpGoroot,
`PATH=` + os.Getenv("PATH"),
}
cmd.Env = appendCommonEnv(cmd.Env)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("cannot get cmd/dist version: %v (%s)", err, out)
}
out = bytes.Trim(out, "\n")
if !bytes.HasPrefix(version, out) {
return fmt.Errorf("Go command out of sync with GOROOT. The command `go version` reports:\n\t%s\nbut the GOROOT %q is version:\n\t%s\nRebuild Go.", version, goEnv("GOROOT"), out)
}
return nil
}
作者:echlebe
项目:erickso
func readConfig() (cfg, error) {
var config cfg
cmd := exec.Command("git", "config", "erickson.url")
output, err := cmd.Output()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
return cfg{}, errNoConfig
} else {
return cfg{}, err
}
}
config.url = string(bytes.Trim(output, "\n"))
cmd = exec.Command("git", "config", "erickson.username")
output, err = cmd.Output()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
// the config option doesn't exist, that's ok
return config, nil
} else {
return cfg{}, err
}
}
config.username = string(bytes.Trim(output, "\n"))
return config, nil
}
作者:nathan-hoa
项目:goso
func (p *parser) parseLoop() {
if p.tokens[p.position+1].id != TOKEN_OPEN_BRACE {
panic("Syntax error: Expected opening brace after loop declaration")
}
//get the arguments for the loop
keyInCollection := bytes.Split(p.currentToken().match, []byte(" in "))
key := string(bytes.Trim(keyInCollection[0], " "))
collection := collectionForKey(p.args, bytes.Trim(keyInCollection[1], " "))
//get the scope
p.position += 2 //move past the opening brace
scopeParser := p.getScope()
p.position += len(scopeParser.tokens) + 1 //+1 for closing brace
//iterate through the collection and make a recusive call for each object in the collection keeping the same scope.
objects := make([][]byte, collection.Len())
for i := 0; i < collection.Len(); i++ {
//reset the fields of the scope parser and set the new loop variable
scopeParser.args[key] = collection.Get(i)
scopeParser.position = 0
scopeParser.result = []byte{}
scopeParser.parse()
objects[i] = scopeParser.result
}
//add the resulting array to the result
p.appendArray(objects)
//remove the loop variable from args
delete(scopeParser.args, key)
}
作者:akave
项目:vfm
func DetectAtxHeader(first, second Line, detectors Detectors) Handler {
if !bytes.HasPrefix(first.Bytes, []byte("#")) {
return nil
}
done := false
return HandlerFunc(func(line Line, ctx Context) (bool, error) {
if done {
return false, nil
}
done = true
block := md.AtxHeaderBlock{
Raw: md.Raw{md.Run(line)},
}
text := bytes.TrimRight(line.Bytes, "\n")
text = bytes.Trim(text, "#")
if len(text) > 0 {
block.Level, _ = mdutils.OffsetIn(line.Bytes, text)
} else {
block.Level = len(bytes.TrimRight(line.Bytes, "\n"))
}
if block.Level > 6 {
block.Level = 6
}
spanRegion := md.Raw{md.Run{
Line: line.Line,
Bytes: bytes.Trim(text, mdutils.Whites),
}}
ctx.Emit(block)
parseSpans(spanRegion, ctx)
ctx.Emit(md.End{})
return true, nil
})
}
作者:nathan-hoa
项目:goso
func (p *parser) parseInclude() {
statement := p.currentToken().match
params := bytes.Split(statement[8:len(statement)-1], []byte{','}) //strip away include() and split by comma
templateName := p.workingDir + string(bytes.Trim(params[0], " "))
template, err := ioutil.ReadFile(templateName + ".goson")
//probably cannot find the template file
if err != nil {
panic(err)
}
lastPathSegmentStart := strings.LastIndex(templateName, "/")
var workingDir string
if lastPathSegmentStart >= 0 {
workingDir = templateName[0 : lastPathSegmentStart+1]
}
tokens := Tokenize(template)
args := explodeIntoArgs(objectForKey(p.args, bytes.Trim(params[1], " ")))
includeParser := &parser{workingDir: workingDir, tokens: tokens, args: args}
includeParser.parse()
p.appendJson(includeParser.result)
p.position++
}
作者:bbandi
项目:cfss
func TestBundleWithECDSAKeyMarshalJSON(t *testing.T) {
b := newCustomizedBundlerFromFile(t, testCFSSLRootBundle, testCFSSLIntBundle, "")
bundle, _ := b.BundleFromFile(leafECDSA256, leafKeyECDSA256, Optimal, "")
jsonBytes, err := json.Marshal(bundle)
if err != nil {
t.Fatal(err)
}
var obj map[string]interface{}
err = json.Unmarshal(jsonBytes, &obj)
if err != nil {
t.Fatal(err)
}
key := obj["key"].(string)
keyBytes, _ := ioutil.ReadFile(leafKeyECDSA256)
keyBytes = bytes.Trim(keyBytes, " \n")
if key != string(keyBytes) {
t.Fatal("key is not recovered.")
}
cert := obj["crt"].(string)
certBytes, _ := ioutil.ReadFile(leafECDSA256)
certBytes = bytes.Trim(certBytes, " \n")
if cert != string(certBytes) {
t.Fatal("cert is not recovered.")
}
keyType := obj["key_type"]
if keyType != "256-bit ECDSA" {
t.Fatal("Incorrect key type:", keyType)
}
}
作者:vtpha
项目:multigenome-si
func ReadSequence(file string) {
f, err := os.Open(file)
if err != nil {
panic(err)
}
defer f.Close()
if file[len(file)-6:] == ".fasta" {
scanner := bufio.NewScanner(f)
byte_array := make([]byte, 0)
for scanner.Scan() {
line := scanner.Bytes()
if len(line) > 0 && line[0] != '>' {
byte_array = append(byte_array, bytes.Trim(line, "\n\r ")...)
}
}
SEQ = append(byte_array, byte('$'))
} else {
byte_array, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
SEQ = append(bytes.Trim(byte_array, "\n\r "), byte('$'))
}
}
作者:jangle
项目:impuls
func instrumentFromRaw(raw *rawInstrument) *Instrument {
return &Instrument{
Filename: string(bytes.Trim(raw.DOSFilename[:], "\x00")),
NewNoteAction: NewNoteAction(raw.NNA),
DuplicateCheckType: DuplicateCheckType(raw.DCT),
DuplicateCheckAction: DuplicateCheckAction(raw.DCA),
FadeOut: raw.FadeOut,
PitchPanSeparation: raw.PPS,
PitchPanCenter: raw.PPC,
GlobalVolume: raw.GbV,
DefaultPan: raw.DfP & 0x7f,
DefaultPanOn: raw.DfP&0x80 == 0,
VolumeSwing: raw.RV,
PanSwing: raw.RP,
NumSamples: raw.NoS,
Name: string(bytes.Trim(raw.Name[:], "\x00")),
DefaultCutoff: raw.IFC,
DefaultResonance: raw.IFR,
MIDIChannel: raw.MCh,
MIDIProgram: raw.MPr,
MIDIBankLow: int8(raw.MIDIBnk[0]),
MIDIBankHigh: int8(raw.MIDIBnk[1]),
KeyboardTable: raw.KeyboardTable,
VolumeEnvelope: envelopeFromRaw(&raw.VolumeEnvelope),
PanningEnvelope: envelopeFromRaw(&raw.PanningEnvelope),
PitchEnvelope: envelopeFromRaw(&raw.PitchEnvelope),
}
}
作者:go-xiaohe
项目:pug
// NewPageOfMarkdown create new page from markdown file
func NewPageOfMarkdown(file, slug string, page *Page) (*Page, error) {
// page-node need not read file
if page != nil && page.Node == true {
return page, nil
}
fileBytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
if len(fileBytes) < 3 {
return nil, fmt.Errorf("page content is too less")
}
if page == nil {
dataSlice := bytes.SplitN(fileBytes, postBlockSeparator, 3)
if len(dataSlice) != 3 {
return nil, fmt.Errorf("page need front-matter block and markdown block")
}
idx := getFirstBreakByte(dataSlice[1])
if idx == 0 {
return nil, fmt.Errorf("page need front-matter block and markdown block")
}
formatType := detectFormat(string(dataSlice[1][:idx]))
if formatType == 0 {
return nil, fmt.Errorf("page front-matter block is unrecognized")
}
page = new(Page)
if formatType == FormatTOML {
if err = toml.Unmarshal(dataSlice[1][idx:], page); err != nil {
return nil, err
}
}
if formatType == FormatINI {
iniObj, err := ini.Load(dataSlice[1][idx:])
if err != nil {
return nil, err
}
if err = newPageFromIniObject(iniObj, page, "DEFAULT", "meta"); err != nil {
return nil, err
}
}
if page.Node == false {
page.Bytes = bytes.Trim(dataSlice[2], "\n")
}
} else {
page.Bytes = bytes.Trim(fileBytes, "\n")
}
page.fileURL = file
if page.Slug == "" {
page.Slug = slug
}
if page.Date == "" && page.Node == false { // page-node need not time
t, _ := com.FileMTime(file)
page.dateTime = time.Unix(t, 0)
}
return page, page.normalize()
}
作者:nellaivija
项目:RosettaCodeDat
func client(c net.Conn) {
// Close the connection when this function returns.
defer c.Close()
br := bufio.NewReader(c)
fmt.Fprintf(c, "Please enter your name: ")
buf, err := br.ReadBytes('\n')
if err != nil {
error_(err, -1)
return
}
name := string(bytes.Trim(buf, " \t\n\r\x00"))
if name == "" {
fmt.Fprintf(c, "!!! %v is invalid !!!\n", name)
}
// Try to add the connection to the map.
if !clients.Add(name, c) {
fmt.Fprintf(c, "!!! %v is not available !!!\n", name)
return
}
// Send a message telling the clients who connected.
fmt.Fprintf(clients, "+++ %v connected +++\n", name)
// Send a disconnected message when the function returns.
defer fmt.Fprintf(clients, "--- %v disconnected ---\n", name)
// Remove the client from the list.
defer delete(clients, name)
for {
buf, err = br.ReadBytes('\n')
if err != nil {
break
}
buf = bytes.Trim(buf, " \t\n\r\x00")
// Ignore empty messages.
if len(buf) == 0 {
continue
}
switch {
// Support for '/me' type messages.
case string(buf[0:3]) == "/me":
buf = append([]byte(name), buf[3:]...)
default:
// Prepend the user-name and '> '.
buf = append([]byte(name+"> "), buf...)
}
// Send the message to all the clients.
fmt.Fprintf(clients, "%v\n", string(buf))
}
}
作者:cyoun
项目:gortlsd
// GetUsbStrings returns the device information. Note, strings may be empty.
func (dev *Context) GetUsbStrings() (manufact, product, serial string, err error) {
m := make([]byte, 257) // includes space for NULL byte
p := make([]byte, 257)
s := make([]byte, 257)
i := int(C.rtlsdr_get_usb_strings((*C.rtlsdr_dev_t)(dev),
(*C.char)(unsafe.Pointer(&m[0])),
(*C.char)(unsafe.Pointer(&p[0])),
(*C.char)(unsafe.Pointer(&s[0]))))
return string(bytes.Trim(m, "\x00")), string(bytes.Trim(p, "\x00")),
string(bytes.Trim(s, "\x00")), libError(i)
}
作者:cyoun
项目:gortlsd
// GetDeviceUsbStrings returns the information of a device by index.
func GetDeviceUsbStrings(index int) (manufact, product, serial string, err error) {
m := make([]byte, 257) // includes space for NULL byte
p := make([]byte, 257)
s := make([]byte, 257)
i := int(C.rtlsdr_get_device_usb_strings(C.uint32_t(index),
(*C.char)(unsafe.Pointer(&m[0])),
(*C.char)(unsafe.Pointer(&p[0])),
(*C.char)(unsafe.Pointer(&s[0]))))
return string(bytes.Trim(m, "\x00")), string(bytes.Trim(p, "\x00")),
string(bytes.Trim(s, "\x00")), libError(i)
}
作者:MrGost
项目:slingsho
func save_file(title string, content []byte) {
title = string(bytes.Trim([]byte(title), "\x00"))
content = bytes.Trim(content, "\x00")
file, err := os.Create(title)
if err != nil {
fmt.Println("Error during file creation -> ", err)
}
_, err = io.WriteString(file, string(content))
if err != nil {
fmt.Println("Error during writing -> ", err)
}
}
作者:go-xiaohe
项目:pug
// NewPostOfMarkdown create new post from markdown file
func NewPostOfMarkdown(file string, post *Post) (*Post, error) {
fileBytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
if len(fileBytes) < 3 {
return nil, fmt.Errorf("post content is too less")
}
if post == nil {
dataSlice := bytes.SplitN(fileBytes, postBlockSeparator, 3)
if len(dataSlice) != 3 {
return nil, fmt.Errorf("post need front-matter block and markdown block")
}
idx := getFirstBreakByte(dataSlice[1])
if idx == 0 {
return nil, fmt.Errorf("post need front-matter block and markdown block")
}
formatType := detectFormat(string(dataSlice[1][:idx]))
if formatType == 0 {
return nil, fmt.Errorf("post front-matter block is unrecognized")
}
post = new(Post)
if formatType == FormatTOML {
if err = toml.Unmarshal(dataSlice[1][idx:], post); err != nil {
return nil, err
}
}
if formatType == FormatINI {
iniObj, err := ini.Load(dataSlice[1][idx:])
if err != nil {
return nil, err
}
section := iniObj.Section("DEFAULT")
if err = newPostFromIniSection(section, post); err != nil {
return nil, err
}
}
post.Bytes = bytes.Trim(dataSlice[2], "\n")
} else {
post.Bytes = bytes.Trim(fileBytes, "\n")
}
post.fileURL = file
if post.Date == "" {
t, _ := com.FileMTime(file)
post.dateTime = time.Unix(t, 0)
}
return post, post.normalize()
}
作者:alis
项目:mint-clien
func TestRestoreDump(t *testing.T) {
b1, err := ioutil.ReadFile(path.Join(TestDir, "data1.json")) // with validators
if err != nil {
t.Fatal(err)
}
b2, err := ioutil.ReadFile(path.Join(TestDir, "data2.json")) // without
if err != nil {
t.Fatal(err)
}
b1 = bytes.Trim(b1, "\n")
b2 = bytes.Trim(b2, "\n")
// restore to a memdir
config.Set("db_backend", "memdb")
cfg.ApplyConfig(config) // Notify modules of new config
CoreRestore("", b1)
stateDB := dbm.GetDB("state")
st := sm.LoadState(stateDB)
acc := st.GetAccount(ptypes.GlobalPermissionsAddress)
fmt.Println(acc)
dump1 := CoreDump(true) // with validators
if bytes.Compare(b1, dump1) != 0 {
ld, lb := len(dump1), len(b1)
max := int(math.Max(float64(ld), float64(lb)))
n := 100
for i := 0; i < max/n; i++ {
dd := dump1[i*n : (i+1)*n]
bb := b1[i*n : (i+1)*n]
if bytes.Compare(dd, bb) != 0 {
t.Fatalf("Error in dumps! Got \n\n\n\n %s \n\n\n\n Expected \n\n\n\n %s", dd, bb)
}
}
}
CoreRestore("", b2)
dump2 := CoreDump(false) //without validators
if bytes.Compare(b2, dump2) != 0 {
ld, lb := len(dump2), len(b2)
max := int(math.Max(float64(ld), float64(lb)))
n := 100
for i := 0; i < max/n; i++ {
dd := dump2[i*n : (i+1)*n]
bb := b2[i*n : (i+1)*n]
if bytes.Compare(dd, bb) != 0 {
t.Fatalf("Error in dumps! Got \n\n\n\n %s \n\n\n\n Expected \n\n\n\n %s", dd, bb)
}
}
}
}