Example #1
0
func main() {
	counts := make(map[rune]int)      // counts of Unicode characters
	var utflen [utf8.UTFMax + 1]int   // count of lengths of UTF-8 encodings
	invalid := 0                      // count of invalid UTF-8 characters
	catCounts := make(map[string]int) // counts per Unicode category
	unknown := 0                      // count of characters of unknown category

	in := bufio.NewReader(os.Stdin)
	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)
		}
		if r == unicode.ReplacementChar && n == 1 {
			invalid++
			continue
		}
		counts[r]++
		utflen[n]++
		switch {
		case unicode.IsLetter(r):
			catCounts["Letter"]++
		case unicode.IsDigit(r):
			catCounts["Digit"]++
		case unicode.IsSymbol(r):
			catCounts["Symbol"]++
		case unicode.IsPunct(r):
			catCounts["Punct"]++
		case unicode.IsSpace(r):
			catCounts["Space"]++
		default:
			unknown++
		}
	}
	fmt.Printf("rune\tcount\n")
	for c, n := range counts {
		fmt.Printf("%q\t%d\n", c, n)
	}
	fmt.Print("\nlen\tcount\n")
	for i, n := range utflen {
		if i > 0 {
			fmt.Printf("%d\t%d\n", i, n)
		}
	}
	if invalid > 0 {
		fmt.Printf("\n%d invalid UTF-8 characters\n", invalid)
	}

	fmt.Print("\ncat\tcount\n")
	for cat, n := range catCounts {
		fmt.Printf("%s\t%d\n", cat, n)
	}
	if unknown > 0 {
		fmt.Printf("\n%d characters of unknown category\n", unknown)
	}
}
Example #2
0
// synopsis extracts the first sentence from s. All runs of whitespace are
// replaced by a single space.
func synopsis(s string) string {

	parts := strings.SplitN(s, "\n\n", 2)
	s = parts[0]

	var buf []byte
	const (
		other = iota
		period
		space
	)
	last := space
Loop:
	for i := 0; i < len(s); i++ {
		b := s[i]
		switch b {
		case ' ', '\t', '\r', '\n':
			switch last {
			case period:
				break Loop
			case other:
				buf = append(buf, ' ')
				last = space
			}
		case '.':
			last = period
			buf = append(buf, b)
		default:
			last = other
			buf = append(buf, b)
		}
	}

	// Ensure that synopsis fits an App Engine datastore text property.
	const m = 400
	if len(buf) > m {
		buf = buf[:m]
		if i := bytes.LastIndex(buf, []byte{' '}); i >= 0 {
			buf = buf[:i]
		}
		buf = append(buf, " ..."...)
	}

	s = string(buf)

	r, n := utf8.DecodeRuneInString(s)
	if n < 0 || unicode.IsPunct(r) || unicode.IsSymbol(r) {
		// ignore Markdown headings, editor settings, Go build constraints, and * in poorly formatted block comments.
		s = ""
	} else {
		for _, prefix := range badSynopsisPrefixes {
			if strings.HasPrefix(s, prefix) {
				s = ""
				break
			}
		}
	}

	return s
}
Example #3
0
// 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
}
Example #4
0
File: scanner.go Project: rread/rsi
func isLetter(ch rune) bool {
	if ch == '(' || ch == ')' || ch == '\'' || ch == '"' {
		return false
	}

	return unicode.IsLetter(ch) || unicode.IsPunct(ch) || unicode.IsSymbol(ch)
}
Example #5
0
func TestRune_IsIndependent(t *testing.T) {
	numbers := make([]rune, 0)
	letters := make([]rune, 0)
	marks := make([]rune, 0)
	symbols := make([]rune, 0)
	puncts := make([]rune, 0)
	others := make([]rune, 0)
	for _, r := range unicode.Myanmar.R16 {
		for c := r.Lo; c <= r.Hi; c++ {
			switch mr := rune(c); true {
			case unicode.IsLetter(mr):
				letters = append(letters, mr)
			case unicode.IsNumber(mr):
				numbers = append(numbers, mr)
			case unicode.IsMark(mr):
				marks = append(marks, mr)
			case unicode.IsPunct(mr):
				puncts = append(puncts, mr)
			case unicode.IsSymbol(mr):
				symbols = append(symbols, mr)
			default:
				others = append(others, mr)
			}
		}
	}

	independents := string(letters) + string(numbers) + string(puncts) + " \t\r\n"
	for _, consonant := range independents {
		if ok, _ := Rune(consonant).IsIndependent(); !ok {
			t.Errorf("[%U] expected result is true, but it returns false", consonant)
		}
	}
}
Example #6
0
File: utils.go Project: akavel/vfmd
func DeEscapeProse(p md.Prose) md.Prose {
	result := make(md.Prose, 0, len(p))
	var buf []byte
runs:
	for i := 0; i < len(p); i++ {
		if buf == nil {
			buf = p[i].Bytes
		}
		for j := 0; ; {
			k := bytes.IndexByte(buf[j:], '\\')
			if k == -1 {
				result = append(result, md.Run{
					Line:  p[i].Line,
					Bytes: buf,
				})
				buf = nil
				continue runs
			}
			j += k
			r, _ := utf8.DecodeRune(buf[j+1:])
			if unicode.IsPunct(r) || unicode.IsSymbol(r) {
				result = append(result, md.Run{
					Line:  p[i].Line,
					Bytes: buf[:j],
				})
				buf = buf[j+1:]
				i--
				continue runs
			}
			j++
		}
	}
	return result
}
Example #7
0
// 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
}
Example #8
0
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]++
	}

}
Example #9
0
// 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"
	}
}
Example #10
0
/*
 * Password rules:
 * at least 7 letters
 * at least 1 number
 * at least 1 upper case
 * at least 1 special character
 */
