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


Golang strings.IndexRune函数代码示例

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


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

示例1: ParseToken

// ParseToken is a helper function that extracts the username and password
// from an authorization token.  Callers should be able to pass in the header
// "Authorization" from an HTTP request, and retrieve the credentials.
//
// If the token is malformed, empty strings are returned for the
// username and password.
func (a *Basic) ParseToken(token string) (username, password string) {
	if token == "" {
		return "", ""
	}

	// Check that the token supplied corresponds to the basic authorization
	// protocol
	ndx := strings.IndexRune(token, ' ')
	if ndx < 1 || token[0:ndx] != "Basic" {
		return "", ""
	}

	// Drop prefix, and decode the base64
	buffer, err := base64.StdEncoding.DecodeString(token[ndx+1:])
	if err != nil {
		return "", ""
	}
	token = string(buffer)

	ndx = strings.IndexRune(token, ':')
	if ndx < 1 {
		return "", ""
	}

	return token[0:ndx], token[ndx+1:]
}
开发者ID:saintfish,项目名称:httpauth-go,代码行数:32,代码来源:basic.go

示例2: varLexNumber

func varLexNumber(l *varLexer) lexState {
	l.accept("+-")
	digits := "0123456789"
	if l.accept("0") {
		if l.accept("x") {
			digits = "0123456789abcdefABCDEF"
		} else {
			digits = "01234567"
		}
	}
	for strings.IndexRune(digits, l.next()) >= 0 {
	}
	l.backup()
	if l.accept(".") {
		for strings.IndexRune(digits, l.next()) >= 0 {
		}
		l.backup()
	}
	if l.accept("eE") {
		l.accept("+-")
		for strings.IndexRune("0123456789", l.next()) >= 0 {
		}
		l.backup()
	}
	if r := l.peek(); unicode.IsLetter(r) {
		l.next()
		return l.errorf("bad number syntax: %q", l.input[l.start:l.pos])
	}
	l.emit(tokNumber)
	return varLexNormal
}
开发者ID:40a,项目名称:bootkube,代码行数:31,代码来源:variant_lexer.go

示例3: Languages

// Languages returns the user's preferred languages in BCP 47 format in
// priority order.
func Languages() (langs []string) {
	add := func(lang string) {
		for _, l := range strings.Split(lang, ":") {
			// Remove encoding (we only support UTF-8).
			if i := strings.IndexRune(l, '.'); i != -1 {
				l = l[:i]
			}
			// Skip empty locales or the "C" locale.
			if l == "" || l == "C" {
				continue
			}
			// Add the locale.
			langs = append(langs, strings.Replace(l, "_", "-", -1))
			// Add the base language if it is a dialect.
			if i := strings.IndexRune(l, '_'); i != -1 {
				langs = append(langs, l[:i])
			}
		}
	}
	add(os.Getenv("LANGUAGE"))
	add(os.Getenv("LC_MESSAGES"))
	add(os.Getenv("LC_ALL"))
	add(os.Getenv("LANG"))
	return
}
开发者ID:BenLubar,项目名称:mnorpg,代码行数:27,代码来源:names_native.go

示例4: init

func init() {
	codel := `nug
rbc vjnmkf kd yxyqci na rbc zjkfoscdd ew rbc ujllmcp
tc rbkso rbyr ejp mysljylc kd kxveddknmc re jsicpdrysi
de kr kd eoya kw aej icfkici re zjkr`
	decol := `bjv
the public is amazed by the quickness of the juggler
we think that our language is impossible to understand
so it is okay if you decided to quit`

	m = map[rune]rune{' ': ' '}
	var misscod, missdec []rune
	for i := 'a'; i <= 'z'; i++ {
		ix := strings.IndexRune(codel, i)
		if ix != -1 {
			m[i] = rune(decol[ix])
		} else {
			misscod = append(misscod, i)
		}

		ix = strings.IndexRune(decol, i)
		if ix == -1 {
			missdec = append(missdec, i)
		}
	}
	for ix, i := range misscod {
		m[i] = missdec[ix]
	}
}
开发者ID:nikai3d,项目名称:ce-challenges,代码行数:29,代码来源:lost_in_translation_test.go

