Varsuuk
January 6th, 2021, 05:18
I copied the sanitize method from the 5E ruleset and added it to my StringUtils package.
While testing cases, I tried passing in "Magic-User", expecting it to be converted to my expected classID of "magic_user". Instead the returned string was "magic-user".
-- Used to convert non-xml names to valid xml names.
-- Replaces invalid characters with "_". In addition, converts string to lower-case.
-- @args s String to be scanned for characters to replace.
-- @returns A string with the indicated characters replaced by "_"
function sanitize(s)
local sSanitized = StringManager.trim(s:gsub("%s%(.*%)$", ""));
-- @TODO Review (posted on forums): I added the "-" at start of the character set since "-" was not being replaced by "_"
sSanitized = sSanitized:gsub("[-.,-():'’/?+–]", "_"):gsub("%s", ""):lower();
return sSanitized
end
NOTE that I added the "-" before the "." in order to get it to work, it wasn't there before. I am working on a high-res monitor with smallish print and lo-res eyes... it seems to me there are 2 "hyphens" in that string (ignoring the dupe I added), one before the "(" and one at the end before the "]". It looks like the latter is a teeny bit wider than the former.
It is because one of those is being misinterpreted when inside the "[]" construct as a special character/escape?
It is "working" now, BUT I am sure what I did is not the solution AND it may even be wrong for every OTHER case for all I know, I broke something doing what I did.
More REGEX-y peeps please chime in (remember, in earlier post that I am Regex-challenged.)
While testing cases, I tried passing in "Magic-User", expecting it to be converted to my expected classID of "magic_user". Instead the returned string was "magic-user".
-- Used to convert non-xml names to valid xml names.
-- Replaces invalid characters with "_". In addition, converts string to lower-case.
-- @args s String to be scanned for characters to replace.
-- @returns A string with the indicated characters replaced by "_"
function sanitize(s)
local sSanitized = StringManager.trim(s:gsub("%s%(.*%)$", ""));
-- @TODO Review (posted on forums): I added the "-" at start of the character set since "-" was not being replaced by "_"
sSanitized = sSanitized:gsub("[-.,-():'’/?+–]", "_"):gsub("%s", ""):lower();
return sSanitized
end
NOTE that I added the "-" before the "." in order to get it to work, it wasn't there before. I am working on a high-res monitor with smallish print and lo-res eyes... it seems to me there are 2 "hyphens" in that string (ignoring the dupe I added), one before the "(" and one at the end before the "]". It looks like the latter is a teeny bit wider than the former.
It is because one of those is being misinterpreted when inside the "[]" construct as a special character/escape?
It is "working" now, BUT I am sure what I did is not the solution AND it may even be wrong for every OTHER case for all I know, I broke something doing what I did.
More REGEX-y peeps please chime in (remember, in earlier post that I am Regex-challenged.)