// Encode takes payload, encodes it and writes it to dst. Payload must be one // of the following: a heartbeat, a handshake, []byte, string, int or anything // than can be marshalled by the default json package. If payload can't be // encoded or the writing fails, an error will be returned. func (enc *sioEncoder) Encode(dst io.Writer, payload interface{}) (err os.Error) { enc.elem.Reset() switch t := payload.(type) { case heartbeat: s := strconv.Itoa(int(t)) _, err = fmt.Fprintf(dst, "%s%d%s%s%s", sioFrameDelim, len(s)+len(sioFrameDelimHeartbeat), sioFrameDelim, sioFrameDelimHeartbeat, s) case handshake: _, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, len(t), sioFrameDelim, t) case []byte: l := utf8.RuneCount(t) if l == 0 { break } _, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, l, sioFrameDelim, t) case string: l := utf8.RuneCountInString(t) if l == 0 { break } _, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, l, sioFrameDelim, t) case int: s := strconv.Itoa(t) if s == "" { break } _, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, len(s), sioFrameDelim, s) default: data, err := json.Marshal(payload) if len(data) == 0 || err != nil { break } err = json.Compact(&enc.elem, data) if err != nil { break } _, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, utf8.RuneCount(enc.elem.Bytes())+len(sioFrameDelimJSON), sioFrameDelim, sioFrameDelimJSON) if err == nil { _, err = enc.elem.WriteTo(dst) } } return err }
func main() { flag.Parse() sides := strings.Split(*s, " ", -1) if len(sides) != 6 { log.Fatal("It must be exactly 6 space separated words") } for _, side := range sides { if len(side) == 0 || utf8.RuneCount([]byte(side)) > 2 { log.Fatal("Every word must be 1 or 2 letters") } } c := &cube{svg.New(os.Stdout), *a} l := *a w := 4 * l h := 11 * l / 2 c.Startview(w, h, 0, 0, 400, 550) c.Gstyle("fill:none;stroke:black;stroke-width:0.2;font-size:100") // c.Rect(200, 100, 100, 400) // c.Rect(100, 200, 300, 100) // c.Line(200, 400, 300, 400, "stroke:black") c.Letter(1, 0, "") c.TopEar(1, 0) c.Letter(0, 1, sides[0]) c.LeftEar(0, 1) c.Letter(1, 1, sides[1]) c.Letter(1, 2, sides[2]) c.LeftEar(1, 2) c.RightEar(1, 2) c.Letter(1, 3, sides[3]) c.Letter(2, 3, sides[4]) c.RightEar(2, 3) c.Letter(1, 4, sides[5]) c.RightEar(1, 4) c.LeftEar(1, 4) // c.Letter(0, 2, "", black) // c.Letter(1, 1, "", black) // c.Letter(1, 2, "П", black) // c.Letter(2, 2, "Х", black) // c.Letter(3, 2, "Ф", black) // c.Letter(2, 3, "Т", black) // // c.Letter(2, 1, "А", red) // // c.Letter(1, 3, "", red) // c.RotatedLetter(2, 4, "Е", red) c.Gend() c.End() }
// Runes returns a slice of runes (Unicode code points) equivalent to s. func Runes(s []byte) []int { t := make([]int, utf8.RuneCount(s)) i := 0 for len(s) > 0 { r, l := utf8.DecodeRune(s) t[i] = r i++ s = s[l:] } return t }
// Encode takes payload, encodes it and writes it to dst. Payload must be one // of the following: a heartbeat, a handshake, []byte, string, int or anything // than can be marshalled by the default json package. If payload can't be // encoded or the writing fails, an error will be returned. func (enc *sioStreamingEncoder) Encode(dst io.Writer, payload interface{}) (err os.Error) { enc.elem.Reset() switch t := payload.(type) { case heartbeat: s := strconv.Itoa(int(t)) _, err = fmt.Fprintf(dst, "%d:%d:%s,", sioMessageTypeHeartbeat, len(s), s) case handshake: _, err = fmt.Fprintf(dst, "%d:%d:%s,", sioMessageTypeHandshake, len(t), t) case []byte: l := utf8.RuneCount(t) if l == 0 { break } _, err = fmt.Fprintf(dst, "%d:%d::%s,", sioMessageTypeMessage, 1+l, t) case string: l := utf8.RuneCountInString(t) if l == 0 { break } _, err = fmt.Fprintf(dst, "%d:%d::%s,", sioMessageTypeMessage, 1+l, t) case int: s := strconv.Itoa(t) if s == "" { break } _, err = fmt.Fprintf(dst, "%d:%d::%s,", sioMessageTypeMessage, 1+len(s), s) default: data, err := json.Marshal(payload) if len(data) == 0 || err != nil { break } err = json.Compact(&enc.elem, data) if err != nil { break } _, err = fmt.Fprintf(dst, "%d:%d:%s\n:", sioMessageTypeMessage, 2+len(SIOAnnotationJSON)+utf8.RuneCount(enc.elem.Bytes()), SIOAnnotationJSON) if err == nil { _, err = enc.elem.WriteTo(dst) if err == nil { _, err = dst.Write([]byte{','}) } } } return err }
// Count counts the number of non-overlapping instances of sep in s. func Count(s, sep []byte) int { if len(sep) == 0 { return utf8.RuneCount(s) + 1 } c := sep[0] n := 0 for i := 0; i+len(sep) <= len(s); i++ { if s[i] == c && (len(sep) == 1 || Equal(s[i:i+len(sep)], sep)) { n++ i += len(sep) - 1 } } return n }
func main() { // Read file into []byte bytes, err := io.ReadFile("math.peg") if err != nil { log.Exit("Error :") } // Convert []byte into []rune bmax := len(bytes) rmax := utf8.RuneCount(bytes) // # runes runes := make([]rune, rmax) for bi, ri := 0, 0; ri < rmax; ri++ { r, size := utf8.DecodeRune(bytes[bi:bmax]) bi += size // Advance in bytestream runes[ri] = rune(r) // Add rune } fmt.Println(runes) }
func main() { for i := 1; i < 21; i++ { for j := 1; j < i; j++ { fmt.Printf("A") } fmt.Printf("\n") } s := "asSASA ddd dsjkdsjs dk" l := len([]byte(s)) total := utf8.RuneCount([]byte(s)) fmt.Printf("length %d", l) fmt.Printf("total bytes %d", total) a := []byte(s) for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 { a[i], a[j] = a[j], a[i] } fmt.Printf("%s\n", string(a)) }
// Update the cell width. func (b *Writer) updateWidth() { b.cell.width += utf8.RuneCount(b.buf.Bytes()[b.pos:b.buf.Len()]) b.pos = b.buf.Len() }
func (l *Line) length() int { return utf8.RuneCount(l.text) }
func main() { str := "dsjkdshdjsdh....js" fmt.Printf("String %s\nLenght: %d, Runes: %d\n", str, len([]byte(str)), utf8.RuneCount([]byte(str))) }
func main() { s := "абв" println(len(s)) println(utf8.RuneCount([]byte(s))) }
func main() { var str = "asSASA ddd dsjkdsjs dk" fmt.Printf("String %s\nLength: %d, Runes: %d\n", str, len([]byte(str)), utf8.RuneCount([]byte(str))) }