Esempio n. 1
0
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
}
Esempio n. 2
0
// 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
}
Esempio n. 3
0
File: irc.go Progetto: zeebo/irc
//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
}
Esempio n. 4
0
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))
}
Esempio n. 5
0
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
}
Esempio n. 6
0
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
}
Esempio n. 7
0
File: slice.go Progetto: jonasi/env
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
}
Esempio n. 8
0
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
}
Esempio n. 9
0
// 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
	}
}
Esempio n. 10
0
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
}
Esempio n. 11
0
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
}
Esempio n. 12
0
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)
}
Esempio n. 13
0
// 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
}
Esempio n. 14
0
// 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)
}
Esempio n. 15
0
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))
	}
}
Esempio n. 16
0
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
}
Esempio n. 17
0
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
}
Esempio n. 18
0
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
}
Esempio n. 19
0
// 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
}
Esempio n. 20
0
// 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)
}
Esempio n. 21
0
func NewGoProject(name string) (pro *GoProject, err error) {
	buf, e := ioutil.ReadFile(name)
	if e != nil {
		err = e
		return
	}
	pro = new(GoProject)
	pro.Name = name
	pro.Values = make(map[string][]string)

	pre, e := regexp.Compile("\\\\[\t| ]*[\r|\n]+[\t| ]*") //("\\\\[^a-z|A-Z|0-9|_|\r|\n]*[\r|\n]+[^a-z|A-Z|0-9|_|\r|\n]*")
	if e != nil {
		err = e
		return
	}
	all := pre.ReplaceAll(buf[:], []byte(" "))
	all = bytes.Replace(all, []byte("\r\n"), []byte("\n"), -1)
	lines := bytes.Split(all, []byte("\n"))

	for _, line := range lines {
		offset := 2
		line = bytes.Replace(line, []byte("\t"), []byte(" "), -1)
		if len(line) >= 1 && line[0] == '#' {
			continue
		}
		find := bytes.Index(line, []byte("+="))
		if find == -1 {
			offset = 1
			find = bytes.Index(line, []byte("="))
		}
		if find != -1 {
			k := bytes.TrimSpace(line[0:find])
			v := bytes.SplitAfter(line[find+offset:], []byte(" "))
			var vall []string
			if offset == 2 {
				vall = pro.Values[string(k)]
			}
			for _, nv := range v {
				nv2 := bytes.TrimSpace(nv)
				if len(nv2) != 0 && string(nv2) != "\\" {
					vall = append(vall, string(nv2))
				}
			}
			pro.Values[string(k)] = vall
		}
	}
	return
}
Esempio n. 22
0
func TestWriter(t *testing.T) {
	a := AppendWriter{}
	w := NewWriter(&a, "\n", false)
	sample := []byte("foo\nbar\nbaz")
	want := bytes.SplitAfter(sample, []byte("\n"))
	w.Write(sample)
	if len(a) != len(want) {
		t.Errorf("got %d writes, want %d", len(a), len(want))
	}

	for i, v := range want {
		if !bytes.Equal(v, a[i]) {
			t.Errorf("%d: got '%s', want '%s'", a[i], v)
		}
	}
}
Esempio n. 23
0
func compareT() {

	fmt.Println(bytes.Compare([]byte("你好"), []byte("你hao")))

	sl := []byte("s '! ' !")
	sli := []byte("'")
	fmt.Println(string(sl), sli,
		bytes.Contains(sl, sli), bytes.Count(sl, sli),
		bytes.Fields(sl), bytes.Fields(sli),
		bytes.Index(sl, sli),
		bytes.Join([][]byte{sl, sli}, []byte("_______________-----")), //php implode
		string(bytes.Replace(sl, []byte("!"), []byte("?"), -1)),
		bytes.Split(sl, []byte("!")),      //php explode
		bytes.SplitAfter(sl, []byte("!")), // 前两个切片包含!
	)
}
Esempio n. 24
0
func (b *Buffer) updLines() {
	slines := bytes.SplitAfter(b.data, []byte("\n"))
	b.lines = make([][]rune, len(slines))
	for i, l := range slines {
		b.lines[i] = bytes.Runes(l)
	}
	if len(b.lines) > 0 {
		i := len(b.lines) - 1
		l := b.lines[i]
		if len(l) > 0 && l[len(l)-1] != '\n' {
			b.lines[i] = append(l, '\n')
		} else if len(b.lines[i]) == 0 {
			b.lines = b.lines[:i]
		}
	}
}
Esempio n. 25
0
/* ProxyChannel copies data from one channel to another */
func ProxyChannel(
	w io.Writer,
	r io.Reader,
	lg *log.Logger,
	tag string,
	wg chan<- int,
	token int,
) {
	defer func(c chan<- int, i int) { c <- i }(wg, token)
	var (
		buf  = make([]byte, BUFLEN)
		done = false
		n    int
		err  error
	)
	var lines [][]byte
	for !done {
		/* Reset buffer */
		buf = buf[:cap(buf)]
		/* Read a bit */
		n, err = r.Read(buf)
		buf = buf[:n]
		if nil != err {
			lg.Printf("[%v] Read Error: %v", tag, err)
			done = true
		}
		/* Don't bother if we didn't get anything */
		if 0 == n {
			continue
		}
		/* Send it on */
		if _, err = w.Write(buf); nil != err {
			lg.Printf("[%v] Write Error: %v", tag, err)
			done = true
		}
		if done {
			continue
		}
		/* Log it all */
		lines = bytes.SplitAfter(buf, []byte{'\n'})
		for i := range lines {
			lg.Printf("[%v] %q", tag, lines[i])
		}
	}
	lg.Printf("[%v] Finished", tag)
}
Esempio n. 26
0
// Listen to an URL and send line by line into a channel
// Returns the connection and said channel
func ListenToURL(url string) (net.Conn, chan []byte) {
	// Connect to the specified URL
	con, err := net.Dial("tcp", url)
	deny(err)

	log.Println("Listening on new connection:", url)

	// Make the channel (it can send and recieve byte-slices)
	ch := make(chan []byte)

	go func() {
		var chBuffer bytes.Buffer
		readFromCon := make([]byte, 1024)

		for {
			// Read 1024 bytes
			bytesRead, err := con.Read(readFromCon)
			if err != nil {
				close(ch)
				log.Println("Connection error:", err)
				return
			}

			// Buffer them
			chBuffer.Write(readFromCon[:bytesRead])

			// Cut into lines
			lines := bytes.SplitAfter(chBuffer.Bytes(), []byte("\n"))

			// Send lines to the through the channel
			for _, line := range lines[:len(lines)-1] {
				n := len(line)
				if n > 1 {
					lineCopy := make([]byte, n)
					copy(lineCopy, line)
					ch <- lineCopy
				}

				chBuffer.Next(n)
			}
		}
	}()

	return con, ch
}
Esempio n. 27
0
// Compile .go file, import data from .o file, and write Go string version.
func mkbuiltin(w io.Writer, name string) {
	args := []string{"tool", "compile", "-A"}
	if name == "runtime" {
		args = append(args, "-u")
	}
	args = append(args, "builtin/"+name+".go")

	if err := exec.Command("go", args...).Run(); err != nil {
		log.Fatal(err)
	}
	obj := name + ".o"
	defer os.Remove(obj)

	b, err := ioutil.ReadFile(obj)
	if err != nil {
		log.Fatal(err)
	}

	// Look for $$ that introduces imports.
	i := bytes.Index(b, []byte("\n$$\n"))
	if i < 0 {
		log.Fatal("did not find beginning of imports")
	}
	i += 4

	// Look for $$ that closes imports.
	j := bytes.Index(b[i:], []byte("\n$$\n"))
	if j < 0 {
		log.Fatal("did not find end of imports")
	}
	j += i + 4

	// Process and reformat imports.
	fmt.Fprintf(w, "\nconst %simport = \"\"", name)
	for _, p := range bytes.SplitAfter(b[i:j], []byte("\n")) {
		// Chop leading white space.
		p = bytes.TrimLeft(p, " \t")
		if len(p) == 0 {
			continue
		}

		fmt.Fprintf(w, " +\n\t%q", p)
	}
	fmt.Fprintf(w, "\n")
}
Esempio n. 28
0
func multipleLines(p []byte, pattern1, pattern2 string) ([]byte, error) {
	lines := bytes.SplitAfter(p, nl)
	line1, err := match(lines, pattern1)
	if err != nil {
		return nil, err
	}
	line2, err := match(lines[line1:], pattern2)
	if err != nil {
		return nil, err
	}
	line2 += line1
	for i := line1; i <= line2; i++ {
		if bytes.HasSuffix(lines[i], omitNl) {
			lines[i] = []byte{}
		}
	}
	return bytes.Join(lines[line1:line2+1], []byte{}), nil
}
Esempio n. 29
0
func (l *LineWriter) Write(p []byte) (n int, err error) {
	for _, line := range bytes.SplitAfter(p, lf) {

		// drop pure whitespace lines and fake successful write of them
		if nospaces := bytes.TrimSpace(line); bytes.Equal(nospaces, lf) {
			n += len(line)
			continue
		}

		var written int
		written, err = l.w.Write(line)
		n += written
		if err != nil {
			return n, err
		}
	}
	return n, err
}
Esempio n. 30
0
// getHeadersList returns headers as list
func getHeadersList(rawHeader *[]byte) (*list.List, error) {
	headersList := list.New()
	currentHeader := []byte{}
	for _, line := range bytes.SplitAfter(*rawHeader, []byte{10}) {
		if line[0] == 32 || line[0] == 9 {
			if len(currentHeader) == 0 {
				return headersList, ErrBadMailFormatHeaders
			}
			currentHeader = append(currentHeader, line...)
		} else {
			// New header, save current if exists
			if len(currentHeader) != 0 {
				headersList.PushBack(string(bytes.TrimRight(currentHeader, "\r\n")))
				currentHeader = []byte{}
			}
			currentHeader = append(currentHeader, line...)
		}
	}
	headersList.PushBack(string(currentHeader))
	return headersList, nil
}