Example #1
0
func main() {
	s := []byte("1,2,3,4")
	sep := []byte{','}
	print(bytes.SplitAfterN(s, sep, 2))
	print(bytes.SplitAfterN(s, sep, 0))
	print(bytes.SplitAfterN(s, sep, -1))
}
Example #2
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))
}
Example #3
0
func allNameSegments(html []byte) (nameSegments [][]byte) {
	lines := bytes.Split(html, []byte("\n"))
	for _, line := range lines {
		anchor := bytes.SplitAfterN(line, []byte("href=\""), 2)
		if len(anchor) > 1 {
			reference := bytes.SplitN(anchor[1], []byte("/\""), 2)
			nameSegments = append(nameSegments, reference[0])
		}
	}
	return
}
Example #4
0
func (mp *MsgParser) lineFrom(input []byte) ([]byte, []byte) {
	split := bytes.SplitAfterN(input, []byte("\n"), 2)
	if len(split) == 2 {
		return split[0][:len(split[0])-1], split[1]
	}

	if !mp.partialReads {
		if len(input) == 0 {
			input = nil
		}
		return input, []byte{}
	}

	if bytes.HasSuffix(input, []byte("\n")) {
		return input[:len(input)-1], []byte{}
	}

	return nil, input
}
Example #5
0
func read_response(dc *net.TCPConn, c *net.TCPConn) []byte {
	var buffer = make([]byte, 0, TCP_BUFFER_SIZE)
	var fbuffer = make([]byte, 0)

	content_length := -1
	cursize := 0

	for {
		// Size the buffer
		buffer = buffer[0:TCP_BUFFER_SIZE]

		// Read from the connection
		size, err := c.Read(buffer)
		if err != nil {
			upstream_log.Printf("%v=>%v: Connection closed", dc.RemoteAddr(), c.RemoteAddr())
			return buffer
		}

		// Resize the buffer
		buffer = buffer[0:size]
		fbuffer = concat(fbuffer, buffer)
		cursize += size

		//if (content_length == -1) {
		split_buffer := bytes.SplitAfterN(fbuffer, []byte("\r\n\r\n"), 2)
		if len(split_buffer) > 1 {
			content_length = read_content_length(string(split_buffer[0]))
			upstream_log.Printf("%v=>%v: Content-Length: %v", dc.RemoteAddr(), c.RemoteAddr(), content_length)
		}
		//}

		if content_length == cursize {
			break
		} else {
			upstream_log.Printf("Packet incomplete %v != %v", content_length, cursize)
		}
	}

	//upstream_log.Printf("%v=>%v: Response Received:\n%v", dc.RemoteAddr(), c.RemoteAddr(), string(buffer))

	return fbuffer
}
func (w *streamWriter) Write(b []byte) (int, error) {
	w.b = append(w.b, b...)
	writtenCount := len(b)
	for len(w.b) > 0 {
		parts := bytes.SplitAfterN(w.b, []byte("\n"), 2)
		err := w.formatter.Format(w.w, parts[0])
		if err != nil {
			if len(parts) == 1 {
				return writtenCount, nil
			} else {
				return writtenCount, fmt.Errorf("Unparseable chunk: %q", string(parts[0]))
			}
		}
		if len(parts) == 1 {
			w.b = []byte{}
		} else {
			w.b = parts[1]
		}
	}
	return writtenCount, nil
}
Example #7
0
// parseJavaProfile returns a new profile from heapz or contentionz
// data. b is the profile bytes after the header.
func parseJavaProfile(b []byte) (*Profile, error) {
	h := bytes.SplitAfterN(b, []byte("\n"), 2)
	if len(h) < 2 {
		return nil, errUnrecognized
	}

	p := &Profile{
		PeriodType: &ValueType{},
	}
	header := string(bytes.TrimSpace(h[0]))

	var err error
	var pType string
	switch header {
	case "--- heapz 1 ---":
		pType = "heap"
	case "--- contentionz 1 ---":
		pType = "contention"
	default:
		return nil, errUnrecognized
	}

	if b, err = parseJavaHeader(pType, h[1], p); err != nil {
		return nil, err
	}
	var locs map[uint64]*Location
	if b, locs, err = parseJavaSamples(pType, b, p); err != nil {
		return nil, err
	}
	if err = parseJavaLocations(b, locs, p); err != nil {
		return nil, err
	}

	// Strip out addresses for better merge.
	if err = p.Aggregate(true, true, true, true, false); err != nil {
		return nil, err
	}

	return p, nil
}
Example #8
0
func main() {
	//Compare
	s1, s2 := "hello", "world"
	fmt.Printf("s1 Compare s2 is %d\n", bytes.Compare([]byte(s1), []byte(s2)))

	//Count
	fmt.Printf("%s has %d 'l'\n", s1, bytes.Count([]byte(s1), []byte("l")))

	//Contains
	fmt.Printf("%s contains 'ab' is %v\n", s2, bytes.Contains([]byte(s2), []byte("ab")))

	//Index
	fmt.Printf("ll is %d at Hello\n", bytes.Index([]byte(s1), []byte("ll")))

	//LastIndex
	fmt.Printf("last l is %d at Hello\n", bytes.LastIndex([]byte(s1), []byte("l")))

	//IndexRune
	fmt.Printf("“五” 在 “第二十六招:炮五进四”中第%d个位子\n", bytes.IndexRune([]byte("“第二十六招:炮五进四”"), rune('五')))

	//IndexAny
	fmt.Printf("ae is %d at helloworld\n", bytes.IndexAny([]byte("helloworld"), "ae"))

	//LastIndexAny

	//SplitN
	fmt.Printf("splitN helloworld by o: %s\n", bytes.SplitN([]byte("helloworld"), []byte("o"), -1))

	//SplitAfterN
	fmt.Printf("splitAflterN helloworld by o: %s\n", bytes.SplitAfterN([]byte("helloworld"), []byte("o"), -1))

	//Split
	//SplitAfter

	//Fields
	fmt.Printf("helloworld fields: %s\n", bytes.Fields([]byte("he ll\ro\nwo\tr' 'l\fd")))

	//TBC
}
Example #9
0
func SignatureVerifyHandler(w http.ResponseWriter, r *http.Request) {
	body, _ := ioutil.ReadAll(r.Body)
	// Get first line from body
	splits := bytes.SplitAfterN(body, []byte("\n"), 2)
	if len(splits) != 2 {
		w.WriteHeader(500)
		return
	}
	// Parse data in the form of: <!--data-->\n
	splits[0] = splits[0][4 : len(splits[0])-4]
	ss := ScriptSignature{}
	err := json.Unmarshal(splits[0], &ss)
	if err != nil {
		w.WriteHeader(500)
		return
	}
	if ss.V != 0 {
		w.WriteHeader(500)
		return
	}
	privJS, err := getUserAttribute("", "private_key")
	var priv dsa.PrivateKey
	if err != nil {
		w.WriteHeader(500)
		return
	}
	err = json.Unmarshal([]byte(privJS), &priv)
	if err != nil {
		w.WriteHeader(500)
		return
	}
	if !SignatureVerify(hashScript(splits[1]), &priv, []byte(ss.R), []byte(ss.S)) {
		w.WriteHeader(500)
		return
	}
}
Example #10
0
func ExampleSplitAfterN() {
	fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2))
	// Output: ["a," "b,c"]
}
Example #11
0
func nullTerminatedString(b []byte) string {
	chunks := bytes.SplitAfterN(b, []byte("\x00"), 2)
	return string(chunks[0])
}