[futurebasic] REPLACE clone

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

From: Ken Shmidheiser <k.shmidheiser@...>
Date: Fri, 19 Oct 2001 01:04:39 -0400
There are many ways to skin a cat.

Below are four clones of the REPLACE function found in some 
programming languages. REPLACE simply allows you to select a letter, 
word or substring from a string and replace it with another or to 
delete it. It's a very handy function in a text-editing application.

There a many ways to do this with FB, including the Toolbox FN Munger.

However, the reasons I'm posting these is to:

1.) See what speed and streamline improvements Jay and Alain will make   :-)

2.) As a brainstorming device for using these-- or possibly something 
entirely different and better--as a basis to create a 
search-and-replace engine for an FB^3 CONTAINER. (Imagine dumping a 
long document into a CONTAINER and performing a search-and-replace or 
spell check on it there, virtually limited only by memory.)

3.) In hopes that some day perhaps this form of function will become available:

LOCAL FN operateOnContainer$$( expressionOne, expressionTwo, etc )
'do your stuff
END FN

4.) As a means to see if someone on the list has something better and 
will share it.

Ken

p.s. Almost forgot:


5.) To tweak ethnocentrics since these are "dark-side" inspired.   ;-}


/*

Many programming languages have a REPLACE function to replace
occurrences of one substring with another substring.
Such a function would be handy in FB if your applications
need text-editing capabilities..

Note: If you specify "" for the newSubStr, FN REPLACE$
will clean the oldSubStr text from the selected string.

Here are four examples of REPLACE clones in FB^3:

*/

'~REPLACE 1 Clone

LOCAL FN REPLACE1$( myStr AS STR255, oldSubStr AS STR255, newSubStr AS STR255 )
DIM place  AS LONG
DIM index  AS INTEGER
DIM length AS INTEGER

IF myStr[0] = 0 OR oldSubStr[0] = 0 THEN EXIT FN

length = oldSubStr[0]
  place = INSTR( 1, myStr, oldSubStr )

DO
myStr = LEFT$( myStr, place - 1 ) + newSubStr + MID$( myStr, place + length )
place = INSTR( place, myStr, oldSubStr )
UNTIL place = 0

END FN = myStr


'~REPLACE 2 Clone

LOCAL FN REPLACE2$( myStr AS STR255, oldSubStr AS STR255, newSubStr AS STR255 )
DiM i    AS INTEGER
DiM tStr AS STR255

i = 1

WHILE _true
i = INSTR( 1, myStr, oldSubStr )
LONG IF i = 0
EXIT WHILE
XELSE
tStr  = LEFT$( myStr, i - 1 )
myStr = tStr + newSubStr + RIGHT$( myStr, myStr[0] - oldSubStr[0] - tStr[0] )
END IF
WEND

END FN = myStr


'~REPLACE 3 Clone

LOCAL FN REPLACE3$( myStr AS STR255, oldSubStr AS STR255, newSubStr AS STR255 )
DIM i    AS LONG
DIM tStr AS STR255

tStr  = ""
     i = INSTR( 1, myStr, oldSubStr )

WHILE i
  tStr = tStr + LEFT$( myStr, i - 1 ) + newSubStr
myStr = RIGHT$( myStr, myStr[0] - i - oldSubStr[0] + 1)
     i = INSTR( 1, myStr, oldSubStr )
WEND

myStr = tStr + myStr

END FN = myStr


'~REPLACE 4 Clone

LOCAL FN REPLACE4$( myStr AS STR255, oldSubStr AS STR255, newSubStr AS STR255 )
DIM index   AS INTEGER
DIM found   AS INTEGER
DIM srchlen AS INTEGER
DIM newlen  AS INTEGER
DIM newpos  AS INTEGER
DIM replen  AS INTEGER

DIM AS INT index, found, srchlen

IF oldSubStr = "" THEN EXIT FN

index   = 1
replen  = newSubStr[0]
srchlen = oldSubStr[0]
newlen  = replen - srchlen

DO
found    = INSTR( index, myStr, oldSubStr )
IF found = 0 THEN EXIT DO
newpos   = found + srchlen
myStr    = LEFT$( myStr, found - 1) + newSubStr + MID$( myStr, newpos )
index    = newpos + newlen
UNTIL 0

END FN = myStr


'~Here's some test stuff

DIM targetStr       AS STR255
DIM toBeReplacedStr AS STR255
DIM replaceWithStr  AS STR255

       targetStr = "Hello, this is the string that I will be modifying."
toBeReplacedStr = "string"
replaceWithStr  = "code sample"

PRINT:PRINT "This is the string before replacement:"
PRINT:PRINT targetStr

PRINT:PRINT "I want to replace this word:"
PRINT:PRINT toBeReplacedStr

PRINT:PRINT "With this phrase:"
PRINT:PRINT replaceWithStr

PRINT:PRINT "This is the string after replacement with FN REPLACE1$:"
PRINT:PRINT FN REPLACE1$( targetStr, toBeReplacedStr, replaceWithStr )

PRINT:PRINT "This is the string after replacement with FN REPLACE2$:"
PRINT:PRINT FN REPLACE2$( targetStr, toBeReplacedStr, replaceWithStr )

PRINT:PRINT "This is the string after replacement with FN REPLACE3$:"
PRINT:PRINT FN REPLACE3$( targetStr, toBeReplacedStr, replaceWithStr )

PRINT:PRINT "This is the string after replacement with FN REPLACE4$:"
PRINT:PRINT FN REPLACE4$( targetStr, toBeReplacedStr, replaceWithStr )