<commit_before>package common

import (
	"fmt"
	"strconv"
)

func makeIntDesc(valName string, minValue, maxValue int64) string {
	return fmt.Sprintf("Parameter %s must be followed by an integer value in the range of [%d, %d]",
		valName, minValue, maxValue)
}

func ParseInt64Param(params []string, minValue, maxValue int64) ([]string, int64, *ErrorResponse) {
	valName := params[0]
	if len(params) < 2 {
		return nil, 0, InvalidRequest(makeIntDesc(valName, minValue, maxValue))
	}
	val, err := strconv.ParseInt(params[1], 10, 0)

	if err == nil && val >= minValue && val <= maxValue {
		return params[2:], val, nil
	}

	return nil, 0, InvalidRequest(makeIntDesc(valName, minValue, maxValue))
}

func makeStrDesc(valName string, minLen, maxLen int64) string {
	return fmt.Sprintf("Parameter %s must be followed by a string value with the min length %d and max length %d",
		minLen, maxLen)
}

func ParseStringParam(params []string, minLen, maxLen int64) ([]string, string, *ErrorResponse) {
	valName := params[0]
	if len(params) < 2 {
		return nil, "", InvalidRequest(makeStrDesc(valName, minLen, maxLen))
	}
	paramLen := int64(len(params[1]))
	if paramLen >= minLen && paramLen <= maxLen {
		return params[2:], params[1], nil
	}
	return nil, "", InvalidRequest(makeStrDesc(valName, minLen, maxLen))
}

// ParseUserItemId parses user provided item id that can not start with '_'.
func ParseUserItemId(params []string, minLen, maxLen int64) ([]string, string, *ErrorResponse) {
	valName := params[0]
	if len(params) >= 2 {
		itemLength := int64(len(params[1]))
		if itemLength >= minLen && itemLength <= maxLen {
			if ValidateUserItemId(params[1]) {
				return params[2:], params[1], nil
			} else {
				return nil, "", InvalidRequest("Only ^[a-zA-Z0-9][_a-zA-Z0-9]* symbols are allowed for id")
			}
		}
	}
	return nil, "", InvalidRequest(makeStrDesc(valName, minLen, maxLen))
}

// ParseItemId parses item id that can use all characters.
func ParseItemId(params []string, minLen, maxLen int64) ([]string, string, *ErrorResponse) {
	valName := params[0]
	if len(params) >= 2 {
		itemLength := int64(len(params[1]))
		if itemLength >= minLen && itemLength <= maxLen {
			if ValidateItemId(params[1]) {
				return params[2:], params[1], nil
			} else {
				return nil, "", InvalidRequest("Only [_a-zA-Z0-9]* symbols are allowed for id")
			}
		}
	}
	return nil, "", InvalidRequest(makeStrDesc(valName, minLen, maxLen))
}<commit_msg>Using error constants instead of creating new error instance each time.<commit_after>package common

import (
	"fmt"
	"strconv"
)

func makeIntDesc(valName string, minValue, maxValue int64) string {
	return fmt.Sprintf("Parameter %s must be followed by an integer value in the range of [%d, %d]",
		valName, minValue, maxValue)
}

func ParseInt64Param(params []string, minValue, maxValue int64) ([]string, int64, *ErrorResponse) {
	valName := params[0]
	if len(params) < 2 {
		return nil, 0, InvalidRequest(makeIntDesc(valName, minValue, maxValue))
	}
	val, err := strconv.ParseInt(params[1], 10, 0)

	if err == nil && val >= minValue && val <= maxValue {
		return params[2:], val, nil
	}

	return nil, 0, InvalidRequest(makeIntDesc(valName, minValue, maxValue))
}

func makeStrDesc(valName string, minLen, maxLen int64) string {
	return fmt.Sprintf("Parameter %s must be followed by a string value with the min length %d and max length %d",
		minLen, maxLen)
}

func ParseStringParam(params []string, minLen, maxLen int64) ([]string, string, *ErrorResponse) {
	valName := params[0]
	if len(params) < 2 {
		return nil, "", InvalidRequest(makeStrDesc(valName, minLen, maxLen))
	}
	paramLen := int64(len(params[1]))
	if paramLen >= minLen && paramLen <= maxLen {
		return params[2:], params[1], nil
	}
	return nil, "", InvalidRequest(makeStrDesc(valName, minLen, maxLen))
}

// ParseUserItemId parses user provided item id that can not start with '_'.
func ParseUserItemId(params []string, minLen, maxLen int64) ([]string, string, *ErrorResponse) {
	valName := params[0]
	if len(params) >= 2 {
		itemLength := int64(len(params[1]))
		if itemLength >= minLen && itemLength <= maxLen {
			if ValidateUserItemId(params[1]) {
				return params[2:], params[1], nil
			} else {
				return nil, "", ERR_MSG_USER_ID_IS_WRONG
			}
		}
	}
	return nil, "", InvalidRequest(makeStrDesc(valName, minLen, maxLen))
}

// ParseItemId parses item id that can use all characters.
func ParseItemId(params []string, minLen, maxLen int64) ([]string, string, *ErrorResponse) {
	valName := params[0]
	if len(params) >= 2 {
		itemLength := int64(len(params[1]))
		if itemLength >= minLen && itemLength <= maxLen {
			if ValidateItemId(params[1]) {
				return params[2:], params[1], nil
			} else {
				return nil, "", ERR_MSG_ID_IS_WRONG
			}
		}
	}
	return nil, "", InvalidRequest(makeStrDesc(valName, minLen, maxLen))
}
{'fraction_non_alphanumeric': 0.141139896373057, 'fraction_numerical': 0.021139896373056993, 'mean_word_length': 4.765830346475508, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '7449198', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1636, 'n_words': 478}