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


Golang strings.IndexAny函数代码示例

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


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

示例1: escape

func escape(w writer, s string) error {
	i := strings.IndexAny(s, escapedChars)
	for i != -1 {
		if _, err := w.WriteString(s[:i]); err != nil {
			return err
		}
		var esc string
		switch s[i] {
		case '&':
			esc = "&"
		case '\'':
			// "'" is shorter than "'" and apos was not in HTML until HTML5.
			esc = "'"
		case '<':
			esc = "&lt;"
		case '>':
			esc = "&gt;"
		case '"':
			// "&#34;" is shorter than "&quot;".
			esc = "&#34;"
		case '\r':
			esc = "&#13;"
		default:
			panic("unrecognized escape character")
		}
		s = s[i+1:]
		if _, err := w.WriteString(esc); err != nil {
			return err
		}
		i = strings.IndexAny(s, escapedChars)
	}
	_, err := w.WriteString(s)
	return err
}
开发者ID:blast-analytics-marketing,项目名称:htmlrender,代码行数:34,代码来源:helpers.go

示例2: NewIncomingMessage

// returns nil for empty or invalid messages
func NewIncomingMessage(line string) *IRCMessage {
	var m IRCMessage
	m.cmds = makeCmdMap()
	m.ncmds = makeNCmdMap()
	line = strings.TrimSpace(line)
	if len(line) == 0 { // empty message
		return nil
	}
	m.prefix = ""
	if line[0] == ':' { // we have optional prefix
		fs := strings.IndexAny(line, " ")
		m.prefix = line[1:fs]
		line = line[fs+1:]
	}
	//m.command = strings.Fields(line)[0]
	fc := strings.IndexAny(line, ":")
	if fc != -1 {
		m.command = strings.TrimSpace(line[0:fc])
		m.argument = line[fc+1:]
	} else {
		m.command = line
	}
	if !m.cmds[pureCmd(m.command)] { // not in literal cmds
		if !m.ncmds[pureCmd(m.command)] { // not in numerical cmds
			return nil
		}
	}
	return &m
}
开发者ID:shoenig,项目名称:CoffeeBot,代码行数:30,代码来源:message.go

示例3: validateProjectTaskIdsAndTags

// validateProjectTaskIdsAndTags ensures that task tags and ids only contain valid characters
func validateProjectTaskIdsAndTags(project *model.Project) []ValidationError {
	errs := []ValidationError{}
	// create a map to hold the task names
	for _, task := range project.Tasks {
		// check task name
		if i := strings.IndexAny(task.Name, model.InvalidCriterionRunes); i == 0 {
			errs = append(errs, ValidationError{
				Message: fmt.Sprintf("task '%v' has invalid name: starts with invalid character %v",
					task.Name, strconv.QuoteRune(rune(task.Name[0])))})
		}
		// check tag names
		for _, tag := range task.Tags {
			if i := strings.IndexAny(tag, model.InvalidCriterionRunes); i == 0 {
				errs = append(errs, ValidationError{
					Message: fmt.Sprintf("task '%v' has invalid tag '%v': starts with invalid character %v",
						task.Name, tag, strconv.QuoteRune(rune(tag[0])))})
			}
			if i := util.IndexWhiteSpace(tag); i != -1 {
				errs = append(errs, ValidationError{
					Message: fmt.Sprintf("task '%v' has invalid tag '%v': tag contains white space",
						task.Name, tag)})
			}
		}
	}
	return errs
}
开发者ID:sr527,项目名称:evergreen,代码行数:27,代码来源:project_validator.go

示例4: main

