<commit_before>package util

import (
	"strconv"
	"strings"
	"time"
)

func validAlways(string) bool {
	return true
}

func validEmptyOr(otherFn func(string) bool) func(string) bool {
	return func(s string) bool {
		return len(s) == 0 || otherFn(s)
	}
}

func validNonEmpty(s string) bool {
	return len(s) > 1
}

func validName(s string) bool {
	if strings.ContainsAny(s, " \t\r\n") {
		return false
	} else if len(s) >= 3 {
		return true
	}
	return false
}

func validPostcode(s string) bool {
	return len(s) > 1
}

func validNumber(s string) bool {
	for _, c := range s {
		if c < '0' && c > '9' {
			return false
		}
	}
	return true
}

func validCC(cc string) bool {
	if !(len(cc) == 13 || len(cc) == 16 || len(cc) == 15) {

		return false
	}
	if !validNumber(cc) {
		return false
	}
	calculatedCheckDigit, err := Luhn(cc[:len(cc)-1])
	if err != nil {
		return false
	}
	givenCheckDigit, err := strconv.Atoi(cc[len(cc)-1:])
	if err != nil {
		return false
	}
	return calculatedCheckDigit == givenCheckDigit
}

func validCVV(cvv string) bool {
	return validNumber(cvv) && (len(cvv) == 3 || len(cvv) == 4)
}

func validISOCountry(code string) bool {
	return len(code) > 1 && len(code) < 4
}

func validExpiry(exp string) bool {
	if len(exp) != 4 {
		return false
	}
	if !validNumber(exp) {
		return false
	}
	mo, _ := strconv.ParseInt(exp[0:1], 10, 8)
	if mo < 1 || mo > 12 {
		return false
	}
	yr, _ := strconv.ParseInt(exp[2:3], 10, 8)

	// this doesn't handle century boundaries well.
	// but if this code is still in use at the end of the 2000s let me know and I'll
	// cook and eat various head-toppers.
	thisYear := time.Now().Year() % 100
	if int(yr) < thisYear || int(yr) > (thisYear+10) {
		return false
	}
	return true

}

// Luhn calculates the Luhn checksum for the given number
func Luhn(number string) (int, error) {
	sum := 0<commit_msg>fix country-code validation<commit_after>package util

import (
	"strconv"
	"strings"
	"time"
)

func validAlways(string) bool {
	return true
}

func validEmptyOr(otherFn func(string) bool) func(string) bool {
	return func(s string) bool {
		return len(s) == 0 || otherFn(s)
	}
}

func validNonEmpty(s string) bool {
	return len(s) > 1
}

func validName(s string) bool {
	if strings.ContainsAny(s, " \t\r\n") {
		return false
	} else if len(s) >= 3 {
		return true
	}
	return false
}

func validPostcode(s string) bool {
	return len(s) > 1
}

func validNumber(s string) bool {
	for _, c := range s {
		if c < '0' && c > '9' {
			return false
		}
	}
	return true
}

func validCC(cc string) bool {
	if !(len(cc) == 13 || len(cc) == 16 || len(cc) == 15) {

		return false
	}
	if !validNumber(cc) {
		return false
	}
	calculatedCheckDigit, err := Luhn(cc[:len(cc)-1])
	if err != nil {
		return false
	}
	givenCheckDigit, err := strconv.Atoi(cc[len(cc)-1:])
	if err != nil {
		return false
	}
	return calculatedCheckDigit == givenCheckDigit
}

func validCVV(cvv string) bool {
	return validNumber(cvv) && (len(cvv) == 3 || len(cvv) == 4)
}

func validISOCountry(code string) bool {
	return len(code) == 2
}

func validExpiry(exp string) bool {
	if len(exp) != 4 {
		return false
	}
	if !validNumber(exp) {
		return false
	}
	mo, _ := strconv.ParseInt(exp[0:1], 10, 8)
	if mo < 1 || mo > 12 {
		return false
	}
	yr, _ := strconv.ParseInt(exp[2:3], 10, 8)

	// this doesn't handle century boundaries well.
	// but if this code is still in use at the end of the 2000s let me know and I'll
	// cook and eat various head-toppers.
	thisYear := time.Now().Year() % 100
	if int(yr) < thisYear || int(yr) > (thisYear+10) {
		return false
	}
	return true

}

// Luhn calculates the Luhn checksum for the given number
func Luhn(number string) (int, error) {
	sum := 0
{'fraction_non_alphanumeric': 0.14497840172786178, 'fraction_numerical': 0.022408207343412527, 'mean_word_length': 3.4318181818181817, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 16, 'https://': 0, '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': '1542576', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1443, 'n_words': 427}