Fixed strings handle differently in FB than PB (or QB)

Started by Paul Squires, September 11, 2015, 10:31:23 AM

Previous topic - Next topic

Paul Squires

I posted this question on the FB forum:
http://www.freebasic.net/forum/viewtopic.php?f=3&t=23926

Here is a copy:

Hi Everyone,

I have read the documentation and read some forum posts about this subject. The behaviour is different than in other BASIC's that I have used. Essentially, a fixed length string is padded with Nulls rather than Spaces so when the fixed length string is assigned to a regular String data type variable, the string is truncated to the first Null character.

Is there any appetite to change the padding of fixed length strings from Null characters to Space characters? I also realize that fixed length strings have the implicit Null terminating character at the end so using them in TYPEs as records in a disk file could be problematic.

Knowing the differing behaviour I can work around the issue but I was just curious where this issue stands to with the community or if it is no big deal at all.

Thanks,
Paul



Type MyType
   stFixed As String * 10
   iInt    As Integer
   nLong   As Long
End Type


Dim t    As MyType
Dim st10 As String * 10
Dim st   As String

t.stFixed = "Paul"
st10 = "Paul"

' Look at the characters composing the fixed length string
' Confirms that fixed length strings are padded with NULLs
' rather than SPACEs.
For i As Integer = 0 To Len(st10) - 1
   ? st10[i];",";
Next   

?
st = t.stFixed    ' assign the fixed length TYPE string to the variable string
? "t.stFixed = [" & t.stFixed & "]", "Len="; Len(t.stFixed)
? "st = [" & st & "]", "Len="; Len(st)    '<-- truncates

st = st10         ' assign the fixed length string to the variable string
? "st10 = [" & st10 & "]", "Len="; Len(st10)
? "st = [" & st & "]", "Len="; Len(st)  '<-- truncates



Paul Squires
PlanetSquires Software

Eddy Van Esch

Quote from: TechSupport on September 11, 2015, 10:31:23 AM
..Essentially, a fixed length string is padded with Nulls rather than Spaces
Isn't this the same in PB ..?

Quote from: TechSupport on September 11, 2015, 10:31:23 AM
Is there any appetite to change the padding of fixed length strings from Null characters to Space characters?
But Space characters can be part of the string data itself .. So how can they be used for padding ..? In other words, how would you then be able to tell which Space char signifies the end of the string?

(It's possible that I misunderstood your question, Paul ..)
Eddy

Paul Squires

Yes, in PB they are initially set to nulls but once text is assigned to the string then they are padded to the defined length with spaces.

Yes, space characters can certainly be part of the fixed string. In order for FB to function similarly to PB you would have to manually pad your fixed length string with spaces rather than relying on the compiler to pad them for you, otherwise, once you assign the fixed length string to a dynamic STRING then the fixed length size gets truncated.
Paul Squires
PlanetSquires Software

José Roca

Another problem is that they are null terminated. This causes problems with members of structures.

Klaas Holland

Perhaps the problems could be solved when FF_Using like this would be possible.

Stnd = FF_Using " \       \   \       \   \ \   \ \ " ; OT.IdNr ; OT.Datum ; OT.Stat ; OT.WeekNr

Klaas

José Roca

The problem of being null terminated can be only solved changing the compiler. Now, if you declare a fixed string of length 30, you only have 29 characters available.