Example #1
0
func RegexpFilter(filterReg *regexp.Regexp, data []byte) (outdata []byte) {
	if nil == filterReg {
		return data
	}

	matches := filterReg.FindAllSubmatch(data, -1)
	if nil == matches {
		log.Printf("[ERROR] failed to match filter regex, pattern %s did not match", filterReg.String())
		if *gDebug {
			log.Println("======= debug: target data =======")
			log.Println(string(data))
			log.Println("==============")
		}
		return nil
	}

	for _, match := range matches {
		for patInd, patName := range filterReg.SubexpNames() {
			switch patName {
			case PATTERN_FILTER:
				outdata = append(outdata, match[patInd]...)
			}
		}
	}

	if *gDebug {
		log.Println("======= debug: filter regex ========")
		log.Println(filterReg.String())
		log.Println("======= debug: filtered data =======")
		log.Println(string(outdata))
		log.Println("==============")
	}
	return
}
Example #2
0
func log(title string, regex *regexp.Regexp, val string) {
	if _, file, line, ok := runtime.Caller(2); ok {
		if data, err := ioutil.ReadFile(file); err == nil {
			// Truncate file name at last file name separator.
			if index := strings.LastIndex(file, "/"); index >= 0 {
				file = file[index+1:]
			} else if index = strings.LastIndex(file, "\\"); index >= 0 {
				file = file[index+1:]
			}
			lines := bytes.Split(data, []byte{'\n'})
			cond := regex.FindAllSubmatch(lines[line-1], 1)
			if len(cond) > 0 && len(cond[0]) > 1 {
				if val == "" {
					fmt.Fprintf(os.Stderr, "\t%s %s:%d: %s\n", title, file, line, cond[0][1])
				} else {
					fmt.Fprintf(os.Stderr, "\t%s %s:%d: %s: %s\n", title, file, line, cond[0][1], val)
				}
			}
		}
	}
}
Example #3
0
// Получает список подключаемых файлов по регулярному выражению.
func getRegIncl(path string, exp *regexp.Regexp) []string {

	f, err := os.OpenFile(path, os.O_RDONLY, 0)
	if err != nil {
		panic(err)
	}
	defer f.Close()

	b, err := ioutil.ReadAll(f)
	if err != nil {
		panic(err)
	}

	inclBytes := exp.FindAllSubmatch(b, -1)

	ret := make([]string, len(inclBytes))
	for i, _ := range inclBytes {
		ret[i] = string(inclBytes[i][1])
	}

	return ret
}