mIRC Home    About    Download    Register    News    Help

Print Thread
lower & upper #214238 25/07/09 10:52 PM
Joined: Jul 2006
Posts: 242
H
HaleyJ Offline OP
Fjord artisan
OP Offline
Fjord artisan
H
Joined: Jul 2006
Posts: 242
Code:
//if (5 isupper) { echo -a How can a number be upper-case? }
//if (5 islower) { echo -a Even if you say its upper-case how can it then be lower-case aswell? }


Newbie
Re: lower & upper [Re: HaleyJ] #214239 25/07/09 11:16 PM
Joined: Oct 2003
Posts: 3,918
A
argv0 Offline
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
Seems perfectly fine behaviour to me. You're not comparing a "number", you're comparing the string "5". The string "5" is equal to the upper-case version of the string "5", so it is an upper-case string. The same goes for lower.

This behaviour is necessary since you may be testing:

if (X5 isupper) echo -a TRUE

Obviously the 5 will be ignored in both strings-- except in your string there's nothing else to compare. Therefore you can't check that the string is all upper-case, you must check that an upper-case string has no lower-case characters. Since the string "5" has no lower-case characters, it is considered upper-case. It also has no upper-case characters, so it is also lower-case. You can consider this "undefined", if you'd like. It's not wrong.

As a sidenote, this is what you'd expect in any programming language:

PHP:
Code:
strtoupper("5") == "5" // true
strtolower("5") == "5" // true


C:
Code:
toupper('5') == '5' // true
tolower('5') == '5' // true


Java:
Code:
"5".toUpperCase() == "5" // true
"5".toLowerCase() == "5" // true


And finally mIRC (this is basically what mIRC is doing):
Code:
$upper(5) == 5 ; true
$lower(5) == 5 ; true


Again, it should be clear: isupper/islower are for string comparison here, not number comparison (there is no number comparison for isupper/lower). String comparison checks that the bytes given are equal-- in the case of isupper, that the bytes given ("5") is equal to bytes of the upper case version of that string.. indeed, they are the same set of bytes, and the same goes for lower-case.


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Re: lower & upper [Re: argv0] #214240 25/07/09 11:25 PM
Joined: Jul 2006
Posts: 242
H
HaleyJ Offline OP
Fjord artisan
OP Offline
Fjord artisan
H
Joined: Jul 2006
Posts: 242
PHP does not recognise a number as lower or upper case. Rather as an element on its own.

Code:
<?php
$char = "5";
if (ctype_upper($char))
{
echo "PHP defines this string as upper-case.\n";
}
else
{
echo "PHP does not define this string as upper-case\n";
}
if (ctype_lower($char))
{
echo "PHP defines this string as lower case.\n";
}
else
{
echo "PHP does not define this string as lower-case either.\n";
}
?>


Output

PHP does not define this string as upper-case.
PHP does not define this string as lower-case either.



Newbie
Re: lower & upper [Re: HaleyJ] #214241 25/07/09 11:34 PM
Joined: Oct 2003
Posts: 3,918
A
argv0 Offline
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
That's a different algorithm for testing than the one I (or mIRC) provided. ctype_upper is checking that all alpha characters are upper-case, not that the bytes are equal to an uppercased version of the string.

Either way, you end up with either a double-positive or a double-negative.. they are both equally meaningful and equally meaningless, since you're saying nothing about the string "5" (it's neither uppercase nor lowercase-- it's both uppercase and lowercase).


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Re: lower & upper [Re: argv0] #214245 26/07/09 08:31 AM
Joined: Jul 2006
Posts: 242
H
HaleyJ Offline OP
Fjord artisan
OP Offline
Fjord artisan
H
Joined: Jul 2006
Posts: 242
I would have thought that a number should be neither. Not upper-case or lower-case as CASE only can apply to alphabetical characters. PHP seems to be doing exactly that.

Additionally Definition of upper-case my dictionary refers to upper/lower case characters in the A.B.C.D range - meaning alphabetical.



Newbie
Re: lower & upper [Re: HaleyJ] #214249 26/07/09 05:42 PM
Joined: Oct 2003
Posts: 3,918
A
argv0 Offline
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
"Upper case" can technically be applied to any string of binary data. Programming is not English, there are terms that have slightly different meaning than defined by Meriam-Webster; a "string" is not a segment of rope. As I mentioned above, in just about every language (including PHP) a string with only numbers in it is the same when converted to upper case as it is when converted to lower case-- this means the strings are the same. The conclusion here based on string data alone is that the string can be considered both upper and lower case. To say that it is not "an uppercase string including alpha characters" would be a *different* test, one you can implement yourself:

Code:
; * NOTE: this is not how ctype_upper is implemented, see bottom 
alias is_uppercase {
  return $iif($regex($1-, /[A-Z]/) && !$regex($1-, /[a-z]/), $true, $false)
}


However to maintain congruence and consistency the behaviour of isupper cannot have that specialized "English language" version of the algorithm.

If mIRC said the following:

Code:
//if (5 isupper) ; false


Then we would expect the following to also be false:

Code:
$upper(5) == 5


But we know the above is true.

This would be incongruent, where one test gives one result but the same test written differently gives another.

In short, "x isupper" should be considered shorthand for "$upper(x) == x". I see nothing wrong with having the behaviour defined this way.

I should also point out that the ctype_upper PHP function you keep referencing cannot be applied here:

Code:
ctype_upper("X5"); // returns false


This means that the "ctype_upper" checks that the string is *entirely* alpha characters and nothing else. Do you know what that means?

PHP:
Code:
ctype_upper("HELLO WORLD") // returns false
ctype_upper("HI!!!") // returns false

mIRC:
Code:
if (HELLO WORLD isupper) ; returns true
if (HI!!! isupper) ; returns true


Looks like mIRC is right where PHP is wrong. If mIRC changed its behaviour to PHP's, we would end up with a much larger bug: the fact that you could test more than one word at a time. That would break plenty of scripts.


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"