Not much else needs said I think

Code: Select all
Procedure.s MSet(text$,length,char$=" ")
t=Len(text$)
If t>0 And length>t
pad=(length-t)/2
char$=Left(char$,1)
text$=RSet(char$,pad,char$)+text$+LSet(char$,pad,char$)
If Len(text$)<length
text$+char$
EndIf
EndIf
ProcedureReturn text$
EndProcedure
Debug "123456789.123456789.123456789.123456789.123456789."
Debug MSet("pad this to 50 chars",50,"-")
Code: Select all
123456789.123456789.123456789.123456789.123456789.
---------------pad this to 50 chars---------------
nice!BarryG wrote: Tue Feb 25, 2025 2:21 am Good idea!I propose the name MSet(), with the "m" for "middle", to match LSet() and RSet().
A workaround for now:
Output:Code: Select all
Procedure.s MSet(text$,length,char$=" ") t=Len(text$) If t>0 And length>t pad=(length-t)/2 char$=Left(char$,1) text$=RSet(char$,pad,char$)+text$+LSet(char$,pad,char$) If Len(text$)<length text$+char$ EndIf EndIf ProcedureReturn text$ EndProcedure Debug "123456789.123456789.123456789.123456789.123456789." Debug MSet("pad this to 50 chars",50,"-")
Code: Select all
123456789.123456789.123456789.123456789.123456789. ---------------pad this to 50 chars---------------
True, that's why I like BarryG's naming suggestion. Will update the topic with it
Nice function! Thanks for sharingBarryG wrote: Tue Feb 25, 2025 2:21 am Good idea!I propose the name MSet(), with the "m" for "middle", to match LSet() and RSet().
A workaround for now:
Output:Code: Select all
Procedure.s MSet(text$,length,char$=" ") t=Len(text$) If t>0 And length>t pad=(length-t)/2 char$=Left(char$,1) text$=RSet(char$,pad,char$)+text$+LSet(char$,pad,char$) If Len(text$)<length text$+char$ EndIf EndIf ProcedureReturn text$ EndProcedure Debug "123456789.123456789.123456789.123456789.123456789." Debug MSet("pad this to 50 chars",50,"-")
Code: Select all
123456789.123456789.123456789.123456789.123456789. ---------------pad this to 50 chars---------------
Code: Select all
Procedure.s MSet(text$,length,char$=" ")
Protected l2 = (length/2) - (Len(text$)/2)
ProcedureReturn (LSet("",l2,char$)+Left(text$,length)+LSet("",l2,char$))
EndProcedure
txt$=mset("Hello World",35,"*")
Debug txt$
Debug Len(txt$)
txt$=mset("Hello World",5,"*")
Debug txt$
Debug Len(txt$)
Code: Select all
Debug LSet("",-3," ") ; No problem
Debug Space(-3) ; Debugger warning/error
Whoa, that's cool. The more you know! Thanks for sharingbenubi wrote: Tue Feb 25, 2025 1:11 pm Just an other "branchless" version.
LSet/Rset has no problem accepting negative values, that does the little "trick" above.Code: Select all
Procedure.s MSet(text$,length,char$=" ") Protected l2 = (length/2) - (Len(text$)/2) ProcedureReturn Left((LSet("",l2,char$)+Left(text$,length)+LSet("",l2,char$)),length) EndProcedure txt$=mset("Hello World",35,"*") Debug txt$ Debug Len(txt$) txt$=mset("Hello World",5,"*") Debug txt$ Debug Len(txt$)
Code: Select all
Debug LSet("",-3," ") ; No problem Debug Space(-3) ; Debugger warning/error
Code: Select all
Procedure.s MSet(text$,length,char$=" ")
Protected l2 = (length/2) - (Len(text$)/2)
ProcedureReturn (LSet("",l2,char$)+Left(text$,length)+LSet("",l2,char$))
EndProcedure
Procedure.s MSet2(text$,length,char$=" ")
Protected l2 = (length/2) - (Len(text$)/2)
Protected m2 = 1 + (Bool(l2<0)*l2*-1)
ProcedureReturn LSet("",l2,char$)+Mid(text$,m2,length)+LSet("",l2,char$)
EndProcedure
txt$=mset2("Hello World",35,"*")
Debug txt$
Debug Len(txt$)
txt$=mset2("Hello World",5,"*")
Debug txt$
Debug Len(txt$)
benubi wrote: Tue Feb 25, 2025 1:16 pmCode: Select all
Procedure.s MSet(text$,length,char$=" ") Protected l2 = (length/2) - (Len(text$)/2) ProcedureReturn (LSet("",l2,char$)+Left(text$,length)+LSet("",l2,char$)) EndProcedure Procedure.s MSet2(text$,length,char$=" ") Protected l2 = (length/2) - (Len(text$)/2) Protected m2 = 1 + (Bool(l2<0)*l2*-1) ProcedureReturn LSet("",l2,char$)+Mid(text$,m2,length)+LSet("",l2,char$) EndProcedure txt$=mset2("Hi",35,"*") Debug txt$ Debug Len(txt$) ; 34 <- BUG txt$=mset2("Hi",5,"*") Debug txt$ Debug Len(txt$) ; 4 <- BUG
Code: Select all
Procedure.s MSet2(text$,length,char$=" ")
Protected l2 = (length/2) - (Len(text$)/2)
Protected m2 = 1 + (Bool(l2<0)*l2*-1)
ProcedureReturn LSet("",l2,char$)+Mid(text$,m2,length)+LSet("",l2 + (length & 1),char$) ;
EndProcedure
txt$=mset2("Hi",35,"*")
Debug txt$
Debug Len(txt$) ;
txt$=mset2("Hi",5,"*")
Debug txt$
Debug Len(txt$) ;
txt$=mset2("Hi",32,"*")
Debug txt$
Debug Len(txt$) ;
txt$=mset2("Hi",3,"*")
Debug txt$
Debug Len(txt$) ;
Code: Select all
Procedure.s MSet3(text$,length,char$=" ")
Protected l2 = (length/2) - (Len(text$)/2)
Protected m2 = (Bool(l2<0)*l2*-1)* SizeOf(Character)
Protected out$ = LSet("",length,char$)
Protected *Z1.character = @text$ + m2
Protected *Z2.character = @out$ + (Bool(l2>0)*l2*SizeOf(Character))
While *Z2\c And *Z1\c
*Z2\c = *Z1\c
*Z1+SizeOf(Character)
*Z2+SizeOf(Character)
Wend
ProcedureReturn out$
EndProcedure
txt$=mset3("Hi",35,"*")
Debug txt$
Debug Len(txt$) ;
txt$=mset3("Hi",5,"*")
Debug txt$
Debug Len(txt$) ;
txt$=mset3("Hi",32,"*")
Debug txt$
Debug Len(txt$) ;
txt$=mset3("Hi",2,"*")
Debug txt$
Debug Len(txt$) ;
Can confirm, incredibly impressive work. Gives me something to work towards in terms of my eventual PB skill level
Not quite Barry, MSet3() is the only one that works. MSet() and MSet2() fail in the below cases.
Code: Select all
Procedure.s MSet(text$,length,char$=" ")
Protected l2 = (length/2) - (Len(text$)/2)
ProcedureReturn (LSet("",l2,char$)+Left(text$,length)+LSet("",l2,char$))
EndProcedure
Procedure.s MSet2(text$,length,char$=" ")
Protected l2 = (length/2) - (Len(text$)/2)
Protected m2 = 1 + (Bool(l2<0)*l2*-1)
ProcedureReturn LSet("",l2,char$)+Mid(text$,m2,length)+LSet("",l2 + (length & 1),char$) ;
EndProcedure
Procedure.s MSet3(text$,length,char$=" ")
Protected l2 = (length/2) - (Len(text$)/2)
Protected m2 = (Bool(l2<0)*l2*-1)* SizeOf(Character)
Protected out$ = LSet("",length,char$)
Protected *Z1.character = @text$ + m2
Protected *Z2.character = @out$ + (Bool(l2>0)*l2*SizeOf(Character))
While *Z2\c And *Z1\c
*Z2\c = *Z1\c
*Z1+SizeOf(Character)
*Z2+SizeOf(Character)
Wend
ProcedureReturn out$
EndProcedure
Debug "123456789.123456789."
Debug MSet("ABC",8,"-") ; Fails
Debug "123456789.123456789."
Debug MSet("ABCD",7,"-") ; Fails
Debug "===================="
Debug "123456789.123456789."
Debug MSet2("ABC",8,"-") ; Fails
Debug "123456789.123456789."
Debug MSet2("ABCD",7,"-")
Debug "===================="
Debug "123456789.123456789."
Debug MSet3("ABC",8,"-")
Debug "123456789.123456789."
Debug MSet3("ABCD",7,"-")