access to characters in string constant

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
janni
Posts: 51
Joined: Wed Aug 24, 2022 2:30 pm

access to characters in string constant

Post by janni » Mon Dec 12, 2022 4:38 pm

It seems that while one can access single characters in a string variable the same isn't possible for string constants.

Code: Select all

dim txt as string(11) = "1234567890"
const txtC = "1234567890"
dim ch as char

ch=txt(1)
ch=txtC(1)    // incompatible types error
Also an attempt to assign single character from string constant to a string variable results in a copy of whole string. Following statements have the same effect

Code: Select all

txt=txtC
txt=txtC(5) 	// copies whole string instead of forming single character string
and, as above, an attempt to assign single character from string constant leads to 'incompatible types' error.

Code: Select all

txt(1)=txtC(5) // incompatible types error 
Why the disparity in treating string constants and variables?

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: access to characters in string constant

Post by David Barker » Tue Dec 13, 2022 9:27 am

It's a known issue. You cannot do that with string constants. However, you can use an array of char constants...

janni
Posts: 51
Joined: Wed Aug 24, 2022 2:30 pm

Re: access to characters in string constant

Post by janni » Tue Dec 13, 2022 12:14 pm

David Barker wrote:
Tue Dec 13, 2022 9:27 am
However, you can use an array of char constants...
Yes, but then it cannot be treated as string, not to mention all the additional commas and quotation marks required.
It's a known issue.
Usually, after such sentence there's one promising that something will be done about it (though not necessarily when :wink: ).

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: access to characters in string constant

Post by David Barker » Tue Dec 13, 2022 12:24 pm

It hopefully will get fixed but internally, it's a complicated change to the compiler and I certainly don't want to break anything! Purely out of interest, why are you accessing a constant in this way in your code?

janni
Posts: 51
Joined: Wed Aug 24, 2022 2:30 pm

Re: access to characters in string constant

Post by janni » Tue Dec 13, 2022 8:31 pm

David Barker wrote:
Tue Dec 13, 2022 12:24 pm
It hopefully will get fixed but internally, it's a complicated change to the compiler and I certainly don't want to break anything!
Nobody wants the latter :evil: and thanks for the promise :) .
Purely out of interest, why are you accessing a constant in this way in your code?
I was writing own strings library, slowly preparing to use SF in future projects, and that's when I noticed the discrepancy. Accessing characters from constant string is quite common, though, certainly, in most cases one may work around the lack of access by using arrays. In simple example of converting decimal to hex string, one may use string constant "0123456789ABCDEF" or type almost four times as many characters to form a character array ("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F") :wink: .

BTW, though nominally SF allows for strings of up to 255 characters, in practice one may work with longer ones (even internal routine copying constant string to string variable does it well). The only real limitation comes from the fact that one cannot pass constant strings by reference above the 64kB limit as the address is limited to 2 bytes (well, only constant string array elements - using string constant invokes 'expression cannot be passed by reference' error). Happily, one may easily work around this, even if it causes disparity in passing string variable and string constant parameters so I was able to make my strings library to work with strings (both variable and constant) longer than 255 characters and address constants in any part of flash.

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: access to characters in string constant

Post by David Barker » Wed Dec 14, 2022 8:59 am

Just for reference, if you want to pass by reference using a 32 bit pointer (rather than 16) use:

Code: Select all

#option large_code_model = true
for example:

Code: Select all

#option large_code_model = true

Const array() As Byte = (1,2,3,4)
Sub mySub(ByRefConst str() As Byte)
End Sub
mySub(array)

janni
Posts: 51
Joined: Wed Aug 24, 2022 2:30 pm

Re: access to characters in string constant

Post by janni » Wed Dec 14, 2022 11:57 am

Right. I've seen this option but forgotten about it :oops: . Still, as one cannot pass string constant by reference, I'll have to use the workaround.

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: access to characters in string constant

Post by David Barker » Wed Dec 14, 2022 12:01 pm

Yes, you will have to use a workaround - something like:

Code: Select all

Sub mySub(addr As Word)
End Sub
mySub(@"1234")
or

Code: Select all

Sub mySub(addr As LongWord)
End Sub
mySub(@"1234")

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: access to characters in string constant

Post by Jerry Messina » Wed Dec 14, 2022 1:25 pm

BTW, though nominally SF allows for strings of up to 255 characters,
I'm not sure I follow what you're getting at here. Where is the 255 char limit?

janni
Posts: 51
Joined: Wed Aug 24, 2022 2:30 pm

Re: access to characters in string constant

Post by janni » Wed Dec 14, 2022 2:36 pm

I'm not sure I follow what you're getting at here. Where is the 255 char limit?
Well, it's in Help

Code: Select all

Type     Bit Size   Range
--------------------------------------------------------
String   Variable  Multiple (up to 255) characters

Swordfish enables you to specify string sizes of up to 256 bytes, 
which equates to 255 individual character elements.
and repeated in the Language Reference Guide in PDF, and the string library that comes with the compiler observes the limit as well by using byte variables for string length. But my point was that there's no real limit and one may work with longer strings.

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: access to characters in string constant

Post by Jerry Messina » Wed Dec 14, 2022 4:08 pm

As you say, the 255 limit really comes from the functions in string.bas and not the string datatype itself.

In most situations I tend to use my own functions too for dealing with strings.
The string handling in SF is very convenient, but memory requirements and execution speed can get out of hand pretty quick if you have large strings.

Post Reply