示例5: NewDependencyInfo

//NewDependencyInfo creates a new DependencyInfo with the given line
func NewDependencyInfo(line string) (*DependencyInfo, error) {
	sep1 := strings.IndexRune(line, ' ')
	if sep1 != 1 {
		return nil, errors.New("Invalid separator")
	}
	sep2 := strings.IndexRune(line[sep1+1:], ' ')
	if sep2 < 0 {
		return nil, errors.New("Invalid separator")
	}
	sep2 += sep1 + 1

	depi := new(DependencyInfo)
	var err error
	depi.To, err = strconv.Atoi(line[sep1+1 : sep2-1])
	if err != nil {
		return nil, err
	}
	depi.DepType = rune(line[sep2-1])

	depi.Features = getFeatures(line[sep2+1:], '>', 1)
	if pasresult, ok := depi.Features["格解析結果"]; ok {
		depi.Pas, err = NewPas(pasresult, true)
	}

	return depi, err
}
开发者ID:shirayu,项目名称:go-jk,代码行数:27,代码来源:depinfo.go

示例6: setState

func setState(in *RuneBuffer, out chan *Item) StateFn {
	if r, ok := in.Peek(); ok {
		switch {
		case strings.IndexRune(" \t\r\n", r) >= 0:
			for ok { //skip whitespace
				_, ok, _ = in.Accept(" \t\r\n")
			}
			return setState
		case strings.IndexRune("*/", r) >= 0:
			in.Next()
			out <- &Item{Type: string(r), Token: string(r)}
			return setState
		case strings.IndexRune("0123456789", r) >= 0:
			numberState(in, out)
			return setState
		case r == ';':
			in.Next()
			out <- &Item{Type: "EOL", Token: "EOL"}
			commentState(in, out)
			return setState
		case r == '>':
			in.Next()
			out <- &Item{Type: ">", Token: ">"}
			return switchState
		default:
			return phonemeState
		}
	} else {
		return nil
	}
}
开发者ID:conlang-software-dev,项目名称:Logopoeist,代码行数:31,代码来源:lexfuncs.go

示例7: lexStart

func lexStart(l *Lexer) (next_fn LexFn) {
	for {
		r := l.Peek()

		if r == EOF {
			return nil
		} else if r == ' ' {
			l.Skip()
		} else if strings.IndexRune(DIGITS, r) >= 0 {
			return lexNumber
		} else if strings.IndexRune(OPERATORS, r) >= 0 {
			l.Expand()
			l.Emit(OperatorLexeme)
		} else if r == '(' {
			l.Expand()
			l.Emit(LeftParenLexeme)
		} else if r == ')' {
			l.Expand()
			l.Emit(RightParenLexeme)
		} else {
			l.Expand()
			l.Emit(ErrLexeme)
		}
	}

	return nil
}
开发者ID:colinmarc,项目名称:gocalc,代码行数:27,代码来源:lex.go

示例8: Colorize

// Colorize processes all colors in a given text block and returns a new string
// with all readable codes translated to ANSI codes.
func Colorize(text string) string {
	final := new(bytes.Buffer)
	toColor := new(bytes.Buffer)
	prevColorFunc := noopColorFunc
	for len(text) > 0 {
		startIndex := strings.IndexRune(text, ColorCodeOpen)
		switch {
		// if it's escaped, skip it.
		case startIndex > 0 && rune(text[startIndex-1]) == ColorCodeEscape:
			toColor.WriteString(text[:startIndex-1])
			toColor.WriteRune(ColorCodeOpen)
			text = text[startIndex+1:]
			continue
		case startIndex < 0:
			toColor.WriteString(text)
			final.WriteString(prevColorFunc(toColor.String()))
			text = ""
		default:
			toColor.WriteString(text[:startIndex])
			final.WriteString(prevColorFunc(toColor.String()))
			toColor = new(bytes.Buffer)
			text = text[startIndex+1:]
			endIndex := strings.IndexRune(text, ColorCodeClose)
			prevColorFunc = getColorFunction(text[:endIndex])
			text = text[endIndex+1:]
		}
	}

	return final.String()
}
开发者ID:bbuck,项目名称:dragon-mud,代码行数:32,代码来源:colors.go

