I have written replacement of replace string with blog (not neccessary in v15)
You could modify it to fit your needs. Please note that this is written for
large text block and not for small text block in a tight loop due to its
overhead. If length
of $newstr if longer than $oldstr. I have another library. This is for unicode
4D text (utf-16). You got the idea.
//$Resulttext:=ES_ReplaceStr_Shrink ($source;$oldStr;$newStr;->$totalFound)
C_TEXT($0;$1;$2;$3;$source;$oldStr;$newStr;$result;$tempTxt)
C_POINTER($4)
C_LONGINT($oldLen;$newLen;$oldStrLen;$NewStrLen;$count;$offset;$offset2;$start;$pos)
C_BLOB($blob;$blob2)
$source:=$1
$oldLen:=Length($source) //$source is the source text
$oldStr:=$2
$newStr:=$3
$oldStrLen:=Length($oldStr)
$newstrlen:=Length($newStr)
SET BLOB SIZE($blob;$oldlen*2+9;0)
$offset:=9
$start:=1
$count:=0
Repeat
$pos:=Position($oldstr;$source;$start;*)
If ($pos>0)
$tempTxt:=Substring($source;$start;$pos-$start)+$newStr
$start:=$pos+$oldStrLen
$offset2:=0
VARIABLE TO BLOB($tempTxt;$blob2;$offset2)
COPY BLOB($blob2;$blob;9;$offset;$offset2-9)
$offset:=$offset+$offset2-9
$count:=$count+1
End if
Until ($pos=0)
If ($count>0)
If ($start<=$oldlen)
$tempTxt:=Substring($source;$start)
$offset2:=0
VARIABLE TO BLOB($tempTxt;$blob2;$offset2)
COPY BLOB($blob2;$blob;9;$offset;$offset2-9)
End if
If ($oldStrlen>$NewStrLen)
$newLen:=$oldLen+(($NewStrLen-$oldStrLen)*$count)
SET BLOB SIZE($blob;$newlen*2+9)
Else
$newlen:=$oldLen
End if
$blob{0x0000}:=Character code("R")
$blob{0x0001}:=Character code("V")
$blob{0x0002}:=Character code("L")
$blob{0x0003}:=Character code("B")
$blob{0x0004}:=0x0021
$blob{0x0005}:=$newlen%256
$blob{0x0006}:=($newlen\256)%256
$blob{0x0007}:=($newlen\65536)%256
$blob{0x0008}:=($newlen\16777216)
$offset:=0
BLOB TO VARIABLE($blob;$result;$offset)
$0:=$result
Else
$0:=$1
End if
If (Count parameters>3)
$4->:=$count
End if
4D iNug Technical <[email protected]> writes:
>You're running only substring and your modifiedSubstring. A piece of code
>segment cannot tell much how you use substring and your modifiedSubstring.
>
>For your information, using blob should be able to take less than 2 seconds in
>compiled.
>
>Alan Chan
>
>4D iNug Technical <[email protected]> writes:
>>ok - doing some testing and recoding.
>>I do not quite understand....
>>
>>I wrote code to implement substring (see far below)
>>I use it in a parsing routine (see below) on a text block of 2.7
>>million characters.
>>time to process the entire block : 36.5 sec.
>>
>>I use the exact same code, using 4D's Substring command
>>Time to process the entire block : 129.8 sec.
>>
>>Why is the (presumably) compiled C code SLOWER, then Interpreted 4D
>>code?
>>by a factor of 4?
>>
>>
>>---------
>>Parsing Routine
>>(initialization code removed)
>>For ($i;1;$How_Many)
>>$Start_Loc:=l_Last_Position+1
>>$Found_Location:=utl_text_Position ($Find;$Source;$Start_Loc)
>>
>>If ($Found_Location>0) //found
>>l_Last_Position:=$Find_Length+$Found_Location-1
>>Else
>>$i:=utl_Exit_Loop
>>End if
>>End for
>>
>>If ($i=MAXLONG) //not found. or not found enough
>>$Return_Text:=utl_text_Faster_Substring ($Source;$Start_Loc)
>>Else //found requested occurence count of Find
>>$Return_Text:=utl_text_Faster_Substring($Source;$Start_Loc;$Found_Location-1)
>>End if
>>$0:=$Return_Text
>
>
>**********************************************************************
>4D Internet Users Group (4D iNUG)
>FAQ: http://lists.4d.com/faqnug.html
>Archive: http://lists.4d.com/archives.html
>Options: http://lists.4d.com/mailman/options/4d_tech
>Unsub: mailto:[email protected]
>**********************************************************************
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ: http://lists.4d.com/faqnug.html
Archive: http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub: mailto:[email protected]
**********************************************************************