Set String Length by pointer range

General FreeBASIC programming questions.
Post Reply
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Set String Length by pointer range

Post by Munair »

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:

Re: Set String Length by pointer range

Post by St_W »

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

Re: Set String Length by pointer range

Post by fxm »

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:

Re: Set String Length by pointer range

Post by Munair »

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

Re: Set String Length by pointer range

Post by Josep Roca »

> 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

Re: Set String Length by pointer range

Post by Josep Roca »

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:

Re: Set String Length by pointer range

Post by Munair »

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.
Post Reply