an equal '=' test string not case sensitive

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: an equal '=' test string not case sensitive

Post by davido »

HI TI-994A,

Thank you for your comments. :D

I missed the finer point. :oops:
DE AA EB
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: an equal '=' test string not case sensitive

Post by skywalk »

Code: Select all

CompilerIf #PB_Compiler_Debugger:CompilerError "Turn OFF debugger for Speed Tests!":CompilerEndIf
s1$ = "hello!": #s = "HELLO!": s2$ = #s
Macro StrCmp(str1, str2, UseCase=#PB_String_NoCase)
  Bool(CompareMemoryString(@str1, @str2, UseCase) = #PB_String_Equal)
EndMacro
Macro StrCmpLC(str1, str2, UseCase=#PB_String_NoCase)
  Bool(LCase(str1) = LCase(str2))
EndMacro
Procedure StrCmpP(a$, b$)
  ProcedureReturn Bool(CompareMemoryString(@a$, @b$, #PB_String_NoCase) = #PB_String_Equal)
EndProcedure
nTries = 1e6
t = ElapsedMilliseconds()
For L = 1 To nTries
  StrCmp(s1$, s2$)
Next
MessageRequester("Fastest", "CompareMemoryString using string variable -> " + Str(ElapsedMilliseconds() - t) + "ms")
t = ElapsedMilliseconds()
For L = 1 To nTries
  StrCmpLC(s1$, #s)
Next
MessageRequester("Slower", "LCase() = LCase() -> " + Str(ElapsedMilliseconds() - t) + "ms")
t = ElapsedMilliseconds()
For L = 1 To nTries
  StrCmpP(s1$, #s)
Next
MessageRequester("Slowest", "Procedure+CompareMemoryString enabling use of string #const -> " + Str(ElapsedMilliseconds() - t) + "ms")
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
idle
Always Here
Always Here
Posts: 5840
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: an equal '=' test string not case sensitive

Post by idle »

simple and full case folding string cmp
viewtopic.php?t=80275
https://dnscope.io/idlefiles/casefold.pb
BarryG
Addict
Addict
Posts: 4135
Joined: Thu Apr 18, 2019 8:17 am

Re: an equal '=' test string not case sensitive

Post by BarryG »

TI-994A wrote: Wed Jul 24, 2013 3:18 amperhaps you could make use of PureBasic's FindString() function with the #PB_String_NoCase flag
Nope. I modified your example to show why:

Code: Select all

s1$ = "hello said! - but this is different"
s2$ = "HELLO Said!"
If FindString(s1$, s2$, 1, #PB_String_NoCase)
  result$ = "The two strings match!" ; This will be the result. :)
Else
  result$ = "The two strings don't match."
EndIf
MessageRequester("Case Insensitive String Compare", result$)
User avatar
idle
Always Here
Always Here
Posts: 5840
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: an equal '=' test string not case sensitive

Post by idle »

What are you on about Barry? Answer this.
idle wrote: Wed Nov 16, 2022 12:56 am simple and full case folding string cmp
viewtopic.php?t=80128
https://dnscope.io/idlefiles/casefold.pb
BarryG
Addict
Addict
Posts: 4135
Joined: Thu Apr 18, 2019 8:17 am

Re: an equal '=' test string not case sensitive

Post by BarryG »

I was replying to TI-994A's code, and showed why it doesn't work. He said to try using FindString() to match strings, and I showed why it can fail (it returns a match when it shouldn't, because s1$ and s2$ are different).
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: an equal '=' test string not case sensitive

Post by Mijikai »

Another way (Windows only):

Code: Select all

EnableExplicit

Import ""
  _wcsicmp.i(*str1,*str2)
EndImport

Macro CompareString(_str1_,_str2_)
  Bool(_wcsicmp(_str1_,_str2_) = 0)  
EndMacro

Global s1.s = "Hello World!"
Global s2.s = "HELLO WORLD!"

Debug CompareString(s1,s2)
Debug CompareString(@s1,@s2)

s2 = ":)"

Debug CompareString(s1,s2)
Debug CompareString(@s1,@s2)

Debug CompareString(":)",s2)

End
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: an equal '=' test string not case sensitive

Post by Caronte3D »

I get: "Unresolved external" error
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: an equal '=' test string not case sensitive

Post by Mijikai »

I used PB 6.00 x64 - the function is part of the crt lib.
Unless i updated it manually it should work out of the box.
User avatar
idle
Always Here
Always Here
Posts: 5840
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: an equal '=' test string not case sensitive

Post by idle »

You have just answered a post from 2013 without looking at the solution I posted to this old topic. It provides a complete solution to the problem.
idle wrote: Wed Nov 16, 2022 12:56 am simple and full case folding string cmp
viewtopic.php?t=80128
https://dnscope.io/idlefiles/casefold.pb
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: an equal '=' test string not case sensitive

Post by Caronte3D »

Mijikai wrote: Wed Nov 16, 2022 7:42 pm I used PB 6.00 x64 - the function is part of the crt lib.
Unless i updated it manually it should work out of the box.
Ok, then doesn't work on x86 :wink:
BarryG
Addict
Addict
Posts: 4135
Joined: Thu Apr 18, 2019 8:17 am

Re: an equal '=' test string not case sensitive

Post by BarryG »

idle wrote: Wed Nov 16, 2022 7:44 pmYou have just answered a post from 2013
Oh, I didn't realise that. I saw your post (which resurrected the thread) and therefore mistakenly thought this was a current topic, including the post that I replied to. I don't check the dates of posts before replying because my board prefs are set to only show me new posts since my last visit.

My comment is still valid regardless though, as an explanation to newbies of why TI-994A's approach won't work.
User avatar
idle
Always Here
Always Here
Posts: 5840
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: an equal '=' test string not case sensitive

Post by idle »

BarryG wrote: Thu Nov 17, 2022 8:26 am
idle wrote: Wed Nov 16, 2022 7:44 pmYou have just answered a post from 2013
Oh, I didn't realise that. I saw your post (which resurrected the thread) and therefore mistakenly thought this was a current topic, including the post that I replied to. I don't check the dates of posts before replying because my board prefs are set to only show me new posts since my last visit.

My comment is still valid regardless though, as a demonstration of why TI-994A's approach won't work.
Thanks, I was just trying to link the topic for search purposes and then got exasperated by all the replies.
I have the emotional intelligence of John macenroe https://youtu.be/fd90m3xKfl8
BarryG
Addict
Addict
Posts: 4135
Joined: Thu Apr 18, 2019 8:17 am

Re: an equal '=' test string not case sensitive

Post by BarryG »

It's all good, mate.
Post Reply