In the last post I showed a function to trim a string. In this post I go the other way and demonstrate how to pad one. This is useful when trying to print data in alignment.
The Function
This function was primarily written to left pad numbers. As such there is a limit of 10 in length which is longer than the longest Action! number. The code is again pretty well documented and will be including in the next release of my Action! library. Here is short summary of how it works.
The procedure is called with 3 parameters. The first is a pointer to a character array, which can not be a static string and must be a variable because the length will be altered. The second parameter is the character you want to pad the string with. Common values are space and zero. The third parameter is the length to pad to (no more than 10).
First the procedure fills a temporary string with 10 of the desired pad character. Next it copies the incoming string into the padded temporary string at the furthest right position to accommodate the length of the incoming string, essentially right justifying it. Next the temporary padded string is copied back to the incoming string pointer, and the incoming string length is set to the specified pad length.
; -------------------------------------- ; Proc..: StrPad(CHAR POINTER pS CHAR bc BYTE bL) ; Param.: pS=Pointer to string to pad ; bC=Character to pad with ; bL=Length to pad to ; Desc..: Left pads a string with a char ; Notes.: Max of 10 in length. ; -------------------------------------- PROC StrPad(CHAR POINTER pS CHAR bC BYTE bL) ; Declare a string filled with 10 spaces CHAR ARRAY pA=" " ; Fill the temp string with desired char ; Use pA+1 which is the first character of the string. ; pA is the length of the string so skip over it. SetBlock(pA+1, 10, bC) ; Copy incoming string into temp string ; Use desired length - incoming string length + 1 as position in temp string ; which puts incoming string at correct rightmost spot to accommodate its length. SAssign(pA, pS, bL-pS(0)+1, bL) ; Copy newly padded temp string to incoming string SCopy(pS, pA) ; Set incoming string pointer to new length pS(0)=bL RETURN
Sample Usage
Here is a short program demonstrating it usage. Note that in this example the StrPad() function is not seen as its been incorporated into the LIBSTR.ACT file as part of my Action! library. This will pad a byte, a card, and int to various lengths.
INCLUDE "D3:DEFINES.ACT" INCLUDE "D3:LIBSTR.ACT" PROC Main() ; Declare a byte, a card, an integer, and character array of 10 in length BYTE bnum CARD cnum INT inum CHAR ARRAY snum(10) ; Pad single character length byte to 2 positions with 0 character bnum=1 ; Convert the number into a string StrB(bnum,snum) ; Call the pad function, passing the string, the pad character, and pad length StrPad(snum, '0, 2) ; Show result PrintF("Num: (2) [.....1]->[%S]%E",snum) ; Pad two character length byte to 4 positions with 0 character bnum=21 ; Convert the number into a string StrB(bnum,snum) ; Call the pad function, passing the string, the pad character, and pad length StrPad(snum, '0, 4) ; Show result PrintF("Num: (4) [....21]->[%S]%E",snum) ; Pad five character length card to 5 positions with 0 character cnum=64256 ; Convert the number into a string StrC(cnum,snum) ; Call the pad function, passing the string, the pad character, and pad length StrPad(snum, '0, 5) ; Show result PrintF("Num: (5) [.64256]->[%S]%E",snum) ; Pad five character length int to 8 positions with space character inum=24768 ; Convert the number into a string StrI(inum,snum) ; Call the pad function, passing the string, the pad character, and pad length StrPad(snum, 32, 8) ; Show result PrintF("Num: (8) [.24768]->[%S]%E",snum) ; Pad six character length negative int to 8 positions with space character inum=-24768 ; Convert the number into a string StrI(inum,snum) ; Call the pad function, passing the string, the pad character, and pad length StrPad(snum, 32, 8) ; Show result PrintF("Num: (8) [-24768]->[%S]%E",snum) RETURN
Output
Here is the output showing that it works: