当前位置: 首页>>代码示例>>Golang>>正文


Golang unicode.IsDigit函数代码示例

本文整理汇总了Golang中unicode.IsDigit函数的典型用法代码示例。如果您正苦于以下问题:Golang IsDigit函数的具体用法?Golang IsDigit怎么用?Golang IsDigit使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了IsDigit函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: delta

func delta(a, b string, elapsed time.Duration, w io.Writer) {
	var ai, bi int
	for {
		for ai < len(a) && !unicode.IsDigit(rune(a[ai])) {
			w.Write([]byte{a[ai]})
			ai++
		}

		for bi < len(b) && !unicode.IsDigit(rune(b[bi])) {
			bi++
		}

		if ai == len(a) {
			break
		}

		numA, lenA := collectDigits(a[ai:])
		ai += lenA

		numB, lenB := collectDigits(b[bi:])
		bi += lenB

		// TODO: look at the next words, what's the unit?
		// TODO: colors
		// TODO: KB/MB
		fmt.Fprintf(w, "%.2f/s",
			float64(numB-numA)/(float64(elapsed)/float64(time.Second*1)))
	}
	fmt.Fprintln(w)
}
开发者ID:chlunde,项目名称:persec,代码行数:30,代码来源:persec.go

示例2: StringTrimNonAlnum

/*
StringTrimNonAlnum remove non alpha-numeric character at the beginning and end
for `text`.
*/
func StringTrimNonAlnum(text string) string {
	r := []rune(text)
	rlen := len(r)
	start := 0

	for ; start < rlen; start++ {
		if unicode.IsLetter(r[start]) || unicode.IsDigit(r[start]) {
			break
		}
	}

	if start >= rlen {
		return ""
	}

	r = r[start:]
	rlen = len(r)
	end := rlen - 1
	for ; end >= 0; end-- {
		if unicode.IsLetter(r[end]) || unicode.IsDigit(r[end]) {
			break
		}
	}

	if end < 0 {
		return ""
	}

	r = r[:end+1]

	return string(r)
}
开发者ID:shuLhan,项目名称:tekstus,代码行数:36,代码来源:string.go

示例3: lexValueSequence

// lexValueSequence scans a value sequence of a series description.
func lexValueSequence(l *lexer) stateFn {
	switch r := l.next(); {
	case r == eof:
		return lexStatements
	case isSpace(r):
		lexSpace(l)
	case r == '+':
		l.emit(itemADD)
	case r == '-':
		l.emit(itemSUB)
	case r == 'x':
		l.emit(itemTimes)
	case r == '_':
		l.emit(itemBlank)
	case unicode.IsDigit(r) || (r == '.' && unicode.IsDigit(l.peek())):
		l.backup()
		lexNumber(l)
	case isAlpha(r):
		l.backup()
		// We might lex invalid items here but this will be caught by the parser.
		return lexKeywordOrIdentifier
	default:
		return l.errorf("unexpected character in series sequence: %q", r)
	}
	return lexValueSequence
}
开发者ID:gitter-badger,项目名称:prometheus,代码行数:27,代码来源:lex.go

示例4: GetNextInt

// GetNextInt returns the next base-10 integer read from a netpbmReader,
// skipping preceding whitespace and comments.
func (nr *netpbmReader) GetNextInt() int {
	// Find the first digit.
	var c rune
	for nr.err == nil && !unicode.IsDigit(c) {
		for c = nr.GetNextByteAsRune(); unicode.IsSpace(c); c = nr.GetNextByteAsRune() {
		}
		if c == '#' {
			// Comment -- discard the rest of the line.
			for c = nr.GetNextByteAsRune(); c != '\n'; c = nr.GetNextByteAsRune() {
			}
		}
	}
	if nr.err != nil {
		return -1
	}

	// Read while we have base-10 digits.  Return the resulting int.
	value := int(c - '0')
	for c = nr.GetNextByteAsRune(); unicode.IsDigit(c); c = nr.GetNextByteAsRune() {
		value = value*10 + int(c-'0')
	}
	if nr.err != nil {
		return -1
	}
	nr.err = nr.UnreadByte()
	if nr.err != nil {
		return -1
	}
	return value
}
开发者ID:tburke,项目名称:netpbm,代码行数:32,代码来源:netpbm.go

