<commit_before>u8R""__RAW_STRING__(

local U = require "togo.utility"
local O = require "Quanta.Object"
local M = U.module(...)

M.debug = false
M.Any = {}

local function filter_selector(x)
	if x == M.Any or U.is_type(x, "boolean") then
		return x
	end
	return U.type_class(x)
end

local function rule_value_to_table(v)
	return U.is_type(v, "table") and v or {v}
end

M.filters = {}

M.filters.func = {}
M.filters.func.init = function(p, r)
	if r.func then
		U.assertl(
			3, U.is_type(r.func, "function"),
			"pattern rule 'func' must be a function"
		)
		return r.func
	end
end
M.filters.func.yes = function(_, _, _, _)
	return true
end

M.filters.name = {}
M.filters.name.init = function(p, r)
	U.assertl(
		3, U.is_type_any(r.name, {"table", "function", "string", "boolean"}, true),
		"pattern rule 'name' has invalid value"
	)
	if r.name == nil then
		return M.filters.name[false]
	elseif r.name == M.Any then
		return nil
	elseif U.is_type(r.name, "table") then
		p.names = #r.name == 0 and r.name or U.table_inverse(r.name, true)
	elseif U.is_type(r.name, "function") then
		return r.name
	elseif U.is_type(r.name, "string") then
		p.name = r.name
	end
	local f = M.filters.name[filter_selector(r.name)]
	U.assertl(3, f ~= nil, "no matching filter for pattern rule 'name' of '%s'", tostring(r.name))
	return f
end

M.filters.name[false] 		= function(_, _, obj, p) return not O.is_named(obj) end
M.filters.name[true] 		= function(_, _, obj, p) return O.is_named(obj) end
M.filters.name["table"] 	= function(_, _, obj, p) return p.names[O.name(obj)] == true end
M.filters.name["string"] 	= function(_, _, obj, p) return p.name == O.name(obj) end

M.filters.value = {}
M.filters.value.init = function(p, r)
	U.assertl(3, r.value == nil or r.vtype ~= nil, "pattern rule 'vtype' must be present when 'value' is")
	if U.is_type(r.vtype, "number") then
		p.value_type_mask = r.vtype
	elseif U.is_type(r.vtype, "function") then
		return r.vtype
	elseif r.vtype == M.Any then
		return nil
	elseif U.is_type(r.vtype, "table") then
		local mask = 0
		for _, vtype in pairs(r.vtype) do
			mask = mask + vtype
		end
		p.value_type_mask = mask
	else
		U.assertl(3, r.vtype == nil, "pattern rule 'vtype' has invalid value")
		return M.filters.value[false]
	end
	if r.value == nil then
		return M.filters.value.typed
	elseif U.is_type(r.value, "function") then
		p.value_func = r.value
		return M.filters.value.typed
	else
		p.values = rule_value_to_table(r.value)
		for _, v in pairs(p.values) do
			local c = M.filters.value.compare[type(v)]
			U.assertl(3, c, "pattern rule 'value' contains invalid type: %s (of type %s)", tostring(v), type(v))<commit_msg>lib/core/match/Match.lua: Pattern: consider any non-nil value as truthy in name filter.<commit_after>u8R""__RAW_STRING__(

local U = require "togo.utility"
local O = require "Quanta.Object"
local M = U.module(...)

M.debug = false
M.Any = {}

local function filter_selector(x)
	if x == M.Any or U.is_type(x, "boolean") then
		return x
	end
	return U.type_class(x)
end

local function rule_value_to_table(v)
	return U.is_type(v, "table") and v or {v}
end

M.filters = {}

M.filters.func = {}
M.filters.func.init = function(p, r)
	if r.func then
		U.assertl(
			3, U.is_type(r.func, "function"),
			"pattern rule 'func' must be a function"
		)
		return r.func
	end
end
M.filters.func.yes = function(_, _, _, _)
	return true
end

M.filters.name = {}
M.filters.name.init = function(p, r)
	U.assertl(
		3, U.is_type_any(r.name, {"table", "function", "string", "boolean"}, true),
		"pattern rule 'name' has invalid value"
	)
	if r.name == nil then
		return M.filters.name[false]
	elseif r.name == M.Any then
		return nil
	elseif U.is_type(r.name, "table") then
		p.names = #r.name == 0 and r.name or U.table_inverse(r.name, true)
	elseif U.is_type(r.name, "function") then
		return r.name
	elseif U.is_type(r.name, "string") then
		p.name = r.name
	end
	local f = M.filters.name[filter_selector(r.name)]
	U.assertl(3, f ~= nil, "no matching filter for pattern rule 'name' of '%s'", tostring(r.name))
	return f
end

M.filters.name[false] 		= function(_, _, obj, p) return not O.is_named(obj) end
M.filters.name[true] 		= function(_, _, obj, p) return O.is_named(obj) end
M.filters.name["table"] 	= function(_, _, obj, p) return p.names[O.name(obj)] ~= nil end
M.filters.name["string"] 	= function(_, _, obj, p) return p.name == O.name(obj) end

M.filters.value = {}
M.filters.value.init = function(p, r)
	U.assertl(3, r.value == nil or r.vtype ~= nil, "pattern rule 'vtype' must be present when 'value' is")
	if U.is_type(r.vtype, "number") then
		p.value_type_mask = r.vtype
	elseif U.is_type(r.vtype, "function") then
		return r.vtype
	elseif r.vtype == M.Any then
		return nil
	elseif U.is_type(r.vtype, "table") then
		local mask = 0
		for _, vtype in pairs(r.vtype) do
			mask = mask + vtype
		end
		p.value_type_mask = mask
	else
		U.assertl(3, r.vtype == nil, "pattern rule 'vtype' has invalid value")
		return M.filters.value[false]
	end
	if r.value == nil then
		return M.filters.value.typed
	elseif U.is_type(r.value, "function") then
		p.value_func = r.value
		return M.filters.value.typed
	else
		p.values = rule_value_to_table(r.value)
		for _, v in pairs(p.values) do
			local c = M.filters.value.compare[type(v)]
			U.assertl(3, c, "pattern rule 'value' contains invalid type: %s (of type %s)", tostring(v), type(v))
{'fraction_non_alphanumeric': 0.15441860465116278, 'fraction_numerical': 0.0033488372093023254, 'mean_word_length': 3.728232189973615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1445600', 'n_tokens_mistral': 2196, 'n_tokens_neox': 2137, 'n_words': 573}