Yet Another Way to Duplicate Quotes in String
This small function is of great need sometimes. My solution is straightforward:
dupquote(str) ; duplicate quotes in str quit $replace(str,$char(34),$char(34,34))
I'm just curious whether other solutions exist. Is there some "standard" and/or quicker approach which I've just overlooked?
Comments
See ISCQT.INC and %occUtility.inc:
<FONT COLOR="#ff0000">QT</FONT><FONT COLOR="#000000">(x) </FONT><FONT COLOR="#0000ff">Q $S</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">x</FONT><FONT COLOR="#000000">'[</FONT><FONT COLOR="#0000ff">$C</FONT><FONT COLOR="#000000">(34):</FONT><FONT COLOR="#800000">x</FONT><FONT COLOR="#000000">,1:</FONT><FONT COLOR="#0000ff">$P</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">x</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#0000ff">$C</FONT><FONT COLOR="#000000">(34))</FONT><FONT COLOR="#0000ff">$C</FONT><FONT COLOR="#000000">(34,34)</FONT><FONT COLOR="#0000ff">$$</FONT><FONT COLOR="#ff0000">QT</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$P</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">x</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#0000ff">$C</FONT><FONT COLOR="#000000">(34),2,</FONT><FONT COLOR="#0000ff">$L</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">x</FONT><FONT COLOR="#000000">)+1)))</FONT> or <FONT COLOR="#0000ff">$e</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$$$quote</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">s</FONT><FONT COLOR="#000000">),2,*-1)</FONT>
Thank you for taking part, Vitaliy!
BTW, your second solution should be amended:
dupocc(str) ; after the macro substitution quit $s(str'[$c(34):str,1:$e($zutil(144,1,str),2,*-1))
otherwise it would destroy numbers:
USER> set b=1 set c=$e($zutil(144,1,b),2,*-1) zwrite b,c b=1 c=""
Stuart,
Your solution is really beatiful. As to speed, it's just a bit slower than the solution #1 (based on $replace) and ~ twice quicker than my solution #2 (pure Mumps). That's a real pearl to add to my snippets collection, thank you!
This is really nice!
And short, so very is suitable for macro.
Yet another variant of quotes duplicator (pure Mumps):
dupP(str) quit:str'[$c(34) str new strdup,j set strdup="" for j=1:1:$length(str,$char(34))-1 set strdup=strdup_$piece(str,$char(34),j)_$char(34,34) quit strdup_$piece(str,$char(34),j+1)
Quick testing showed that the quickest variant is my initial $$dupquote(), $$dupocc() is very close (5-15% slower), $$dupP() took 3d place (2 times slower), $$QT() is the slowest (not surprise as it uses recursion).
It was interesting to find that the good quality of $replace() implementation allowed it to beat $zutil(144,) "system" function. While I'll keep using it, analyzing Vitaliy's code I've found the error in my own $$dupP().
I use
QUOTE(x) QUIT $EXTRACT($NAME(%(x)),3,*-1)
to quote a string, so just removing the leading and trailing quote is what you want, or
DupQuote(x) QUIT $EXTRACT($NAME(%(x)),4,*-2)