[futurebasic] Ken's clones

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2001 : Group Archive : Group : All Groups

From: Jay Reeve <jktr@...>
Date: Thu, 18 Oct 01 02:04:38 -0500
Ken,

I enjoyed your tirade (good point, very well made) and your clone fns. You know my passion for optimizing these little gems--I couldn't resist redoing them in my style.

Some may find these a bit less readable. I don't, but amplifying some var names could help that. In any case, they offer faster execution, especially when using register vars, and IMHO show off to advantage some FB^3's new capacities.

I made a change in the split fn, so that it creates a dynamic array of all the delimited elements and returns the index of the last array member. I think this gives better flexibility. If one merely wants to extract one delimited element, there's no need to even create the array.

Enjoy,
 0"0
 =J= a  y
  "
NOTE: Each of these contains a working demo. They need to be separated, the way Ken did, to run. (I used #IF 0...#ENDIF, but didn't include it here.)

'~trimAllSpaces (removes all spaces from a string)

LOCAL FN trimAllSpaces$( textStr AS STR255 )
DIM AS INTEGER c1, c2, L

L  = textStr[0]
c2 = 0

FOR c1 = 1 TO L

long if textStr[c1] <> _" "
c2 ++
textStr[c2] = textStr[c1]
end if

next
textStr[0] = c2

END FN = textStr

DIM myStr AS STR255

myStr  = "I wonder how many spaces will be    "
myStr += "trimmed from    this string  ! ? ! ? !  "

PRINT:Print "FN trimAllSpaces$ all the spaces in this string:"
PRINT FN trimAllSpaces$( myStr )

'~StrReverse Clone (reverses a string)

LOCAL FN StrReverse$( theStr AS STR255 )
DIM i1, i2, temp

i1 = theStr[0] / 2
i2 = theStr[0] - i1 + 1
while i1
temp       = theStr[i1]
theStr[i1] = theStr[i2]
theStr[i2] = temp
i1 --
i2 ++
wend

END FN = theStr

DIM myStr AS STR255

PRINT:Print "FN StrReverse$ reversed the letters these strings:"
myStr = "Even number of chars"
PRINT FN StrReverse$( myStr )

myStr = "Odd number of chars"
PRINT FN StrReverse$( myStr )

'~splitWords
BEGIN GLOBALS
dynamic splitArray(255) AS STR255
END GLOBALS

LOCAL FN Split( strToSplit AS STR25, splitChar AS STR15 )
DIM AS INTEGER splits, i, delim, L

L      = strToSplit[0]
delim  = splitChar[1]
splits = 0
poke @splitArray(splits), 0

FOR i = 1 TO L
LONG IF strToSplit[i] = delim
splits ++
poke @splitArray(splits), 0
xelse
splitArray(splits) = splitArray(splits) + chr$(strToSplit[i])
END IF
NEXT i
compress dynamic splitArray
END FN = splits

DIM myStr    as STR255
DIM delimStr AS STR15
DIM i        AS INT
DIM t        AS INT

myStr = "Alain,Jonathan,Steve,Robert,Chris,Paul"
delimStr = ","

PRINT:PRINT "Using FN Split$ we can create an array ¬
from this comma-delimited string:"
PRINT:PRINT myStr
PRINT:PRINT "Here are the array elements from the string:"
PRINT
t = FN Split( myStr, delimStr )
FOR i = 0 TO t
PRINT i;"> ";splitArray(i)
NEXT i

PRINT:Print "A single element can be pulled from the array like this:
PRINT:PRINT "splitArray(2)"
PRINT:PRINT splitArray(2)

myStr = "12:32:17"
delimStr = ":"
PRINT:PRINT myStr
PRINT:PRINT "Here are the array elements from the string:"
PRINT
t = FN Split( myStr, delimStr )
FOR i = 0 TO t
PRINT i;"> ";splitArray(i)
NEXT i

'~TRIM Clone (removes leading and trailing spaces)

LOCAL FN TRIM$( trimStr AS STR255 )
dim c, L

L = trimStr[0]
WHILE trimStr[L] = _" "
L --
WEND

c = 1
WHILE trimStr[c] = _" "
c ++
WEND

trimStr[0] = L
trimStr    = mid$(trimStr,c)
END FN = trimStr

DIM myStr AS STR255

myStr = "        Hello, how are you?        "'Notice leading and trailing spaces

PRINT:Print "FN TRIM$ removed the leading and trailing spaces in this string:"
print """";myStr;""""
PRINT """";FN TRIM$( myStr );""""

'~RTRIM Clone (removes trailing spaces)

LOCAL FN RTRIM$( rTrimStr AS STR255 )
dim L

L = rTrimStr[0]
WHILE rTrimStr[L] = _" "
L --
WEND
rTrimStr[0] = L
END FN = rTrimStr

DIM myStr AS STR255

myStr = "Hello, how are you?        "'Notice trailing spaces

PRINT:Print "FN RTRIM$ removed the trailing spaces in this string:"
print """";myStr;""""
PRINT """";FN RTRIM$( myStr );""""

'~LTRIM Clone (removes leading spaces)

LOCAL FN LTRIM$( lTrimStr AS STR255 )
dim c,L

L = lTrimStr[0]
c = 1
for c = 1 to L
long if lTrimStr[c] <> _" "
ltrimStr = mid$(ltrimStr,c)
exit fn
end if
next
lTrimStr[0] = 0
END FN = lTrimStr

DIM myStr AS STR255

myStr = "        Hello, how are you?"'Notice leading spaces

PRINT:Print "FN LTRIM$ removed the leading spaces in this string:"
print """";myStr;""""
PRINT """";FN LTRIM$( myStr );""""