lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On Mar 21, 2005, at 13:22, Adam D. Moss wrote:

Actually I just noticed that you have a similar problem with
&#XXX; entities which can't be solved with merely re-ordering
the substitutions.  But you can fold every &*; case into the
aFunction and be happy (likely somewhat faster too).

Well... not sure about happiness... but is the current concoction:

-- class method to decode a given string
thisClass.decode = function( aString )
        if ( aString ~= nil ) then
                local aFunction = function( aValue )
                                if ( aValue == "lt" ) then
                                        return "<"
                                elseif ( aValue == "gt" ) then
                                        return ">"
                                elseif ( aValue == "apos" ) then
                                        return "'"
                                elseif ( aValue == "quot" ) then
                                        return "\""
elseif ( string.sub( aValue, 1, 1 ) == "#" ) then local aNumber = tonumber( string.sub( aValue, 2 ) )

if ( ( aNumber ~= nil ) and ( aNumber > 0 ) and ( aNumber < 255 ) ) then return string.char( aNumber )
                                        end
                                end

                                return string.format( "&%s;", aValue )
                      end

                aString = string.gsub( aString, "&(%w+);", aFunction )
                aString = string.gsub( aString, "&amp;", "&" )
        end

        return aString
end

Alternatively, somebody, somewhere, somehow must have written this a dozen time already. Is there not a little code sample somewhere to show how to decode an XML string in Lua? Sigh...

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/