func ValidatePassword(value, local string) error {

	fmt.Println("Validate password", value)
	if len(value) < 7 {
		return errors.New(i18n.Translate(local, i18nSec, "text03"))
	}

	var num, lower, upper, spec bool
	for _, r := range value {
		switch {
		case unicode.IsDigit(r):
			num = true
		case unicode.IsUpper(r):
			upper = true
		case unicode.IsLower(r):
			lower = true
		case unicode.IsSymbol(r), unicode.IsPunct(r):
			spec = true
		}
	}
	if num && lower && upper && spec {
		return nil
	}

	return errors.New(i18n.Translate(local, i18nSec, "text03"))
}
Example #11
0
func (l *GutsLex) emitIfKeywordMatches() bool {
	l.remember()
	for keyword, typ := range LexKeywords {
		var match bool = true
	next_keyword:
		for _, sc := range keyword {
			c := l.next()
			if c == eof {
				match = false
				break next_keyword
			}
			if sc != c {
				match = false
				break next_keyword
			}
		}
		if match {
			c := l.next()
			if c == '\n' || c == eof || c == ' ' || c == '\t' || unicode.IsSymbol(c) {
				l.backup()
				l.emit(TokenType(typ))
				return true
			}
		}
		l.rollback()
	}
	return false
}
Example #12
0
func Sanitize(r rune) rune {
	switch {
	case unicode.IsPunct(r):
		return ' '
	case unicode.IsMark(r):
		return ' '
	case unicode.IsSymbol(r):
		return ' '
	}
	return r
}
Example #13
0
func main() {
	counts := make(map[string]int)
	var utflen [utf8.UTFMax + 1]int
	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
		}

		utflen[n]++
		switch {
		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.IsSymbol(r):
			counts["Symbol"]++
		case unicode.IsSpace(r):
			counts["Space"]++
		default:
			counts["Other"]++
		}
	}

	fmt.Printf("rune\tcount\n")
	for c, n := range counts {
		fmt.Printf("%s\t%d\n", c, n)
	}
	fmt.Print("\nlen\tcount\n")
	for i, n := range utflen {
		if i > 0 {
			fmt.Printf("%d\t%d\n", i, n)
		}
	}
	if invalid > 0 {
		fmt.Printf("\n%d invalid UTF-8 characters\n", invalid)
	}
}
Example #14
0
func main() {
	in := bufio.NewReader(os.Stdin)
	counts := make(map[string]int)  // counts of Unicode character types
	var utflen [utf8.UTFMax + 1]int // count of lengths of UTF-8 encodings
	invalid := 0                    // count of invalid UTF-8 characters

	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)
		}
		if r == unicode.ReplacementChar && n == 1 {
			invalid++
			continue
		}

		switch {
		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"]++
		}
		utflen[n]++
	}
	fmt.Printf("rune\tcount\n")
	for c, n := range counts {
		fmt.Printf("%q\t%d\n", c, n)
	}
	fmt.Print("\nlen\tcount\n")
	for i, n := range utflen {
		if i > 0 {
			fmt.Printf("%d\t%d\n", i, n)
		}
	}
	if invalid > 0 {
		fmt.Printf("\n%d invalid UTF-8 characters\n", invalid)
	}
}
Example #15
0
File: ex8.go Project: yyBeta/gopl
func main() {
	counts := make(map[rune]int) // counts of Unicode characters
	var utflen [utf8.UTFMax]int  // count of lengths of UTF-8 encodings
	invalid := 0                 // count of invalid UTF-8 characters
	cats := make(map[string]int) // counts of Unicode categories

	// In a terminal, use CTRL+Z at line start to signal EOF with ENTER.
	in := bufio.NewReader(os.Stdin)
	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)
		}
		if r == unicode.ReplacementChar && n == 1 {
			invalid++
			continue
		}
		switch {
		case unicode.IsLetter(r):
			cats["letter"]++
		case unicode.IsDigit(r):
			cats["digit"]++
		case unicode.IsControl(r):
			cats["control"]++
		case unicode.IsMark(r):
			cats["mark"]++
		case unicode.IsPunct(r):
			cats["punct"]++
		case unicode.IsSymbol(r):
			cats["symbol"]++
		}
		counts[r]++
		utflen[n-1]++
	}
	fmt.Printf("rune\tcount\n")
	for c, n := range counts {
		fmt.Printf("%q\t%d\n", c, n)
	}
	fmt.Print("\nlen\tcount\n")
	for i, n := range utflen {
		fmt.Printf("%d\t%d\n", i+1, n)
	}
	fmt.Print("\ncat\tcount\n")
	for s, n := range cats {
		fmt.Printf("%v\t%d\n", s, n)
	}
	fmt.Printf("\n%d invalid UTF-8 characters\n", invalid)
}
Example #16
0
// atTerminator reports whether the input is at valid termination character to
// appear after an identifier.
func (l *Scanner) atTerminator() bool {
	r := l.peek()
	if r == eof || isSpace(r) || isEndOfLine(r) || unicode.IsPunct(r) || unicode.IsSymbol(r) {
		return true
	}
	// Does r start the delimiter? This can be ambiguous (with delim=="//", $x/2 will
	// succeed but should fail) but only in extremely rare cases caused by willfully
	// bad choice of delimiter.
	if rd, _ := utf8.DecodeRuneInString(l.rightDelim); rd == r {
		return true
	}
	return false
}
Example #17
0
File: jee.go Project: ltt1987/gojee
func getIdent(r rune) int {
	i, ok := Ident[r]
	switch {
	case ok:
		return i
	case unicode.IsNumber(r):
		return CONST
	case unicode.IsLetter(r) || unicode.IsPunct(r) || unicode.IsSymbol(r):
		return RESERVED
	case unicode.IsSpace(r):
		return SPACE
	}
	return ZERO
}
Example #18
0
func passwordContainsSymbols(p *Password) (bool, error) {
	// password-length should be sufficiently large.
	p.passwordLength.Set("256")
	passwordExecuteFn := p.ExecuteFn()
	cmdResult, err := passwordExecuteFn()
	if err != nil {
		return false, err
	}
	isSymbolFound := false
	for _, r := range cmdResult.(string) {
		if unicode.IsSymbol(r) {
			isSymbolFound = true
			break
		}
	}
	return isSymbolFound, nil
}
Example #19
0
//returns true when it contains each requirement.
func VerifyPassword(s string) (tenOrMore, number, upper, special bool) {
	letters := 0
	for _, s := range s {
		switch {
		case unicode.IsUpper(s):
			upper = true
			letters++
		case unicode.IsPunct(s) || unicode.IsSymbol(s):
			special = true
		case unicode.IsLetter(s) || unicode.IsNumber(s):
			letters++
		default:
			//return false, false, false, false
		}
	}
	tenOrMore = letters >= 10
	return
}
Example #20
0
func isSpecialChar(s string) (t bool, err error) {

	r := []rune(s)

	l := len(r)
	if l == 0 {
		return
	}
	if l != 1 {
		err = fmt.Errorf("string must contains only one character, got %s", s)
		return
	}

	if unicode.IsPunct(r[0]) || unicode.IsSymbol(r[0]) || unicode.IsSpace(r[0]) || unicode.IsControl(r[0]) {
		t = true
		return
	}
	return
}
Example #21
0
func stripurl(inputstring string) string {
	// Detect http* within the string.
	startindex := strings.Index(inputstring, "http")
	endindex := strings.Index(inputstring[startindex:], " ")
	inputurl := inputstring[startindex : startindex+endindex]

	// Parse into a url and detect password
	urlpart, err := url.Parse(inputurl)
	if err != nil {
		return inputstring
	}

	u := urlpart.User
	if u == nil {
		return inputstring
	}

	uname := u.Username()
	pwd, _ := u.Password()

	//Find how many symbols there are in the User string
	num := 0

	for _, letter := range fmt.Sprintf("%v", pwd) {
		if (unicode.IsSymbol(letter) || unicode.IsPunct(letter)) && letter != '*' {
			num = num + 1
		}
	}

	// detect the index on the password
	startindex = strings.Index(inputstring, uname)

	//reform the error message, with * as the password
	inputstring = inputstring[:startindex+len(uname)+1] + "*" + inputstring[startindex+len(uname)+1+len(pwd):]

	//Replace all the special characters encoding
	for num > 0 {
		num = num - 1
		inputstring = stripurl(inputstring)
	}

	return inputstring
}
Example #22
0
func parseCondition(con string) conditions {
	var conds conditions
	for len(con) > 0 {
		v := conditionVar{}
		for con[0] == '.' {
			con = con[1:]
			v.structField++
		}
		pos := strings.IndexFunc(con, func(r rune) bool { return !unicode.In(r, unicode.Letter, unicode.Digit) })
		if pos == -1 {
			panic("invalid condition")
		}
		v.name = con[:pos]
		con = con[pos:]
		con = strings.TrimSpace(con)

		c := condition{l: v}

		pos = strings.IndexFunc(con, func(r rune) bool { return !unicode.IsSymbol(r) && r != '!' })
		c.cond = con[:pos]
		con = con[pos:]
		con = strings.TrimSpace(con)

		r := conditionVar{}
		for con[0] == '.' {
			r.structField++
			con = con[1:]
		}

		pos = strings.IndexFunc(con, func(r rune) bool { return !unicode.In(r, unicode.Letter, unicode.Digit) && r != '"' })
		if pos == -1 {
			pos = len(con)
		}
		r.name = con[:pos]
		c.r = r
		conds = append(conds, c)
		con = con[pos:]
		con = strings.TrimSpace(con)
	}
	return conds
}
Example #23
0
func main() {
	fromrune := make(map[rune]Reference, len(entity))
	for name, r := range entity {
		if !unicode.IsSymbol(r) && !unicode.IsPunct(r) {
			continue
		}

		// use the shorter name
		if cur, exists := fromrune[r]; exists {
			if len(name) < len(cur.Name) {
				fromrune[r] = Reference{r, name}
			}
			continue
		}
		fromrune[r] = Reference{r, name}
	}

	sorted := make([]Reference, 0, len(fromrune))
	for _, t := range fromrune {
		t.Name = strings.ToLower(t.Name)
		t.Name = strings.Trim(t.Name, ";")
		sorted = append(sorted, t)
	}
	sort.Sort(ByRune(sorted))

	file, err := os.Create("runename.go")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	fmt.Fprintln(file, "// generated code")
	fmt.Fprintln(file, "package fedwiki")
	fmt.Fprintln(file)
	fmt.Fprintln(file, "var runename = map[rune]string {")
	for _, tok := range sorted {
		fmt.Fprintf(file, "\t'\\U%08X': \"%s\",\n", tok.Rune, tok.Name)
	}
	fmt.Fprintln(file, "}")
}
Example #24
0
func lexArticle(l *lexer) stateFn {
	switch r := l.next(); {
	case r == eof:
		l.emit(itemEOF)
		return nil
	case r == '{' && l.peek() == '{':
		l.next()
		l.emit(itemLeftMeta)
		return lexArticle
	case r == '}' && l.peek() == '}':
		l.next()
		l.emit(itemRightMeta)
		return lexArticle
	case r == '[' && l.peek() == '[':
		l.next()
		l.emit(itemLeftTag)
		return lexArticle
	case r == ']' && l.peek() == ']':
		l.next()
		l.emit(itemRightTag)
		return lexArticle
	case r == '\'':
		return lexQuote
	case r == '<':
		l.backup()
		return lexXML
	case r == '=':
		return lexTitle
	case isSpace(r):
		return lexSpace
	case unicode.IsMark(r) || unicode.IsSymbol(r) || unicode.IsPunct(r):
		l.emit(itemMark)
		return lexArticle
	case isAlphaNumeric(r):
		return lexWord
	}
	return lexArticle
}
Example #25
0
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")
	}
}
Example #26
0
File: utils.go Project: akavel/vfmd
func DeEscape(s string) string {
	buf := bytes.NewBuffer(nil)
	esc := false
	for _, c := range []rune(s) {
		if esc {
			if !unicode.IsPunct(c) && !unicode.IsSymbol(c) {
				buf.WriteByte('\\')
			}
			buf.WriteRune(c)
			esc = false
			continue
		}
		if c != '\\' {
			buf.WriteRune(c)
			continue
		}
		esc = true
	}
	if esc {
		buf.WriteByte('\\')
	}
	return buf.String()
}
Example #27
0
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
}
Example #28
0
func ScoreText(src []byte) float32 {
	score := 0
	for _, r := range bytes.Runes(src) {
		if unicode.IsLetter(r) {
			score++
		}
		if unicode.IsLower(r) {
			score += 4
		}
		if unicode.IsSpace(r) {
			score += 30
		}
		if unicode.IsPunct(r) {
			score -= 2
		}
		if unicode.IsSymbol(r) {
			score -= 30
		}
		if !unicode.IsPrint(r) && !unicode.IsSpace(r) {
			score -= 20
		}
	}
	return float32(score) / float32(len(src))
}
Example #29
0
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
}
Example #30
0
func bindStringPredicates(e *Env) {

	e.Method("is-control", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		for _, c := range s {
			if !unicode.IsControl(c) {
				return t.Return(False)
			}
		}

		return t.Return(True)
	})

	e.Method("is-digit", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		for _, c := range s {
			if !unicode.IsDigit(c) {
				return t.Return(False)
			}
		}

		return t.Return(True)
	})

	e.Method("is-graphic", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		for _, c := range s {
			if !unicode.IsGraphic(c) {
				return t.Return(False)
			}
		}

		return t.Return(True)
	})

	e.Method("is-letter", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		for _, c := range s {
			if !unicode.IsLetter(c) {
				return t.Return(False)
			}
		}

		return t.Return(True)
	})

	e.Method("is-lower", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		for _, c := range s {
			if !unicode.IsLower(c) {
				return t.Return(False)
			}
		}

		return t.Return(True)
	})

	e.Method("is-mark", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		for _, c := range s {
			if !unicode.IsMark(c) {
				return t.Return(False)
			}
		}

		return t.Return(True)
	})

	e.Method("is-print", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		for _, c := range s {
			if !unicode.IsPrint(c) {
				return t.Return(False)
			}
		}

		return t.Return(True)
	})

	e.Method("is-punct", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		for _, c := range s {
			if !unicode.IsPunct(c) {
				return t.Return(False)
			}
		}

		return t.Return(True)
	})

	e.Method("is-space", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		for _, c := range s {
			if !unicode.IsSpace(c) {
				return t.Return(False)
			}
		}

		return t.Return(True)
	})

	e.Method("is-symbol", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		for _, c := range s {
			if !unicode.IsSymbol(c) {
				return t.Return(False)
			}
		}

		return t.Return(True)
	})

	e.Method("is-title", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		for _, c := range s {
			if !unicode.IsTitle(c) {
				return t.Return(False)
			}
		}

		return t.Return(True)
	})

	e.Method("is-upper", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		for _, c := range s {
			if !unicode.IsUpper(c) {
				return t.Return(False)
			}
		}

		return t.Return(True)
	})

	e.Method("to-lower", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		return t.Return(NewString(t, strings.ToLower(s)))
	})

	e.Method("to-title", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		return t.Return(NewString(t, strings.ToTitle(s)))
	})

	e.Method("to-upper", func(t *Task, args Cell) bool {
		s := raw(toString(Car(t.Scratch).(Binding).Self()))

		return t.Return(NewString(t, strings.ToUpper(s)))
	})
}