func main() {
	fmt.Println(strings.Index("Hello, world!", "He"))  // 0: He가 맨 처음에 있으므로 0
	fmt.Println(strings.Index("Hello, world!", "wor")) // 7: wor가 8번째에 있으므로 7
	fmt.Println(strings.Index("Hello, world!", "ow"))  // -1: ow는 없으므로 -1

	fmt.Println(strings.IndexAny("Hello, world!", "eo")) // 1: e가 2번째에 있으므로 1
	fmt.Println(strings.IndexAny("Hello, world!", "f"))  // -1: f는 없으므로 -1

	var c byte
	c = 'd'
	fmt.Println(strings.IndexByte("Hello, world!", c)) // 11: d가 12번째에 있으므로 11
	c = 'f'
	fmt.Println(strings.IndexByte("Hello, world!", c)) // -1: f는 없으므로 -1

	var r rune
	r = '언'
	fmt.Println(strings.IndexRune("고 언어", r)) // 4: "언"이 시작되는 인덱스가 4

	f := func(r rune) bool {
		return unicode.Is(unicode.Hangul, r) // r이 한글 유니코드이면 true를 리턴
	}
	fmt.Println(strings.IndexFunc("Go 언어", f))       // 3: 한글이 4번째부터 시작하므로 3
	fmt.Println(strings.IndexFunc("Go Language", f)) // -1: 한글이 없으므로 -1

	fmt.Println(strings.LastIndex("Hello Hello Hello, world!", "Hello"))
	// 12: 마지막 Hello가 13번째에 있으므로 12

	fmt.Println(strings.LastIndexAny("Hello, world", "ol")) // 10: 마지막 l이 11번째에 있으므로 10

	fmt.Println(strings.LastIndexFunc("Go 언어 안녕", f)) // 13: 마지막 한글인 '녕'이 시작되는 인덱스가 13
}
开发者ID:jemoonkim,项目名称:golangbook,代码行数:31,代码来源:strings_Index.go

示例5: escape

func escape(w writer, s string) os.Error {
	i := strings.IndexAny(s, escapedChars)
	for i != -1 {
		if _, err := w.WriteString(s[:i]); err != nil {
			return err
		}
		var esc string
		switch s[i] {
		case '&':
			esc = "&amp;"
		case '\'':
			esc = "&apos;"
		case '<':
			esc = "&lt;"
		case '>':
			esc = "&gt;"
		case '"':
			esc = "&quot;"
		default:
			panic("unrecognized escape character")
		}
		s = s[i+1:]
		if _, err := w.WriteString(esc); err != nil {
			return err
		}
		i = strings.IndexAny(s, escapedChars)
	}
	_, err := w.WriteString(s)
	return err
}
开发者ID:qtse,项目名称:go_fetch,代码行数:30,代码来源:escape.go

示例6: begin

// begin prepare the current path to walk through it
func (d *decoder) begin() (err error) {
	d.curr = d.main
	fields := strings.Split(d.field, ".")
	for i, field := range fields {
		b := strings.IndexAny(field, "[")
		if b != -1 {
			// is a array
			e := strings.IndexAny(field, "]")
			if e == -1 {
				return errors.New("formam: bad syntax array")
			}
			d.field = field[:b]
			if d.index, err = strconv.Atoi(field[b+1 : e]); err != nil {
				return errors.New("formam: the index of array not is a number")
			}
			if len(fields) == i+1 {
				return d.end()
			}
			if err = d.walk(); err != nil {
				return
			}
		} else {
			// not is a array
			d.field = field
			d.index = -1
			if len(fields) == i+1 {
				return d.end()
			}
			if err = d.walk(); err != nil {
				return
			}
		}
	}
	return
}
开发者ID:grafov,项目名称:formam,代码行数:36,代码来源:formam.go

示例7: CompareVer

func CompareVer(ver1, ver2 string) int {
	ver1 += "."
	ver2 += "."
	res := 0
	s1 := ""
	s2 := ""
	for res == 0 {

		n1 := strings.IndexAny(ver1, ".-")
		n2 := strings.IndexAny(ver2, ".-")

		if n1 > -1 {
			s1 = fmt.Sprintf("%010s", ver1[:n1])
			ver1 = ver1[n1+1:]
		} else {
			s1 = "0000000000"
		}

		if n2 > -1 {
			s2 = fmt.Sprintf("%010s", ver2[:n2])
			ver2 = ver2[n2+1:]
		} else {

			s2 = "0000000000"
		}

		res = strings.Compare(s1, s2)
		if n1 < 0 && n2 < 0 {
			break
		}
	}

	return res
}
开发者ID:SokoloffA,项目名称:zrpm,代码行数:34,代码来源:package.go

