// Regexp checks the answer by regexp
517739
func Regexp(message string, reg *regexp.Regexp, defaultAnswer string) string {
	return (&Prompter{
		Message: message,
		Regexp:  reg,
		Default: defaultAnswer,
	}).Prompt()
}
1342519
func analyze(re *syntax.Regexp) (ret regexpInfo) {
	//println("analyze", re.String())
	//defer func() { println("->", ret.String()) }()
	var info regexpInfo
	switch re.Op {
	case syntax.OpNoMatch:
		return noMatch()

	case syntax.OpEmptyMatch,
		syntax.OpBeginLine, syntax.OpEndLine,
		syntax.OpBeginText, syntax.OpEndText,
		syntax.OpWordBoundary, syntax.OpNoWordBoundary:
		return emptyString()

	case syntax.OpLiteral:
		if re.Flags&syntax.FoldCase != 0 {
			switch len(re.Rune) {
			case 0:
				return emptyString()
			case 1:
				// Single-letter case-folded string:
				// rewrite into char class and analyze.
				re1 := &syntax.Regexp{
					Op: syntax.OpCharClass,
				}
				re1.Rune = re1.Rune0[:0]
				r0 := re.Rune[0]
				re1.Rune = append(re1.Rune, r0, r0)
				for r1 := unicode.SimpleFold(r0); r1 != r0; r1 = unicode.SimpleFold(r1) {
					re1.Rune = append(re1.Rune, r1, r1)
				}
				info = analyze(re1)
				return info
			}
			// Multi-letter case-folded string:
			// treat as concatenation of single-letter case-folded strings.
			re1 := &syntax.Regexp{
				Op:    syntax.OpLiteral,
				Flags: syntax.FoldCase,
			}
			info = emptyString()
			for i := range re.Rune {
				re1.Rune = re.Rune[i : i+1]
				info = concat(info, analyze(re1))
			}
			return info
		}
		info.exact = stringSet{string(re.Rune)}
		info.match = allQuery

	case syntax.OpAnyCharNotNL, syntax.OpAnyChar:
		return anyChar()

	case syntax.OpCapture:
		return analyze(re.Sub[0])

	case syntax.OpConcat:
		return fold(concat, re.Sub, emptyString())

	case syntax.OpAlternate:
		return fold(alternate, re.Sub, noMatch())

	case syntax.OpQuest:
		return alternate(analyze(re.Sub[0]), emptyString())

	case syntax.OpStar:
		// We don't know anything, so assume the worst.
		return anyMatch()

	case syntax.OpRepeat:
		if re.Min == 0 {
			// Like OpStar
			return anyMatch()
		}
		fallthrough
	case syntax.OpPlus:
		// x+
		// Since there has to be at least one x, the prefixes and suffixes
		// stay the same.  If x was exact, it isn't anymore.
		info = analyze(re.Sub[0])
		if info.exact.have() {
			info.prefix = info.exact
			info.suffix = info.exact.copy()
			info.exact = nil
		}

	case syntax.OpCharClass:
		info.match = allQuery

		// Special case.
		if len(re.Rune) == 0 {
			return noMatch()
		}

		// Special case.
		if len(re.Rune) == 1 {
			info.exact = stringSet{string(re.Rune[0])}
			break
		}

		n := 0
		for i := 0; i < len(re.Rune); i += 2 {
			n += int(re.Rune[i+1] - re.Rune[i])
		}
		// If the class is too large, it's okay to overestimate.
		if n > 100 {
			return anyChar()
		}

		info.exact = []string{}
		for i := 0; i < len(re.Rune); i += 2 {
			lo, hi := re.Rune[i], re.Rune[i+1]
			for rr := lo; rr <= hi; rr++ {
				info.exact.add(string(rr))
			}
		}
	}

	info.simplify(false)
	return info
}
755722
func RegexpToQuery(r *syntax.Regexp, minTextSize int) Q {
	q := regexpToQueryRecursive(r, minTextSize)
	q = Simplify(q)
	return q
}
430123
func literal(s string) *regexp.Regexp {
	re := match(regexp.QuoteMeta(s))

	if _, complete := re.LiteralPrefix(); !complete {
		panic("must be a literal")
	}

	return re
}
517540
func StringToMap(str string) (data map[string]string) {
	re := regexp.MustCompile(`\s*([\w-]+)\s*=\s*(['"]?)(.*)`)
	data = make(map[string]string)
	matches := re.FindAllStringSubmatch(str, -1)
	if len(matches) > 0 {
		first := matches[0]
		var reg2_txt string
		if first[2] == "'" || first[2] == `"` {
			reg2_txt = fmt.Sprintf(`([^%s]*)%s(\s+.*)?`, first[2], first[2])
		} else if first[2] == "" {
			reg2_txt = `(\S+)\s*(.*)`
		}
		re2 := regexp.MustCompile(reg2_txt)
		subResult := re2.FindAllStringSubmatch(first[3], -1)

		if len(subResult) > 0 && len(subResult[0]) > 1 {
			data[first[1]] = subResult[0][1]
			if len(subResult[0][2]) > 0 {
				_subResult := StringToMap(subResult[0][2])
				for k, v := range _subResult {
					data[k] = v
				}
			}
		}
	}
	return
}
416973
func MatchesExpr(cr expr.EvalContext, node expr.Node) (bool, bool) {
	return matchesExpr(cr, node, 0)
}
152980
func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) bool {
	return Regexp(a.t, rx, str, msgAndArgs...)
}
836207
func RegexMatch(regexStr string) gopter.Gen {
	regexSyntax, err1 := syntax.Parse(regexStr, syntax.Perl)
	regex, err2 := regexp.Compile(regexStr)
	if err1 != nil || err2 != nil {
		return Fail(reflect.TypeOf(""))
	}
	return regexMatchGen(regexSyntax.Simplify()).SuchThat(func(v string) bool {
		return regex.MatchString(v)
	}).WithShrinker(StringShrinker)
}
1018377
func (v *VerbalExpression) Regex() *regexp.Regexp {

	if !v.compiled {
		v.regexp = regexp.MustCompile(
			strings.Join([]string{
				strings.Join(v.parts, ""),
				`(?` + v.getFlags() + `)`,
				v.prefixes,
				v.expression,
				v.suffixes}, ""))
		v.compiled = true
	}
	return v.regexp
}
166031
func validateRegex(v string) error {
	_, err := regexp.Compile(v)
	return err
}
857193
func getRegEx(pattern string) *regexp.Regexp {
	for _, trs := range []struct {
		old, new string
	}{
		{".", `\.`}, // Escape all '.' in the pattern
		{"(", `\(`}, // Escape all '(' in the pattern
		{")", `\)`}, // Escape all ')' in the pattern
		{"*", ".*"}, // Replace a '*' with '.*' that matches zero or more chars
		{"{", "("},  // Change a '{' to '('
		{",", "|"},  // Change a ',' to '|'
		{"}", ")"},  // Change a '}' to ')'
		{"?", "."},  // Change a '?' to '.'
	} {
		pattern = strings.Replace(pattern, trs.old, trs.new, -1)
	}

	return regexp.MustCompile(pattern)
}
259268
func ValidateRegexp(v interface{}, k string) (ws []string, errors []error) {
	if _, err := regexp.Compile(v.(string)); err != nil {
		errors = append(errors, fmt.Errorf("%q: %s", k, err))
	}
	return
}
355328
func Grep(root string, pattern *regexp.Regexp) (paths []string) {
	panic(fmt.Errorf("Not Implemented"))
}
1000264
func NewRegexpQuery(name string, regexp string) RegexpQuery {
	return RegexpQuery{name: name, regexp: regexp}
}
331568
func (re *RegExp) String() string {
	var b bytes.Buffer
	return re.toStringBuilder(&b).String()
}
327982
func Expr(expression string, args ...interface{}) *expr {
	return &expr{expr: expression, args: args}
}
499360
func RegexError(msg string, fmt string, examples ...string) string {
	if len(examples) == 0 {
		return msg + " (regex used for validation is '" + fmt + "')"
	}
	msg += " (e.g. "
	for i := range examples {
		if i > 0 {
			msg += " or "
		}
		msg += "'" + examples[i] + "', "
	}
	msg += "regex used for validation is '" + fmt + "')"
	return msg
}