|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > String commands > !STR.- String Command > BASIC-String Commands > String Operations |
MiniRobotLanguage (MRL)
STR.Repeat
Repeat a String N Times

All Tests passed.
Intention
This command creates a string consisting of multiple copies of a specified string (P1) repeated P2 times.
The result is placed on the Top of Stack (TOS) or in an optional variable (P3).
This command is similar to PowerBASIC's REPEAT$ function. If P2 is 0 or negative, returns an empty string. The command is binary-safe for P1, with variables resolved only once to prevent unwanted expansion.
This command is useful for creating padding, separators, or repeated patterns in text processing.
Schematic
Source: "abc", Count: 3 --> "abcabcabc"
|a|b|c|a|b|c|a|b|c|
^ (3 copies of "abc")
Syntax
STR.Repeat|P1|P2[|P3]
Parameter Explanation
•P1 - (String to Repeat) The string to duplicate. Variable or literal. Binary-compatible variable resolution.
•P2 - (Count) Numeric, number of times to repeat P1. If <= 0, returns empty string.
•P3 - (Optional Result Variable) Variable for result. If omitted, pushed to TOS.
Examples
' Repeat "abc" 3 times
$$STR=abc
$$CNT=3
STR.Repeat|$$STR|$$CNT
$$RES=$tos$
' $$RES = "abcabcabc"
' Repeat single character
STR.Repeat|*|5|$$STAR
' $$STAR = "*****"
' Zero count returns empty string
STR.Repeat|abc|0
$$EMPTY=$tos$
' $$EMPTY = ""
' Negative count returns empty string
STR.Repeat|xyz|-2|$$NEG
' $$NEG = ""
'-------------------------------------------------------------
' =================================================================
' SELF-VALIDATING TEST SCRIPT for STR.Repeat (Repeat String N Times)
' Purpose: Verify string repetition with various counts and edge cases.
' Uses short jumps (JIV.) and clear PRT. for readability.
' =================================================================
' Initialize counters
$$PAS=0
$$FAI=0
STS.CLEAR
PRT. ===================================================
PRT. 1. BASIC REPETITION TESTS
PRT. ===================================================
PRT. Test 1.1: Repeat "abc" 3 times
$$STR=abc
$$CNT=3
STR.Repeat|$$STR|$$CNT
$$EXP=abcabcabc
JIV.$tos$!$$EXP|Lab_Error1
PRT. -> PASS
VIC.$$PAS
JMP.Lab_Next1
:Lab_Error1
$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)
PRT.$$MSG
VIC.$$FAI
:Lab_Next1
STS.CLEAR
PRT. Test 1.2: Repeat single character 5 times
STR.Repeat|*|5
$$EXP=*****
JIV.$tos$!$$EXP|Lab_Error2
PRT. -> PASS
VIC.$$PAS
JMP.Lab_Next2
:Lab_Error2
$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)
PRT.$$MSG
VIC.$$FAI
:Lab_Next2
STS.CLEAR
PRT. Test 1.3: Repeat with variable result
$$STR=xyz
$$CNT=2
STR.Repeat|$$STR|$$CNT|$$RES
$$EXP=xyzxyz
JIV.$$RES!$$EXP|Lab_Error3
PRT. -> PASS
VIC.$$PAS
JMP.Lab_Next3
:Lab_Error3
$$MSG= -> FAIL - Result: $$RES (exp: $$EXP)
PRT.$$MSG
VIC.$$FAI
:Lab_Next3
STS.CLEAR
PRT. Test 1.4: Multi-character string repetition
STR.Repeat|hello |3
$$EXP=hello hello hello
JIV.$tos$!$$EXP|Lab_Error4
PRT. -> PASS
VIC.$$PAS
JMP.Lab_Next4
:Lab_Error4
$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)
PRT.$$MSG
VIC.$$FAI
:Lab_Next4
STS.CLEAR
PRT. ===================================================
PRT. 2. EDGE CASE TESTS
PRT. ===================================================
PRT. Test 2.1: Zero count returns empty string
STR.Repeat|abc|0
$$EXP=
JIV.$tos$!$$EXP|Lab_Error5
PRT. -> PASS
VIC.$$PAS
JMP.Lab_Next5
:Lab_Error5
$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)
PRT.$$MSG
VIC.$$FAI
:Lab_Next5
STS.CLEAR
PRT. Test 2.2: Negative count returns empty string
STR.Repeat|xyz|-2
$$EXP=
JIV.$tos$!$$EXP|Lab_Error6
PRT. -> PASS
VIC.$$PAS
JMP.Lab_Next6
:Lab_Error6
$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)
PRT.$$MSG
VIC.$$FAI
:Lab_Next6
STS.CLEAR
PRT. Test 2.3: Empty string to repeat
STR.Repeat||5
$$EXP=
JIV.$tos$!$$EXP|Lab_Error7
PRT. -> PASS
VIC.$$PAS
JMP.Lab_Next7
:Lab_Error7
$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)
PRT.$$MSG
VIC.$$FAI
:Lab_Next7
STS.CLEAR
PRT. Test 2.4: Count of 1 returns original string
STR.Repeat|hello|1
$$EXP=hello
JIV.$tos$!$$EXP|Lab_Error8
PRT. -> PASS
VIC.$$PAS
JMP.Lab_Next8
:Lab_Error8
$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)
PRT.$$MSG
VIC.$$FAI
:Lab_Next8
STS.CLEAR
PRT. Test 2.5: Large count (100) with single char
STR.Repeat|A|100
STR.String|A|100|$$EXP
JIV.$tos$!$$EXP|Lab_Error9
PRT. -> PASS
VIC.$$PAS
JMP.Lab_Next9
:Lab_Error9
$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)
PRT.$$MSG
VIC.$$FAI
:Lab_Next9
STS.CLEAR
PRT. ===================================================
PRT. 3. BINARY DATA TEST
PRT. ===================================================
PRT. Test 3.1: Repeat binary string with null bytes
$$STR=$nul$$sp$A
STR.Repeat|$$STR|2
$$EXP=$nul$$sp$A$nul$$sp$A
JIV.$tos$!$$EXP|Lab_Error10
PRT. -> PASS
VIC.$$PAS
JMP.Lab_Next10
:Lab_Error10
$$MSG= -> FAIL - Result: $tos$ (exp: $$EXP)
PRT.$$MSG
VIC.$$FAI
:Lab_Next10
STS.CLEAR
PRT. ===================================================
PRT. TEST SUMMARY
PRT. ===================================================
CAL.$$TOT=$$PAS+$$FAI
$$MSG=Total Tests: $$TOT
PRT.$$MSG
$$MSG=Passed: $$PAS
PRT.$$MSG
$$MSG=Failed: $$FAI
PRT.$$MSG
' Display final result
JIV.$$FAI=0|Lab_Success
$$MSG=FAILURE: $$FAI of $$TOT tests failed.
MBX.$$MSG|Test Result|16
JMP.Lab_End
:Lab_Success
MBX.SUCCESS: All tests passed!|Test Result|64
:Lab_End
ENR.
Remarks
-Binary-safe for P1; variables resolved only once to avoid unwanted expansion.
-P2 is resolved numerically; non-numeric values may cause errors.
-If P2 <= 0, returns empty string regardless of P1.
-Similar to BASIC's REPEAT$ function.
-No stack output if P3 is provided; use $tos$ to access TOS.
Limitations
-P2 must be numeric; invalid values may cause undefined behavior.
-No limit on repetition count, but memory constraints apply.
-Only repeats the entire string in P1; no substring repetition.
See also:
STR.STRING - Repeat character N times