// 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]) }
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) }
// 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 }
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 }
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() }
// 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" } }
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) } } }
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]++ } }
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 }
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 }
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)) } }
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) } }
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() }
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 != "" }
// 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 }
func CheckFileName(nome string, min, max int) error { if len(nome) < min || len(nome) > max { return e.New(ErrInvNumberChars) } for _, v := range nome { if !unicode.IsGraphic(v) { return e.Push(e.New(ErrInvCharacter), e.New("the character '%v' in filename is invalid", string([]byte{byte(v)}))) } } return nil }
func ValidateUsername(name string) error { if len(name) == 0 || len(name) > 32 { return ErrInvalidUsername } for _, c := range name { if !unicode.IsGraphic(c) { return ErrInvalidUsername } } return nil }
// Verify that our IsGraphic agrees with unicode.IsGraphic. func TestIsGraphic(t *testing.T) { n := 0 for r := rune(0); r <= unicode.MaxRune; r++ { if IsGraphic(r) != unicode.IsGraphic(r) { t.Errorf("IsGraphic(%U)=%t incorrect", r, IsGraphic(r)) n++ if n > 10 { return } } } }
func (this *ConsoleScreen) Write(text string) error { gm := this.ws.glyphMap nx := this.cx * gm.charWidth ny := this.cy * gm.charHeight sx := nx sy := ny ex := nx + gm.charWidth ey := ny + gm.charHeight for _, c := range text { if unicode.IsGraphic(c) { nx = gm.renderGlyph(c, glyphStyleNormal, this.sfcs[this.sfcIx], nx, ny) this.cx++ if nx > ex { ex = nx } } if c == '\n' || nx >= this.sfcs[this.sfcIx].W() { this.cx = 0 nx = 0 this.cy++ ny += gm.charHeight if ny+gm.charHeight >= this.sfcs[0].H() { src := this.sfcs[this.sfcIx] dst := this.sfcs[1-this.sfcIx] dst.SetColor(color.RGBA{0, 0, 0, 255}) dst.Fill(0, 0, dst.W(), dst.H()) dst.DrawSurface(0, -gm.charHeight, src) this.sfcIx = 1 - this.sfcIx this.cy-- ny -= gm.charHeight sx = 0 sy = 0 ex = dst.W() } if ny > ey { ey = ny } } } this.channel.Publish(newRegionMessage(MT_UpdateText, this.sfcs[this.sfcIx], []*Region{&Region{sx, sy, ex - sx, ey - sy}})) return nil }
func main() { fmt.Println(unicode.IsGraphic('1')) // true: 1은 화면에 표시되는 숫자이므로 true fmt.Println(unicode.IsGraphic('a')) // true: a는 화면에 표시되는 문자이므로 true fmt.Println(unicode.IsGraphic('한')) // true: '한'은 화면에 표시되는 문자이므로 true fmt.Println(unicode.IsGraphic('漢')) // true: '漢'은 화면에 표시되는 문자이므로 true fmt.Println(unicode.IsGraphic('\n')) // false: \n 화면에 표시되는 문자가 아니므로 false fmt.Println(unicode.IsLetter('a')) // true: a는 문자이므로 true fmt.Println(unicode.IsLetter('1')) // false: 1은 문자가 아니므로 false fmt.Println(unicode.IsDigit('1')) // true: 1은 숫자이므로 true fmt.Println(unicode.IsControl('\n')) // true: \n은 제어 문자이므로 true fmt.Println(unicode.IsMark('\u17c9')) // true: \u17c9는 마크이므로 true fmt.Println(unicode.IsPrint('1')) // true: 1은 Go 언어에서 표시할 수 있으므로 true fmt.Println(unicode.IsPunct('.')) // true: .은 문장 부호이므로 true fmt.Println(unicode.IsSpace(' ')) // true: ' '는 공백이므로 true fmt.Println(unicode.IsSymbol('♥')) // true: ♥는 심볼이므로 true fmt.Println(unicode.IsUpper('A')) // true: A는 대문자이므로 true fmt.Println(unicode.IsLower('a')) // true: a는 소문자이므로 true }
// lexValue scans the value in the content line // // value = *VALUE-CHAR // VALUE-CHAR = WSP / %x21-7E / NON-US-ASCII ; Any textual character func lexValue(l *lexer) stateFn { Loop: for { switch r := l.next(); { case unicode.IsGraphic(r): // absorb default: l.backup() l.emit(itemValue) break Loop } } return lexNewLine }
func scanPrintable(data []byte, atEOF bool) (advance int, token []byte, err error) { advance, token, err = ScanLines(data, atEOF) if err == nil && token != nil { s := make([]byte, 0, len(token)) for len(token) > 0 { r, l := utf8.DecodeRune(token) if unicode.IsGraphic(r) || unicode.IsSpace(r) { s = append(s, token[:l]...) } token = token[l:] } token = s } return }
func validatedImportPath(path string) (string, error) { s, err := strconv.Unquote(path) if err != nil { return "", err } if s == "" { return "", fmt.Errorf("empty string") } const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD" for _, r := range s { if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) { return s, fmt.Errorf("invalid character %#U", r) } } return s, nil }
// AcceptIdent consumes runes until it hits anything that does not // qualify as a valid identifier, or is one of the reserved tokens in our // syntax struct. func (l *Lexer) AcceptIdent() int { var r rune for { if r = l.NextRune(); r == EOF { return EOF } isReserved := l.syntax.IsReserved(r) && r != '\'' if isReserved || unicode.IsSpace(r) || !unicode.IsGraphic(r) { l.Rewind() if l.start != l.pos { return 1 } return 0 } } }
func sanitizeImportPath(lit *ast.BasicLit) *ast.BasicLit { // Note: An unmodified AST generated by go/parser will already // contain a backward- or double-quoted path string that does // not contain any invalid characters, and most of the work // here is not needed. However, a modified or generated AST // may possibly contain non-canonical paths. Do the work in // all cases since it's not too hard and not speed-critical. // if we don't have a proper string, be conservative and return whatever we have if lit.Kind != token.STRING { return lit } s, err := strconv.Unquote(lit.Value) if err != nil { return lit } // if the string is an invalid path, return whatever we have // // spec: "Implementation restriction: A compiler may restrict // ImportPaths to non-empty strings using only characters belonging // to Unicode's L, M, N, P, and S general categories (the Graphic // characters without spaces) and may also exclude the characters // !"#$%&'()*,:;<=>?[\]^`{|} and the Unicode replacement character // U+FFFD." if s == "" { return lit } const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD" for _, r := range s { if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) { return lit } } // otherwise, return the double-quoted path s = strconv.Quote(s) if s == lit.Value { return lit // nothing wrong with lit } return &ast.BasicLit{ValuePos: lit.ValuePos, Kind: token.STRING, Value: s} }
/* Get the next string from a scanner after printing a prompt, or an error */ func NextString(p string, s bufio.Scanner) (string, error) { fmt.Printf("%v: ", p) /* See if there's anything to scan */ if !s.Scan() { /* If not, return why */ e := s.Err() if nil == e { e = io.EOF } return "", e } /* Return the last printable chunk */ f := strings.FieldsFunc( s.Text(), func(r rune) bool { return !unicode.IsGraphic(r) }, ) if 0 == len(f) { return "", nil } return f[len(f)-1], nil }
func print_rune_types(char rune) { if unicode.IsControl(char) { fmt.Println(" Control") } if unicode.IsDigit(char) { fmt.Println(" Digit") } if unicode.IsGraphic(char) { fmt.Println(" Graphic") } if unicode.IsLetter(char) { fmt.Println(" Letter") } if unicode.IsLower(char) { fmt.Println(" Lower") } if unicode.IsMark(char) { fmt.Println(" Mark") } if unicode.IsPrint(char) { fmt.Println(" Print") } if unicode.IsPunct(char) { fmt.Println(" Punct") } if unicode.IsSpace(char) { fmt.Println(" Space") } if unicode.IsSymbol(char) { fmt.Println(" Symbol") } if unicode.IsTitle(char) { fmt.Println(" Title") } if unicode.IsUpper(char) { fmt.Println(" Upper") } }
func newViewport(driver *driver, width, height int, title string, fullscreen bool) *viewport { v := &viewport{ fullscreen: fullscreen, scaling: 1, title: title, } glfw.DefaultWindowHints() glfw.WindowHint(glfw.Samples, 4) var monitor *glfw.Monitor if fullscreen { monitor = glfw.GetPrimaryMonitor() if width == 0 || height == 0 { vm := monitor.GetVideoMode() width, height = vm.Width, vm.Height } } wnd, err := glfw.CreateWindow(width, height, v.title, monitor, nil) if err != nil { panic(err) } width, height = wnd.GetSize() // At this time, width and height serve as a "hint" for glfw.CreateWindow, so get actual values from window. wnd.MakeContextCurrent() v.context = newContext() cursorPoint := func(x, y float64) math.Point { // HACK: xpos is off by 1 and ypos is off by 3 on OSX. // Compensate until real fix is found. x -= 1.0 y -= 3.0 return math.Point{X: int(x), Y: int(y)}.ScaleS(1 / v.scaling) } wnd.SetCloseCallback(func(*glfw.Window) { v.Close() }) wnd.SetPosCallback(func(w *glfw.Window, x, y int) { v.Lock() v.position = math.NewPoint(x, y) v.Unlock() }) wnd.SetSizeCallback(func(_ *glfw.Window, w, h int) { v.Lock() v.sizeDipsUnscaled = math.Size{W: w, H: h} v.sizeDips = v.sizeDipsUnscaled.ScaleS(1 / v.scaling) v.Unlock() v.onResize.Fire() }) wnd.SetFramebufferSizeCallback(func(_ *glfw.Window, w, h int) { v.Lock() v.sizePixels = math.Size{W: w, H: h} v.Unlock() gl.Viewport(0, 0, w, h) gl.ClearColor(kClearColorR, kClearColorG, kClearColorB, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) }) wnd.SetCursorPosCallback(func(w *glfw.Window, x, y float64) { p := cursorPoint(w.GetCursorPos()) v.Lock() if v.pendingMouseMoveEvent == nil { v.pendingMouseMoveEvent = &gxui.MouseEvent{} driver.Call(func() { v.Lock() ev := *v.pendingMouseMoveEvent v.pendingMouseMoveEvent = nil v.Unlock() v.onMouseMove.Fire(ev) }) } v.pendingMouseMoveEvent.Point = p v.pendingMouseMoveEvent.State = getMouseState(w) v.Unlock() }) wnd.SetCursorEnterCallback(func(w *glfw.Window, entered bool) { p := cursorPoint(w.GetCursorPos()) ev := gxui.MouseEvent{ Point: p, } ev.State = getMouseState(w) if entered { v.onMouseEnter.Fire(ev) } else { v.onMouseExit.Fire(ev) } }) wnd.SetScrollCallback(func(w *glfw.Window, xoff, yoff float64) { p := cursorPoint(w.GetCursorPos()) v.Lock() if v.pendingMouseScrollEvent == nil { v.pendingMouseScrollEvent = &gxui.MouseEvent{} driver.Call(func() { v.Lock() ev := *v.pendingMouseScrollEvent v.pendingMouseScrollEvent = nil ev.ScrollX, ev.ScrollY = int(v.scrollAccumX), int(v.scrollAccumY) if ev.ScrollX != 0 || ev.ScrollY != 0 { v.scrollAccumX -= float64(ev.ScrollX) v.scrollAccumY -= float64(ev.ScrollY) v.Unlock() v.onMouseScroll.Fire(ev) } else { v.Unlock() } }) } v.pendingMouseScrollEvent.Point = p v.scrollAccumX += xoff * platform.ScrollSpeed v.scrollAccumY += yoff * platform.ScrollSpeed v.pendingMouseScrollEvent.State = getMouseState(w) v.Unlock() }) wnd.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) { p := cursorPoint(w.GetCursorPos()) ev := gxui.MouseEvent{ Point: p, Modifier: translateKeyboardModifier(mod), } ev.Button = translateMouseButton(button) ev.State = getMouseState(w) if action == glfw.Press { v.onMouseDown.Fire(ev) } else { v.onMouseUp.Fire(ev) } }) wnd.SetKeyCallback(func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) { ev := gxui.KeyboardEvent{ Key: translateKeyboardKey(key), Modifier: translateKeyboardModifier(mods), } switch action { case glfw.Press: v.onKeyDown.Fire(ev) case glfw.Release: v.onKeyUp.Fire(ev) case glfw.Repeat: v.onKeyRepeat.Fire(ev) } }) wnd.SetCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) { if !unicode.IsControl(char) && !unicode.IsGraphic(char) && !unicode.IsLetter(char) && !unicode.IsMark(char) && !unicode.IsNumber(char) && !unicode.IsPunct(char) && !unicode.IsSpace(char) && !unicode.IsSymbol(char) { return // Weird unicode character. Ignore } ev := gxui.KeyStrokeEvent{ Character: char, Modifier: translateKeyboardModifier(mods), } v.onKeyStroke.Fire(ev) }) wnd.SetRefreshCallback(func(w *glfw.Window) { if v.canvas != nil { v.render() } }) fw, fh := wnd.GetFramebufferSize() posX, posY := wnd.GetPos() // Pre-multiplied alpha blending gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) gl.Enable(gl.BLEND) gl.Enable(gl.SCISSOR_TEST) gl.Viewport(0, 0, fw, fh) gl.Scissor(0, 0, int32(fw), int32(fh)) gl.ClearColor(kClearColorR, kClearColorG, kClearColorB, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) wnd.SwapBuffers() v.window = wnd v.driver = driver v.onClose = driver.createAppEvent(func() {}) v.onResize = driver.createAppEvent(func() {}) v.onMouseMove = gxui.CreateEvent(func(gxui.MouseEvent) {}) v.onMouseEnter = driver.createAppEvent(func(gxui.MouseEvent) {}) v.onMouseExit = driver.createAppEvent(func(gxui.MouseEvent) {}) v.onMouseDown = driver.createAppEvent(func(gxui.MouseEvent) {}) v.onMouseUp = driver.createAppEvent(func(gxui.MouseEvent) {}) v.onMouseScroll = gxui.CreateEvent(func(gxui.MouseEvent) {}) v.onKeyDown = driver.createAppEvent(func(gxui.KeyboardEvent) {}) v.onKeyUp = driver.createAppEvent(func(gxui.KeyboardEvent) {}) v.onKeyRepeat = driver.createAppEvent(func(gxui.KeyboardEvent) {}) v.onKeyStroke = driver.createAppEvent(func(gxui.KeyStrokeEvent) {}) v.onDestroy = driver.createDriverEvent(func() {}) v.sizeDipsUnscaled = math.Size{W: width, H: height} v.sizeDips = v.sizeDipsUnscaled.ScaleS(1 / v.scaling) v.sizePixels = math.Size{W: fw, H: fh} v.position = math.Point{X: posX, Y: posY} return v }
func isNotGraphicUnicode(r rune) bool { return !unicode.IsGraphic(r) }
func isNotGraphicUnicodeOrTabOrNewline(r rune) bool { return r != '\t' && r != '\n' && !unicode.IsGraphic(r) }