Fastest way to do case insensitive search and replace?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Fastest way to do case insensitive search and replace?

Markus Fritsche-4
Hi,

is there a way to search case insensitively and replace in Strings in
one shot?
If
MethodFinder methodFor: #(('HaLLol' 'll' 'tt') 'Hattol')
doesn't find anything, does that mean "no" right away?

Kind regards
   Markus

Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to do case insensitive search and replace?

Marcus Denker-4

> On 11 Dec 2014, at 13:42, Markus Fritsche <[hidden email]> wrote:
>
> Hi,
>
> is there a way to search case insensitively and replace in Strings in one shot?
> If
> MethodFinder methodFor: #(('HaLLol' 'll' 'tt') 'Hattol')
> doesn't find anything, does that mean "no" right away?
>

No, method finder uses a whitelist that is not maintained.

        Marcus


Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to do case insensitive search and replace?

jhwoods
In reply to this post by Markus Fritsche-4
I take it you want to preserve the case of the original?  Otherwise you just convert both source, and replace terms into asLowercase or asUppercase.

('HaLLol' asLowercase copyReplaceAll: 'll' asLowercase with: 'tt' asLowercase) =  'hattol'

So, I'm presuming you want something that keeps the case everywhere it doesn't replace?

How important is performance?  I'd write simple code first then enhance if necessary.  Probably the simplest way to do what you want is to use a regex replace.

(('ll' asRegexIgnoringCase) copy: 'HaLLol' replacingMatchesWith: 'tt') =  'Hattol'

Does this help?

... John
Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to do case insensitive search and replace?

Markus Fritsche-4
In reply to this post by Marcus Denker-4
On 2014-12-11 13:48, Marcus Denker wrote:
>> If
>> MethodFinder methodFor: #(('HaLLol' 'll' 'tt') 'Hattol')
>> doesn't find anything, does that mean "no" right away?

> No, method finder uses a whitelist that is not maintained.

Thanks for claryfying.

The answer to my first question:

| r |
r := 'll' asRegexIgnoringCase.
r copy: 'HaLLol' replacingMatchesWith: 'tt'.

Reply | Threaded
Open this post in threaded view
|

Re: Fastest way to do case insensitive search and replace?

jhwoods
Hi Markus,

Yes, that's the one.  I did send you the answer but my post was rejected by the mailing list.  However I see it didn't take you long :-)

I find that Regex library pretty marvellous tbh, one of the best bits of Smalltalk I've come across, and it's so damn fast that it's often better than anything specific I can write.

Does Vassili Bykov hang around on any of these forums?  If I ever meet him, I'm buying him a beer!