Example #1
0
func BenchmarkWritePid(b *testing.B) {
	b.StopTimer()
	c := new(Context)

	rand.Seed(time.Now().UnixNano())
	max := 64
	length := 16
	pids := make([]Pid, max)

	for i := 0; i < max; i++ {
		s := bytes.Repeat([]byte{'a'}, length)
		b := bytes.Map(randRune, s)
		b[6] = '@'
		pids[i] = Pid{
			Atom(b),
			uint32(rand.Intn(65536)),
			uint32(rand.Intn(256)),
			byte(rand.Intn(16)),
		}
	}

	b.StartTimer()

	for i := 0; i < b.N; i++ {
		in := pids[i%max]
		if err := c.writePid(Discard, in); err != nil {
			b.Fatal(in, err)
		}
	}
}
Example #2
0
func (h *hub) run(st *store) {
	h.db = db
	for {
		select {
		case c := <-h.register:
			Log("Registering...\n")
			h.connections[c] = true
			c.stcmd = st.cmd
		case c := <-h.unregister:
			if _, ok := h.connections[c]; ok {
				Log("UnRegistering...\n")
				delete(h.connections, c)
				close(c.cmd)
			}
		case m := <-h.broadcast:
			Log("Broadcasting...\n")
			m = bytes.Map(func(r rune) rune {
				if r < ' ' {
					return -1
				}
				return r
			}, m)
			for c := range h.connections {
				c.cmd <- m
			}
		}
	}
}
Example #3
0
File: util.go Project: ichyo/go-aoj
func APIRequest(api string, values url.Values) ([]byte, error) {
	query := values.Encode()
	if query != "" {
		query = "?" + query
	}
	url := api + query

	response, err := http.Get(url)
	if err != nil {
		return []byte{}, err
	}

	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return []byte{}, err
	}

	res := bytes.Map(func(r rune) rune {
		switch r {
		case '\r', '\n':
			return -1
		default:
			return r
		}
	}, body)

	return res, nil
}
Example #4
0
func BenchmarkReadPid(b *testing.B) {
	b.StopTimer()
	c := new(Context)

	rand.Seed(time.Now().UnixNano())
	max := 64
	length := 16
	pids := make([]*bytes.Buffer, max)

	for i := 0; i < max; i++ {
		w := new(bytes.Buffer)
		s := bytes.Repeat([]byte{'a'}, length)
		b := bytes.Map(randRune, s)
		b[6] = '@'
		w.Write([]byte{ettPid, ettSmallAtom, byte(length)})
		w.Write(b)
		w.Write([]byte{0, 0, 0, uint8(rand.Int())})
		w.Write([]byte{0, 0, 0, uint8(rand.Int())})
		w.Write([]byte{uint8(rand.Int())})
		pids[i] = w
	}

	b.StartTimer()

	for i := 0; i < b.N; i++ {
		in := pids[i%max]
		_, err := c.Read(in)

		if err != io.EOF && err != nil {
			b.Fatal(err)
		}
	}
}
Example #5
0
func BenchmarkReadString(b *testing.B) {
	b.StopTimer()
	c := new(Context)

	rand.Seed(time.Now().UnixNano())
	max := 64
	length := 64
	strings := make([]*bytes.Buffer, max)

	for i := 0; i < max; i++ {
		w := new(bytes.Buffer)
		s := bytes.Repeat([]byte{'a'}, length)
		b := bytes.Map(randRune, s)
		w.Write([]byte{ettString})
		binary.Write(w, binary.BigEndian, uint16(len(b)))
		w.Write(b)
		strings[i] = w
	}

	b.StartTimer()

	for i := 0; i < b.N; i++ {
		in := strings[i%max]
		_, err := c.Read(in)

		if err != io.EOF && err != nil {
			b.Fatal(err)
		}
	}
}
Example #6
0
func BenchmarkReadBinary(b *testing.B) {
	b.StopTimer()
	c := new(Context)

	rand.Seed(time.Now().UnixNano())
	max := 64
	length := 64
	binaries := make([]*bytes.Buffer, max)

	for i := 0; i < max; i++ {
		w := new(bytes.Buffer)
		s := bytes.Repeat([]byte{'a'}, length)
		b := bytes.Map(func(rune) rune { return rune(byte(rand.Int())) }, s)
		w.Write([]byte{ettBinary})
		binary.Write(w, binary.BigEndian, uint32(len(b)))
		w.Write(b)
		binaries[i] = w
	}

	b.StartTimer()

	for i := 0; i < b.N; i++ {
		in := binaries[i%max]
		_, err := c.Read(in)

		if err != io.EOF && err != nil {
			b.Fatal(err)
		}
	}
}
Example #7
0
func BenchmarkWriteBinary(b *testing.B) {
	b.StopTimer()
	c := new(Context)

	rand.Seed(time.Now().UnixNano())
	max := 64
	length := 64
	binaries := make([][]byte, max)

	for i := 0; i < max; i++ {
		s := bytes.Repeat([]byte{'a'}, length)
		binaries[i] = bytes.Map(
			func(rune) rune { return rune(byte(rand.Int())) },
			s,
		)
	}

	b.StartTimer()

	for i := 0; i < b.N; i++ {
		in := binaries[i%max]
		if err := c.writeBinary(Discard, in); err != nil {
			b.Fatal(in, err)
		}
	}
}
Example #8
0
// Parse input parses a slice of bytes representing the sudoku challenge and
// returns a slice of ints.
func parseInput(in []byte) ([]int, error) {
	// Use bytes.Map to eliminate all characters that are not ints or underscores.
	// Replace underscores by 0.
	res := bytes.Map(func(r rune) rune {
		if string(r) == "_" {
			return '0'
		} else if _, err := strconv.Atoi(string(r)); err != nil {
			return -1
		} else {
			return r
		}
	}, in)

	// The result should be 81 characters long. If not, the input was not valid.
	if len(res) != 81 {
		return nil, errors.New("Input was not in a valid format!")
	}

	// Convert the result in a slice of integers.
	ints := make([]int, 81)
	for i, r := range string(res) {
		// no need to check for errs, we made sure everything is an int during the
		// mapping earlier
		ints[i], _ = strconv.Atoi(string(r))
	}
	return ints, nil
}
Example #9
0
func main() {
	s1 := []byte("大家上午好")
	s2 := []byte("12345678")
	m1 := func(r rune) rune {
		if r == '上' {
			return '下'
		}
		return r
	}
	m2 := func(r rune) rune {
		return r + 1
	}
	fmt.Println(string(bytes.Map(m1, s1)))
	fmt.Println(string(s1))
	fmt.Println(string(bytes.Map(m2, s2)))
	fmt.Println(string(s2))
}
Example #10
0
func removeNewlines(p []byte) []byte {
	return bytes.Map(func(r rune) rune {
		switch r {
		case '\n', '\r':
			return -1
		}
		return r
	}, p)
}
Example #11
0
func chordMechanic(in []byte) (string, string, int) {
	var chord, khord string
	var mec int
	var isRelease, isButton bool
	if spl := bytes.Split(in, []byte("+")); spl[len(spl)-1][0] == RELEASE {
		isRelease = true
	}
	if bytes.Contains(in, []byte("button")) {
		isButton = true
	}
	cut := func(r rune) rune {
		if r == '@' {
			return -1
		}
		return r
	}
	switch {
	case !isRelease && !isButton:
		s := string(in)
		chord = s
		khord = s
		mec = KeyPress
	case isRelease && !isButton:
		chord = string(bytes.Map(cut, in))
		khord = string(in)
		mec = KeyRelease
	case !isRelease && isButton:
		s := string(in)
		chord = s
		khord = s
		mec = ButtonPress
	case isRelease && isButton:
		chord = string(bytes.Map(cut, in))
		khord = string(in)
		mec = ButtonRelease
	default:
		s := string(in)
		chord = s
		khord = s
		mec = KeyPress
	}
	return chord, khord, mec
}
Example #12
0
func sortCompact(text []byte) (buf []byte, histo map[string]int, numTokens int) {

	// text = bytes.Replace(text, []byte(" hbr"), []byte{}, -1)
	// text = bytes.Replace(text, []byte(" sbr"), []byte{}, -1)
	text = bytes.Replace(text, []byte(`[img] `), []byte{}, -1)
	// text = bytes.Replace(text, []byte(`[a] `), []byte{}, -1)

	mapping := func(r rune) rune {
		if ret, ok := sortCompactReplace[r]; ok {
			return ret
		}
		return r
	}

	text = bytes.Map(mapping, text)

	words := bytes.Fields(text)

	histo = map[string]int{}
	for _, word := range words {
		sword := string(word)
		sword = strings.TrimSpace(sword)
		sword = strings.ToLower(sword)
		if len(words) > 3 {
			if len(sword) > 3 {
				histo[sword]++
			}
		} else {
			histo[sword]++ // no minimum length for tiny texts
		}
	}
	numTokens = len(histo)

	keys := make([]string, 0, len(histo))
	for k, _ := range histo {
		keys = append(keys, k)
	}

	sort.Strings(keys)

	buf = []byte{32}
	for _, key := range keys {
		if len(key) > 1 {
			buf = append(buf, []byte(key)...)
			buf = append(buf, byte(32))
			// num := fmt.Sprintf("%v", mp[key])
			// buf = append(buf, []byte(num)...)
			// buf = append(buf, byte(32))
		}
	}

	buf = bytes.TrimSpace(buf)

	return
}
Example #13
0
func main() {
	flag.Parse()

	log.Printf("Fetching %q...", *remote)
	resp, err := http.Get(*remote)
	if err != nil {
		log.Fatalf("failed to download from %q: %s", *remote, err)
	}
	defer resp.Body.Close()

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalf("failed to read %q: %s", *remote, err)
	}

	ids, cls, err := usbid.ParseIDs(bytes.NewReader(data))
	if err != nil {
		log.Fatalf("failed to parse %q: %s", *remote, err)
	}

	log.Printf("Successfully fetched %q:", *remote)
	log.Printf("  Loaded %d Vendor IDs", len(ids))
	log.Printf("  Loaded %d Class IDs", len(cls))

	rawTemplate, err := ioutil.ReadFile(*dataFile)
	if err != nil {
		log.Fatalf("failed to read template %q: %s", *dataFile)
	}

	template, err := template.New("").Parse(string(rawTemplate))
	if err != nil {
		log.Fatalf("failed to parse template %q: %s", *dataFile, err)
	}

	out, err := os.Create(*outFile)
	if err != nil {
		log.Fatalf("failed to open output file %q: %s", *outFile, err)
	}
	defer out.Close()

	templateData := struct {
		Data      []byte
		Generated time.Time
		RFC3339   string
	}{
		Data:      bytes.Map(sanitize, data),
		Generated: time.Now(),
	}
	if err := template.Execute(out, templateData); err != nil {
		log.Fatalf("failed to execute template: %s", err)
	}

	log.Printf("Successfully wrote %q", *outFile)
}
Example #14
0
func maptest() {
	s := []byte("各位上午好")
	m := func(r rune) rune {
		if r == '上' {
			r = '下'
		}
		return r
	}

	fmt.Println(string(s))
	fmt.Println(string(bytes.Map(m, s)))
}
Example #15
0
func ExampleMap() {
	rot13 := func(r rune) rune {
		switch {
		case r >= 'A' && r <= 'Z':
			return 'A' + (r-'A'+13)%26
		case r >= 'a' && r <= 'z':
			return 'a' + (r-'a'+13)%26
		}
		return r
	}
	fmt.Printf("%s", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher...")))
	// Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
}
Example #16
0
// BenchmarkByteByteMap compares byteByteImpl against Map.
func BenchmarkByteByteMap(b *testing.B) {
	str := strings.Repeat("a", 100) + strings.Repeat("b", 100)
	fn := func(r rune) rune {
		switch r {
		case 'a':
			return 'A'
		case 'b':
			return 'B'
		}
		return r
	}
	for i := 0; i < b.N; i++ {
		bytes.Map(fn, []byte(str))
	}
}
Example #17
0
func FixedXor(input string, mask string) (output string, err error) {
	var decodeErr error
	var inputBytes, maskBytes []byte
	inputBytes, decodeErr = hex.DecodeString(input)
	if decodeErr != nil {
		return "", decodeErr
	}
	maskBytes, decodeErr = hex.DecodeString(mask)
	if decodeErr != nil {
		return "", decodeErr
	}

	resultBytes := bytes.Map(makeRepeatingXorClojure(maskBytes), inputBytes)
	return hex.EncodeToString(resultBytes), nil
}
Example #18
0
// normalize does unicode normalization.
func normalize(in []byte) ([]byte, error) {
	// We need a new transformer for each input as it cannot be reused.
	filter := func(r rune) bool {
		return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks (to be removed)
	}
	transformer := transform.Chain(norm.NFD, transform.RemoveFunc(filter), norm.NFC)
	out, _, err := transform.Bytes(transformer, in)
	out = bytes.Map(func(r rune) rune {
		if unicode.IsPunct(r) { // Replace punctuations with spaces.
			return ' '
		}
		return unicode.ToLower(r) // Convert to lower case.
	}, out)
	return out, err
}
Example #19
0
func ParseGrass(src []byte) Code {
	filter := func(r rune) rune {
		switch r {
		case 'w', 'w':
			return 'w'
		case 'W', 'W':
			return 'W'
		case 'v', 'V', 'v', 'V':
			return 'v'
		}
		return -1
	}
	pat := regexp.MustCompile("^(w*)(W+w+)*$")
	apppat := regexp.MustCompile("(W+)(w+)")
	insts := bytes.Split(bytes.Map(filter, src), []byte{'v'})
	tmp := make([]*Code, len(insts))
	codelen := 0
	for i, p := range insts {
		if len(p) == 0 {
			die("syntax error at section %d", i)
		}
		m := pat.FindSubmatchIndex(p)
		if len(m) == 0 {
			die("syntax error at section %d", i)
		}
		arity := m[3] - m[2]
		apps := apppat.FindAllSubmatchIndex(p[arity:], -1)
		body := make(Code, len(apps))
		for j, m := range apps {
			body[j] = &App{fun: m[3] - m[2] - 1, arg: m[5] - m[4] - 1}
		}
		for arity > 0 {
			body = Code{&Abs{body: body}}
			arity--
		}
		tmp[i] = &body
		codelen += len(body)
	}
	code := make(Code, codelen, codelen+1)
	i := 0
	for _, c := range tmp {
		for _, insn := range *c {
			code[i] = insn
			i++
		}
	}
	return code
}
Example #20
0
func main() {
	golang := []byte("golang")

	// Map
	loudGolang := bytes.Map(asciiAlphaUpcase, golang)
	log.Printf("Turned %q into %q (ASCII alphabet upcase!)", golang, loudGolang)

	// Repalce
	original := []byte("go")
	replacement := []byte("Google Go")
	googleGolang := bytes.Replace(golang, original, replacement, -1)
	log.Printf("Replaced %q in %q with %q to get %q", original, golang, replacement, googleGolang)

	// Runes
	runes := bytes.Runes(golang)
	log.Printf("%q is made up of the following runes (in this case, ASCII codes): %v", golang, runes)

	// Repeat
	n := 8
	na := []byte("Na")
	batman := []byte(" Batman!")
	log.Printf("Made %d copies of %q and appended %q to get %q", n, na, batman, append(bytes.Repeat(na, n), batman...))
}
Example #21
0
func BenchmarkWriteString(b *testing.B) {
	b.StopTimer()
	c := new(Context)

	rand.Seed(time.Now().UnixNano())
	max := 64
	length := 64
	strings := make([]string, max)

	for i := 0; i < max; i++ {
		s := bytes.Repeat([]byte{'a'}, length)
		strings[i] = string(bytes.Map(randRune, s))
	}

	b.StartTimer()

	for i := 0; i < b.N; i++ {
		in := strings[i%max]
		if err := c.writeString(Discard, in); err != nil {
			b.Fatal(in, err)
		}
	}
}
Example #22
0
func getTranslation(source, target, text string) (res string, err error) {
	uri := "https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dt=t&dt=bd&q=%s"
	uri = fmt.Sprintf(uri, source, target, url.QueryEscape(text))

	var req *http.Request
	if req, err = http.NewRequest("GET", uri, nil); err != nil {
		return
	}
	req.Header.Add("User-Agent", "")

	hc := new(http.Client)
	var resp *http.Response
	if resp, err = hc.Do(req); err != nil {
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode >= 400 {
		err = errors.New("Google Translate: " + resp.Status)
		return
	}

	var body []byte
	if body, err = ioutil.ReadAll(resp.Body); err != nil {
		return
	}

	// Fixes bad JSON
	var prev rune
	body = bytes.Map(func(r rune) rune {
		if r == 44 && prev == 44 {
			return 32
		}
		prev = r
		return r
	}, body)

	var data []interface{}
	if err = json.Unmarshal(body, &data); err != nil {
		return
	}

	// Concatenates output
	// Hold on tight, here we go. Aaah!!!
	for _, v := range data[:1] {
		for _, v := range v.([]interface{}) {
			res += v.([]interface{})[0].(string)
		}
	}
	if len(data) > 2 {
		for _, v := range data[1:2][0].([]interface{}) {
			res += "\n" + v.([]interface{})[0].(string) + ": "
			for _, v := range v.([]interface{})[1].([]interface{}) {
				res += v.(string) + ", "
			}
			res = strings.TrimRight(res, ", ")
		}
		res = strings.ToLower(res)
	}

	return
}
Example #23
0
func (t *Test) GofmtFile(fname string) bool {
	formatted, err := exec.Command("gofmt", fname).CombinedOutput()
	if err != nil {
		outf, err := os.Create(fname + ".gofmt")
		if err != nil {
			log.Printf("failed to create output file: %v", err)
		} else {
			outf.Write(formatted)
			outf.Close()
		}
		log.Printf("gofmt failed, seed %v\n", t.seed)
		atomic.AddUint64(&statGofmt, 1)
		return true
	}
	fname1 := fname + ".formatted"
	outf, err := os.Create(fname1)
	if err != nil {
		log.Printf("failed to create output file: %v", err)
		return false
	}
	outf.Write(formatted)
	outf.Close()

	formatted2, err := exec.Command("gofmt", fname1).CombinedOutput()
	if err != nil {
		outf, err := os.Create(fname + ".gofmt")
		if err != nil {
			log.Printf("failed to create output file: %v", err)
		} else {
			outf.Write(formatted2)
			outf.Close()
		}
		log.Printf("gofmt failed, seed %v\n", t.seed)
		atomic.AddUint64(&statGofmt, 1)
		return true
	}
	outf2, err := os.Create(fname + ".formatted2")
	if err != nil {
		log.Printf("failed to create output file: %v", err)
		return false
	}
	outf2.Write(formatted2)
	outf2.Close()

	// Fails too often due to http://golang.org/issue/8021
	if true {
		if bytes.Compare(formatted, formatted2) != 0 {
			log.Printf("nonidempotent gofmt, seed %v\n", t.seed)
			atomic.AddUint64(&statGofmt, 1)
			return true
		}
	}

	removeWs := func(r rune) rune {
		// Chars that gofmt can remove/shuffle.
		if r == ' ' || r == '\t' || r == '\n' || r == '(' || r == ')' || r == ',' || r == ';' {
			return -1
		}
		return r
	}
	origfile, err := ioutil.ReadFile(fname)
	if err != nil {
		log.Printf("failed to read file: %v", err)
	}
	stripped := bytes.Map(removeWs, origfile)
	stripped2 := bytes.Map(removeWs, formatted)
	if bytes.Compare(stripped, stripped2) != 0 {
		writeStrippedFile(fname+".stripped0", stripped)
		writeStrippedFile(fname+".stripped1", stripped2)
		log.Printf("corrupting gofmt, seed %v\n", t.seed)
		atomic.AddUint64(&statGofmt, 1)
		return true
	}
	return false
}
Example #24
0
func RepeatingXor(input []byte, mask []byte) []byte {
	return bytes.Map(makeRepeatingXorClojure(mask), input)
}
Example #25
0
func FixedXorWithSingleByteMask(input []byte, mask byte) []byte {
	return bytes.Map(makeRepeatingXorClojure([]byte{mask}), input)
}
Example #26
0
func toascii(in []byte) string {
	return string(bytes.Map(toasciichar, in))
}
Example #27
0
// Decode decodes src using the encoding enc.  It writes at most
// DecodedLen(len(src)) bytes to dst and returns the number of bytes
// written.  If src contains invalid base64 data, it will return the
// number of bytes successfully written and CorruptInputError.
// New line characters (\r and \n) are ignored.
func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
	src = bytes.Map(removeNewlinesMapper, src)
	n, _, err = enc.decode(dst, src)
	return
}
Example #28
0
// label computes a label from the given name with maxlen length and the
// left and right cutsets trimmed from their respective ends.
func label(name []byte, maxlen int, left, right string) []byte {
	return trimCut(bytes.Map(mapping, name), maxlen, left, right)
}
Example #29
0
func ToServiceName(name []byte) string {
	return string(bytes.Map(toLower, name))
}
Example #30
-1
func consumeLine(data []byte, atEOF bool) (advance int, token []byte, err error) {
	advance, token, err = bufio.ScanLines(data, atEOF)
	if advance == 0 && len(token) == 0 {
		return
	}
	tokenString := string(token)
	//Skip empty lines
	if advance == 2 {
		advance, token, err = consumeLine(data[advance:len(data)], atEOF)
		advance = advance + 2
	}
	//Drop lines with comments OR drop line ending with comments
	if strings.IndexAny(tokenString, "/") == 0 {
		storedAdvance := advance
		advance, token, err = consumeLine(data[storedAdvance:len(data)], atEOF)
		advance = advance + storedAdvance
	} else if commentIndex := strings.IndexAny(tokenString, "/"); commentIndex != -1 {
		token = token[0:commentIndex]
	}

	//Remove all spaces
	token = bytes.Map(func(r rune) (newR rune) {
		if unicode.IsSpace(r) {
			newR = -1
		} else {
			newR = r
		}
		return
	}, token)
	return
}