示例5: extractLetterSequence

// extractLetterSequence extracts first word (sequence of letters ending with a non-letter)
// starting with the specified index and wraps it to dateStringLayoutItem according to the type
// of the word.
func extractLetterSequence(originalStr string, index int) (it dateStringLayoutItem) {
	letters := ""

	bytesToParse := []byte(originalStr[index:])
	runeCount := utf8.RuneCount(bytesToParse)

	var isWord bool
	var isDigit bool

	for i := 0; i < runeCount; i++ {
		rune, runeSize := utf8.DecodeRune(bytesToParse)
		bytesToParse = bytesToParse[runeSize:]

		if i == 0 {
			isWord = unicode.IsLetter(rune)
			isDigit = unicode.IsDigit(rune)
		} else {
			if (isWord && (!unicode.IsLetter(rune) && !unicode.IsDigit(rune))) ||
				(isDigit && !unicode.IsDigit(rune)) ||
				(!isWord && unicode.IsLetter(rune)) ||
				(!isDigit && unicode.IsDigit(rune)) {
				break
			}
		}

		letters += string(rune)
	}

	it.item = letters
	it.isWord = isWord
	it.isDigit = isDigit

	return
}
开发者ID:henriklundgren,项目名称:monday,代码行数:37,代码来源:utils_layout.go

示例6: ParseQuestion

func ParseQuestion(s string) *Question {
	var isDigit bool
	positions := make([]int, 0)
	i, start := 0, -1

	for _, c := range s {
		isDigit = unicode.IsDigit(c)
		if start == -1 {
			if isDigit {
				start = i
			}
		} else if !isDigit {
			// Include a number's decimals, commas, and trailing commas (for percentages)
			if c != '%' && !((c == '.' || c == ',') && i < len(s)-1 && unicode.IsDigit(rune(s[i+1]))) {
				positions = append(positions, start, i-1)
				start = -1
			}
		}
		i++
	}

	if len(positions) > 0 {
		return &Question{
			FullText:  s,
			Positions: positions,
		}
	} else {
		return nil
	}
}
开发者ID:mrap,项目名称:guestimator,代码行数:30,代码来源:question.go

示例7: scanFormat

func scanFormat(l *State, fs string) string {
	i := 0
	skipDigit := func() {
		if unicode.IsDigit(rune(fs[i])) {
			i++
		}
	}
	flags := "-+ #0"
	for i < len(fs) && strings.ContainsRune(flags, rune(fs[i])) {
		i++
	}
	if i >= len(flags) {
		Errorf(l, "invalid format (repeated flags)")
	}
	skipDigit()
	skipDigit()
	if fs[i] == '.' {
		i++
		skipDigit()
		skipDigit()
	}
	if unicode.IsDigit(rune(fs[i])) {
		Errorf(l, "invalid format (width or precision too long)")
	}
	i++
	return "%" + fs[:i]
}
开发者ID:telemetryapp,项目名称:go-lua,代码行数:27,代码来源:string.go

示例8: FmtFieldName

// FmtFieldName formats a string as a struct key
//
// Example:
// 	FmtFieldName("foo_id")
// Output: FooID
func FmtFieldName(s string) string {
	runes := []rune(s)
	for len(runes) > 0 && !unicode.IsLetter(runes[0]) && !unicode.IsDigit(runes[0]) {
		runes = runes[1:]
	}
	if len(runes) == 0 {
		return "_"
	}

	s = stringifyFirstChar(string(runes))
	name := lintFieldName(s)
	runes = []rune(name)
	for i, c := range runes {
		ok := unicode.IsLetter(c) || unicode.IsDigit(c)
		if i == 0 {
			ok = unicode.IsLetter(c)
		}
		if !ok {
			runes[i] = '_'
		}
	}
	s = string(runes)
	s = strings.Trim(s, "_")
	if len(s) == 0 {
		return "_"
	}
	return s
}
开发者ID:ChimeraCoder,项目名称:gojson,代码行数:33,代码来源:json-to-struct.go

示例9: lexIdSelector

