package parsers

import (
	"regexp"
	"strconv"

	"github.com/sirupsen/logrus"
	"github.rackspace.com/gorax/tools/util"
)

func regexpFindString(r string, s *string) *string {
	re, err := regexp.Compile(r)
	if err != nil {
		logrus.Errorf("unable to compile %s, Error: %s", r, err)
		return nil
	}

	t := re.FindStringSubmatch(*s)
	if len(t) > 0 {
		return &t[1]
	}

	return nil
}

func regexpAtoi(r regexp.Regexp, s string) *int {
	re := util.RegexpFindStringPtr(&r, s)
	if re != nil {
		i, err := strconv.Atoi(*re)
		if err != nil {
			return nil
		}
		return &i
	}
	return nil
}
