本文整理汇总了Golang中unicode.IsControl函数的典型用法代码示例。如果您正苦于以下问题:Golang IsControl函数的具体用法?Golang IsControl怎么用?Golang IsControl使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IsControl函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestEventsRedirectStdout
// #5979
func (s *DockerSuite) TestEventsRedirectStdout(c *check.C) {
since := daemonTime(c).Unix()
dockerCmd(c, "run", "busybox", "true")
file, err := ioutil.TempFile("", "")
c.Assert(err, checker.IsNil, check.Commentf("could not create temp file"))
defer os.Remove(file.Name())
command := fmt.Sprintf("%s events --since=%d --until=%d > %s", dockerBinary, since, daemonTime(c).Unix(), file.Name())
_, tty, err := pty.Open()
c.Assert(err, checker.IsNil, check.Commentf("Could not open pty"))
cmd := exec.Command("sh", "-c", command)
cmd.Stdin = tty
cmd.Stdout = tty
cmd.Stderr = tty
c.Assert(cmd.Run(), checker.IsNil, check.Commentf("run err for command %q", command))
scanner := bufio.NewScanner(file)
for scanner.Scan() {
for _, ch := range scanner.Text() {
c.Assert(unicode.IsControl(ch), checker.False, check.Commentf("found control character %v", []byte(string(ch))))
}
}
c.Assert(scanner.Err(), checker.IsNil, check.Commentf("Scan err for command %q", command))
}
示例2: doIPTables
func doIPTables(args ...interface{}) error {
flatArgs := flatten(args, nil)
output, err := exec.Command("iptables", flatArgs...).CombinedOutput()
switch errt := err.(type) {
case nil:
case *exec.ExitError:
if !errt.Success() {
// sanitize iptables output
limit := 200
sanOut := strings.Map(func(ch rune) rune {
if limit == 0 {
return -1
}
limit--
if unicode.IsControl(ch) {
ch = ' '
}
return ch
}, string(output))
return ipTablesError{
cmd: strings.Join(flatArgs, " "),
output: sanOut,
}
}
default:
return err
}
return nil
}
示例3: quoteTo
func quoteTo(buf *bytes.Buffer, s string) {
buf.Grow(2 + len(s))
buf.WriteByte('"')
for i, c := range s {
switch {
case unicode.IsControl(c):
if s, ok := ctrlMap[c]; ok {
buf.WriteString(s)
} else {
fmt.Fprintf(buf, "\\u%04x", c)
}
case c == '"', c == '\\', c == '\'':
buf.WriteByte('\\')
buf.WriteRune(c)
case c <= unicode.MaxASCII:
buf.WriteRune(c)
case c == unicode.ReplacementChar:
// In correctly-encoded UTF-8, we should never see a replacement
// char. Some text in the wild has valid Unicode characters that
// aren't UTF-8, and this case lets us be more forgiving of those.
fmt.Fprintf(buf, "\\u%04x", s[i])
case c <= 0xffff:
fmt.Fprintf(buf, "\\u%04x", c)
default:
fmt.Fprintf(buf, "\\U%08x", c)
}
}
buf.WriteByte('"')
}
示例4: 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"
}
}
示例5: CharCount
// CharCount scans a *bufio.Reader and returns a map of the counts of its
// Unicode character types.
func CharCount(in *bufio.Reader) map[string]int {
counts := make(map[string]int) // counts of Unicode character types
for {
r, n, err := in.ReadRune() // returns rune, nbytes, error
if err == io.EOF {
break
}
if err != nil {
fmt.Fprintf(os.Stderr, "charcount: %v\n", err)
os.Exit(1)
}
switch {
case r == unicode.ReplacementChar && n == 1:
counts["invalid"]++
case unicode.IsControl(r):
counts["control"]++
case unicode.IsLetter(r):
counts["letter"]++
case unicode.IsMark(r):
counts["mark"]++
case unicode.IsNumber(r):
counts["number"]++
case unicode.IsPunct(r):
counts["punct"]++
case unicode.IsSpace(r):
counts["space"]++
case unicode.IsSymbol(r):
counts["symbol"]++
}
}
return counts
}
示例6: isKeyValid
// Some Simple Item Key Validations
func isKeyValid(key string) error {
// Valid Unicode
if !utf8.ValidString(key) {
return ErrKeyContainsNonUnicode
}
// No Slashes
if strings.Contains(key, "/") {
return ErrKeyContainsSlash
}
for _, rune := range key {
// No White Space
if unicode.IsSpace(rune) {
return ErrKeyContainsWhiteSpace
}
// No Control Characters
if unicode.IsControl(rune) {
return ErrKeyContainsControlChar
}
}
// return an empty error on success
return nil
}
示例7: lexString
func lexString(l *lexer) stateFn {
l.next()
for {
switch r := l.next(); {
case r == '"':
l.emit(itemString)
return lexText
case r == '\\':
if l.accept(`"\/bfnrt`) {
break
} else if r := l.next(); r == 'u' {
for i := 0; i < 4; i++ {
if !l.accept(hexdigit) {
return l.errorf("expected 4 hexadecimal digits")
}
}
} else {
return l.errorf("unsupported escape character")
}
case unicode.IsControl(r):
return l.errorf("cannot contain control characters in strings")
case r == eof:
return l.errorf("unclosed string")
}
}
}
示例8: 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
}
示例9: EventLoop
func EventLoop(s *selecta.Search) {
for !s.Done {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyCtrlC:
termbox.Close()
os.Exit(0)
case termbox.KeyBackspace, termbox.KeyBackspace2:
s.Backspace()
case termbox.KeyEnter:
s.Done = true
case termbox.KeyCtrlN:
s.Down()
case termbox.KeyCtrlP:
s.Up()
case termbox.KeyCtrlW:
s.DeleteWord()
default:
char := rune(ev.Ch)
if !unicode.IsControl(char) {
s.AppendQuery(string(char))
} else if ev.Key == termbox.KeySpace {
s.AppendQuery(" ")
}
}
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
DrawApp(s)
}
}
}
示例10: 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]++
}
}
示例11: removeDross
func removeDross(s string) string {
return strings.Map(func(r rune) rune {
if r < 32 || r > 0x7f || unicode.IsControl(r) {
return -1
}
return r
}, s)
}
示例12: isWord
// isWord will return true if the rune is not a special character, and can only be a word character.
func isWord(ch rune) bool {
return !isNewLine(ch) &&
!isWhiteSpace(ch) &&
!isInLine(ch) &&
!isLineStart(ch) &&
!isBlock(ch) &&
!unicode.IsControl(ch) &&
!strings.ContainsRune("\\|];:", ch)
}
示例13: scanString
func (self *Scanner) scanString() (*Token, error) {
var (
buf bytes.Buffer
screen bool
stop bool
)
// skip first quote
char := self.read()
if char != '"' {
// todo err here?
self.unread()
return &Token{STRING, buf.String(), bytes.Runes(buf.Bytes())}, nil
}
buf.WriteRune(char)
for !stop {
char := self.read()
if unicode.IsControl(char) {
self.unread()
stop = true
continue
}
if screen {
screen = false
switch char {
case '"', '\\', '/', 'b', 'f', 'n', 'r', 't', 'u':
buf.WriteRune(char)
default:
return nil, fmt.Errorf("unexpected end of input: %q", string(char))
}
continue
}
switch {
case char == '\\':
screen = true
buf.WriteRune(char)
case char == '"':
buf.WriteRune(char)
stop = true
case char == eof:
stop = true
default:
buf.WriteRune(char)
}
}
return &Token{STRING, buf.String(), bytes.Runes(buf.Bytes())}, nil
}
示例14: semanticScore
// Given two strings, compute a score representing whether the internal
// boundary falls on logical boundaries
// Scores range from 6 (best) to 0 (worst).
func semanticScore(one, two string) (score int) {
if one == "" || two == "" {
// Edges are the best
return 6
}
// Each port of this function behaves slightly differently due to
// subtle differences in each language's definition of things like
// 'whitespace'. Since this function's purpose is largely cosmetic,
// the choice has been made to use each language's native features
// rather than force total conformity.
r1 := lastRune(one)
r2 := firstRune(two)
nonAlphaNum1 := !unicode.IsLetter(r1) && !unicode.IsDigit(r1)
nonAlphaNum2 := !unicode.IsLetter(r2) && !unicode.IsDigit(r2)
space1 := nonAlphaNum1 && unicode.IsSpace(r1)
space2 := nonAlphaNum2 && unicode.IsSpace(r2)
lineBreak1 := space1 && unicode.IsControl(r1)
lineBreak2 := space2 && unicode.IsControl(r2)
blankLine1 := lineBreak1 && blankLineEnd.MatchString(one)
blankLine2 := lineBreak2 && blankLineStart.MatchString(two)
switch {
case blankLine1 || blankLine2:
// blank lines
score = 5
case lineBreak1 || lineBreak2:
// line breaks
score = 4
case nonAlphaNum1 && !space1 && space2:
// end of sentences
score = 3
case space1 || space2:
// whitespace
score = 2
case nonAlphaNum1 || nonAlphaNum2:
// non-alphanumeric
score = 1
}
return
}
示例15: hasContent
// hasContent indicates if a string has any character that's not a whitespace or control character
func hasContent(text string) bool {
if len(text) == 0 {
return false
}
for _, r := range text {
if !unicode.IsSpace(r) && !unicode.IsControl(r) {
return true
}
}
return false
}