func lexIdSelector(l *Lexer) stateFn {
	var foundInterpolation = false
	var r = l.next()
	r = l.next()
	if !unicode.IsLetter(r) && r != '#' && l.peek() != '{' {
		l.error("An identifier should start with at least a letter, Got '%s'", r)
	}
	for {
		if IsInterpolationStartToken(r, l.peek()) {
			l.backup()
			lexInterpolation(l, false)
			foundInterpolation = true
		} else if !unicode.IsLetter(r) && !unicode.IsDigit(r) {
			break
		}
		r = l.next()
	}
	l.backup()

	r = l.next()
	for unicode.IsLetter(r) || unicode.IsDigit(r) {
		r = l.next()
	}
	l.backup()

	if foundInterpolation {
		l.emit(ast.T_INTERPOLATION_SELECTOR)
	} else {
		l.emit(ast.T_ID_SELECTOR)
	}
	return lexSelectors
}
开发者ID:se77en,项目名称:c6,代码行数:32,代码来源:lexer_selector.go

示例10: lexNumberOrDurationOrDot

func lexNumberOrDurationOrDot(l *lexer) stateFn {
	foundDecimal := false
	first := true
	for {
		switch r := l.next(); {
		case r == '.':
			if first && !unicode.IsDigit(l.peek()) {
				l.emit(TokenDot)
				return lexToken
			}
			if foundDecimal {
				return l.errorf("multiple decimals in number")
			}
			foundDecimal = true
		case unicode.IsDigit(r):
			//absorb
		case !foundDecimal && isDurUnit(r):
			if r == 'm' && l.peek() == 's' {
				l.next()
			}
			l.emit(TokenDuration)
			return lexToken
		default:
			l.backup()
			l.emit(TokenNumber)
			return lexToken
		}
		first = false
	}
}
开发者ID:wutaizeng,项目名称:kapacitor,代码行数:30,代码来源:lex.go

示例11: scanNumeral

func (p *PrimaryLexer) scanNumeral() {
	buf := new(bytes.Buffer)

	first := p.Consume()
	buf.WriteRune(first)

	if first == '0' && p.Peek() == 'x' {
		// Hexadecimal
		buf.WriteRune(p.Consume())

		if !isHexadecimal(p.Peek()) {
			p.Emit(TokenError, "Expected digits after '0x' while lexing hexadecimal numeral")
			return
		}

		for isHexadecimal(p.Peek()) {
			buf.WriteRune(p.Consume())
		}
	} else {
		// Decimal
		for unicode.IsDigit(p.Peek()) {
			buf.WriteRune(p.Consume())
		}

		// Decimal digits
		if p.Peek() == '.' {
			dot := p.Consume()

			// Special case: 'digits' '..' 'expr'
			if !unicode.IsDigit(p.Peek()) {
				p.Emit(TokenNumeral, buf.String())
				p.Emit(TokenSymbol, ".")

				return
			}

			buf.WriteRune(dot)

			for unicode.IsDigit(p.Peek()) {
				buf.WriteRune(p.Consume())
			}
		}

		// Exponent
		if p.Peek() == 'e' {
			buf.WriteRune(p.Consume())

			if !unicode.IsDigit(p.Peek()) {
				p.Emit(TokenError, "Expected digits after '"+buf.String()+"' while lexing exponent of numeral")
				return
			}

			for unicode.IsDigit(p.Peek()) {
				buf.WriteRune(p.Consume())
			}
		}
	}

	p.Emit(TokenNumeral, buf.String())
}
开发者ID:kiljacken,项目名称:jai,代码行数:60,代码来源:lexer.go

示例12: scanNumber

// scanNumber consumes a numeric lexeme and returns its token and value. The
// numeric value may be an integer or real number.
func (s *Scanner) scanNumber() (token.Token, string) {
	var ch rune
	var buf bytes.Buffer

	buf.WriteRune(s.read())
	for {
		ch = s.read()
		if !unicode.IsDigit(ch) {
			break
		}
		buf.WriteRune(ch)
	}

	if ch == '.' {
		buf.WriteRune(ch)
		for {
			ch = s.read()
			if !unicode.IsDigit(ch) {
				break
			}
			buf.WriteRune(ch)
		}
	}
	s.unread()

	return token.NUMBER, buf.String()
}
开发者ID:tboronczyk,项目名称:Kiwi,代码行数:29,代码来源:scanner.go