示例9: getCharset

func getCharset() string {
	// Determine the character set.  This can help us later.
	// Per POSIX, we search for LC_ALL first, then LC_CTYPE, and
	// finally LANG.  First one set wins.
	locale := ""
	if locale = os.Getenv("LC_ALL"); locale == "" {
		if locale = os.Getenv("LC_CTYPE"); locale == "" {
			locale = os.Getenv("LANG")
		}
	}
	if locale == "POSIX" || locale == "C" {
		return "US-ASCII"
	}
	if i := strings.IndexRune(locale, '@'); i >= 0 {
		locale = locale[:i]
	}
	if i := strings.IndexRune(locale, '.'); i >= 0 {
		locale = locale[i+1:]
	} else {
		// Default assumption, and on Linux we can see LC_ALL
		// without a character set, which we assume implies UTF-8.
		return "UTF-8"
	}
	// XXX: add support for aliases
	return locale
}
开发者ID:gdamore,项目名称:tcell,代码行数:26,代码来源:charset_unix.go

示例10: Parse

func Parse(str string) (*Selector, error) {
	// TODO proper parsing
	var reqAttr string
	var subAttr string
	var re *regexp.Regexp
	startPos := 0
	endPos := len(str)
	negate := false
	if str[0] == '!' {
		negate = true
		startPos = 1
	}
	if idx := strings.IndexRune(str, '='); idx != -1 {
		var err error
		re, err = regexp.Compile(str[idx+1:])
		if err != nil {
			return nil, errors.New("invalid regexp")
		}
		endPos = idx
	}

	if idx := strings.IndexRune(str, ':'); idx >= startPos {
		reqAttr = str[startPos:idx]
		subAttr = str[idx+1 : endPos]
	} else {
		reqAttr = str[startPos:endPos]
	}
	if reqAttr == "" {
		return nil, errors.New("missing request attribute")
	}
	return &Selector{reqAttr, subAttr, re, negate}, nil
}
开发者ID:Rocknrenew,项目名称:filtron,代码行数:32,代码来源:selector.go

示例11: Purge

// Purge will remove color codes from the given string.
func Purge(text string) string {
	final := new(bytes.Buffer)
	for len(text) > 0 {
		startIndex := strings.IndexRune(text, ColorCodeOpen)
		switch {
		case startIndex >= 0:
			if startIndex > 0 && text[startIndex-1] == ColorCodeEscape {
				final.WriteString(text[:startIndex-1])
				final.WriteRune(ColorCodeOpen)
				text = text[startIndex+1:]
				continue
			} else if startIndex > 0 {
				final.WriteString(text[:startIndex])
			}

			endIndex := strings.IndexRune(text, ColorCodeClose)
			text = text[endIndex+1:]
		default:
			final.WriteString(text)
			text = ""
		}
	}

	return final.String()
}
开发者ID:bbuck,项目名称:dragon-mud,代码行数:26,代码来源:colors.go

示例12: unSlackify

func (sc *SlackClient) unSlackify(str string) string {
	// Links e.g. <http://heise.de|heise.de>, <http://heise.de>
	if strings.HasPrefix(str, "<http") {
		endpos := strings.IndexRune(str, '|')
		if endpos != -1 {
			return str[1:endpos]
		}
		return str[1 : len(str)-1]
	}
	// Highlights e. g. <@U02A2A2A2>
	if strings.HasPrefix(str, "<@U") {
		userID := str[2 : len(str)-1]
		user, ok := sc.userIDMap[userID]
		if ok {
			return user.Name
		}
		// if we do not have a match, just return the ID.
		return str[1 : len(str)-1]
	}
	// Mail addresses e.g. <mailto:[email protected]|[email protected]>
	if strings.HasPrefix(str, "<mailto:") {
		return str[8:strings.IndexRune(str, '|')]
	}
	// Channels <#C02A2A2A2>
	if strings.HasPrefix(str, "<#C") {
		chanID := str[2 : len(str)-1]
		channel, ok := sc.chanIDMap[chanID]
		if ok {
			return fmt.Sprintf("#%v", channel.Name)
		}
		return str[1 : len(str)-1]
	}
	return str
}
开发者ID:simonkern,项目名称:slirc,代码行数:34,代码来源:utility.go

