General FreeBASIC programming questions.
Munair
Posts: 1286 Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:
Post
by Munair » Dec 07, 2017 15:38
Is there a way to set the length of a string by means of a pointer range? In Free Pascal there is SetLength:
Code: Select all
Dim Result as String
Dim as zstring ptr p1, p2
p1 = strptr(Result)
p2 = p1
p2 += 20
SetLength(Result, p2 - p1)
There is of course Allocate, Reallocate, but that would require an extra memory buffer and a copy of the string.
St_W
Posts: 1626 Joined: Feb 11, 2009 14:24
Location: Austria
Contact:
Post
by St_W » Dec 07, 2017 16:32
FreeBasic has several string types, the only one with length information is String (ZString and WString do not have such information). A String consists of a String descriptor and the actual string data. The string descriptor contains the length (among other information) and of course it could be modified, BUT I wouldn't suggest to tamper with FreeBasic's internal string handling in any way as long if you do not have a very good reason to do so. You could easily do things wrong and of course it may break at any time if compiler internals are changed.
fxm
Moderator
Posts: 12107 Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE
Post
by fxm » Dec 07, 2017 17:37
Code: Select all
Var S : String;
begin
Setlength(S,100);
FillChar(S[1],100,#32);
Writeln ('"',S,'"');
end.
In FreeBASIC, you can define the length of the string and fill it in one shoot:
Code: Select all
Dim As String S
S = String(100, " ") '' or S = String(100, 32)
Print """" & S & """"
Sleep
Munair
Posts: 1286 Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:
Post
by Munair » Dec 07, 2017 18:05
fxm wrote: Code: Select all
Var S : String;
begin
Setlength(S,100);
FillChar(S[1],100,#32);
Writeln ('"',S,'"');
end.
In FreeBASIC, you can define the length of the string and fill it in one shoot:
Code: Select all
Dim As String S
S = String(100, " ") '' or S = String(100, 32)
Print """" & S & """"
Sleep
Yes, but in the particular case I'm working on, the string is already filled. In FreePascal:
Code: Select all
// Final correction of the buffer size
SetLength(Result, OutStr - PChar(Result));
Maybe the routine SetLength creates a new buffer and copies the string to fit the new size. I guess I'm looking for some type of
redim preserve but for strings.
Josep Roca
Posts: 564 Joined: Sep 27, 2016 18:20
Location: Valencia, Spain
Post
by Josep Roca » Dec 07, 2017 18:40
> Maybe the routine SetLength creates a new buffer and copies the string to fit the new size.
You can bet on it.
Josep Roca
Posts: 564 Joined: Sep 27, 2016 18:20
Location: Valencia, Spain
Post
by Josep Roca » Dec 07, 2017 18:51
It's easy to replicate the functionality
Code: Select all
SUB SetLength (BYREF s AS STRING, BYVAL n AS LONG)
IF n > LEN(s) THEN s += SPACE(n - LEN(s)) ELSE s = LEFT(s, n)
END SUB
Munair
Posts: 1286 Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:
Post
by Munair » Dec 07, 2017 19:42
Josep Roca wrote: It's easy to replicate the functionality
Code: Select all
SUB SetLength (BYREF s AS STRING, BYVAL n AS LONG)
IF n > LEN(s) THEN s += SPACE(n - LEN(s)) ELSE s = LEFT(s, n)
END SUB
I was just about to do the same. ;-) Thanks.