It is currently March 28th, 2024, 9:35 pm

[Feature Suggestion] How about a new function: StrCmp ?

Report bugs with the Rainmeter application and suggest features.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm

[Feature Suggestion] How about a new function: StrCmp ?

Post by SilverAzide »

Hello,
I did a search on the forums to make sure I wasn't rehashing an old thread, but didn't find anything. I see lots of folks trying to do string comparisons in conditional statements. We know this isn't possible now... but what if Rainmeter supported the C-style strcmp function? This would be a straight-up port; i.e., it would return the same values as the C function does. This would still preserve the numbers-only conditional expressions. Unless parsing the expression is a problem, this could be a big help. (P.S.: and strcmpi...?)

Just a thought...
Thanks for listening!
Gadgets Wiki GitHub More Gadgets...
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: [Feature Suggestion] How about a new function: StrCmp ?

Post by balala »

SilverAzide wrote: February 27th, 2020, 6:37 pm I see lots of folks trying to do string comparisons in conditional statements. We know this isn't possible now... but what if Rainmeter supported the C-style strcmp function? This would be a straight-up port; i.e., it would return the same values as the C function does. This would still preserve the numbers-only conditional expressions.
And what the StrCmp C function does? I suppose it compares strings. Am I right? If I am, this can be done with IfMatch, no need for a new function for this. However as yourself said, Rainmeter's IfCondition is entirely numeric, it can't work at all with strings. So, probably such a function can't be implemented in Rainmeter. At least, this is my thought...
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm

Re: [Feature Suggestion] How about a new function: StrCmp ?

Post by SilverAzide »

I know all this, please re-read my post, and check the documentation on strcmp before commenting, please.
Gadgets Wiki GitHub More Gadgets...
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [Feature Suggestion] How about a new function: StrCmp ?

Post by jsmorley »

I assume the advantage of a true/false comparison of two strings over what you can do with IFMatch today that you see is the ability to have a measure test a string that isn't just the direct string value of the measure itself?

I'm not opposed to this in principle, but I'm not sure it can be shoehorned into an IFCondition or the Formula in a Calc measure, that today assume and require that all the components of the formula be numeric in nature. Brian would have to weigh in on this.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm

Re: [Feature Suggestion] How about a new function: StrCmp ?

Post by SilverAzide »

My thought would be to use the function in an IfCondition where you need both string and numeric comparisons, or a Calc measure Formula where you could use StrCmp in a ternary expression to do some calcs. For a simplified example, something like:

IfCondition=(StrCmp([SomeStringMeasure], "hello") == 0 && [NumberMeasure] > 10)
Formula=(StrCmp([SomeStringMeasure], "Celsius") == 0 ? 100 : 212)

If you just needed a string comparison, or just a number, then IfMatch and IfCondition is totally fine, but there are plenty of times where you need both.
Gadgets Wiki GitHub More Gadgets...
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [Feature Suggestion] How about a new function: StrCmp ?

Post by jsmorley »

SilverAzide wrote: February 27th, 2020, 11:53 pm My thought would be to use the function in an IfCondition where you need both string and numeric comparisons, or a Calc measure Formula where you could use StrCmp in a ternary expression to do some calcs. For a simplified example, something like:

IfCondition=(StrCmp([SomeStringMeasure], "hello") == 0 && [NumberMeasure] > 10)
Formula=(StrCmp([SomeStringMeasure], "Celsius") == 0 ? 100 : 212)

If you just needed a string comparison, or just a number, then IfMatch and IfCondition is totally fine, but there are plenty of times where you need both.
I see. As I said, not opposed in principle, just not sure how hard it would be to implement.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm

Re: [Feature Suggestion] How about a new function: StrCmp ?

Post by SilverAzide »

OK, cool... just an idea. Not sure if there is a philosophical concern for "abuse" because of the presence of such a function. I mean, virtually every new Rainmeter user, once they discover the IfCondition, the first thing they want to do is a string comparison. Especially if they come from a traditional programming background. I know I did that, and there's plenty of forum posts from other folks trying this too. This function would blur the hard separation between number-only IfConditions and string-only IfMatches. And if adding strcmp functionality is possible, then the very next thing someone will ask for is strcmpi...
:)
Gadgets Wiki GitHub More Gadgets...
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: [Feature Suggestion] How about a new function: StrCmp ?

Post by balala »

SilverAzide wrote: February 27th, 2020, 11:05 pm I know all this, please re-read my post, and check the documentation on strcmp before commenting, please.
Sorry if I offended you, was not my intention.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [Feature Suggestion] How about a new function: StrCmp ?

Post by jsmorley »

In the meantime, in between sniping at Balala, you might do something like:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]

[MeasureString]
Measure=String
String=The value of the string is TRUE at this time
IfMatch=( TRUE )
IfMatchAction=[!SetOption MeasureCompare Formula "1"]
IfNotMatchAction=[!SetOption MeasureCompare Formula "-1"]

[MeasureValue]
Measure=Calc
Formula=(MeasureValue + 1) % 11

[MeasureCompare]
Measure=Calc
IfCondition=(MeasureValue >=5) && (MeasureCompare > 0)
IfTrueAction=[!SetOption MeterResult Text "Both conditions are true"]
IfFalseAction=[!SetOption MeterResult Text "One or more conditions are false"]

[MeterResult]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
As an aside, I fear that StrCmp / StrCmpi are going to be disappointing for many, as they have none of the power of regular expression that IfMatch has. They are a very brute-force "is at least this much of this entire string the same as that entire string?" approach that may not be as flexible as folks will quickly want. Note my example...

I could perhaps see blending the power of regular expression matching with the power of numeric value comparisons in some new option like IfEvaluate or some such, I sorta like the flexibility of "if this string and this value", and I've long thought that the way IfMatch can only evaluate the string value of the measure it is on, unlike the way IfCondition works, is a bit of a limitation. Having it behave like IfAboveValue feels like a step back a bit to me.

But I'm not sure I'm a fan of trying to shoehorn the less powerful StrCmp into the existing IfCondition.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm

Re: [Feature Suggestion] How about a new function: StrCmp ?

Post by SilverAzide »

Yes, that will work... I guess I was just trying to think of an approach that folks could use without needing a workaround of an extra "evaluation" measure, especially for newbies. Obviously, there have been a whole lot of skins created over the years without ever having a strcmp/strcmpi available. Plus I think there are internationalization issues with strcmp/strcmpi with regard to Unicode which makes these only useful for true/false matches (i.e., the C functions return negative/positive integers indicating less than/greater than, but I don't think this works properly with Unicode strings).

I just thought I'd throw the enhancement idea out there to see if it stuck....
Gadgets Wiki GitHub More Gadgets...