本文整理汇总了Golang中unicode.IsGraphic函数的典型用法代码示例。如果您正苦于以下问题:Golang IsGraphic函数的具体用法?Golang IsGraphic怎么用?Golang IsGraphic使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IsGraphic函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: trimNonGraphic
// trimNonGraphic returns a slice of the string s, with all leading and trailing
// non graphic characters and spaces removed.
//
// Graphic characters include letters, marks, numbers, punctuation, symbols,
// and spaces, from categories L, M, N, P, S, Zs.
// Spacing characters are set by category Z and property Pattern_White_Space.
func trimNonGraphic(s string) string {
if s == "" {
return s
}
var first *int
var last int
for i, r := range []rune(s) {
if !unicode.IsGraphic(r) || unicode.IsSpace(r) {
continue
}
if first == nil {
f := i // copy i
first = &f
last = i
} else {
last = i
}
}
// If first is nil, it means there are no graphic characters
if first == nil {
return ""
}
return string([]rune(s)[*first : last+1])
}
示例2: main
func main() {
letter := 0
graphic := 0
digit := 0
invalid := 0
in := bufio.NewReader(os.Stdin)
for {
r, n, err := in.ReadRune()
if err == io.EOF {
break
}
if err != nil {
fmt.Fprintf(os.Stderr, "charcount: %v\n", err)
os.Exit(1)
}
if r == unicode.ReplacementChar && n == 1 {
invalid++
continue
}
if unicode.IsLetter(r) {
letter++
} else if unicode.IsGraphic(r) {
graphic++
} else if unicode.IsDigit(r) {
digit++
}
}
fmt.Println(letter, graphic, digit, invalid)
}
示例3: Stat
// Stat calculates statistics for all runes read from r.
func (m *Main) Stat(r io.RuneReader) (Stats, error) {
var stats Stats
for {
// Read next character.
ch, sz, err := r.ReadRune()
if err == io.EOF {
break
} else if err != nil {
return stats, err
}
// Calculate stats.
stats.TotalN++
if unicode.IsControl(ch) {
stats.ControlN++
}
if unicode.IsDigit(ch) {
stats.DigitN++
}
if unicode.IsGraphic(ch) {
stats.GraphicN++
}
if unicode.IsLetter(ch) {
stats.LetterN++
}
if unicode.IsLower(ch) {
stats.LowerN++
}
if unicode.IsMark(ch) {
stats.MarkN++
}
if unicode.IsNumber(ch) {
stats.NumberN++
}
if unicode.IsPrint(ch) {
stats.PrintN++
}
if unicode.IsPunct(ch) {
stats.PunctN++
}
if unicode.IsSpace(ch) {
stats.SpaceN++
}
if unicode.IsSymbol(ch) {
stats.SymbolN++
}
if unicode.IsTitle(ch) {
stats.TitleN++
}
if unicode.IsUpper(ch) {
stats.UpperN++
}
if sz > 1 {
stats.MultiByteN++
}
}
return stats, nil
}
示例4: defaultInsert
func defaultInsert(ed *Editor, k Key) *leReturn {
if k.Mod == 0 && k.Rune > 0 && unicode.IsGraphic(k.Rune) {
return insertKey(ed, k)
}
ed.pushTip(fmt.Sprintf("Unbound: %s", k))
return nil
}
示例5: Comment
func (l *Lexer) Comment() StateFn {
switch l.Current() {
case "/*":
// Look for the next '/' preceded with '*'
var last rune
for {
r, _ := l.Advance()
if last == '*' && r == '/' {
break
} else if r == EOF {
// TODO: Surface language parsing errors
log.Println("Invalid comment found", l.Current())
break
}
last = r
}
l.Emit(CMT)
case "//":
// Single line comments
for {
r, _ := l.Advance()
if !unicode.IsGraphic(r) {
break
}
}
l.Emit(CMT)
}
return l.Action()
}
示例6: CharType
// CharType returns a string representing the unicode type of a rune
func CharType(r rune) string {
switch {
case unicode.IsLetter(r):
return "letter"
case unicode.IsSpace(r):
return "space"
case unicode.IsPunct(r):
return "punct"
case unicode.IsNumber(r):
return "number"
case unicode.IsSymbol(r):
return "symbol"
case unicode.IsMark(r):
return "mark"
case unicode.IsDigit(r):
return "digit"
case unicode.IsPrint(r):
return "print"
case unicode.IsControl(r):
return "control"
case unicode.IsGraphic(r):
return "graphic"
default:
return "invalid"
}
}
示例7: main
func main() {
bs, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println(err)
return
}
m := make(map[rune]int)
for _, r := range string(bs) {
m[r]++
}
// answer is now in m. sort and format output:
lfs := make(lfList, 0, len(m))
for l, f := range m {
lfs = append(lfs, &letterFreq{l, f})
}
sort.Sort(lfs)
fmt.Println("file:", file)
fmt.Println("letter frequency")
for _, lf := range lfs {
if unicode.IsGraphic(lf.rune) {
fmt.Printf(" %c %7d\n", lf.rune, lf.freq)
} else {
fmt.Printf("%U %7d\n", lf.rune, lf.freq)
}
}
}
示例8: incrementCount
func incrementCount(r rune, counts map[int]int) {
switch {
case unicode.IsControl(r):
counts[isControl]++
case unicode.IsNumber(r):
counts[isNumber]++
case unicode.IsDigit(r):
counts[isDigit]++
case unicode.IsLetter(r):
counts[isLetter]++
case unicode.IsMark(r):
counts[isMark]++
case unicode.IsPunct(r):
counts[isPunct]++
case unicode.IsSpace(r):
counts[isSpace]++
case unicode.IsSymbol(r):
counts[isSymbol]++
case unicode.IsPrint(r):
counts[isPrint]++
case unicode.IsGraphic(r):
counts[isGraphic]++
}
}
示例9: ReadLine
func (this *ConsoleScreen) ReadLine() (string, error) {
cursorPos := 0
chars := make([]rune, 0, 10)
this.drawEditLine(cursorPos, chars)
this.channel.Resume()
m := this.channel.Wait()
for ; m != nil; m = this.channel.Wait() {
switch ks := m.(type) {
case *KeyMessage:
{
switch ks.Sym {
case K_RETURN:
line := string(chars)
this.clearEditLine()
this.Write(line)
this.Write("\n")
this.channel.Pause()
return line, nil
case K_LEFT:
if cursorPos > 0 {
cursorPos--
}
case K_RIGHT:
if cursorPos < len(chars) {
cursorPos++
}
case K_HOME:
cursorPos = 0
case K_END:
cursorPos = len(chars)
case K_BACKSPACE:
if cursorPos > 0 {
chars = append(chars[:cursorPos-1], chars[cursorPos:]...)
cursorPos--
}
case K_DELETE:
if cursorPos < len(chars) {
chars = append(chars[:cursorPos], chars[cursorPos+1:]...)
}
default:
if ks.Char != 0 {
if unicode.IsGraphic(ks.Char) {
if cursorPos == len(chars) {
chars = append(chars, ks.Char)
} else {
chars = append(chars[:cursorPos],
append([]rune{ks.Char}, chars[cursorPos:]...)...)
}
cursorPos++
}
}
}
this.drawEditLine(cursorPos, chars)
}
}
}
return "", nil
}
示例10: makeImportValid
func makeImportValid(r rune) rune {
// Should match Go spec, compilers, and ../../go/parser/parser.go:/isValidImport.
const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
return '_'
}
return r
}
示例11: defaultInsert
func defaultInsert(ed *Editor) {
k := ed.lastKey
if k.Mod == 0 && k.Rune > 0 && unicode.IsGraphic(k.Rune) {
insertKey(ed)
} else {
ed.pushTip(fmt.Sprintf("Unbound: %s", k))
}
}
示例12: addRune
func (ctx *context) addRune(buf *bytes.Buffer, x byte, idx int) {
r := rune(x)
if unicode.IsSpace(r) || unicode.IsGraphic(r) {
buf.WriteRune(r)
} else {
msg := fmt.Sprintf("non-graphic character found, code: %d, index in value: %d", int(x), idx)
ctx.validate.AddErrorN(msg, ctx.lineNr)
}
}
示例13: removeNongraphic
func removeNongraphic(msg string) string {
var buffer bytes.Buffer
for _, char := range msg {
if unicode.IsGraphic(char) && !unicode.IsSpace(char) {
buffer.WriteRune(char)
}
}
return buffer.String()
}
示例14: Valid
func Valid(importpath string) bool {
const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
s, _ := strconv.Unquote(importpath) // go/scanner returns a legal string literal
for _, r := range s {
if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
return false
}
}
return s != ""
}
示例15: CheckPassword
// Check the user password. Graphics character are allowed. See unicode.IsGraphic.
func CheckPassword(pass string, min, max int) error {
if len(pass) < min || len(pass) > max {
return e.New(ErrInvalidPassLength)
}
for _, r := range pass {
if !unicode.IsGraphic(r) {
return e.New(ErrInvalidPassChar)
}
}
return nil
}