Created
July 19, 2015 21:50
-
-
Save badcc/88249ee5584c57ba37e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local Tests = { | |
'<string name="Name">TEST</string>', -- pass | |
'<>TEST</string>', -- load | |
'< >TEST</string>', -- load | |
'<a a>TEST</string>', -- fail = not found | |
'<string a=" name="Name">TEST</string>', -- fail = not found | |
'<string a= a=""="">TEST</string>', -- ??? load | |
'<string a= ">TEST</string>', -- ??? load | |
'<string a= a=""="=>TEST</string' -- fail " not found | |
} | |
local function Parse(Str) | |
local OpenFirstTagMatch = '^<%s*%w*%s*([^>]*)' | |
local PropertyMatch = '(%w+)(=?"?[^"]*"?)' | |
local CloseFirstTagMatch = '%s*>' | |
local ValueMatch = '>(.+)<' | |
local SecondTagMatch = '</(%w+)>' | |
local Properties = Str:match(OpenFirstTagMatch) | |
for PropertyName, PropertyValue in Properties:gmatch(PropertyMatch) do | |
if (PropertyValue:sub(1, 1) ~= '=') then | |
return 'fail (\'=\' not found)' | |
end | |
if (not PropertyValue:match('"[^"]+"')) then | |
return 'fail (\'"\' not found)' | |
end | |
end | |
local Value = Str:match(ValueMatch) | |
if (not Value) then | |
return 'fail' | |
end | |
local SecondTagName = Str:match(SecondTagMatch) | |
if (not SecondTagName) then | |
return 'fail' | |
end | |
if (Value == 'TEST' and #Properties > 0) then | |
return 'pass' | |
end | |
return 'load' | |
end | |
for _,Str in next,Tests do | |
local Result = Parse(Str) | |
print(Result) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment