本文整理汇总了Golang中unicode/utf8.ValidRune函数的典型用法代码示例。如果您正苦于以下问题:Golang ValidRune函数的具体用法?Golang ValidRune怎么用?Golang ValidRune使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ValidRune函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ExampleValidRune
func ExampleValidRune() {
valid := 'a'
invalid := rune(0xfffffff)
fmt.Println(utf8.ValidRune(valid))
fmt.Println(utf8.ValidRune(invalid))
// Output:
// true
// false
}
示例2: keywords
// keywords returns a slice of searchable tokens extracted
// from the given UserID packet keywords string.
func keywords(key *openpgp.PrimaryKey) []string {
m := make(map[string]bool)
for _, uid := range key.UserIDs {
s := strings.ToLower(uid.Keywords)
lbr, rbr := strings.Index(s, "<"), strings.LastIndex(s, ">")
if lbr != -1 && rbr > lbr {
m[s[lbr+1:rbr]] = true
}
if lbr != -1 {
fields := strings.FieldsFunc(s[:lbr], func(r rune) bool {
if !utf8.ValidRune(r) {
return true
}
if unicode.IsLetter(r) || unicode.IsNumber(r) {
return false
}
return true
})
for _, field := range fields {
m[field] = true
}
}
}
var result []string
for k := range m {
result = append(result, k)
}
return result
}
示例3: loadTestData
func loadTestData() []Test {
f := openReader(*testdata)
buffer, err := ioutil.ReadAll(f)
f.Close()
Error(err)
archive, err := zip.NewReader(bytes.NewReader(buffer), int64(len(buffer)))
Error(err)
tests := []Test{}
for _, f := range archive.File {
// Skip the short versions, which are simply duplicates of the long versions.
if strings.Contains(f.Name, "SHORT") || f.FileInfo().IsDir() {
continue
}
ff, err := f.Open()
Error(err)
defer ff.Close()
input := bufio.NewReader(ff)
test := Test{name: path.Base(f.Name)}
for {
line, err := input.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
log.Fatal(err)
}
if len(line) <= 1 || line[0] == '#' {
if m := versionRe.FindStringSubmatch(line); m != nil {
if m[1] != unicode.Version {
log.Printf("warning:%s: version is %s; want %s", f.Name, m[1], unicode.Version)
}
}
continue
}
m := testRe.FindStringSubmatch(line)
if m == nil || len(m) < 3 {
log.Fatalf(`Failed to parse: "%s" result: %#v`, line, m)
}
str := []byte{}
// In the regression test data (unpaired) surrogates are assigned a weight
// corresponding to their code point value. However, utf8.DecodeRune,
// which is used to compute the implicit weight, assigns FFFD to surrogates.
// We therefore skip tests with surrogates. This skips about 35 entries
// per test.
valid := true
for _, split := range strings.Split(m[1], " ") {
r, err := strconv.ParseUint(split, 16, 64)
Error(err)
valid = valid && utf8.ValidRune(rune(r))
str = append(str, string(rune(r))...)
}
if valid {
test.str = append(test.str, str)
test.comment = append(test.comment, m[2])
}
}
tests = append(tests, test)
}
return tests
}
示例4: genRune
func genRune(int64Gen gopter.Gen) gopter.Gen {
return int64Gen.Map(func(value interface{}) interface{} {
return rune(value.(int64))
}).SuchThat(func(v interface{}) bool {
return utf8.ValidRune(v.(rune))
})
}
示例5: Tl
func (t *Textbox) Tl(u rune, v rune) error {
//Check if rune is valid
if !utf8.ValidRune(v) {
return errors.New("invalid rune.")
}
for i, val := range t.canvas {
for j, w := 0, 0; j < len(val); j += w {
runeValue, width := utf8.DecodeRune(val[j:])
if runeValue == u {
tmp := t.canvas[i][j : j+width]
replaceWidth := utf8.RuneLen(v)
if width == replaceWidth {
// replace rune
utf8.EncodeRune(tmp, v)
} else if width > replaceWidth {
// replace and pad
utf8.EncodeRune(tmp, v)
tmp = tmp[replaceWidth:]
for len(tmp) > 0 {
replaceWidth = utf8.EncodeRune(tmp, 0x0)
tmp = tmp[replaceWidth:]
}
} else {
// rune wont fit.
return errors.New("rune too large.")
}
}
w = width
}
}
return nil
}
示例6: main
func main() {
var b1 []byte = []byte("안녕하세요")
fmt.Println(utf8.Valid(b1)) // true: "안녕하세요"는 UTF-8이 맞으므로 true
var b2 []byte = []byte{0xff, 0xf1, 0xc1}
fmt.Println(utf8.Valid(b2)) // false: 0xff 0xf1 0xc1은 UTF-8이 아니므로 false
var r1 rune = '한'
fmt.Println(utf8.ValidRune(r1)) // true: '한'은 UTF-8이 맞으므로 true
var r2 rune = 0x11111111
fmt.Println(utf8.ValidRune(r2)) // false: 0x11111111은 UTF-8이 아니므로 false
var s1 string = "한글"
fmt.Println(utf8.ValidString(s1)) // true: "한글"은 UTF-8이 맞으므로 true
var s2 string = string([]byte{0xff, 0xf1, 0xc1})
fmt.Println(utf8.ValidString(s2)) // false: 0xff 0xf1 0xc1은 UTF-8이 아니므로 false
}
示例7: getUtf8FistValidChar
func getUtf8FistValidChar(str []rune) int {
for i, c := range str {
if utf8.ValidRune(c) {
return i
}
}
return -1
}
示例8: appendQuotedRuneWith
func appendQuotedRuneWith(buf []byte, r rune, quote byte, ASCIIonly, graphicOnly bool) []byte {
buf = append(buf, quote)
if !utf8.ValidRune(r) {
r = utf8.RuneError
}
buf = appendEscapedRune(buf, r, utf8.RuneLen(r), quote, ASCIIonly, graphicOnly)
buf = append(buf, quote)
return buf
}
示例9: stripNonUTF8
func stripNonUTF8(str string) string {
buf := new(bytes.Buffer)
for _, r := range str {
if utf8.ValidRune(r) {
buf.WriteRune(r)
}
}
return buf.String()
}
示例10: escape
// escape replaces any characters which are not printable with corresponding
// hexadecimal escape sequence (\XX).
func escape(s string) string {
// Check if a replacement is required.
extra := 0
for i := 0; i < len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
if utf8.ValidRune(r) && unicode.IsPrint(r) {
i += size
continue
}
// Two extra bytes are required for each non-printable byte; e.g.
// "\n" -> `\0A`
// "\x00" -> `\00`
extra += 2
i++
}
if extra == 0 {
return s
}
// Replace non-printable bytes.
const hextable = "0123456789ABCDEF"
buf := make([]byte, len(s)+extra)
j := 0
for i := 0; i < len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
if utf8.ValidRune(r) && unicode.IsPrint(r) {
for k := 0; k < size; k++ {
buf[j+k] = s[i+k]
}
i += size
j += size
continue
}
b := s[i]
buf[j] = '\\'
buf[j+1] = hextable[b>>4]
buf[j+2] = hextable[b&0x0F]
i++
j += 3
}
return string(buf)
}
示例11: asciiEscapeToUnicode
func (p *parser) asciiEscapeToUnicode(bs []byte) rune {
s := string(bs)
hex, err := strconv.ParseUint(strings.ToLower(s), 16, 32)
if err != nil {
p.bug("Could not parse '%s' as a hexadecimal number, but the "+
"lexer claims it's OK: %s", s, err)
}
if !utf8.ValidRune(rune(hex)) {
p.panicf("Escaped character '\\u%s' is not valid UTF-8.", s)
}
return rune(hex)
}
示例12: similarEscapeCustomChar
// similarEscapeCustomChar converts a SQL:2008 regexp pattern to POSIX style,
// so it can be used by our regexp engine. This version of the function allows
// for a custom escape character.
func similarEscapeCustomChar(pattern string, escapeChar rune) string {
patternBuilder := make([]rune, 0, utf8.RuneCountInString(pattern))
inCharClass := false
afterEscape := false
numQuotes := 0
for _, c := range pattern {
switch {
case afterEscape:
// For SUBSTRING patterns
if c == '"' && !inCharClass {
if numQuotes%2 == 0 {
patternBuilder = append(patternBuilder, '(')
} else {
patternBuilder = append(patternBuilder, ')')
}
numQuotes++
} else {
patternBuilder = append(patternBuilder, '\\', c)
}
afterEscape = false
case utf8.ValidRune(escapeChar) && c == escapeChar:
// SQL99 escape character; do not immediately send to output
afterEscape = true
case inCharClass:
if c == '\\' {
patternBuilder = append(patternBuilder, '\\')
}
patternBuilder = append(patternBuilder, c)
if c == ']' {
inCharClass = false
}
case c == '[':
patternBuilder = append(patternBuilder, c)
inCharClass = true
case c == '%':
patternBuilder = append(patternBuilder, '.', '*')
case c == '_':
patternBuilder = append(patternBuilder, '.')
case c == '(':
// Convert to non-capturing parenthesis
patternBuilder = append(patternBuilder, '(', '?', ':')
case c == '\\', c == '.', c == '^', c == '$':
// Escape these characters because they are NOT
// metacharacters for SQL-style regexp
patternBuilder = append(patternBuilder, '\\', c)
default:
patternBuilder = append(patternBuilder, c)
}
}
return string(patternBuilder)
}
示例13: lexLexer
func lexLexer(l *Lexer) stateFunc {
for {
switch r := l.next(); {
case utf8.ValidRune(r):
fmt.Printf("hello rune %v\n", r)
l.output = append(l.output, r)
return lexNext
/*
case r >= 0 || r <= 1023: //<= BasicLatin.order[0] || r >= BasicLatin.order[1]:
fmt.Println("hello stateFunc next:", r)
l.emit(itemBasicLatin)
return lexNext
*/
}
}
}
示例14: IndexRune
// IndexRune returns the index of the first instance of the Unicode code point
// r, or -1 if rune is not present in s.
// If r is utf8.RuneError, it returns the first instance of any
// invalid UTF-8 byte sequence.
func IndexRune(s string, r rune) int {
switch {
case 0 <= r && r < utf8.RuneSelf:
return IndexByte(s, byte(r))
case r == utf8.RuneError:
for i, r := range s {
if r == utf8.RuneError {
return i
}
}
return -1
case !utf8.ValidRune(r):
return -1
default:
return Index(s, string(r))
}
}
示例15: writeTables
func writeTables() {
propTrie := triegen.NewTrie("derivedProperties")
w := gen.NewCodeWriter()
defer w.WriteGoFile(*outputFile, "precis")
gen.WriteUnicodeVersion(w)
// Iterate over all the runes...
for i := rune(0); i < unicode.MaxRune; i++ {
r := rune(i)
if !utf8.ValidRune(r) {
continue
}
e, ok := exceptions[i]
p := e.prop
switch {
case ok:
case !unicode.In(r, assigned):
p = unassigned
case r >= 0x0021 && r <= 0x007e: // Is ASCII 7
p = pValid
case unicode.In(r, disallowedRunes, unicode.Cc):
p = disallowed
case hasCompat(r):
p = idDisOrFreePVal
case isLetterDigits(r):
p = pValid
case isIdDisAndFreePVal(r):
p = idDisOrFreePVal
default:
p = disallowed
}
cat := runeCategory[r]
// Don't set category for runes that are disallowed.
if p == disallowed {
cat = exceptions[r].cat
}
propTrie.Insert(r, uint64(p)|uint64(cat))
}
sz, err := propTrie.Gen(w)
if err != nil {
log.Fatal(err)
}
w.Size += sz
}