LCase/UCase to work on .c

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

LCase/UCase to work on .c

Post by Mistrel »

LCase and UCase only work on strings. What about chars? :?
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Debug UCase(Chr('a')) ?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

Is it possible to change the case of a unicode character without changing it to a string? From a performance perspective I parse using character arrays because it deals purely with integers. For a string search I convert the input string to lower case before running a comparison.

I think this operation would be faster if I could change the case of only the characters I parse. But this only seems feasible without having to change the character to a string first.
Last edited by Mistrel on Sat Dec 27, 2008 1:03 pm, edited 1 time in total.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

No, but you can always change it back with Asc().
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

Caught me while I was editing, Trond. See my clarification. Maybe you can provide a different solution.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

You could have converted the characters to lowercase before putting them into the array if case is not significant.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Note, that the ASCII-characters have a special offset.
Between 'A' and 'a' are 32 others characters:

Code: Select all

Debug 'a'-'A'
Debug 'b'-'B'
; and so on...
So you can easily add or substract 32 to each .c-variable to turn it into upper or lower case.

With pointer you can even scan whole strings.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

AND51 wrote:Note, that the ASCII-characters have a special offset.
Between 'A' and 'a' are 32 others characters:

Code: Select all

Debug 'a'-'A'
Debug 'b'-'B'
; and so on...
So you can easily add or substract 32 to each .c-variable to turn it into upper or lower case.
Remember to check if it's actually a letter first. Subtracting 32 from a symbol like backspace will not create a "lower-case backspace".
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Post by flaith »

Or with XOR

Code: Select all

a.c = Asc("A")  ; = 65  ;1000001
; or
;a.c = Asc("a")  ; = 97  ;1100001

Debug Chr(a)

b.c = a ! %00100000

Debug Chr(b)
“Fear is a reaction. Courage is a decision.” - WC
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

Excellent! I had no idea about this significant offset. Thank you. :)
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Post by flaith »

you're welcome :D
“Fear is a reaction. Courage is a decision.” - WC
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Also it doesn't work for ß and such characters.
Post Reply