示例8: getDepfileImpl

func getDepfileImpl(ss string) (string, error) {
	tss := ss + " "
	if (!strings.Contains(tss, " -MD ") && !strings.Contains(tss, " -MMD ")) || !strings.Contains(tss, " -c ") {
		return "", nil
	}

	mfIndex := strings.Index(ss, " -MF ")
	if mfIndex >= 0 {
		mf := trimLeftSpace(ss[mfIndex+4:])
		if strings.Index(mf, " -MF ") >= 0 {
			return "", fmt.Errorf("Multiple output file candidates in %s", ss)
		}
		mfEndIndex := strings.IndexAny(mf, " \t\n")
		if mfEndIndex >= 0 {
			mf = mf[:mfEndIndex]
		}

		return mf, nil
	}

	outIndex := strings.Index(ss, " -o ")
	if outIndex < 0 {
		return "", fmt.Errorf("Cannot find the depfile in %s", ss)
	}
	out := trimLeftSpace(ss[outIndex+4:])
	if strings.Index(out, " -o ") >= 0 {
		return "", fmt.Errorf("Multiple output file candidates in %s", ss)
	}
	outEndIndex := strings.IndexAny(out, " \t\n")
	if outEndIndex >= 0 {
		out = out[:outEndIndex]
	}
	return stripExt(out) + ".d", nil
}
开发者ID:dalinaum,项目名称:kati,代码行数:34,代码来源:ninja.go

示例9: consumeLine

func consumeLine(data []byte, atEOF bool) (advance int, token []byte, err error) {
	advance, token, err = bufio.ScanLines(data, atEOF)
	if advance == 0 && len(token) == 0 {
		return
	}
	tokenString := string(token)
	//Skip empty lines
	if advance == 2 {
		advance, token, err = consumeLine(data[advance:len(data)], atEOF)
		advance = advance + 2
	}
	//Drop lines with comments OR drop line ending with comments
	if strings.IndexAny(tokenString, "/") == 0 {
		storedAdvance := advance
		advance, token, err = consumeLine(data[storedAdvance:len(data)], atEOF)
		advance = advance + storedAdvance
	} else if commentIndex := strings.IndexAny(tokenString, "/"); commentIndex != -1 {
		token = token[0:commentIndex]
	}

	//Remove all spaces
	token = bytes.Map(func(r rune) (newR rune) {
		if unicode.IsSpace(r) {
			newR = -1
		} else {
			newR = r
		}
		return
	}, token)
	return
}
开发者ID:jw56578,项目名称:nand2tetris-2,代码行数:31,代码来源:assembler.go

示例10: ExampleIndexAny

func ExampleIndexAny() {
	fmt.Println(strings.IndexAny("chicken", "aeiouy"))
	fmt.Println(strings.IndexAny("crwth", "aeiouy"))
	// Output:
	// 2
	// -1
}
开发者ID:2thetop,项目名称:go,代码行数:7,代码来源:example_test.go

示例11: escape

func escape(buf *bytes.Buffer, s string) {
	i := strings.IndexAny(s, escapedChars)
	for i != -1 {
		buf.WriteString(s[0:i])
		var esc string
		switch s[i] {
		case '&':
			esc = "&amp;"
		case '\'':
			esc = "&apos;"
		case '<':
			esc = "&lt;"
		case '>':
			esc = "&gt;"
		case '"':
			esc = "&quot;"
		default:
			panic("unrecognized escape character")
		}
		s = s[i+1:]
		buf.WriteString(esc)
		i = strings.IndexAny(s, escapedChars)
	}
	buf.WriteString(s)
}
开发者ID:GNA-SERVICES-INC,项目名称:MoNGate,代码行数:25,代码来源:escape.go

示例12: Divide

