<commit_before>package paths

import (
	"strings"

	"github.com/Kiricon/Rapid"
)

type path struct {
	path     string
	subPaths map[string]path
}

var paths map[string]path
var pathHandlers map[string]func(rapid.Connection)

// AddPath - Add route to the map of paths
func AddPath(pathString string, handler func(rapid.Connection)) {

	pathArr := formatPath(pathString)

	if paths == nil {
		paths = make(map[string]path)
		pathHandlers = make(map[string]func(rapid.Connection))
	}

	insertPath(paths, pathArr, 0)
	pathHandlers[pathString] = handler

}

func formatPath(pathString string) []string {
	pathArr := strings.Split(pathString, "/")
	//loop := true

	for i := 0; i < len(pathArr); i++ {
		if i+1 != len(pathArr) {
			pathArr[i] += "/"
		}
	}

	return pathArr
}

// Insert a path in to the global paths map
func insertPath(paths map[string]path, pathArr []string, index int) {

	singlePath := pathArr[index]
	singlePath = checkPathParams(singlePath)

	if _, ok := paths[singlePath]; ok && index+1 < len(pathArr) {
		insertPath(paths[singlePath].subPaths, pathArr, index+1)
	} else {
		emptySlice := make(map[string]path)
		paths[singlePath] = path{strings.Join(pathArr, ""), emptySlice}

		if index+1 < len(pathArr) {
			insertPath(paths[singlePath].subPaths, pathArr, index+1)
		}

	}

}

// Check if the current path directory is a url parameter
// If it is a param then replace it with  * wild card
func checkPathParams(singlePath string) string {
	if len(singlePath) > 1 {
		if singlePath[0] == ':' {
			if singlePath[len(singlePath)-1] == '/' {
				singlePath = "*/"
			} else {
				singlePath = "*"
			}
		}
	}

	return singlePath
}

func findCorrectPath(path string) {<commit_msg>Added find correct path function<commit_after>package paths

import (
	"strings"

	"github.com/Kiricon/Rapid"
)

type path struct {
	path     string
	subPaths map[string]path
}

var paths map[string]path
var pathHandlers map[string]func(rapid.Connection)

// AddPath - Add route to the map of paths
func AddPath(pathString string, handler func(rapid.Connection)) {

	pathArr := formatPath(pathString)

	if paths == nil {
		paths = make(map[string]path)
		pathHandlers = make(map[string]func(rapid.Connection))
	}

	insertPath(paths, pathArr, 0)
	pathHandlers[pathString] = handler

}

func formatPath(pathString string) []string {
	pathArr := strings.Split(pathString, "/")
	//loop := true

	for i := 0; i < len(pathArr); i++ {
		if i+1 != len(pathArr) {
			pathArr[i] += "/"
		}
	}

	return pathArr
}

// Insert a path in to the global paths map
func insertPath(paths map[string]path, pathArr []string, index int) {

	singlePath := pathArr[index]
	singlePath = checkPathParams(singlePath)

	if _, ok := paths[singlePath]; ok && index+1 < len(pathArr) {
		insertPath(paths[singlePath].subPaths, pathArr, index+1)
	} else {
		emptySlice := make(map[string]path)
		paths[singlePath] = path{strings.Join(pathArr, ""), emptySlice}

		if index+1 < len(pathArr) {
			insertPath(paths[singlePath].subPaths, pathArr, index+1)
		}

	}

}

// Check if the current path directory is a url parameter
// If it is a param then replace it with  * wild card
func checkPathParams(singlePath string) string {
	if len(singlePath) > 1 {
		if singlePath[0] == ':' {
			if singlePath[len(singlePath)-1] == '/' {
				singlePath = "*/"
			} else {
				singlePath = "*"
			}
		}
	}

	return singlePath
}

// Find the correct path to asscociate with a request url
func findCorrectPath(path string) string {
	pathArr := formatPath(path)
	currentPath := paths
	lastMatch := "/"

	for i := 0; i < len(pathArr); i++ {
		dir := pathArr[i]

		if _, ok := currentPath[dir]; ok {
			lastMatch = currentPath[dir].path
			currentPath = currentPath[dir].subPaths
		} else if _, ok := currentPath["*"]; ok {
			lastMatch = currentPath["*"].path
			currentPath = currentPath["*"].subPaths
		} else if _, ok := currentPath["*/"]; ok {
			lastMatch = currentPath["*/"].path
			currentPath = currentPath["*/"].subPaths
		} else {
			return "404 No Match"
		}
	}

	return lastMatch
{'fraction_non_alphanumeric': 0.139067922657412, 'fraction_numerical': 0.005949429846306396, 'mean_word_length': 3.9448529411764706, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 5, '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': '55646', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1373, 'n_words': 353}