Written by

Technical Lead at SP.ARM
Question Alexey Maslov · Oct 17, 2017

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

Vitaliy Serdtsev · Oct 17, 2017

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>
0
Alexey Maslov  Oct 17, 2017 to Vitaliy Serdtsev

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=""
0
Alexey Maslov  Oct 31, 2017 to Stuart Salzer

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!

0
Evgeny Shvarov  Oct 31, 2017 to Stuart Salzer

This is really nice!

And short, so very is suitable for macro. 

0
Alexey Maslov · Oct 20, 2017

Yet another variant of quotes duplicator (pure Mumps): 

dupP(str)  quit:str'[$c(34) str  new strdup,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().

0
Stuart Salzer · Oct 30, 2017

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)

0