- ALLTRIM(Expression [, nFlags] [, cParseChar [, cParseChar2 [, ...]]])
- LTRIM(Expression [, nFlags] [, cParseChar [, cParseChar2 [, ...]]])
- RTRIM(cExpression [, nFlags] [, cParseChar [, cParseChar2 [, ...]]])
Testing (but no stress test):
Code: Select all
Procedure Trim_Test ()
? 'Alltrim(" ") == ""', IIF(Alltrim(" ") == "", "OK", "Fail")
? 'Alltrim(e" n ", 0, " ", CHR(9), CHR(10), CHR(13)) == ""', IIF(Alltrim(e" n ", 0, " ", CHR(9), CHR(10), CHR(13)) == "", "OK", "Fail")
? 'Alltrim("Test ") == "Test"', IIF(Alltrim("Test ") == "Test", "OK", "Fail")
? 'Alltrim(e"Test n ", 0, " ", CHR(9), CHR(10), CHR(13)) == "Test"', IIF(Alltrim(e"Test n ", 0, " ", CHR(9), CHR(10), CHR(13)) == "Test", "OK", "Fail")
? 'Alltrim("TeTeTest", 0, "te") == "TeTeTest"', IIF(Alltrim("TeTeTest", 0, "te") == "TeTeTest", "OK", "Fail")
? 'Alltrim("TeTeTest", 1, "te") == "st"', IIF(Alltrim("TeTeTest", 1, "te") == "st", "OK", "Fail")
? 'Alltrim("TeTeTest", 0, "t", "e") == "TeTeTes"', IIF(Alltrim("TeTeTest", 0, "t", "e") == "TeTeTes", "OK", "Fail")
? 'Alltrim("TeTeTest", 1, "t", "e") == "s"', IIF(Alltrim("TeTeTest", 1, "t", "e") == "s", "OK", "Fail")
? 'Alltrim("***abc*", 0, "*") == "abc"', IIF(Alltrim("***abc*", 0, "*") == "abc", "OK", "Fail")
? 'Ltrim("***abc*", 0, "*") == "abc*"', IIF(Ltrim("***abc*", 0, "*") == "abc*", "OK", "Fail")
? 'Rtrim("***abc*", 0, "*") == "***abc"', IIF(Rtrim("***abc*", 0, "*") == "***abc", "OK", "Fail")
? 'Alltrim("abcdefghab", 1, "AB", "B") == "cdefgh"', IIF(Alltrim("abcdefghab", 1, "AB", "B") == "cdefgh", "OK", "Fail")
? 'Alltrim("abcdefghab", 1, "B", "AB") == "cdefgha"', IIF(Alltrim("abcdefghab", 1, "B", "AB") == "cdefgha", "OK", "Fail")
// ? 'ISNULL(Rtrim(NULL))', IIF(ISNULL(Rtrim(NULL)), "OK", "Fail")
End Proc
The functions set:
Code: Select all
Function Alltrim (Expression AS String) AS String
Return _trim(.T., .T., Expression, 0, " ")
End Function
Function Alltrim (Expression AS String, Flags AS Int, TrimChars PARAMS String[]) AS String STRICT
Return _trim(.T., .T., Expression, Flags, TrimChars)
End Function
Function Ltrim (Expression AS String) AS String
Return _trim(.T., .F., Expression, 0, " ")
End Function
Function Ltrim (Expression AS String, Flags AS Int, TrimChars PARAMS String[]) AS String STRICT
Return _trim(.T., .F., Expression, Flags, TrimChars)
End Function
Function Rtrim (Expression AS String) AS String
Return _trim(.F., .T., Expression, 0, " ")
End Function
Function Rtrim (Expression AS String, Flags AS Int, TrimChars PARAMS String[]) AS String STRICT
Return _trim(.F., .T., Expression, Flags, TrimChars)
End Function
STATIC Function _trim (TrimLeft AS Boolean, TrimRight AS Boolean, Expression As String, Flags As Int, TrimChars PARAMS String[]) AS String STRICT
LOCAL parmNdx AS Int
LOCAL Trimmed = .T. AS Boolean
LOCAL LRTrimmed AS Int
LOCAL comparison = StringComparison.Ordinal
LOCAL compared AS String
IF Expression = NULL
Return Expression
END IF
IF Flags = 1
comparison = StringComparison.OrdinalIgnoreCase
END IF
DO WHILE Trimmed
Trimmed = .F.
FOR parmNdx = 1 TO TrimChars:Length
IF TrimLeft
LRTrimmed = 0
compared = TrimChars[parmNdx]
DO WHILE Expression:StartsWith(compared, comparison)
LRTrimmed = compared:Length
compared = String.Concat(compared, TrimChars[parmNdx])
END DO
IF LRTrimmed > 0
Expression = Expression:Substring(LRTrimmed)
Trimmed = .T.
END IF
END IF
IF TrimRight
LRTrimmed = 0
compared = TrimChars[parmNdx]
DO WHILE Expression:EndsWith(compared, comparison)
LRTrimmed = compared:Length
compared = String.Concat(compared, TrimChars[parmNdx])
END DO
IF LRTrimmed > 0
Expression = Expression:Substring(0, Expression:Length - LRTrimmed)
Trimmed = .T.
END IF
END IF
NEXT
END DO
Return Expression
End Function