[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Question on translate() function

Subject: Re: Question on translate() function
From: "Mailing Lists Mail daktapaal@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 26 Sep 2017 01:44:36 -0000
Re:  Question on translate() function
I also have another question..
I want to replace '[()''+\-*$=]' with the character + a #
like
if(a+b+(c - d) * $x = 3*x) then do funct() else do exit()

should become

if#(#a#+#b#+#(#c# -# d#)# #*# #$#x = 3#*#x#)# then do funct#(# #)#
else do exit#(#)#

which means, if I have a string, and i find the following characters:
'[()''+\-*$=]'
then i should surround each of these characters with # .. x becomes
#x# where x = one of  '[()''+\-*$=]'

i can do one boring way of creating a variable

var rep = replace($str, '+' , '#+#');
var rep = replace ( $rep, '-', '#-#')
var rep = replace ( $rep, '$', '#$#')

so on and so forth for all the chars i have in my list..( which are limited )

or I can store all the chars in a variable as sequence, and then
somehow loop them and replace them in the string... any good way ?


dak


On Mon, Sep 25, 2017 at 9:34 PM, Mailing Lists Mail
daktapaal@xxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> Great..
> I have seen expressions like :
> translate($xxx, 'ABCDE','x');
> which is to say, replace ABCDE with x .. this also works fine
> is this not valid?
>
> or should we do ..translate($xxx, 'ABCDE','xxxxx');
>
> dak
>
> On Mon, Sep 25, 2017 at 6:00 PM, Liam R. E. Quin liam@xxxxxx
> <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>> On Mon, 2017-09-25 at 17:42 +0000, Syd Bauman s.bauman@xxxxxxxxxxxxxxxx
>> wrote:
>>>
>>> I have always presumed that translate() is faster than replace().[1]
>>
>> I'd say, use whichever is clearer, braver, more noble. Take pity on
>> your future self trying to understand replace("[{]\\\$[()][}]", "#",
>> "g")...
>>
>> As for which is faster, it depends not only (as the inestimable Dr Kay
>> has expressed) upon the quality of the optimizer but for some
>> implementations the quality of the underlying code. Unfortunately there
>> are enough differences between XSLT and Perl regular expressions that
>> using libpcre has become difficult, as that library gets extensive
>> optimization outside of our universe and then returns through alternate
>> dimensions to dazzle us :)
>>
>>> But I can't help it, sometimes -- I'd really like to know if
>>> translate() is significantly more efficient (computationally) than
>>> replace() or not.
>>
>> There's no inherent reason why
>>   translate("abc", "ddd")
>> should be faster or slower than
>>   replace("[abc]", "d", "g")
>> once the parsing has been done; however, the work to recognize these
>> two cases may be easier for translate().
>>
>> There are implementations that are faster than a freshly-oiled cow at
>> recognizing regular expression character classes, so if your input
>> string is, say, 100MBytes long, you might well be able to measure the
>> difference. In ASCII days there were implementations that used a bit
>> mask, and then you compare each input character (or four characters at
>> a time, say, using a wider mask) with the or'd mask and only do the
>> more expensive computation when needed. So an implementation using a
>> heavily optimized regular expression library might go faster with
>> replace() than translate(), because the XSLT/XPath/XQuery implementor
>> of translate() might not have done that sort of optimization.
>>
>> Liam
>>
>> --
>> Liam Quin, W3C, http://www.w3.org/People/Quin/
>> Staff contact for Verifiable Claims WG, SVG WG, XQuery WG
>>
>> Web slave for http://www.fromoldbooks.org/

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.