Feature Request: Clearing Fixed Length Strings

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Feature Request: Clearing Fixed Length Strings

Post by Munair »

In other BASIC dialects it is possible to clear a fixed length string of null characters and replace them with space characters simply by assigning an empty string:

Code: Select all

dim buffer as string * 20 = ""
This would avoid the need of 'clearing' the string explicitly:

Code: Select all

buffer = space(20)
Currently a problem occurs when data in the string 'gets lost' in the middle:

Code: Select all

dim buffer as string * 20
dim s as string

mid(buffer, 4) = "word"
s = buffer
print len(s)
Allowing the string to be 'cleared' gives a programmer a choice whether or not to get rid of the null characters in a simple manner.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Feature Request: Clearing Fixed Length Strings

Post by dodicat »

Most surprisingly this works for a fixed length string
You can delete from line 14 on if you wish, the results are different for different compilers / optimisations.

Code: Select all


dim buffer as string * 20=string(len(buffer),32)
dim s as string

mid(buffer, 4) = "press a key"
s = buffer

print len(s)
print s
sleep


dim as string x=string(len(x),"?")
print x
print len(x)
sleep

  
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Feature Request: Clearing Fixed Length Strings

Post by Munair »

The fixed string result is not surprising as the null characters are replaced by spaces. However, the variable length string x declaration and assignment is indeed surprising. I would expect an error of undefined x with the len statement, but apparently the compiler accepts it as a kind of forward declaration.

In my console the loop decided to stop at 4216541. That's on Manjaro Linux x64 with FB version 1.06:
FreeBASIC Compiler - Version 1.06.0 (10-20-2018), built for linux-x86_64 (64bit)
Last edited by Munair on Nov 25, 2018 14:18, edited 1 time in total.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Feature Request: Clearing Fixed Length Strings

Post by grindstone »

Here (WinXP 32/FB 1.05) Len(x) is 16600. Very strange...

EDIT:
FB 0.90 + 1.02: Len(x) = 16592
FB 1.03 and later: Len(x) = 16600
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Feature Request: Clearing Fixed Length Strings

Post by Munair »

I just tried the latest build (FreeBASIC Compiler - Version 1.06.0 (11-22-2018), built for linux-x86_64 (64bit)) and the loop stops at 4202560. Might be trivial, but just wanted to point that out.

BTW, this discussion covers the same issue:
viewtopic.php?f=17&t=27149&start=15#p254948
Post Reply