Recent

Author Topic: UpCase vs. UpperCase  (Read 8090 times)

Pascal

  • Hero Member
  • *****
  • Posts: 932
UpCase vs. UpperCase
« on: December 07, 2017, 07:11:31 am »
Which is best to use?
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Noodly

  • Jr. Member
  • **
  • Posts: 70
Re: UpCase vs. UpperCase
« Reply #1 on: December 07, 2017, 08:04:32 am »
UpCase is more flexible as it works on chars as well as more string types than UpperCase.
Windows 10 Home, Lazarus 2.02 (svn 60954), FPC 3.04

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: UpCase vs. UpperCase
« Reply #2 on: December 07, 2017, 10:09:48 am »
UpCase is more flexible as it works on chars as well as more string types than UpperCase.
Absolutely wrong, Not correct, they are completely equivalent:
   https://www.freepascal.org/docs-html/rtl/sysutils/uppercase.html
The most flexible one has the misleading name AnsiUpperCase which will work with any character set provided a string manager is installed: 
   https://www.freepascal.org/docs-html/rtl/sysutils/ansiuppercase.html

It is rather sad that both of you did not simply RTFM. <grumpy  >:D >:D>
The FPC manuals are excellent. Always read them first.

So: read those two links and you gain some expertise on the subject  ::)

« Last Edit: December 07, 2017, 10:41:53 am by Thaddy »
Specialize a type, not a var.

Noodly

  • Jr. Member
  • **
  • Posts: 70
Re: UpCase vs. UpperCase
« Reply #3 on: December 07, 2017, 11:22:21 am »
Before posting I did RTFM and saw the "completely equivalent statement" in the UpperCase entry. In the context it's badly worded because if it's true then this would compile:

Code: [Select]
program uptest;
uses sysutils;
var
  a,b : char;
begin
  a:='a';
  a:=UpCase(a);
  b:='b';
  b:=UpperCase(b);
end.   

What the FM should say is that "UpperCase has been superseded by (and therefore can be replaced by) the more capable UpCase function and is provided for compatibility only" or words to that effect.
Windows 10 Home, Lazarus 2.02 (svn 60954), FPC 3.04

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: UpCase vs. UpperCase
« Reply #4 on: December 07, 2017, 11:49:28 am »
Is there any reason why new coding to allow for new string formats can't be added to the original command name ?

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: UpCase vs. UpperCase
« Reply #5 on: December 07, 2017, 12:13:17 pm »
Is there any reason why new coding to allow for new string formats can't be added to the original command name ?
Compatibility: the char version from system.pas of UpCase needs to be AnsiChar in the range 1..127. That's a Pascal language issue.

Also not that Uppercase for string in sysutils is deprecated because it is code duplication with upcase for string in system. They are exactly the same.
So if you don't need anything fancy, use upcase from system because you have lighter code as you don't have to include sysutils.
« Last Edit: December 07, 2017, 12:24:04 pm by Thaddy »
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: UpCase vs. UpperCase
« Reply #6 on: December 07, 2017, 12:15:07 pm »
Before posting I did RTFM and saw the "completely equivalent statement" in the UpperCase entry. In the context it's badly worded because if it's true then this would compile:

Code: [Select]
program uptest;
uses sysutils;
var
  a,b : char;
begin
  a:='a';
  a:=UpCase(a);
  b:='b';
  b:=UpperCase(b);
end.   

What the FM should say is that "UpperCase has been superseded by (and therefore can be replaced by) the more capable UpCase function and is provided for compatibility only" or words to that effect.

You are mixing up the old style upcase for char with the new style upcase for string in system. And that one is the same as Uppercase in sysutils. No extra's both ways, same code.
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$ifdef fpc}{$mode delphi}{$H+}{$I-}{$endif}
  3. uses sysutils;
  4. var
  5.   a,b : string;
  6. begin
  7.   a:='a';
  8.   a:=UpCase(a);
  9.   b:='b';
  10.   b:=UpperCase(b);
  11. end.
   
« Last Edit: December 07, 2017, 12:24:41 pm by Thaddy »
Specialize a type, not a var.

Francois_C

  • New member
  • *
  • Posts: 9
Re: UpCase vs. UpperCase
« Reply #7 on: August 13, 2018, 05:29:43 pm »
The most flexible one has the misleading name AnsiUpperCase which will work with any character set provided a string manager is installed: 
   https://www.freepascal.org/docs-html/rtl/sysutils/ansiuppercase.html

Thank you very much, Thaddy: AnsiUpperCase does the trick for UTF8. Exactly what I was looking for.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: UpCase vs. UpperCase
« Reply #8 on: August 29, 2018, 10:56:20 am »
The upcase/uppercase situation is a bit convoluted because TP for very long didn't have anything but the upcase(char).  So any codebase did its own enhancements (FPC with locase and later string versions), FV had an uppercase, and later Delphi compatibility added more. (sysutils and later strutils)

Afaik the only unicode one is the ansi* and the "sysutils.UnicodeUppercase" variants.   

The sysutils.uppercase(unicodestring) are limited, probably to keep them fast.





 

TinyPortal © 2005-2018