当前位置: 首页>>代码示例>>Golang>>正文


Golang utf8.RuneCount函数代码示例

本文整理汇总了Golang中utf8.RuneCount函数的典型用法代码示例。如果您正苦于以下问题:Golang RuneCount函数的具体用法?Golang RuneCount怎么用?Golang RuneCount使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了RuneCount函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Encode

// 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
}
开发者ID:WaylandGod,项目名称:go-socket.io,代码行数:54,代码来源:codec_sio.go

示例2: main

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()
}
开发者ID:krasin,项目名称:abc-cube,代码行数:55,代码来源:abc-cube.go

示例3: Runes

// 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
}
开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:12,代码来源:bytes.go

示例4: Encode

// 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
}
开发者ID:WaylandGod,项目名称:go-socket.io,代码行数:57,代码来源:codec_siostreaming.go

示例5: Count

// 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
}
开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:15,代码来源:bytes.go

示例6: main

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)
}
开发者ID:machinaut,项目名称:go-play,代码行数:19,代码来源:pakrat.go

示例7: main

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))
}
开发者ID:sumitarora,项目名称:Go-Examples,代码行数:21,代码来源:aa.go

示例8: updateWidth

// 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()
}
开发者ID:8l,项目名称:go-learn,代码行数:5,代码来源:tabwriter.go

示例9: length

func (l *Line) length() int {
	return utf8.RuneCount(l.text)
}
开发者ID:dforsyth,项目名称:dvi-go,代码行数:3,代码来源:buffer.go

示例10: main

func main() {
	str := "dsjkdshdjsdh....js"
	fmt.Printf("String %s\nLenght: %d, Runes: %d\n", str,
		len([]byte(str)), utf8.RuneCount([]byte(str)))
}
开发者ID:hujinpu,项目名称:gobook,代码行数:5,代码来源:string2.go

示例11: main

func main() {
	s := "абв"
	println(len(s))
	println(utf8.RuneCount([]byte(s)))
}
开发者ID:ash,项目名称:go-tests,代码行数:5,代码来源:q4-2.go

示例12: main

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)))
}
开发者ID:ashokgelal,项目名称:al-GO-rhythm,代码行数:4,代码来源:utfcount.go


注:本文中的utf8.RuneCount函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。