作者:jdoline
项目:kubernete
func splitByPreformatted(input []byte) fileBlocks {
f := fileBlocks{}
cur := []byte(nil)
preformatted := false
// SplitAfter keeps the newline, so you don't have to worry about
// omitting it on the last line or anything. Also, the documentation
// claims it's unicode safe.
for _, line := range bytes.SplitAfter(input, []byte("\n")) {
if !preformatted {
if preformatRE.Match(line) && !notPreformatRE.Match(line) {
if len(cur) > 0 {
f = append(f, fileBlock{false, cur})
}
cur = []byte{}
preformatted = true
}
cur = append(cur, line...)
} else {
cur = append(cur, line...)
if preformatEndRE.Match(line) {
if len(cur) > 0 {
f = append(f, fileBlock{true, cur})
}
cur = []byte{}
preformatted = false
}
}
}
if len(cur) > 0 {
f = append(f, fileBlock{preformatted, cur})
}
return f
}
作者:kcye
项目:kubernete
// Calls 'replace' for all sections of the document not in ``` / ``` blocks. So
// that you don't have false positives inside those blocks.
func replaceNonPreformatted(input []byte, replace func([]byte) []byte) []byte {
output := []byte(nil)
cur := []byte(nil)
keepBlock := true
// SplitAfter keeps the newline, so you don't have to worry about
// omitting it on the last line or anything. Also, the documentation
// claims it's unicode safe.
for _, line := range bytes.SplitAfter(input, []byte("\n")) {
if keepBlock {
if preformatRE.Match(line) {
cur = replace(cur)
output = append(output, cur...)
cur = []byte{}
keepBlock = false
}
cur = append(cur, line...)
} else {
cur = append(cur, line...)
if preformatRE.Match(line) {
output = append(output, cur...)
cur = []byte{}
keepBlock = true
}
}
}
if keepBlock {
cur = replace(cur)
}
output = append(output, cur...)
return output
}
作者:zeeb
项目:ir
//Flushes any fully formed messages out to the channel
func (conn *Connection) Flush() {
//split the messages in the buffer into individual messages
messages := bytes.SplitAfter(conn.buf, []byte{'\n'})
//if theres only one message, then theres no newline yet
//so continue to buffer
if len(messages) == 1 {
return
}
//chop off the last message because it's just a blank string
for _, message := range messages[:len(messages)-1] {
//attempt to send the message
if n, err := conn.SendMessage(string(message)); err != nil {
//if an error occurs, chop off the bit that was sent
conn.buf = conn.buf[n:]
return
}
//chop off the message from the buffer
conn.buf = conn.buf[len(message):]
}
return
}
作者:johnvilsac
项目:golang-stuf
func main() {
languages := []byte("golang haskell ruby python")
individualLanguages := bytes.Fields(languages)
log.Printf("Fields split %q on whitespace into %q", languages, individualLanguages)
vowelsAndSpace := "aeiouy "
split := bytes.FieldsFunc(languages, func(r rune) bool {
return strings.ContainsRune(vowelsAndSpace, r)
})
log.Printf("FieldsFunc split %q on vowels and space into %q", languages, split)
space := []byte{' '}
splitLanguages := bytes.Split(languages, space)
log.Printf("Split split %q on a single space into %q", languages, splitLanguages)
numberOfSubslices := 2 // Not number of splits
singleSplit := bytes.SplitN(languages, space, numberOfSubslices)
log.Printf("SplitN split %q on a single space into %d subslices: %q", languages, numberOfSubslices, singleSplit)
splitAfterLanguages := bytes.SplitAfter(languages, space)
log.Printf("SplitAfter split %q AFTER a single space (keeping the space) into %q", languages, splitAfterLanguages)
splitAfterNLanguages := bytes.SplitAfterN(languages, space, numberOfSubslices)
log.Printf("SplitAfterN split %q AFTER a single space (keeping the space) into %d subslices: %q", languages, numberOfSubslices, splitAfterNLanguages)
languagesBackTogether := bytes.Join(individualLanguages, space)
log.Printf("Languages are back togeher again! %q == %q? %v", languagesBackTogether, languages, bytes.Equal(languagesBackTogether, languages))
}
作者:GoodGame
项目:ScrollsTradeBo
func ListenTo(url string) (net.Conn, chan []byte) {
con, err := net.Dial("tcp", url)
deny(err)
ch := make(chan []byte)
go func() {
var replyBuffer bytes.Buffer
readBuffer := make([]byte, 1024)
for {
con.SetDeadline(time.Now().Add(time.Minute))
bytesRead, err := con.Read(readBuffer)
if err != nil {
close(ch)
log.Printf("ListenTo connection error: %s", err)
return
}
replyBuffer.Write(readBuffer[:bytesRead])
lines := bytes.SplitAfter(replyBuffer.Bytes(), []byte("\n"))
for _, line := range lines[:len(lines)-1] {
n := len(line)
if n > 1 {
lineCopy := make([]byte, n)
copy(lineCopy, line)
ch <- lineCopy
}
replyBuffer.Next(n)
}
}
}()
return con, ch
}
作者:hg3rdroc
项目:percona-agen
func (c *CmdTest) Run() <-chan string {
output := make(chan string, 1024)
go func() {
for {
b := make([]byte, 8192)
n, err := c.reader.Read(b)
if n > 0 {
lines := bytes.SplitAfter(b[:n], []byte("\n"))
// Example: Split(a\nb\n\c\n) => ["a\n", "b\n", "c\n", ""]
// We are getting empty element because data for split was ending with delimiter (\n)
// We don't want it, so we remove it
lastPos := len(lines) - 1
if len(lines[lastPos]) == 0 {
lines = lines[:lastPos]
}
for i := range lines {
line := string(lines[i])
//log.Printf("%#v", line)
output <- line
}
}
if err != nil {
break
}
}
}()
return output
}
作者:jonas
项目:en
func (s *SliceWriter) Write(b []byte) (int, error) {
lines := bytes.SplitAfter(b, split)
if len(lines) == 0 {
return 0, nil
}
if len(s.buf) > 0 {
lines[0] = append(s.buf, lines[0]...)
}
for _, line := range lines {
if line[len(line)-1] != '\n' {
s.buf = line
break
}
drop := 1
if len(line) > 2 && line[len(line)-2] == '\r' {
drop = 2
}
s.data = append(s.data, string(line[:len(line)-drop]))
}
return len(b), nil
}
作者:mrsmartpant
项目:timberlak
func findStacktrace(client *hdfs.Client, name string) (string, error) {
log.Println("Reading", name)
file, err := client.Open(name)
if err != nil {
return "", err
}
data, err := ioutil.ReadAll(file)
if err != nil {
return "", err
}
var logs [][]byte
lines := bytes.SplitAfter(data, []byte("\n"))
for _, line := range lines {
matched := false
for _, token := range logsToSkip {
if bytes.Contains(line, token) {
matched = true
break
}
}
if !matched {
logs = append(logs, line)
}
}
log.Println("Finished", name)
return string(bytes.Join(logs, nil)), nil
}
作者:Xmagice
项目:origi
// ListPools returns the names of all existing pools.
func (c *Conn) ListPools() (names []string, err error) {
buf := make([]byte, 4096)
for {
ret := int(C.rados_pool_list(c.cluster,
(*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))))
if ret < 0 {
return nil, RadosError(int(ret))
}
if ret > len(buf) {
buf = make([]byte, ret)
continue
}
tmp := bytes.SplitAfter(buf[:ret-1], []byte{0})
for _, s := range tmp {
if len(s) > 0 {
name := C.GoString((*C.char)(unsafe.Pointer(&s[0])))
names = append(names, name)
}
}
return names, nil
}
}
作者:garybur
项目:s3we
func oneLine(p []byte, pattern string) ([]byte, error) {
lines := bytes.SplitAfter(p, nl)
i, err := match(lines, pattern)
if err != nil {
return nil, err
}
return lines[i], nil
}
作者:yana
项目:monst
func (s moduleLog) Write(p []byte) (int, error) {
parts := bytes.SplitAfter(p, []byte("\n"))
for _, part := range parts {
if len(part) > 0 {
s.Log.Print(s.Type, ": ", string(part))
}
}
return len(p), nil
}
作者:gaopeng110
项目:liteide.liteid
func main() {
flag.Parse()
if *showVer == true {
fmt.Println("GoproMake 0.2.1: go files auto build tools. make by [email protected]")
}
gobin, err := newGoBin(*goroot)
if err != nil {
exitln(err)
}
var pro *Gopro
if len(*proFileName) > 0 {
pro, err = makePro(*proFileName)
if err != nil {
exitln(err)
}
} else if len(*goFileName) > 0 {
var input []byte = []byte(*goFileName)
all := bytes.SplitAfter(input, []byte(" "), -1)
pro = new(Gopro)
pro.Values = make(map[string][]string)
for _, v := range all {
pro.Values["GOFILES"] = append(pro.Values["GOFILES"], string(v))
}
}
if pro == nil || err != nil {
Usage()
os.Exit(1)
}
if len(*goTargetName) > 0 {
pro.Values["TARGET"] = []string{*goTargetName}
}
fmt.Println("GoproMake parser files...")
files := pro.Gofiles()
pro.array = ParserFiles(files)
if printDep != nil && *printDep == true {
fmt.Printf("AllPackage:\n%s\n", pro.array)
}
if pro.array.HasMain == false {
*buildLib = true
}
status, err := pro.MakeTarget(gobin)
if err != nil {
exitln(err)
} else if status.ExitStatus() != 0 {
exitln(os.NewError("Error"))
}
os.Exit(0)
}
作者:GavinHw
项目:cockroac
// DecodeIntDecreasingSlice decodes a byte slice
// in decreasing order and return an int64 slice.
func DecodeIntDecreasingSlice(buf []byte) []int64 {
s := bytes.SplitAfter(buf, []byte{orderedEncodingTerminator})
res := make([]int64, len(s))
// There will be an empty slice after the last terminator,
// thus we have to skip that.
for idx := 0; idx < len(s)-1; idx++ {
res[idx] = DecodeIntDecreasing(s[idx])
}
return res
}
作者:krouse
项目:goyan
// Bytes returns b with each line in b prefixed by indent.
func Bytes(indent, b []byte) []byte {
if len(indent) == 0 || len(b) == 0 {
return b
}
lines := bytes.SplitAfter(b, []byte{'\n'})
if len(lines[len(lines)-1]) == 0 {
lines = lines[:len(lines)-1]
}
return bytes.Join(append([][]byte{[]byte{}}, lines...), indent)
}
作者:Goryudyum
项目:go-wor
func handleClient(conn net.Conn) {
defer conn.Close()
conn.SetReadDeadline(time.Now().Add(100 * time.Second))
//fmt.Println("client accept!")
messageBuf := make([]byte, 1024)
messageLen, err := conn.Read(messageBuf)
checkError(err)
message := string(messageBuf[:messageLen])
//message = strings.Trim(message, "\n")
//fmt.Println(message)
if check_regexp(`[^(KILHZON\+\-\.\d\s)]`, message) {
//fmt.Println(message)
out := []byte("error2\n")
//fmt.Println("error2")
conn.SetWriteDeadline(time.Now().Add(100 * time.Second))
conn.Write([]byte(out))
} else {
cmd := exec.Command("/root/work/cpp/dobutsu/checkState", message)
if err != nil {
//fmt.Println(err)
//os.Exit(1)
}
out, err := cmd.Output()
if err != nil {
//fmt.Println(err)
out = []byte("error\n")
//os.Exit(1)
} else {
out2 := bytes.SplitAfter(out, []byte("Move : "))
out3 := bytes.SplitAfter(out2[1], []byte("\n"))
out = out3[0]
}
//fmt.Println(err)
//fmt.Println(out)
//out = strings.Trim(out, "\n")
conn.SetWriteDeadline(time.Now().Add(100 * time.Second))
conn.Write([]byte(out))
}
}
作者:nightlyon
项目:periodicnois
func (l *LineWriter) Write(p []byte) (n int, err error) {
for _, line := range bytes.SplitAfter(p, lf) {
var written int
written, err = l.w.Write(line)
n += written
if err != nil {
return n, err
}
}
return n, err
}
作者:rrawrri
项目:life-ctr
func ReadParam(l []byte, field string) ([]byte, error) {
cutset := []byte(fmt.Sprintf("%v:", field))
s := bytes.SplitAfter(l, cutset)
if len(s) != 2 {
m := fmt.Sprintf("Wrong %v line", field)
err := errors.New(m)
return []byte{}, err
}
return CleanLine(s[1]), nil
}
作者:tonymagr
项目:asig
func (s *ASign) Write(p []byte) (n int, err error) {
parts := bytes.SplitAfter(p, []byte{STX})
var pn int
for _, p := range parts {
pn, err = s.Rw.Write(p)
n += pn
if err != nil {
return
}
time.Sleep(time.Millisecond * s.StxDelay)
}
return
}
作者:objStorag
项目:grado
// bufToStringSlice converts a C buffer containing several strings separated by \0 to a string slice.
func bufToStringSlice(bufAddr *C.char, ret C.int) []string {
reader := bufToReader(bufAddr, ret)
buf := new(bytes.Buffer)
buf.ReadFrom(reader)
result := make([]string, 0)
tmp := bytes.SplitAfter(buf.Bytes()[:ret-1], []byte{0})
for _, s := range tmp {
if len(s) > 0 {
result = append(result, string(s))
}
}
return result
}
作者:krouse
项目:goyan
// Write implements io.Writer.
func (w *iw) Write(buf []byte) (int, error) {
if len(buf) == 0 {
return 0, nil
}
lines := bytes.SplitAfter(buf, []byte{'\n'})
if len(lines[len(lines)-1]) == 0 {
lines = lines[:len(lines)-1]
}
if !w.partial {
lines = append([][]byte{[]byte{}}, lines...)
}
buf = bytes.Join(lines, w.prefix)
w.partial = buf[len(buf)-1] != '\n'
return w.w.Write(buf)
}