示例13: ParseNamedDockerImageReference

// ParseNamedDockerImageReference parses a Docker pull spec string into a
// NamedDockerImageReference.
func ParseNamedDockerImageReference(spec string) (NamedDockerImageReference, error) {
	var ref NamedDockerImageReference

	namedRef, err := reference.ParseNamed(spec)
	if err != nil {
		return ref, err
	}

	name := namedRef.Name()
	i := strings.IndexRune(name, '/')
	if i == -1 || (!strings.ContainsAny(name[:i], ":.") && name[:i] != "localhost") {
		ref.Name = name
	} else {
		ref.Registry, ref.Name = name[:i], name[i+1:]
	}

	if named, ok := namedRef.(reference.NamedTagged); ok {
		ref.Tag = named.Tag()
	}

	if named, ok := namedRef.(reference.Canonical); ok {
		ref.ID = named.Digest().String()
	}

	// It's not enough just to use the reference.ParseNamed(). We have to fill
	// ref.Namespace from ref.Name
	if i := strings.IndexRune(ref.Name, '/'); i != -1 {
		ref.Namespace, ref.Name = ref.Name[:i], ref.Name[i+1:]
	}

	return ref, nil
}
开发者ID:juanluisvaladas,项目名称:origin,代码行数:34,代码来源:reference.go

示例14: breadcrumbsFn

func breadcrumbsFn(pdoc *doc.Package) string {
	if !strings.HasPrefix(pdoc.ImportPath, pdoc.ProjectRoot) {
		return ""
	}
	var buf bytes.Buffer
	i := 0
	j := len(pdoc.ProjectRoot)
	if j == 0 {
		buf.WriteString("<a href=\"/-/go\" title=\"Standard Packages\">☆</a> ")
		j = strings.IndexRune(pdoc.ImportPath, '/')
		if j < 0 {
			j = len(pdoc.ImportPath)
		}
	}
	for {
		buf.WriteString(`<a href="/`)
		buf.WriteString(urlFn(pdoc.ImportPath[:j]))
		buf.WriteString(`">`)
		buf.WriteString(template.HTMLEscapeString(pdoc.ImportPath[i:j]))
		buf.WriteString("</a>")
		i = j + 1
		if i >= len(pdoc.ImportPath) {
			break
		}
		buf.WriteByte('/')
		j = strings.IndexRune(pdoc.ImportPath[i:], '/')
		if j < 0 {
			j = len(pdoc.ImportPath)
		} else {
			j += i
		}
	}
	return buf.String()
}
开发者ID:jeffallen,项目名称:gopkgdoc,代码行数:34,代码来源:template.go

示例15: ParseImageTag

// ParseImageTag parses the docker image tag string and returns a validated Version or error
func ParseImageTag(s string) (CombinedVersion, error) {
	// try parse as a SEMVER
	prefix := ""
	version, err := semver.Parse(s)
	if err == nil {
		return CombinedVersion{Prefix: prefix, Version: version}, nil
	}
	// accept format such as `vSEMVER`
	if vIndex := strings.IndexRune(s, 'v'); vIndex == 0 {
		prefix = s[:vIndex+1]
		version, err = semver.Parse(s[vIndex+1:])
		if err == nil {
			return CombinedVersion{Prefix: prefix, Version: version}, nil
		}
	}
	// accept format such as `ANY-SEMVER`
	if firstHyphenIndex := strings.IndexRune(s, '-'); firstHyphenIndex != -1 {
		prefix = s[:firstHyphenIndex+1]
		version, err = semver.Parse(s[firstHyphenIndex+1:])
		if err == nil {
			return CombinedVersion{Prefix: prefix, Version: version}, nil
		}
	}
	return CombinedVersion{}, err
}
开发者ID:aclisp,项目名称:kubecon,代码行数:26,代码来源:algorithm.go


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