C
Christopher Causer
I have a long document which I am trying to parse. I am coming unstuck
trying to speed it up and get all the substitutions out the way quickly.
I have a hash array of original and new things to substitute, and it
works. However, I'm sure there is a more efficient way of doing things.
RegExp has a union method, but that doesn't seem to handle string
capture within matches.
One final thing is that I would like this sub to throw something if no
substitutions are performed on the str.
--------------------------------------------------
def makeSubs(str)
subCurlyArray={/section%LCURL%(\d+)(.*?)%RCURL%\1/ => "\n"+'---+\2',
/subsection%LCURL%(\d+)(.*?)%RCURL%\1/ => "\n"+'---++\2',
/subsubsection%LCURL%(\d+)(.*?)%RCURL%\1/ => "\n"+'---+++\2',
/emph%LCURL%(\d+)(.*?)%RCURL%\1/ => '<em>\2</em>',
/includegraphics\[(.*)\]%LCURL%(\d+)(.*?)%RCURL%\1/ =>'IMAGE <img
src="\3" \2> ENDIMAGE',
/texttt%LCURL%(\d+)(.*?)%RCURL%\1/ => '<verbatim>\2 </verbatim>',
/code%LCURL%(\d+)(.*?)%RCURL%\1/ => '<verbatim>\2</verbatim>',
/textbf%LCURL%(\d+)(.*?)%RCURL%\1/ => '*\2*',
/textit%LCURL%(\d+)(.*?)%RCURL%\1/ => '_\2_',
}
subArray.each do |orig, new|
str.gsub!(orig, new)
else
return str
end
trying to speed it up and get all the substitutions out the way quickly.
I have a hash array of original and new things to substitute, and it
works. However, I'm sure there is a more efficient way of doing things.
RegExp has a union method, but that doesn't seem to handle string
capture within matches.
One final thing is that I would like this sub to throw something if no
substitutions are performed on the str.
--------------------------------------------------
def makeSubs(str)
subCurlyArray={/section%LCURL%(\d+)(.*?)%RCURL%\1/ => "\n"+'---+\2',
/subsection%LCURL%(\d+)(.*?)%RCURL%\1/ => "\n"+'---++\2',
/subsubsection%LCURL%(\d+)(.*?)%RCURL%\1/ => "\n"+'---+++\2',
/emph%LCURL%(\d+)(.*?)%RCURL%\1/ => '<em>\2</em>',
/includegraphics\[(.*)\]%LCURL%(\d+)(.*?)%RCURL%\1/ =>'IMAGE <img
src="\3" \2> ENDIMAGE',
/texttt%LCURL%(\d+)(.*?)%RCURL%\1/ => '<verbatim>\2 </verbatim>',
/code%LCURL%(\d+)(.*?)%RCURL%\1/ => '<verbatim>\2</verbatim>',
/textbf%LCURL%(\d+)(.*?)%RCURL%\1/ => '*\2*',
/textit%LCURL%(\d+)(.*?)%RCURL%\1/ => '_\2_',
}
subArray.each do |orig, new|
str.gsub!(orig, new)
else
return str
end