-- TO BE REPLACED BY encode_entities in [[Module:string utilities]]. This function decodes on input by default to prevent double-encoding, which the new function does not, so implementations need to take this into account when being converted.

local debug_track_module = "Module:debug/track"
local string_utilities_module = "Module:string utilities"

local require = require

local function decode_entities(...)
	decode_entities = require(string_utilities_module).decode_entities
	return decode_entities(...)
end

local function encode_entities(...)
	encode_entities = require(string_utilities_module).encode_entities
	return encode_entities(...)
end

local function track(...)
	track = require(debug_track_module)
	return track(...)
end

return function(str, charset, raw)
	if not raw then
		local decoded = decode_entities(str)
		if decoded ~= str then
			track("string/encode entities/decoded first")
		end
		str = decoded
	end
	return encode_entities(str, charset, nil, true)
end