Helping with whitelisting characters

Hello Developers,

I’ve been doing some formatting for my plugin, and today I wanted to add a character whitelisting, what I am formatting is a time format, it is not done (yet). Time format should look like this hh:mm basically 05:02 for example, anyways back to the issue, I’ve got a problem…


Script:

local function timeFormat(text, setting)
	local chars = {"1","2","3","4","5","6","7","8","9","0",":"}
	for _, char in ipairs(chars) do
		if text.match(char, text)==nil then
			if #text == 1 then
				return "0"..text..":00"
			elseif #text == 2 then
				return text..":00"
			elseif #text == 3 then
				return text.."00"
			elseif #text == 4 then
				local splittedTime = text:split(":")
				return splittedTime[1]..":".."0"..splittedTime[2]
			else
				return plugin:GetSetting(setting)
			end
		else
			return plugin:GetSetting(setting)
		end
	end
end

This is the original code I made. Before adding these loops and if text.match(char, text)==nil then everything worked perfectly fine, when I started implementing the character whitelisting, things started acting up…


Problems:

When messing around with the code like adding not() or other stuff to try to make this work things happening like:

image

or (in this case) not even reacting to the given input, or it does format, but formats the other blacklisted characters too.


Thanks for helping!

Hello, I didn’t got any help for 12h, I am pushing this to get help, thanks.

I don’t see what’s the point with the char table and the if statement with it could you comment it out and see what happens

The char table and if statement is needed to blacklist the characters that aren’t in the list. But if needed, it does format properly, if the input is 5, it will format it to 05:00, etc., but sadly the same happens with letters, if the input is a it will format it to 0a:00, and what I am trying to do is, if the input contains characters that aren’t in the list, it converts to the previous and correctly formatted value.

Pushing this up again

char limit sob

local function timeFormat(text, setting)
	local chars = {"1","2","3","4","5","6","7","8","9","0",":"}
	for _, char in ipairs(chars) do
		if text.match(char, text)==nil then
			if char == "1" then
				return "0"..text..":00"
			elseif char == "2" then
				return text..":00"
			elseif char == "3" then
				return text.."00"
			elseif char == "4" then
				local splittedTime = text:split(":")
				return splittedTime[1]..":".."0"..splittedTime[2]
			else
--				return plugin:GetSetting(setting)
			end
		else
--			return plugin:GetSetting(setting)
		end
	end
end
print(timeFormat("2", 1))

Fixed just this loop. Took out GetSetting(setting) to test.
I appreciate you putting this in a format that can be debugged easy. But, I hope you didn’t pick logic that isn’t what you’re looking for …

The fact that I can test this very easily, as I am creating a plugin, also I got the
image
which is all I wanted, but all I am just trying to do is whitelisting the characters

I can test format with this
image
as I created it just for that

Now I’m going to ask it … them labels “1”,“2”,“3” … These are going to be different names right?
If not, this could have been done in a few lines.

If in the chars variable, no these are the whitelisted characters, also I don’t understand why did you put
image
char instead of #text, as I format by the character count in the provided text string. And the char is the whitelisted character, and the if text.match(char, text)==nil then searches for it.

I was testing (made that a none factor).

text is the source to check, if there are any char. My code might be not understandable and not logical. But that’s what I’m looking for.

This was just a test … I think you got it

I’m starting to be confused on what you are trying to do…

Ya, me too … If this was for a whitelist that’s pretty much what I posted.
If this was to change a 2 into a 02:00 then …

local function toMS(s)
	s = s * 60 return string.format("%02i:%02i", s/60%60, s%60)
end
print(toMS(2))