本文整理汇总了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
}
示例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()
}
示例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
}
示例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
}
示例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
}
示例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)
}
示例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))
}
示例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()
}
示例9: length
func (l *Line) length() int {
return utf8.RuneCount(l.text)
}
示例10: main
func main() {
str := "dsjkdshdjsdh....js"
fmt.Printf("String %s\nLenght: %d, Runes: %d\n", str,
len([]byte(str)), utf8.RuneCount([]byte(str)))
}
示例11: main
func main() {
s := "абв"
println(len(s))
println(utf8.RuneCount([]byte(s)))
}
示例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)))
}