Esempio n. 1
0
func extractIssueArgs(text string, re *regexp.Regexp) (*github.IssueRequest, error) {
	match := re.FindAllString(text, -1)
	m := make([]string, len(match))
	for i, v := range match {
		m[i] = removeQuotes(v)
	}
	if m == nil || len(m) == 0 {
		return nil, &issueError{text}
	}
	var title, body, assignee *string
	title = &m[0]
	if len(m) >= 2 {
		body = &m[1]
	}
	if len(m) >= 3 {
		assignee = &m[2]
	}
	issueState := "open"
	request := github.IssueRequest{
		Title:     title,
		Body:      body,
		Labels:    nil,
		Assignee:  assignee,
		State:     &issueState,
		Milestone: nil,
	}
	return &request, nil
}
Esempio n. 2
0
func extractAndMatch(lines []string, pattern *regexp.Regexp) chan string {
	matchesChan := make(chan string)
	go func() {
		defer close(matchesChan)

		for _, line := range lines {
			if len(line) < 1 {
				continue
			}

			matches := pattern.FindAllString(line, -1)

			mappings := make(map[string]bool)

			for _, match := range matches {
				_, ok := mappings[match]
				if ok {
					continue
				}

				matchesChan <- match
				mappings[match] = true
			}
		}
	}()

	return matchesChan
}
Esempio n. 3
0
func reFindAll(re *regexp.Regexp) lua.Function {
	return func(l *lua.State) int {
		s := lua.CheckString(l, 1)
		n := lua.CheckInteger(l, 2)
		all := re.FindAllString(s, n)
		return util.DeepPush(l, all)
	}
}
Esempio n. 4
0
// Tokenize tokenizes the given string with the delimiter
func Tokenize(str *string, delimiter *regexp.Regexp) []Token {
	if delimiter == nil {
		// AWK-style (\S+\s*)
		tokens, prefixLength := awkTokenizer(str)
		return withPrefixLengths(tokens, prefixLength)
	}
	tokens := delimiter.FindAllString(*str, -1)
	return withPrefixLengths(tokens, 0)
}
Esempio n. 5
0
// ExtractBy returns a string.
func (source *Source) ExtractBy(r *regexp.Regexp) string {
	results := r.FindAllString(source.line, 1)
	if len(results) != 1 {
		return ""
	}
	result := results[0]
	source.line = source.line[len(result):]
	return result
}
Esempio n. 6
0
func scan(re *regexp.Regexp, r io.Reader) {
	scanner := bufio.NewScanner(r)
	scanner.Split(bufio.ScanWords)
	for scanner.Scan() {
		word := scanner.Text()
		for _, match := range re.FindAllString(word, -1) {
			fmt.Println(match)
		}
	}
}
Esempio n. 7
0
func grep(re *regexp.Regexp, name string, r io.Reader) error {
	b := bufio.NewReader(r)
	c := uint64(0)
	for n := uint64(1); ; n++ {
		ln, err := fgetln(b)
		m := re.FindAllString(ln, -1)

		if len(m) > 0 {
			if *qflag {
				os.Exit(status)
			}
			if *cflag {
				c += uint64(len(m))
			}
			found = true
		}

		if !*cflag {
			for _, s := range m {
				if *nflag {
					s = fmt.Sprintf("%d:%s", n, s)
				}
				if many {
					s = fmt.Sprintf("%s:%s", name, s)
				}

				if strings.IndexFunc(s, isNonPrint) >= 0 {
					fmt.Println("Binary file", name, "matches")
					return nil
				} else {
					fmt.Println(s)
				}
			}
		}

		if err == io.EOF {
			break
		}

		if err != nil {
			return err
		}
	}

	if *cflag {
		s := fmt.Sprintf("%d", c)
		if many {
			s = fmt.Sprintf("%s:%s", name, s)
		}
		fmt.Println(s)
	}

	return nil
}
Esempio n. 8
0
// listObjects returns a list of entries in a bucket
func (r *s3Client) listObjects(bucket, path string, recursive bool) (list []*objectFile, err error) {

	log.Debugf("retrieving the secrets from bucket: %s, path: '%s'", bucket, path)

	pathBaseDirectory := filepath.Dir(path)
	if pathBaseDirectory == "." {
		pathBaseDirectory = ""
	}

	var regex *regexp.Regexp
	switch recursive {
	case true:
		regex, err = regexp.Compile("^" + strings.Replace(path, "*", ".*", 0) + "[\\w-_\\.]*[$/]?.*")
	default:
		regex, err = regexp.Compile("^" + strings.Replace(path, "*", ".*", 0) + "[\\w-_\\.]*[$/]?")
	}
	if err != nil {
		return list, err
	}

	resp, err := r.client.ListObjects(&s3.ListObjectsInput{
		Bucket: aws.String(bucket),
		Prefix: aws.String(pathBaseDirectory),
	})
	if err != nil {
		return list, err
	}

	seenDir := make(map[string]bool, 0)

	for _, object := range resp.Contents {
		matches := regex.FindAllString(*object.Key, 1)
		if len(matches) <= 0 {
			continue
		}

		entry := &objectFile{path: matches[0]}
		if !strings.HasSuffix(entry.path, "/") {
			entry.size = *object.Size
			entry.lastModified = object.LastModified
			entry.owner = *object.Owner.DisplayName
		} else {
			if found := seenDir[entry.path]; found {
				continue
			}
			seenDir[entry.path] = true
			entry.directory = true
		}

		list = append(list, entry)
	}

	return list, nil
}
Esempio n. 9
0
// Parse sets a operand value to the register operand and
// returns the remain string and true.
//
// If the source is invalid,
// This returns the source itself and false.
// In this case doesn't change the register operand.
func (operand *Single) Parse(source string, byRegex *regexp.Regexp) (string, bool) {
	matches := byRegex.FindAllString(string(source), 1)
	if len(matches) <= 0 {
		return source, false
	}
	operand.SingleValue = strings.Trim(matches[0], " \t,")
	operand.HasValue = true
	remains := byRegex.ReplaceAllString(string(source), "")
	if strings.HasSuffix(matches[0], ",") {
		remains = "," + remains
	}
	return remains, true
}
Esempio n. 10
0
func (g *GoltGenerator) replaceRegex(regex *regexp.Regexp, value *string) {
	/*
		Given a specific regular expression, a pointer to a string and a map of stored variable
		This method will have the side effect of changing the value pointer by the string if the regex is matching
	*/
	if regex.MatchString(*value) {
		for _, foundMatch := range regex.FindAllString(*value, -1) {
			mapKey := foundMatch[2 : len(foundMatch)-1]
			extractedValue := g.RegexMap[mapKey]
			*value = strings.Replace(*value, foundMatch, extractedValue, -1)
		}
	}
}
Esempio n. 11
0
func fullMatchString(re *regexp.Regexp, s string) []string {
	var rs = re.FindAllStringIndex(s, -1)
	var cur int
	for _, r := range rs {
		if notWhiteSpace(s, cur, r[0]) {
			return nil
		}
		if cur > 0 && cur == r[0] {
			return nil
		}
		cur = r[1]
	}

	if notWhiteSpace(s, cur, len(s)) {
		return nil
	}

	return re.FindAllString(s, -1)
}
Esempio n. 12
0
func readProductType(lines []string, regex *regexp.Regexp) int64 {
	for _, line := range lines {
		if regex.MatchString(line) {
			result := regex.FindAllString(line, 1)
			if len(result) > 0 {
				productString := result[0]
				productId := productString[len(productString)-6:]
				product, err := strconv.ParseInt(productId, 0, 32)
				if err != nil {
					fmt.Println("Error parsing product id", err)
					return 0
				}
				return product
			}
			return 0
		}
	}
	return 0
}
Esempio n. 13
0
func scanPath(re *regexp.Regexp, path string) error {
	r := os.Stdin
	if path != "-" {
		f, err := os.Open(path)
		if err != nil {
			return err
		}
		defer f.Close()
		r = f
	}
	scanner := bufio.NewScanner(r)
	scanner.Split(bufio.ScanWords)
	for scanner.Scan() {
		word := scanner.Text()
		for _, match := range re.FindAllString(word, -1) {
			fmt.Println(match)
		}
	}
	return scanner.Err()
}
Esempio n. 14
0
// ReadList takes a Matcher list, that is then read and scanned for data.
func ReadList(buf *bufio.Reader, mt Matcher, reg *regexp.Regexp) {
	line, err := buf.ReadString('\n')
	if err != nil {
		panic(fmt.Errorf("Unexpected end of file"))
	}
	var n int
	fmt.Sscanf(line, "%d", &n)
	// Reset the list.
	mt.CreateList(n)
	// Matches all lines and scan in.
	for err != io.EOF {
		line, err = buf.ReadString('\n')
		if err != nil && err != io.EOF {
			panic(err)
		}
		matches := reg.FindAllString(line, -1)
		if len(matches) == 4 {
			index := -1
			fmt.Sscanf(matches[0], "%d", &index)
			mt.Match(matches, index)
		}
	}
}
Esempio n. 15
0
func bench(b *testing.B, re *regexp.Regexp, str string) {
	for i := 0; i < b.N; i++ {
		re.FindAllString(str, -1)
	}
}
Esempio n. 16
0
func (p *print) Start() {
	FileMatchCount = 0
	MatchCount = 0
	for arg := range p.In {

		if p.Option.FilesWithRegexp != "" {
			p.printPath(arg.Path)
			fmt.Println()
			FileMatchCount++
			continue
		}

		if len(arg.Matches) == 0 {
			continue
		}

		if p.Option.Count {
			p.printPath(arg.Path)

			var re *regexp.Regexp
			pattern := arg.Pattern
			if pattern.UseRegexp || pattern.IgnoreCase {
				re = pattern.Regexp
			} else {
				re = regexp.MustCompile(regexp.QuoteMeta(pattern.Pattern))
			}

			count := 0
			for _, m := range arg.Matches {
				count += len(re.FindAllString(m.Line.Str, -1))
				MatchCount++
			}
			p.printCount(count)
			fmt.Println()
			FileMatchCount++
			continue
		}

		if p.Option.FilesWithMatches {
			p.printPath(arg.Path)
			fmt.Println()
			FileMatchCount++
			continue
		}
		if !p.Option.NoGroup {
			p.printPath(arg.Path)
			fmt.Println()
			FileMatchCount++
		}
		lastLineNum := 0
		enableContext := p.Option.Before > 0 || p.Option.After > 0
		for _, v := range arg.Matches {
			if v == nil {
				continue
			}
			if enableContext {
				if lastLineNum > 0 && lastLineNum+1 != v.FirstLineNum() {
					fmt.Println("--")
				}
				lastLineNum = v.LastLineNum()
			}
			if p.Option.NoGroup {
				p.printPath(arg.Path)
			}
			p.printContext(v.Befores)
			p.printMatch(arg.Pattern, v)
			MatchCount++
			fmt.Println()
			p.printContext(v.Afters)
		}
		if !p.Option.NoGroup {
			fmt.Println()
		}
	}
	if p.Option.Stats {
		fmt.Printf("%d Files Matched\n", FileMatchCount)
		fmt.Printf("%d Total Text Matches\n", MatchCount)
	}
	p.Done <- struct{}{}
}
Esempio n. 17
0
func extractCandidates(regex *regexp.Regexp, s string) []string {
	matches := regex.FindAllString(s, -1)
	candidates := extractDigits(matches)

	return candidates
}
Esempio n. 18
0
// Gather the information from the repositories and determine how many incoming changesets are available.
// Post an environment aware notification for repositories with pending changesets.
func gatherer(directory string) {
	repo := path.Dir(directory)

	// Execute the appropriate subcommand for `incoming`
	var buffer bytes.Buffer
	app := &cmd.App{
		Args:      []string{"incoming"},
		Directory: repo,
		Stdout:    &buffer,
	}

	err := app.Run()
	if err == nil {
		base := path.Base(directory)
		b := buffer.String()

		// Special logic to count the individual changesets
		var re *regexp.Regexp
		switch {
		case base == ".git":
			re = regexp.MustCompile("commit")
			break
		case base == ".hg":
			re = regexp.MustCompile("changeset")
			break
		case base == ".tf":
			re = regexp.MustCompile("[\r\n]([0-9]+)")
			break
		case base == ".bzr":
			re = regexp.MustCompile("commit")
			break
		}

		count := len(re.FindAllString(b, -1))
		if count > 0 {
			a := []string{}

			// Log to standard output
			log.Println("Repository found at", path.Base(repo), "with", count, "incoming changes")

			// Build an array for command execuation based on the environment
			switch runtime.GOOS {
			case "darwin":
				a = append(a, "growlnotify")
				a = append(a, "-n")
				a = append(a, "code-notify")
				a = append(a, "-m")
				a = append(a, fmt.Sprintf("%s: incoming changeset(s)", path.Base(repo)))
				a = append(a, fmt.Sprintf("%d changesets upstream\n\nDirectory:\n%s", count, repo))
				break
			case "linux":
				a = append(a, "notify-send")
				a = append(a, "-a")
				a = append(a, "code-notify")
				a = append(a, fmt.Sprintf("%s: incoming changeset(s)", path.Base(repo)))
				a = append(a, fmt.Sprintf("%d changesets upstream\n\nDirectory:\n%s", count, repo))
				break
			case "windows":
				a = append(a, "growlnotify")
				a = append(a, "/t:")
				a = append(a, fmt.Sprintf("%s: incoming changeset(s)", path.Base(repo)))
				a = append(a, fmt.Sprintf("%d changesets upstream\n\nDirectory:\n%s", count, repo))
				break
			}

			// Send the notification to the system
			cmd := exec.Command(a[0], a[1:]...)
			cmd.Run()
		}
	}
}
func (e *RegexExtractor) ExtractAndCheck(r *http.Request) (SessionID string, returnOverrides ReturnOverrides) {
	var extractorOutput string
	var err error

	var config RegexExtractorConfig

	err = mapstructure.Decode(e.Config.ExtractorConfig, &config)

	if err != nil {
		returnOverrides = e.Error(r, err, "Can't decode RegexExtractor configuration")
		return SessionID, returnOverrides
	}

	if e.Config.ExtractorConfig["regex_expression"] == nil {
		returnOverrides = e.Error(r, nil, "RegexExtractor expects an expression")
		return SessionID, returnOverrides
	}

	var expression *regexp.Regexp
	expression, err = regexp.Compile(config.RegexExpression)

	if err != nil {
		returnOverrides = e.Error(r, nil, "RegexExtractor found an invalid expression")
		return SessionID, returnOverrides
	}

	switch e.Config.ExtractFrom {
	case tykcommon.HeaderSource:
		extractorOutput, err = e.ExtractHeader(r)
	case tykcommon.BodySource:
		extractorOutput, err = e.ExtractBody(r)
	case tykcommon.FormSource:
		extractorOutput, err = e.ExtractForm(r, config.FormParamName)
	}

	if err != nil {
		returnOverrides = e.Error(r, err, "RegexExtractor error")
		return SessionID, returnOverrides
	}

	var regexOutput []string
	regexOutput = expression.FindAllString(extractorOutput, -1)

	SessionID = e.GenerateSessionID(regexOutput[config.RegexMatchIndex], e.TykMiddleware)

	var keyExists bool
	var previousSessionState SessionState
	previousSessionState, keyExists = e.TykMiddleware.CheckSessionAndIdentityForValidKey(SessionID)

	if keyExists {

		lastUpdated, _ := strconv.Atoi(previousSessionState.LastUpdated)

		deadlineTs := int64(lastUpdated) + previousSessionState.IdExtractorDeadline

		if deadlineTs > time.Now().Unix() {
			e.PostProcess(r, previousSessionState, SessionID)
			returnOverrides = ReturnOverrides{
				ResponseCode: 200,
			}
		}
	}

	return SessionID, returnOverrides
}