Ejemplo n.º 1
0
func extractString(src []byte, rx *regexp.Regexp) (s string) {
	m := rx.FindSubmatch(src)
	if m != nil {
		s = strings.TrimSpace(string(m[1]))
	}
	return
}
Ejemplo n.º 2
0
func checkNewChapter(re *regexp.Regexp, l []byte) (depth int, title string) {
	if m := re.FindSubmatch(l); m != nil && m[1][0] == m[3][0] {
		depth = int(m[1][0] - '0')
		title = string(m[2])
	}
	return
}
Ejemplo n.º 3
0
func parseDeath(tag, msg []byte) ([]byte, []byte) {
	if bytes.Compare(tag, []byte("ActivityManager")) == 0 {
		var matcher *regexp.Regexp
		swap := false
		if PID_KILL.Match(msg) {
			matcher = PID_KILL
			swap = true
		} else if PID_LEAVE.Match(msg) {
			matcher = PID_LEAVE
		} else if PID_DEATH.Match(msg) {
			matcher = PID_DEATH
		}
		if matcher != nil {
			match := matcher.FindSubmatch(msg)
			pid := match[2]
			pack := match[1]
			if swap {
				pid = pack
				pack = match[2]
			}
			return pid, pack
		}
	}

	return nil, nil
}
Ejemplo n.º 4
0
func GetWireDefaultGateway() (net.IP, error) {
	var regex *regexp.Regexp
	var command *exec.Cmd

	switch runtime.GOOS {
	case "darwin":
		command = exec.Command("netstat", "-rn", "-f", "inet")
		regex = regexp.MustCompile(`default\s+(\d+\.\d+\.\d+\.\d+)\s+.*`)
	case "linux":
		command = exec.Command("ip", "-4", "route", "show")
		regex = regexp.MustCompile(`default\s+via\s+(\d+\.\d+\.\d+\.\d+)\s+.*`)
	default:
		return nil, fmt.Errorf("Getting gateway is not supported in %v", runtime.GOOS)
	}

	output, err := command.Output()
	if err != nil {
		return nil, err
	}
	if result := regex.FindSubmatch(output); result == nil {
		return nil, fmt.Errorf("Unable to get default gateway")
	} else {
		return net.ParseIP(string(result[1])), nil
	}
}
Ejemplo n.º 5
0
// ParseHeadID parse ID from head by IDRegexp
func ParseHeadID(idRegexp *regexp.Regexp, head []byte) []byte {
	found := idRegexp.FindSubmatch(head)
	if found == nil { // not match
		return head
	}
	return found[1]
}
Ejemplo n.º 6
0
func processColorTemplates(colorTemplateRegexp *regexp.Regexp, buf []byte) []byte {
	// We really want ReplaceAllSubmatchFunc, i.e.: https://github.com/golang/go/issues/5690
	// Instead we call FindSubmatch on each match, which means that backtracking may not be
	// used in custom Regexps (matches must also match on themselves without context).
	colorTemplateReplacer := func(token []byte) []byte {
		tmp2 := []byte{}
		groups := colorTemplateRegexp.FindSubmatch(token)
		var ansiActive ActiveAnsiCodes
		for _, codeBytes := range bytes.Split(groups[1], bytesComma) {
			colorCode, ok := ansiColorCodes[string(codeBytes)]
			if !ok {
				// Don't modify the text if we don't recognize any of the codes
				return groups[0]
			}
			for _, code := range colorCode.GetAnsiCodes() {
				ansiActive.add(code)
				tmp2 = append(tmp2, ansiEscapeBytes(code)...)
			}
		}
		if len(groups[2]) > 0 {
			tmp2 = append(tmp2, groups[3]...)
			tmp2 = append(tmp2, ansiActive.getResetBytes()...)
		}
		return tmp2
	}
	return colorTemplateRegexp.ReplaceAllFunc(buf, colorTemplateReplacer)
}
func findFirst(r *regexp.Regexp, body []byte) string {
	matches := r.FindSubmatch(body)
	if len(matches) >= 1 {
		return string(matches[1])
	}
	return ""
}
Ejemplo n.º 8
0
func findSubmatch(re *regexp.Regexp, b []byte) (retByte []byte) {
	matches := re.FindSubmatch(b)
	if len(matches) != 2 {
		log.Fatal("No match")
	}
	retByte = matches[1]
	return
}
Ejemplo n.º 9
0
Archivo: util.go Proyecto: wizos/gofeed
func ParsePubDate(formatReg *regexp.Regexp, dateStr string) (time.Time, error) {
	if nil == formatReg {
		log.Printf("[ERROR] error parsing pubdate, date format regexp is nil")
		return time.Time{}, errors.New("date format regexp is nil")
	}

	pubdateStr := strings.TrimSpace(dateStr)
	if "" == pubdateStr {
		log.Printf("[ERROR] error parsing pubdate, pubdate string is empty")
		return time.Time{}, errors.New("pubdate string is empty")
	}

	now := time.Now()
	year := now.Year()
	month := int(now.Month())
	day := now.Day()
	hour := now.Hour()
	minute := now.Minute()
	second := now.Second()

	match := formatReg.FindSubmatch([]byte(pubdateStr))
	if nil == match {
		log.Printf("[ERROR] error parsing pubdate %s, pattern %s match failed", pubdateStr, formatReg.String())
		return time.Time{}, errors.New("failed to match pubdate pattern")
	}
	for patInd, patName := range formatReg.SubexpNames() {
		var err error
		patVal := string(match[patInd])
		switch patName {
		case PATTERN_YEAR:
			year, err = strconv.Atoi(patVal)
		case PATTERN_MONTH:
			month, err = ParseDateMonth(patVal)
		case PATTERN_DAY:
			day, err = strconv.Atoi(patVal)
		case PATTERN_HOUR:
			hour, err = strconv.Atoi(patVal)
		case PATTERN_MINUTE:
			minute, err = strconv.Atoi(patVal)
		case PATTERN_SECOND:
			second, err = strconv.Atoi(patVal)
		}

		if nil != err {
			log.Printf("[ERROR] error parsing pubdate: %s, time value %s cannot be parsed: %s",
				pubdateStr,
				match[patInd],
				err,
			)
			return time.Time{}, err
		}
	}

	return time.Date(year, time.Month(month), day, hour, minute, second, 0, GOFEED_DEFAULT_TIMEZONE), nil
}
Ejemplo n.º 10
0
func extractValue(s string, r *regexp.Regexp) (bool, int, error) {
	matches := r.FindSubmatch([]byte(s))
	if len(matches) == 2 {
		val, err := strconv.ParseInt(string(matches[1]), 10, 32)
		if err != nil {
			return false, -1, err
		}
		return true, int(val), nil
	}
	return false, -1, nil
}
Ejemplo n.º 11
0
// Generic submatch mapper.  Returns nil if no match.
func RegexpSubmatchesToMap(re *regexp.Regexp, input *[]byte) map[string]string {
	submatches := re.FindSubmatch(*input)
	if submatches == nil {
		return nil
	}
	data := map[string]string{}
	for i, key := range haProxyLogRe.SubexpNames() {
		data[key] = string(submatches[i])
		//fmt.Printf("data[%v] = \"%v\"\n", key, data[key])
	}
	return data
}
Ejemplo n.º 12
0
Archivo: spec.go Proyecto: postfix/rpm
func (s *SpecFile) findSubmatch(re *regexp.Regexp, nmatches int) ([]byte, error) {
	matches := re.FindSubmatch(s.raw)
	if matches != nil {
		if len(matches) > nmatches {
			return nil, ErrTooManySubs
		} else if len(matches) < nmatches {
			return nil, ErrTooFewSubs
		}
	} else {
		return nil, ErrNoSubs
	}
	return matches[nmatches-1], nil
}
Ejemplo n.º 13
0
// parseCapacity matches a Regexp in a []byte, returning the resulting value in bytes.
// Assumes that the value matched by the Regexp is in KB.
func parseCapacity(b []byte, r *regexp.Regexp) (uint64, error) {
	matches := r.FindSubmatch(b)
	if len(matches) != 2 {
		return 0, fmt.Errorf("failed to match regexp in output: %q", string(b))
	}
	m, err := strconv.ParseUint(string(matches[1]), 10, 64)
	if err != nil {
		return 0, err
	}

	// Convert to bytes.
	return m * 1024, err
}
Ejemplo n.º 14
0
// ParseHeadID parse ID from head by IDRegexp
func parseHeadID(idRegexp *regexp.Regexp, head []byte) []byte {
	if isUsingDefaultIDRegexp {
		if i := bytes.IndexByte(head, ' '); i > 0 {
			return head[0:i]
		}
		return head
	}

	found := idRegexp.FindSubmatch(head)
	if found == nil { // not match
		return head
	}
	return found[1]
}
Ejemplo n.º 15
0
func parseDetail(b []byte, detailregex *regexp.Regexp) (*Detail, error) {
	detail, err := getok(detailregex.FindSubmatch(b), 1)
	if err != nil {
		panic("silk: failed to parse detail: " + err.Error())
	}
	sep := bytes.IndexAny(detail, ":=")
	if sep == -1 || sep > len(detail)-1 {
		return nil, errors.New("malformed detail")
	}
	key := clean(detail[0:sep])
	return &Detail{
		Key:   string(bytes.TrimSpace(key)),
		Value: ParseValue(detail[sep+1:]),
	}, nil
}
Ejemplo n.º 16
0
func getFormID(url string, re *regexp.Regexp) string {
	resp, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	idb := re.FindSubmatch(body)
	if len(idb) < 2 {
		log.Fatal("Could not find the form_build_id")
	}
	return string(idb[1])
}
Ejemplo n.º 17
0
func (c *Checker) checkTextCore(bw *Browser, r *regexp.Regexp, f func(m [][]byte) bool, s, msg, title, body string) error {
	matchs := r.FindSubmatch([]byte(bw.Find(s).Text()))
	if len(matchs) == 3 {
		if f(matchs) {
			flg := c.Cache.GetBool("CheckText:" + s + r.String())
			if !flg {
				if !c.Silent {
					fmt.Println(msg)
				}
				c.Cache.Set("CheckText:"+s+r.String(), true)
				return c.pushNotify(title, body)
			}
		} else {
			c.Cache.Del("CheckText:" + s + r.String())
		}
	}
	return nil
}
Ejemplo n.º 18
0
func findRedirects(filename string) (result []*redirect, err error) {
	var (
		f        *os.File
		re       *regexp.Regexp
		line     []byte
		isPrefix bool
	)
	if f, err = os.Open(filename); err != nil {
		return
	}
	defer f.Close()
	r := bufio.NewReader(f)
	if re, err = regexp.Compile("^::1 (.*) #redirects-to (.*)$"); err != nil {
		return
	}
	result = []*redirect{}
	for {
		if line, isPrefix, err = r.ReadLine(); err == io.EOF {
			break
		}
		if isPrefix {
			err = fmt.Errorf("line too long: %s...", line)
			break
		}
		if match := re.FindSubmatch(line); match != nil {
			var shortTo, longTo string
			allRedirects := strings.Split(string(match[2]), " ")
			shortTo = allRedirects[0]
			if len(allRedirects) > 1 {
				longTo = allRedirects[1]
			}
			result = append(result, &redirect{
				from:    string(match[1]),
				shortTo: shortTo,
				longTo:  longTo})
		}
	}
	if err == io.EOF {
		err = nil
	}
	return
}
Ejemplo n.º 19
0
func (self *inputTail) Run(runner InputRunner) error {
	defer func() {
		if err := recover(); err != nil {
			logs.Fatalln("recover panic at err:", err)
		}
	}()

	var seek int
	if self.offset > 0 {
		seek = os.SEEK_SET
	} else {
		seek = os.SEEK_END
	}

	t, err := tail.TailFile(self.path, tail.Config{
		Poll:      true,
		ReOpen:    true,
		Follow:    true,
		MustExist: false,
		Location:  &tail.SeekInfo{int64(self.offset), seek}})
	if err != nil {
		return err
	}

	f, err := os.OpenFile(self.pos_file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
	if err != nil {
		log.Fatalln("os.OpenFile", err)
	}
	defer f.Close()

	var re regexp.Regexp
	if string(self.format[0]) == string("/") || string(self.format[len(self.format)-1]) == string("/") {
		format := strings.Trim(self.format, "/")
		trueformat := regexp.MustCompile("\\(\\?<").ReplaceAllString(format, "(?P<")
		if trueformat != format {
			log.Printf("pos_file:%s, format:%s", self.path, trueformat)
		}
		re = *regexp.MustCompile(trueformat)
		self.format = "regexp"
	} else if self.format == "json" {

	}

	tick := time.NewTicker(time.Second * time.Duration(self.sync_interval))
	count := 0

	for {
		select {
		case <-tick.C:
			{
				if count > 0 {
					offset, err := t.Tell()
					if err != nil {
						log.Println("Tell return error: ", err)
						continue
					}

					str := strconv.Itoa(int(offset))

					_, err = f.WriteAt([]byte(str), 0)
					if err != nil {
						log.Println("f.WriteAt", err)
						return err
					}

					count = 0
				}
			}
		case line := <-t.Lines:
			{
				pack := <-runner.InChan()

				pack.MsgBytes = []byte(line.Text)
				pack.Msg.Tag = self.tag
				pack.Msg.Timestamp = line.Time.Unix()

				if self.format == "regexp" {
					text := re.FindSubmatch([]byte(line.Text))
					if text == nil {
						pack.Recycle()
						continue
					}

					for i, name := range re.SubexpNames() {
						if len(name) > 0 {
							pack.Msg.Data[name] = string(text[i])
						}
					}
				} else if self.format == "json" {
					dec := codec.NewDecoderBytes([]byte(line.Text), self.codec)
					err := dec.Decode(&pack.Msg.Data)
					if err != nil {
						log.Println("json.Unmarshal", err)
						pack.Recycle()
						continue
					} else {
						t, ok := pack.Msg.Data[self.time_key]
						if ok {
							if time, xx := t.(uint64); xx {
								pack.Msg.Timestamp = int64(time)
								delete(pack.Msg.Data, self.time_key)
							} else if time64, oo := t.(int64); oo {
								pack.Msg.Timestamp = time64
								delete(pack.Msg.Data, self.time_key)
							} else {
								log.Println("time is not int64, ", t, " typeof:", reflect.TypeOf(t))
								pack.Recycle()
								continue
							}
						}
					}
				}

				count++
				runner.RouterChan() <- pack
			}
		}
	}

	err = t.Wait()
	if err != nil {
		return err
	}

	return err
}
Ejemplo n.º 20
0
Archivo: lexer.go Proyecto: abesto/sexp
func (l *lexer) match(re *regexp.Regexp) bool {
	if l.matches = re.FindSubmatch(l.input[l.pos:]); l.matches != nil {
		return true
	}
	return false
}