// Divide splits a word into its vowels
func Divide(word string, vowels string) (syllables []string) {
	word = strings.ToLower(word)
	var lv, rv, dist int

	for len(word) > 0 {
		lv = strings.IndexAny(word, vowels)
		if lv == -1 {
			syllables = append(syllables, word)
			break
		}

		rv = strings.IndexAny(word[lv+1:], vowels)
		for rv == 0 {
			lv++
			rv = strings.IndexAny(word[lv+1:], vowels)
		}

		if rv == -1 {
			syllables = append(syllables, word)
			break
		}

		dist = ((rv + lv) - lv) / 2
		if dist > -1 {
			syllables = append(syllables, word[:lv+1+dist])
			word = word[lv+1+dist:]
		}
	}

	return
}
开发者ID:der-antikeks,项目名称:NameGen,代码行数:32,代码来源:divider.go

示例13: IndexAny

// IndexAny returns the index of the first instance of any Unicode code point from chars in s
// or -1 if no Unicode code point from chars is present in s
func IndexAny(s, chars string) int {
	fmt.Println(strings.IndexAny("chicken", "aeiouy"))    // 2
	fmt.Println(strings.IndexAny("crwth", "aeiouy"))      // -1
	fmt.Println(strings.IndexAny("crwth", "axrrrrrrrr"))  // 1
	fmt.Println(strings.IndexAny("crwth", "acsxxxrrrrr")) // 0
	return strings.IndexAny(s, chars)
}
开发者ID:upccup,项目名称:cuplearn,代码行数:9,代码来源:stringsTest.go

示例14: Test_OCRADataInputQuestion

func Test_OCRADataInputQuestion(t *testing.T) {
	ocraSuiteFmt := []string{"OCRA-1:HOTP-SHA1-6:Q%v%02d", "OCRA-1:HOTP-SHA1-6:C-Q%v%02d", "OCRA-1:HOTP-SHA1-6:C-Q%v%02d-T1M"}
	key := "12345678"
	questionN := "1234112233445566778899001122334455667788990011223344556677889900"
	questionB := make([]byte, maxOcraSuiteDataInputQuestionLength)
	var question string

	for _, s := range ocraSuiteFmt {
		for i := ' '; i <= '}'; i++ {
			format := string(i)
			for j := 0; j < 200; j++ {
				if format == ocraSuiteDataInputQuestionAlfabetToken {
					io.ReadFull(rand.Reader, questionB)
					question = string(questionB)
				} else {
					question = questionN
				}
				str := strings.ToLower(fmt.Sprintf(s, format, j))
				_, err := GenerateOCRAAdvance(str, key, "", question, "", "", "")
				if err == nil && (strings.IndexAny(strings.ToLower(format), ocraSuiteDataInputQuestionTypeOptions) == -1 ||
					j < minOcraSuiteDataInputQuestionLength || j > maxOcraSuiteDataInputQuestionLength) {
					t.Error("Test fail: Accept illegal OCRA dataInput question format:", str)
				}
				if err != nil && (strings.IndexAny(format, ocraSuiteDataInputQuestionTypeOptions) != -1 &&
					(j >= minOcraSuiteDataInputQuestionLength && j <= maxOcraSuiteDataInputQuestionLength)) {
					t.Error("Test fail: legal OCRA dataInput question format:", str, "wasn't accpeted, error:", err)
				}
			}
		}
	}
}
开发者ID:rgw5267,项目名称:libsecurity-go,代码行数:31,代码来源:ocra_test.go

示例15: BenchmarkStringsIndexAny

func BenchmarkStringsIndexAny(b *testing.B) {
	for i := 0; i < b.N; i++ {
		strings.IndexAny(printable, "abcdefghijklmnopqrstuvwxyz")
		strings.IndexAny(printable, " abcdefghijklmnopqrstuvwxyz")
		strings.IndexAny(printable, " ")
	}
}
开发者ID:jmptrader,项目名称:byteutil,代码行数:7,代码来源:byteutil_benchmark_test.go


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