String Constants and Chr()

Everything else that doesn't fall into one of the other PB categories.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ebs.

I prefer to use constants like #DoubleQuote instead of Chr(34). I think it makes your code easier to understand.

The history file for PB 3.50 says:

- Optimized: the code generation: Chr(#Constant) is now considered as a litteral string and some speed enhancements

This led me to believe that the following code would be OK:

Code: Select all

#Underscore = "_"
#DoubleQuote = Chr(34)

String1.s = Chr(34) + "Hello!" + Chr(34)
MessageRequester("String Constant Test", "String1 = " + String1, 0)
; produces String1 = "Hello!"    GOOD

String2.s = #Underscore + "Hello!" + #Underscore
MessageRequester("String Constant Test", "String2 = " + String2, 0)
; produces String2 = _Hello!_    GOOD

String3.s = #DoubleQuote + "Hello!" + #DoubleQuote
MessageRequester("String Constant Test", "String3 = " + String3, 0)
; produces String3 = 34Hello!34   BAD

End
Is it possible to create a string constant using Chr(), or another method? I know I could create a variable and set it equal to Chr(34), but it makes more sense to use a string constant.

Eric
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by horst.

[quoteIs it possible to create a string constant using Chr..[/quote]

I had also hoped that this was possible with the new version,
but the handling of constant strings seems to be quite buggy:

Code: Select all

Definition              resulting string:      
-----------------------------------------
#s = chr(13)            "13"
#s = chr(13) + chr(10)  "10"
#s = "some" + "thing"   "thing"  (!)
#t = "foo" 
#s = #t                 "0"      (!!)
#s = "b" + #t           (error)
-----------------------------------------


And this crashes: #s = Asc("A")



Horst
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> #DoubleQuote = Chr(34)
> #s = Asc("A")

Chr and Asc are runtime commands. You can't assign them to a constant
because they aren't constants. All other examples (#Underscore = "_")
are constants because they're not part of a runtime command (Chr/Asc).
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

@PB:

Well, that's the thing, that is new in v3.5, Chr(34) is internally replaced by a Constant, so it's faster.

> #DoubleQuote = Chr(34)


But eventhough, i think, something like this wasn't meant to be possible.

Timo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

I will try to add such feature, as it could be interresting.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by horst.
Originally posted by PB

Chr and Asc are runtime commands.
Actually, Chr(n) should be treated as a one-byte string,
like in string variables.
In any case it would be nice to get an error message,
if I use a construct that is not supported.

(The ASC example was just a crash report)


Horst
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ebs.

Fred,

Thank you!

Eric
Originally posted by fred

I will try to add such feature, as it could be interresting.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by TheBeck.

I agree, a good compiler should evaluate ALL constant expressions and chr() commands at compile time. Most modern compilers do this don't they?

For example:
A = 10 + 20 would be compiled as A = 30
or
A = "The" + "Beck" would be compiled as A= "TheBeck"
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

PureBasic do it. It even takes in case the special Chr(#Constant) which other Basic doesn't do (AFAIK).

Fred - AlphaSND
Post Reply