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?

0 895
Discussion (6)1
Log in or sign up to continue


QT(x) Q $S(x'[$C(34):x,1:$P(x,$C(34))_$C(34,34)_$$QT($P(x,$C(34),2,$L(x)+1)))

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


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): 

  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().

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)