<commit_before>package validator

import (
	"fmt"
	"regexp"
)

var (
	rDateTime = regexp.MustCompile("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$")
	rEmail    = regexp.MustCompile("^.+@.+\\..+$")
	rURI      = regexp.MustCompile("^[0-9a-zA-Z]+:\\/\\/.+$")
)

type FormatValidator struct {
	definition FormatValidatorDefinition
}

type FormatValidatorDefinition struct {
	Format string `json:"format"`
}

type FormatValidationError struct {
	Definition FormatValidatorDefinition `json:"definition"`
	Input      string                    `json:"input"`
}

func (f FormatValidationError) Error() string {
	return fmt.Sprintf("input value '%s' does not match the pattern for '%s'\n",
		f.Input, f.Definition.Format)
}

func NewFormatValidator(definition FormatValidatorDefinition) (FormatValidator, error) {
	switch definition.Format {
	case "date-time", "email", "hostname", "uri":
		return FormatValidator{definition}, nil
	}
	return FormatValidator{}, InvalidFormatError{"the format is not found"}
}

func (f FormatValidator) Validate(input string) error {
	switch f.definition.Format {
	case "date-time":
		ok := rDateTime.MatchString(input)
		if ok == true {
			return nil
		}
		break
	case "email":
		ok := rEmail.MatchString(input)
		if ok == true {
			return nil
		}
		break
	case "hostname":
		ok := isHostName(input)
		if ok == true {
			return nil
		}
		break
	case "uri":
		ok := rURI.MatchString(input)
		if ok == true {
			return nil
		}
		break
	}
	return &FormatValidationError{
		f.definition,
		input,
	}
}

// isHostName stolen from https://golang.org/src/net/dnsclient.go
func isHostName(s string) bool {
	if len(s) == 0 {
		return false
	}
	if len(s) > 255 {
		return false
	}

	last := byte(',')
	ok := false
	partlen := 0<commit_msg>fix comment<commit_after>package validator

import (
	"fmt"
	"regexp"
)

var (
	rDateTime = regexp.MustCompile("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$")
	rEmail    = regexp.MustCompile("^.+@.+\\..+$")
	rURI      = regexp.MustCompile("^[0-9a-zA-Z]+:\\/\\/.+$")
)

type FormatValidator struct {
	definition FormatValidatorDefinition
}

type FormatValidatorDefinition struct {
	Format string `json:"format"`
}

type FormatValidationError struct {
	Definition FormatValidatorDefinition `json:"definition"`
	Input      string                    `json:"input"`
}

func (f FormatValidationError) Error() string {
	return fmt.Sprintf("input value '%s' does not match the pattern for '%s'\n",
		f.Input, f.Definition.Format)
}

func NewFormatValidator(definition FormatValidatorDefinition) (FormatValidator, error) {
	switch definition.Format {
	case "date-time", "email", "hostname", "uri":
		return FormatValidator{definition}, nil
	}
	return FormatValidator{}, InvalidFormatError{"the format is not found"}
}

func (f FormatValidator) Validate(input string) error {
	switch f.definition.Format {
	case "date-time":
		ok := rDateTime.MatchString(input)
		if ok == true {
			return nil
		}
		break
	case "email":
		ok := rEmail.MatchString(input)
		if ok == true {
			return nil
		}
		break
	case "hostname":
		ok := isHostName(input)
		if ok == true {
			return nil
		}
		break
	case "uri":
		ok := rURI.MatchString(input)
		if ok == true {
			return nil
		}
		break
	}
	return &FormatValidationError{
		f.definition,
		input,
	}
}

// isHostName is cited from https://golang.org/src/net/dnsclient.go
func isHostName(s string) bool {
	if len(s) == 0 {
		return false
	}
	if len(s) > 255 {
		return false
	}

	last := byte(',')
	ok := false
	partlen := 0
{'fraction_non_alphanumeric': 0.1506849315068493, 'fraction_numerical': 0.007420091324200913, 'mean_word_length': 3.581699346405229, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1934849', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1200, 'n_words': 297}