示例13: StringToVersion

// StringToVersion function parses a string into a Version struct which can be compared
//
// The implementation is based on http://man.he.net/man5/deb-version
// on https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version
//
// It uses the dpkg-1.17.25's algorithm  (lib/parsehelp.c)
func StringToVersion(str string) (Version, error) {
	var version Version

	// Trim leading and trailing space
	str = strings.TrimSpace(str)

	if len(str) <= 0 {
		return Version{}, errors.New("Version string is empty")
	}

	// Find Epoch
	sepEpoch := strings.Index(str, ":")
	if sepEpoch > -1 {
		intEpoch, err := strconv.Atoi(str[:sepEpoch])
		if err == nil {
			version.Epoch = intEpoch
		} else {
			return Version{}, errors.New("Epoch in version is not a number")
		}
		if intEpoch < 0 {
			return Version{}, errors.New("Epoch in version is negative")
		}
	} else {
		version.Epoch = 0
	}

	// Find UpstreamVersion / DebianRevision
	sepDebianRevision := strings.LastIndex(str, "-")
	if sepDebianRevision > -1 {
		version.UpstreamVersion = str[sepEpoch+1 : sepDebianRevision]
		version.DebianRevision = str[sepDebianRevision+1:]
	} else {
		version.UpstreamVersion = str[sepEpoch+1:]
		version.DebianRevision = "0"
	}
	// Verify format
	if len(version.UpstreamVersion) == 0 {
		return Version{}, errors.New("No UpstreamVersion in version")
	}

	if !unicode.IsDigit(rune(version.UpstreamVersion[0])) {
		return Version{}, errors.New("UpstreamVersion in version does not start with digit")
	}

	for i := 0; i < len(version.UpstreamVersion); i = i + 1 {
		r := rune(version.UpstreamVersion[i])
		if !unicode.IsDigit(r) && !unicode.IsLetter(r) && !containsRune(upstreamVersionAllowedSymbols, r) {
			return Version{}, errors.New("invalid character in UpstreamVersion")
		}
	}

	for i := 0; i < len(version.DebianRevision); i = i + 1 {
		r := rune(version.DebianRevision[i])
		if !unicode.IsDigit(r) && !unicode.IsLetter(r) && !containsRune(debianRevisionAllowedSymbols, r) {
			return Version{}, errors.New("invalid character in DebianRevision")
		}
	}

	return version, nil
}
开发者ID:pombredanne,项目名称:dpkgcomp,代码行数:66,代码来源:compare.go

示例14: get_klog_tokenizer

//	statefull tokenizer for linux printk() message buffer
//
//	BUG(nath): may need some generic API
func get_klog_tokenizer() func(rune) bool {
	started := false
	state := "priority"

	return func(c rune) bool {
		switch state {

		case "dispatch":
			switch {
			case c == '<':
				state = "priority"
				started = true
				return true

			case c == '[':
				state = "date"
				started = true
				return true

			case started:
				state = "message"
				return unicode.IsSpace(c)

			default:
				started = true
				return true
			}

		case "priority":
			switch {
			case c == '<':
				return true

			case c == '>':
				state = "dispatch"
				return true

			default:
				return !unicode.IsDigit(c)
			}

		case "date":
			switch {
			case c == '[' || c == '.':
				return true

			case c == ']':
				state = "dispatch"
				return true

			default:
				return !unicode.IsDigit(c)
			}

		default:
			return false
		}
	}
}
开发者ID:nathanaelle,项目名称:mercury,代码行数:62,代码来源:klog_linux.go

示例15: AtEow

func (c *Cursor) AtEow() bool {
	r, _ := c.RuneAfter()
	rb, _ := c.RuneBefore()
	if !(unicode.IsLetter(r) || unicode.IsDigit(r)) && (unicode.IsLetter(rb) || unicode.IsDigit(rb)) {
		return true
	}
	return false
}
开发者ID:kybin,项目名称:tor,代码行数:8,代码来源:cursor.go


注:本文中的unicode.IsDigit函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。