Skip to content

Commit 6e45f1e

Browse files
committed
Add RTRIM() and TRIM functions
TRIM is a composition of LTRIM(RTRIM())
1 parent 6d0ae3a commit 6e45f1e

2 files changed

Lines changed: 85 additions & 3 deletions

File tree

src/arch/zx48k/library/string.bas

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ end function
242242
' ----------------------------------------------------------------
243243
' function ltrim(ByVal s$, rep$)
244244
'
245-
' Returns a copy of b$, removing all occurrences of rep$ on the
245+
' Returns a copy of s$, removing all occurrences of rep$ on the
246246
' left side (beginning) of s$. For example:
247-
'
247+
' ltrim(": Hello world", ": ") returns "Hello World"
248248
' ----------------------------------------------------------------
249249
function ltrim(ByVal s$, ByVal rep$) as String
250250
DIM i as Uinteger = 0
@@ -264,6 +264,47 @@ function ltrim(ByVal s$, ByVal rep$) as String
264264
end function
265265

266266

267+
268+
' ----------------------------------------------------------------
269+
' function rtrim(ByVal s$, rep$)
270+
'
271+
' Returns a copy of s$, removing all occurrences of rep$ on the
272+
' right side (ending) of s$. For example:
273+
' rtrim("Hello world. ", ". ") returns "Hello World"
274+
' ----------------------------------------------------------------
275+
function rtrim(ByVal s$, ByVal rep$) as String
276+
DIM i as Integer
277+
DIM d, l2 as Uinteger
278+
DIM l as Uinteger = len(rep$)
279+
280+
l2 = len(s$)
281+
if not l or l2 < l then return s$
282+
283+
d = l - 1
284+
i = l2 - 1
285+
286+
while i >= d and rep = s(i - d to i)
287+
i = i - l
288+
end while
289+
290+
if i < 0 then return ""
291+
292+
return s$(to i)
293+
end function
294+
295+
296+
297+
' ----------------------------------------------------------------
298+
' function trim(ByVal s$, rep$)
299+
'
300+
' Returns a copy of s$, removing all occurrences of rep$ on the
301+
' left and right side (ending) of s$. For example:
302+
' rtrim(";.;.Hello world;.;.", ";.") returns "Hello World"
303+
' ----------------------------------------------------------------
304+
function trim(ByVal s$, ByVal rep$) as String
305+
return ltrim(rtrim(s$, rep$), rep$)
306+
end function
307+
267308
#undef __MAX_LEN__
268309

269310
#pragma pop(string_base)

src/arch/zxnext/library/string.bas

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ end function
242242
' ----------------------------------------------------------------
243243
' function ltrim(ByVal s$, rep$)
244244
'
245-
' Returns a copy of b$, removing all occurrences of rep$ on the
245+
' Returns a copy of s$, removing all occurrences of rep$ on the
246246
' left side (beginning) of s$. For example:
247247
' ltrim(": Hello world", ": ") returns "Hello World"
248248
' ----------------------------------------------------------------
@@ -264,6 +264,47 @@ function ltrim(ByVal s$, ByVal rep$) as String
264264
end function
265265

266266

267+
268+
' ----------------------------------------------------------------
269+
' function rtrim(ByVal s$, rep$)
270+
'
271+
' Returns a copy of s$, removing all occurrences of rep$ on the
272+
' right side (ending) of s$. For example:
273+
' rtrim("Hello world. ", ". ") returns "Hello World"
274+
' ----------------------------------------------------------------
275+
function rtrim(ByVal s$, ByVal rep$) as String
276+
DIM i as Integer
277+
DIM d, l2 as Uinteger
278+
DIM l as Uinteger = len(rep$)
279+
280+
l2 = len(s$)
281+
if not l or l2 < l then return s$
282+
283+
d = l - 1
284+
i = l2 - 1
285+
286+
while i >= d and rep = s(i - d to i)
287+
i = i - l
288+
end while
289+
290+
if i < 0 then return ""
291+
292+
return s$(to i)
293+
end function
294+
295+
296+
297+
' ----------------------------------------------------------------
298+
' function trim(ByVal s$, rep$)
299+
'
300+
' Returns a copy of s$, removing all occurrences of rep$ on the
301+
' left and right side (ending) of s$. For example:
302+
' rtrim(";.;.Hello world;.;.", ";.") returns "Hello World"
303+
' ----------------------------------------------------------------
304+
function trim(ByVal s$, ByVal rep$) as String
305+
return ltrim(rtrim(s$, rep$), rep$)
306+
end function
307+
267308
#undef __MAX_LEN__
268309

269310
#pragma pop(string_base)

0 commit comments

Comments
 (0)