Author Topic: Background info about the ?? operator in BCX 757  (Read 582 times)

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 786
    • View Profile
Background info about the ?? operator in BCX 757
« on: October 04, 2020, 11:07:20 AM »
Robert Wishlaw shared the following link with me recently:

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stricmp-wcsicmp-mbsicmp-stricmp-l-wcsicmp-l-mbsicmp-l?view=vs-2019

About half down the article is this paragraph:
==========================================================================

Because _stricmp does lowercase comparisons, it may result in unexpected behavior.
To illustrate when case conversion by _stricmp affects the outcome of a comparison,  assume that you have the two strings JOHNSTON and JOHN_HENRY  . The string JOHN_HENRY will be considered less than JOHNSTON because the "_" has a lower ASCII value than a lowercase S. In fact, any character that has an ASCII value between 91 and 96 will be considered less than any letter.


=========================================================================
The newly added BCX_STRICMP (and its alias ICOMPARE), and the ?? operator which uses it, do not suffer
from the issues stated by Microsoft.  Robert and I collaborated on the BCX_STRICMP and did a good amount of testing. 

The MS article was stunning to me because I have been wriiting statements like this for decades:
IF LCASE$(SomeString$) = LCASE$(AnotherString$) THEN DoSomeThing.

If any of those kinds of statements contained a character whose ASCII value was between 91 and 96,
these characters:   [   \    ]   ^   _   ` , there is a good chance that those comparisons were flawed.

Anyone reading this now has no excuse for making those kinds of mistakes  ;)

« Last Edit: October 04, 2020, 11:25:47 AM by MrBcx »

jcfuller

  • Sr. Member
  • ****
  • Posts: 277
    • View Profile
Re: Background info about the ?? operator in BCX 757
« Reply #1 on: October 04, 2020, 11:16:48 AM »
Ok what am I doing wrong? I get NO

James


Code: [Select]
Function main()
Dim s1$,s2$
s1$ = "James"
s2$ = "JAMES"
If ICompare(s1$,s2$) Then
?  "YES"
Else
? "NO"
EndIf
Pause
End Function


MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 786
    • View Profile
Re: Background info about the ?? operator in BCX 757
« Reply #2 on: October 04, 2020, 11:52:06 AM »
JC,

You made a wrong assumption about how ICOMPARE works.

ICOMPARE and BCX_STRICMP both call the new runtime function bcx_stricmp which was modeled
from the stricmp function.  stricmp and ICOMPARE and BCX_STRICMP all produce these results:

  0<    string1 is less than string2
  0       string1 equals string2
>0      string1 is greater than string2


Unlike the c runtime _stricmp, BCX's functions produce results CORRECTLY.

strcmp is another C runtime function that performs normal string comparisons (case sensitive)
and also produces 0, <0, and >0 as a result.
« Last Edit: October 04, 2020, 12:00:49 PM by MrBcx »

jcfuller

  • Sr. Member
  • ****
  • Posts: 277
    • View Profile
Re: Background info about the ?? operator in BCX 757
« Reply #3 on: October 04, 2020, 12:23:57 PM »
That is not very intuitive from a BASIC coding standpoint is it?
In what situation would I care if one string is greater or less than the other?
I want to know if they are equal and would expect a true if they are.
BCX is a translator to c/c++ but it IS BASIC.

I guess I will use the new ?? for any case-insensitive string comparisons.
Good addition.

James

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 786
    • View Profile
Re: Background info about the ?? operator in BCX 757
« Reply #4 on: October 04, 2020, 01:24:04 PM »
That is not very intuitive from a BASIC coding standpoint is it?
In what situation would I care if one string is greater or less than the other?
I want to know if they are equal and would expect a true if they are.
BCX is a translator to c/c++ but it IS BASIC.

I guess I will use the new ?? for any case-insensitive string comparisons.
Good addition.

James

If everything was intuitive, there would be little need for schools, books, or HELP FILES.  I know Robert is
updating the BCX Help file, so a little patience will go a long way.  In fact, it was Robert who expressed
the need for bcx_stricmp to be made into a keyword.  Although you may never use the BCX_STRICMP()
or ICOMPARE() functions directly, I know at least one person who will.

There are many things in BCX that I have never used and will likely never use.  There are other things
in BCX that I use all the time.  That's the nature of things.  I am already using the new ?? operator in a
personal project.  I have wanted that operator for a long time and after several failed attempts at
implementing it into BCX, I finally found an algorithm that works.

Robert

  • Hero Member
  • *****
  • Posts: 601
    • View Profile
Re: Background info about the ?? operator in BCX 757
« Reply #5 on: October 04, 2020, 08:26:21 PM »
That is not very intuitive from a BASIC coding standpoint is it?
In what situation would I care if one string is greater or less than the other?
I want to know if they are equal and would expect a true if they are.
BCX is a translator to c/c++ but it IS BASIC.

I guess I will use the new ?? for any case-insensitive string comparisons.
Good addition.

James

Hi James:

Equality? You mean zero difference?

Situation? What's unbalanced?

Code: [Select]

DIM s1$, s2$, RetVal%

s1$ = "123"
s2$ = "456"
RetVal% = ICOMPARE(s1$,s2$)
PRINT RetVal%

SELECT CASE RetVal%
CASE 0
  ? "Case insensitive s1$ s2$ have zero difference."
CASE > 0
  ? "Case insensitive ", s1$, " has higher ASCII value than ", s2$, "."
CASE < 0
  ? "Case insensitive ", s2$, " has higher ASCII value than ", s1$, "."
END SELECT

PAUSE


It's interesting to me that MrBCX had very much the same attitude as you when I first proposed that bcx_stricmp be exposed for coder's use.

I'll probably get in doo-doo for being a tattle-tale rat but anyway here is what he wrote.

Quote

What advantage does a function have over the ?? mechanism? .... I cannot imagine a more accessible avenue to bcx_stricmp than the ?? operator.


So blame me not him.

Anyway, I still think there is an advantage to knowing more than

123 <> 456

and that

Black is not White

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 786
    • View Profile
Re: Background info about the ?? operator in BCX 757
« Reply #6 on: October 04, 2020, 09:14:17 PM »

It's interesting to me that MrBCX had very much the same attitude as you when I first proposed that bcx_stricmp be exposed for coder's use.

I'll probably get in doo-doo for being a tattle-tale rat but anyway here is what he wrote.

Quote

What advantage does a function have over the ?? mechanism? .... I cannot imagine a more accessible avenue to bcx_stricmp than the ?? operator.


So blame me not him.

Anyway, I still think there is an advantage to knowing more than

123 <> 456

and that

Black is not White

Robert -- you are not in "doo-doo for being a tattle-tale rat".

Context is useful.

If you wanted me to add a BCX_SHADES_OF_PINK () function, I would surely do it, if for no other reason but to say thank you for all you've given to BCX over the last two decades.  Besides, you're not going to live forever and I can always deprecate  BCX_SHADES_OF_PINK once you kick the bucket  :D

Let's all keep in mind, none of this stuff matters ... we're all just having fun trying to keep our minds active.



dgarner

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Background info about the ?? operator in BCX 757
« Reply #7 on: October 05, 2020, 02:48:11 AM »
Quote
Let's all keep in mind, none of this stuff matters ... we're all just having fun trying to keep our minds active.

Maybe "none of this stuff matters", but I hope you realize how significant BCX is to at least a few of us.  Microsoft may no longer place much value on BASIC, but I certainly do.  I'm still looking forward to tapping the potential that I see in using BCX as a gateway to many other programming languages.