DIM-ing a constant string

New to FreeBASIC? Post your questions here.
Post Reply
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

DIM-ing a constant string

Post by fzabkar »

I would like to Dim a constant 512-byte string consisting of 256 repetitions of "Er".

This pseudocode is essentially what I want:

Code: Select all

Dim sErrorSec as Const String * 512 = "Er" * 256
... but this is the only code I can come up with:

Code: Select all

Dim sErrorSec As String
Dim i As uByte

sErrorSec = "Er"

For i = 1 To 8

  sErrorSec = sErrorSec & sErrorSec

Next i

print sErrorSec
sleep
I have the feeling that the solution is obvious, but I just can't see it.

Thanks for any help.
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: DIM-ing a constant string

Post by coderJeff »

There is no builtin function to do this.

But you can kind of make one by repeating 4 times by 4 times (4 ^ 4 = 256)

Code: Select all

#define REP4(s)   ((s)+(s)+(s)+(s))
#define REP256(s) REP4(REP4(REP4(REP4(s)))) 

Const TwoChars = "Er"

'' constant string (like a literal)
const c as string = REP256( TwoChars )
print c
print len(c)

'' fixed length string that promises to be constant
dim fc as const string * 512 = REP256( TwoChars )
print fc
print len(fc)

'' variable length string that promises to be constant 
dim vc as const string = REP256( TwoChars )
print vc
print len(vc)
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: DIM-ing a constant string

Post by fzabkar »

That's very clever. Many thanks once again.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: DIM-ing a constant string

Post by dodicat »

This works also:

Code: Select all

 
function MakePair(pair as const string) as const string 
    dim as  ubyte u(511)
    for n as long=0 to 511
        if n mod 2=0 then u(n)=pair[0] else u(n)=pair[1]
    next
    return *cast(zstring ptr,@u(0))
end function

dim as const string * 512 a=MakePair("Er")

dim as const string * 512 b=MakePair("Fb")

dim as const string * 512 c=MakePair("Ok")

print a
print b
print c
print len(a),len(b),len(c)
'not allowed:
'a=b
'b="y"
'c="z"


sleep
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: DIM-ing a constant string

Post by fxm »

A predefined 'String()' function exists:
Declare Function String(ByVal count As Integer, ByRef ch As Const String) As String
but it only takes into account the first character of 'ch'.

The user can define his own '_String_()' function, but which takes into account all the characters of 'ch':

Code: Select all

Function _String_(ByVal count As Integer, ByRef ch As Const String) As String
    Dim As String s = String(count * Len(ch), 0)
    For i As Integer = 0 To (count - 1) * Len(ch) Step Len(ch)
        StrPtr(s)[i] = ch
    Next i
    Return s
End Function

Dim As String s = _String_(256, "Er")
Print Len(s)
Print s

Sleep
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: DIM-ing a constant string

Post by dodicat »

Because this is in the beginner's section, this task is done without using pointers.

Code: Select all

Function Set(n As Long,Chars As String,mode As boolean=false) As String
    If mode=true Then n*=Len(Chars)
    Var c=String(n,0),count=-1
    For m As Long=0 To n-1 Step Len(Chars)
        For k As Long=0 To Len(Chars)-1
            count+=1
            If count>n-1 Then Exit For,For
            c[count]=Chars[k]
        Next k
    Next
    Return c 
End Function

'mode false = absolute number of characters.
'mode true = number of groups of characters.

Dim As Const String g=Set(200,"Free") '200 characters
Print g
Print Len(g)

Dim As Const String * 512 k=Set(Len(k),"Er")'512 characters
Print k
Print Len(k)

Dim As Const String z=Set(12,"0123456789"+Chr(10),true)'12 groups on a new line each (chr(10))
Print z
Print Len(z)," (12 X 11)"
' not allowed
'g="1"
'k="2"
'z="3"
Sleep 
Happy New Year all when it arrives (at various times for forum members), but sometime today.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: DIM-ing a constant string

Post by TJF »

fzabkar wrote: Dec 28, 2023 19:05 I would like to Dim a constant 512-byte string consisting of 256 repetitions of "Er".
Why do you think you'll need such a CONST STING?

From my point of view, there's an issue in your concept.

Regards
Post Reply