本文整理汇总了Golang中utf8.RuneCountInString函数的典型用法代码示例。如果您正苦于以下问题:Golang RuneCountInString函数的具体用法?Golang RuneCountInString怎么用?Golang RuneCountInString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RuneCountInString函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: BenchmarkHyphenation
func BenchmarkHyphenation(b *testing.B) {
b.StopTimer()
trie := setupTrie()
if trie == nil {
return
}
testStr := `.hyphenation.`
v := make([]int, utf8.RuneCountInString(testStr))
b.StartTimer()
for i := 0; i < b.N; i++ {
for i := 0; i < len(v); i++ {
v[i] = 0
}
vIndex := 0
for pos, _ := range testStr {
t := testStr[pos:]
strs, values := trie.AllSubstringsAndValues(t)
for i := 0; i < values.Len(); i++ {
str := strs.At(i)
val := values.At(i).(*vector.IntVector)
diff := val.Len() - len(str)
vs := v[vIndex-diff:]
for i := 0; i < val.Len(); i++ {
if val.At(i) > vs[i] {
vs[i] = val.At(i)
}
}
}
vIndex++
}
}
}
示例2: verifyChar
func (v *verifier) verifyChar(x *Token) int {
s := x.String
if utf8.RuneCountInString(s) != 1 {
v.error(x.Pos(), "single char expected, found "+s)
return 0
}
ch, _ := utf8.DecodeRuneInString(s)
return ch
}
示例3: Runes
// Runes returns a slice of runes (Unicode code points) equivalent to the string s.
func Runes(s string) []int {
t := make([]int, utf8.RuneCountInString(s))
i := 0
for _, r := range s {
t[i] = r
i++
}
return t
}
示例4: numLeadingSpaces
func numLeadingSpaces(s string) int {
for i, char := range s {
if char != int(' ') {
return i;
}
}
return utf8.RuneCountInString(s);
}
示例5: TestCaseConsistency
func TestCaseConsistency(t *testing.T) {
// Make a string of all the runes.
numRunes := unicode.MaxRune + 1
if testing.Short() {
numRunes = 1000
}
a := make([]int, numRunes)
for i := range a {
a[i] = i
}
s := string(a)
// convert the cases.
upper := ToUpper(s)
lower := ToLower(s)
// Consistency checks
if n := utf8.RuneCountInString(upper); n != numRunes {
t.Error("rune count wrong in upper:", n)
}
if n := utf8.RuneCountInString(lower); n != numRunes {
t.Error("rune count wrong in lower:", n)
}
if !equal("ToUpper(upper)", ToUpper(upper), upper, t) {
t.Error("ToUpper(upper) consistency fail")
}
if !equal("ToLower(lower)", ToLower(lower), lower, t) {
t.Error("ToLower(lower) consistency fail")
}
/*
These fail because of non-one-to-oneness of the data, such as multiple
upper case 'I' mapping to 'i'. We comment them out but keep them for
interest.
For instance: CAPITAL LETTER I WITH DOT ABOVE:
unicode.ToUpper(unicode.ToLower('\u0130')) != '\u0130'
if !equal("ToUpper(lower)", ToUpper(lower), upper, t) {
t.Error("ToUpper(lower) consistency fail");
}
if !equal("ToLower(upper)", ToLower(upper), lower, t) {
t.Error("ToLower(upper) consistency fail");
}
*/
}
示例6: trim
func trim(s string, num int) string { //trims num characters off the end of s
dat := make([]int, utf8.RuneCountInString(s)-num);
for i, char := range s {
dat[i] = char;
if i == len(dat)-1 { //get outa here before it segfaults because s has more values than dat has room for
break
}
}
return string(dat)
}
示例7: 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
}
示例8: truncate
// truncate truncates the string to the specified precision, if present.
func (f *fmt) truncate(s string) string {
if f.precPresent && f.prec < utf8.RuneCountInString(s) {
n := f.prec
for i := range s {
if n == 0 {
s = s[:i]
break
}
n--
}
}
return s
}
示例9: 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
}
示例10: Count
// Count counts the number of non-overlapping instances of sep in s.
func Count(s, sep string) int {
if sep == "" {
return utf8.RuneCountInString(s) + 1
}
c := sep[0]
n := 0
for i := 0; i+len(sep) <= len(s); i++ {
if s[i] == c && (len(sep) == 1 || s[i:i+len(sep)] == sep) {
n++
i += len(sep) - 1
}
}
return n
}
示例11: padString
// append s to buf, padded on left (w > 0) or right (w < 0 or f.minus).
// clear flags aftewards.
func (f *fmt) padString(s string) {
var padding []byte
var left, right int
if f.widPresent && f.wid != 0 {
padding, left, right = f.computePadding(utf8.RuneCountInString(s))
}
if left > 0 {
f.writePadding(left, padding)
}
f.buf.WriteString(s)
if right > 0 {
f.writePadding(right, padding)
}
}
示例12: doesMatch
func doesMatch(s string) bool {
for _, match := range matches {
rx := regexp.MustCompile(match.exp);
res := rx.ExecuteString(s);
if len(res) == 0 {
continue
}
for i := 0; i < len(res)/2; i += 2 {
if res[i] == 0 && res[i + 1] == utf8.RuneCountInString(s) {
return true
}
}
}
return false
}
示例13: maxOptionColsize
func maxOptionColsize(op *OptionParser, max, limit int) int {
for _, opt := range op.options {
length := utf8.RuneCountInString(opt.String());
if length > max && length < limit {
max = length
}
}
for _, sub := range op.optGroups {
submax := maxOptionColsize(sub, max, limit);
if submax > max {
max = submax
}
}
return max
}
示例14: explode
// explode splits s into an array of UTF-8 sequences, one per Unicode character (still strings) up to a maximum of n (n <= 0 means no limit).
// Invalid UTF-8 sequences become correct encodings of U+FFF8.
func explode(s string, n int) []string {
l := utf8.RuneCountInString(s)
if n <= 0 || n > l {
n = l
}
a := make([]string, n)
var size, rune int
i, cur := 0, 0
for ; i+1 < n; i++ {
rune, size = utf8.DecodeRuneInString(s[cur:])
a[i] = string(rune)
cur += size
}
// add the rest
a[i] = s[cur:]
return a
}
示例15: hyphenateWord
func (h *Hyphenator) hyphenateWord(s, hyphen string) string {
testStr := `.` + s + `.`
v := make([]int, utf8.RuneCountInString(testStr))
vIndex := 0
for pos, _ := range testStr {
t := testStr[pos:]
strs, values := h.patterns.AllSubstringsAndValues(t)
for i := 0; i < values.Len(); i++ {
str := strs.At(i)
val := values.At(i).(*vector.IntVector)
diff := val.Len() - len(str)
vs := v[vIndex-diff:]
for i := 0; i < val.Len(); i++ {
if val.At(i) > vs[i] {
vs[i] = val.At(i)
}
}
}
vIndex++
}
var outstr string
// trim the values for the beginning and ending dots
markers := v[1 : len(v)-1]
mIndex := 0
u := make([]byte, 4)
for _, ch := range s {
l := utf8.EncodeRune(ch, u)
outstr += string(u[0:l])
// don't hyphenate between (or after) the last two characters of a string
if mIndex < len(markers)-2 {
// hyphens are inserted on odd values, skipped on even ones
if markers[mIndex]%2 != 0 {
outstr += hyphen
}
}
mIndex++
}
return outstr
}