function matchWildCard()

in edge/nodejs/anti-hotlinking/anti-hotlinking/app.js [36:65]


function matchWildCard(target, pattern) {
	// Deny the access if the referer is null
	if (undefined == target) {
		return false;
	}

	const targetLen = target.length;
	const pLen = pattern.length;

	const dp = new Array(targetLen + 1);
	for (let i = 0; i < targetLen + 1; i++) {
		dp[i] = new Array(pLen + 1).fill(false);
	}

	dp[0][0] = true;
	for (let j = 1; j <= pLen; j++) {
		dp[0][j] = pattern[j - 1] == '*' && dp[0][j - 1];
	}

	for (let i = 1; i <= targetLen; i++) {
		for (let j = 1; j <= pLen; j++) {
			if (pattern[j - 1] == '?' || target[i - 1] == pattern[j - 1])
				dp[i][j] = dp[i - 1][j - 1];
			else if (pattern[j - 1] == '*' && (dp[i - 1][j] || dp[i][j - 1]))
				dp[i][j] = true;
		}
	}

	return dp[targetLen][pLen];
}