|
|
|
DD382200.HTM DD-Software.StringsFiles1 Add this page to your favorites Save this document |
|
|
|
|
udfStrOverlay (sOverlay, sTarget, iStart, iLength, sPadChar) |
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfstroverlay",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfstroverlay
#DefineFunction udfStrOverlay (sOverlay, sTarget, iStart, iLength, sPadChar)
If !StrLen(sOverlay) Then Return (sTarget)
iTargetLength = StrLen(sTarget)
If !iTargetLength Then Return ("")
If (sPadChar=="") Then sPadChar = " "
iLength = Max(0,iLength)
iStart = Max(0,iStart)
a = StrSub(sTarget,1,iStart-1)
b = StrSub(sTarget,iStart+iLength,-1)
Return (StrSub(StrCat(a,StrFix(sOverlay,sPadChar,iLength),b),1,iTargetLength))
;..........................................................................................................................................
; Returns string sTarget with the string sOverlay overlayed at position iStart.
; Prior to the operation, sOverlay is truncated to iLength
; or padded to iLength with the sPadChar character, which defaults to a blank.
;
; Detlev Dalitz.20020209
;..........................................................................................................................................
#EndFunction
:skip_udfstroverlay
;------------------------------------------------------------------------------------------------------------------------------------------
;--- test ---
sMsgTitle = "Demo udfStrOverlay (sOverlay, sTarget, iStart, iLength, sPadChar)"
:test1
t1 = "dark,edge of night,9,5,*" ; change at end of sTarget ; "edge of dark*"
t2 = "dark,edge of night,1,4,*" ; change at iStart of sTarget ; "dark of night"
t3 = "dark,edge of night,0,5,*" ; no change ; "edge of night"
t4 = "dark,edge of night,20,5,*" ; no change ; "edge of night"
t5 = "dark,edge of night,-3,5,*" ; no change ; "edge of night"
t6 = "dark,edge of night,3,0," ; no change ; "edge of night"
t7 = "dark,edge of night,3,2," ; change ; "edda of night"
t8 = ",edge of night,3,2," ; no change ; "edge of night"
For i=1 To 8
sOverlay = ItemExtract(1,t%i%,",")
sTarget = ItemExtract(2,t%i%,",")
iStart = ItemExtract(3,t%i%,",")
iLength = ItemExtract(4,t%i%,",")
sPadChar = ItemExtract(5,t%i%,",")
sNew = udfStrOverlay(sOverlay,sTarget,iStart,iLength,sPadChar)
sMsgText = ""
sMsgText = StrCat(sMsgText,'sOverlay' ,@TAB,@TAB,'"',sOverlay,'"',@CRLF)
sMsgText = StrCat(sMsgText,'sTarget' ,@TAB,@TAB,'"',sTarget ,'"',@CRLF)
sMsgText = StrCat(sMsgText,'iStart' ,@TAB,@TAB,'"',iStart ,'"',@CRLF)
sMsgText = StrCat(sMsgText,'iLength' ,@TAB,@TAB,'"',iLength ,'"',@CRLF)
sMsgText = StrCat(sMsgText,'sPadChar' ,@TAB,@TAB,'"',sPadChar,'"',@CRLF)
sMsgText = StrCat(sMsgText,'sNew' ,@TAB,@TAB,'"',sNew ,'"',@CRLF)
Pause(sMsgTitle,sMsgText)
Next
:test2
BoxOpen("Demo udfStrOverlay (sOverlay, sTarget, iStart, iLength, sPadChar)","")
iTargetLength = 10
sTarget = StrFill("-",iTargetLength)
For i=1 To iTargetLength
sOut = udfStrOverlay("*",sTarget,i,1,"")
sOut = StrCat("[",sOut,"]")
BoxText(sOut)
TimeDelay(0.1/iTargetLength)
Next
For i=iTargetLength To 1 By -1
sOut = udfStrOverlay("*",sTarget,i,1,"")
sOut = StrCat("[",sOut,"]")
BoxText(sOut)
TimeDelay(0.1/iTargetLength)
Next
BoxShut()
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;*EOF*
|
|
If you have questions about WinBatch, you are encouraged to use online WebBoard BBS at http://webboard.windowware.com
|
|
|
|
|
udfStrCnt (sourcestr, searchstr, matchcase) |
;==========================================================================================================================================
; How to count substrings in a string?
;==========================================================================================================================================
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfstrcnt_1",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfstrcnt_1
#DefineFunction udfStrCnt_1 (sourcestr, searchstr, matchcase)
n=0
pos=0
If Min(1,Max(0,matchcase))
While 1
pos=StrIndex(sourcestr,searchstr,pos+1,@FWDSCAN)
If (pos>0) Then n=n+1
Else Return (n)
EndWhile
Else
While 1
pos=StrIndexNC(sourcestr,searchstr,pos+1,@FWDSCAN)
If (pos>0) Then n=n+1
Else Return (n)
EndWhile
EndIf
;..........................................................................................................................................
; Detlev Dalitz.20020209
;..........................................................................................................................................
#EndFunction
:skip_udfstrcnt_1
;------------------------------------------------------------------------------------------------------------------------------------------
;--- test ---
msgtitle = "Demo udfStrCnt (str, searchstr, matchcase)"
str = "TO test or NOT to test"
searchstr = "O"
n = udfStrCnt_1(str,searchstr,@TRUE)
msgtext = ""
msgtext = StrCat(msgtext,@CRLF,'udfStrCnt("',str,'" ,"',searchstr,'" ,@true)')
msgtext = StrCat(msgtext,@CRLF,n,' occurences of "',searchstr,'"')
Message(msgtitle,msgtext)
; ==> '2 occurences of "O"'
searchstr = "O"
n = udfStrCnt_1(str,searchstr,@FALSE)
msgtext = StrCat(msgtext,@CRLF)
msgtext = StrCat(msgtext,@CRLF,'udfStrCnt("',str,'" ,"',searchstr,'" ,@false)')
msgtext = StrCat(msgtext,@CRLF,n,' occurences of "',searchstr,'"')
Message(msgtitle,msgtext)
; ==> '4 occurences of "O"'
searchstr = "TEST"
n = udfStrCnt_1(str,searchstr,@FALSE)
msgtext = StrCat(msgtext,@CRLF)
msgtext = StrCat(msgtext,@CRLF,'udfStrCnt("',str,'" ,"',searchstr,'" ,@false)')
msgtext = StrCat(msgtext,@CRLF,n,' occurences of "',searchstr,'"')
Message(msgtitle,msgtext)
; ==> '2 occurences of "TEST"'
;==========================================================================================================================================
;==========================================================================================================================================
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfstrcnt_2",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfstrcnt_2
#DefineFunction udfStrCnt_2 (sourcestr, searchstr, matchcase)
If (0==Min(1,Max(0,matchcase)))
sourcestr = StrLower(sourcestr)
searchstr = StrLower(searchstr)
EndIf
Return ((StrLen(sourcestr) - StrLen(StrReplace(sourcestr,searchstr,""))) / StrLen(searchstr))
;..........................................................................................................................................
; Adapted from WinBatch TechBase
; Article ID: W14502
; Filename: Number of Instances of Char in a String.txt
; File Created: 2000:03:30:11:28:01
; Last Updated: 2000:03:30:11:43:44
;
; Slightly modified by Detlev Dalitz.20020521
;..........................................................................................................................................
#EndFunction
:skip_udfstrcnt_2
;------------------------------------------------------------------------------------------------------------------------------------------
; --- test ---
str = "abcannaDnnnannannnDndnnd"
count = udfStrCnt_2(str,"dn",0)
Message("Demo udfStrCnt_2 Count",count)
; ==> 3
count = udfStrCnt_2(str,"dn",1)
Message("Demo udfStrCnt_2 Count",count)
;==> 1
;==========================================================================================================================================
;==========================================================================================================================================
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfstrcnt_3",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfstrcnt_3
#DefineFunction udfStrCnt_3 (sourcestr, searchstr, matchcase)
n=0
pos=0
While 1
If (matchcase==1) Then pos=StrIndex(sourcestr,searchstr,pos+1,@FWDSCAN)
If (matchcase==0) Then pos=StrIndexNC(sourcestr,searchstr,pos+1,@FWDSCAN)
If (pos<>0) Then n=n+1
Else Break
EndWhile
Return (n)
;..........................................................................................................................................
; Published by Guido sedar@yahoo.com, Tuesday, June 26, 2001 09:05 AM, WinBatch Forum
; This function counts the occurrences of a sub-string in a string.
;
; Parameters:
; str: the string to be searched for a sub-string.
; substr: the string to look for within the main string.
; matchcase: 1=Case sensitive, 0=Ignore case
; Returns the Number of occurrences of sub-string found.
;..........................................................................................................................................
#EndFunction
:skip_udfstrcnt_3
;------------------------------------------------------------------------------------------------------------------------------------------
;--- test ---
count1 = udfStrCnt_3("green","e",0)
; ==> 2
count2 = udfStrCnt_3("green","E",1)
; ==> 0
count3 = udfStrCnt_3("green","E",0)
; ==> 2
;==========================================================================================================================================
;==========================================================================================================================================
;------------------------------------------------------------------------------------------------------------------------------------------
:Performancetest
sMsgTitle = "Demo Performance Test"
sTestString = "TO test or NOT to test"
iTestLoop = 1
iMaxTests = 3
iTest=1
Display(1,sMsgTitle,"Running Test %iTest%, please wait ...")
Exclusive(@ON)
iStart = GetTickCount()
For i=1 To iTestLoop
Result = udfStrCnt_1(sTestString,"TEST",0)
Result = udfStrCnt_1(sTestString,"TEST",1)
Result = udfStrCnt_1(sTestString,"O",0)
Result = udfStrCnt_1(sTestString,"O",1)
Next
iStop = GetTickCount()
Exclusive(@OFF)
iTicks%iTest% = iStop-iStart
iTest=2
Display(1,sMsgTitle,"Running Test %iTest%, please wait ...")
Exclusive(@ON)
iStart = GetTickCount()
For i=1 To iTestLoop
Result = udfStrCnt_2(sTestString,"TEST",0)
Result = udfStrCnt_2(sTestString,"TEST",1)
Result = udfStrCnt_2(sTestString,"O",0)
Result = udfStrCnt_2(sTestString,"O",1)
Next
iStop = GetTickCount()
Exclusive(@OFF)
iTicks%iTest% = iStop-iStart
iTest=3
Display(1,sMsgTitle,"Running Test %iTest%, please wait ...")
Exclusive(@ON)
iStart = GetTickCount()
For i=1 To iTestLoop
Result = udfStrCnt_3(sTestString,"TEST",0)
Result = udfStrCnt_3(sTestString,"TEST",1)
Result = udfStrCnt_3(sTestString,"O",0)
Result = udfStrCnt_3(sTestString,"O",1)
Next
iStop = GetTickCount()
Exclusive(@OFF)
iTicks%iTest% = iStop-iStart
iMaxTicks = 0
For iTest=1 To iMaxTests
iMaxTicks = Max(iMaxTicks,iTicks%iTest%)
Next
For iTest=1 To iMaxTests
iPct%iTest% = 100*iTicks%iTest%/iMaxTicks
Next
sMsgText = ""
For iTest=1 To iMaxTests
sMsgText = StrCat(sMsgText,"Test ",iTest,@TAB,"Ticks = ",@TAB,iTicks%iTest%,@TAB,iPct%iTest%," %%",@CRLF)
Next
ClipPut(sMsgText)
Message(sMsgTitle,sMsgText)
; WB Studio Debug
; Test 1 Ticks = 48143 91 %
; Test 2 Ticks = 14571 27 % <== The Winner.
; Test 3 Ticks = 52609 100 %
; WB Studio Run
; Test 1 Ticks = 4923 83 %
; Test 2 Ticks = 2657 45 % <== The Winner.
; Test 3 Ticks = 5877 100 %
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
|
|
If you have questions about WinBatch, you are encouraged to use online WebBoard BBS at http://webboard.windowware.com
|
|
|
|
|
udfStrCompose (str, composelist, delimiter) |
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfstrcompose",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfstrcompose
#DefineFunction udfStrCompose (sString, sComposeList, sDelimiter)
If (sComposeList == "") Then Return (sString)
sErrMsg = "*** udfStrCompose: Bad parameter sComposeList ***"
iCount = ItemCount(sComposeList,sDelimiter)
If (iCount mod 2) Then Return (sErrMsg)
sComposeStr = ""
iCount = iCount/2
For ii=1 To iCount
vItem1 = ItemExtract(ii+ii-1,sComposeList,sDelimiter)
vItem2 = ItemExtract(ii+ii ,sComposeList,sDelimiter)
If !IsInt(vItem2) Then Return (sErrMsg)
If IsInt(vItem1)
iLen = Max(1+vItem2-vItem1,1)
sComposeStr = StrCat(sComposeStr,StrFix(StrSub(sString,vItem1,iLen)," ",iLen))
Else
For id=1 To 3
sDelim = StrSub(""",',`",id,1)
vItem = ItemExtract(2,vItem1,sDelim)
If (StrCat(sDelim,vItem,sDelim) == vItem1)
vItem1 = vItem
Break
EndIf
Next
vItem = vItem1
For in=2 To vItem2
vItem = StrCat(vItem,vItem1)
Next
sComposeStr = StrCat(sComposeStr,vItem)
EndIf
Next
Return (sComposeStr)
;..........................................................................................................................................
; Returns a string which is composed as defined in sComposeList.
; Example:
;
; csDelim = ","
; clist = ""
; clist = Iteminsert(StrCat( ": " ,csDelim, "1" ),-1,clist,csDelim) ; append ': ' times 1
; clist = Iteminsert(StrCat( "#35" ,csDelim, "1" ),-1,clist,csDelim) ; append '#35' times 1
; clist = Iteminsert(StrCat( "'4711'" ,csDelim, "2" ),-1,clist,csDelim) ; append '4711' times 2
; clist = Iteminsert(StrCat( "2" ,csDelim, "5" ),-1,clist,csDelim) ; append column 2 thru 5
; clist = Iteminsert(StrCat( " " ,csDelim, "5" ),-1,clist,csDelim) ; append blank times 5
; clist = Iteminsert(StrCat( "2" ,csDelim, "3" ),-1,clist,csDelim) ; append column 2 thru 3
; clist = Iteminsert(StrCat( "." ,csDelim, "1" ),-1,clist,csDelim) ; append '.' times 1
; clist = Iteminsert(StrCat( "4" ,csDelim, "5" ),-1,clist,csDelim) ; append column 4 thru 5
; clist = Iteminsert(StrCat( "%%" ,csDelim, "1" ),-1,clist,csDelim) ; append symbol percent times 1
; clist = Iteminsert(StrCat( "%@crlf%" ,csDelim, "2" ),-1,clist,csDelim) ; append @crlf times 2
; clist = Iteminsert(StrCat( ";""" ,csDelim, "1" ),-1,clist,csDelim) ; append ';"' times 1
; clist = Iteminsert(StrCat( "%@tab%" ,csDelim, "1" ),-1,clist,csDelim) ; append @tab times 1
; clist = Iteminsert(StrCat( "- . " ,csDelim, "20" ),-1,clist,csDelim) ; append '- . ' times 20
; clist = Iteminsert(StrCat( """" ,csDelim," 1" ),-1,clist,csDelim) ; append '"' times 1
; clist = Iteminsert(StrCat( "%@crlf%" ,csDelim, "1" ),-1,clist,csDelim) ; append @crlf times 1
;
; Composing the tag string looks somewhat cryptic, but don't worry, be happy.
; - Use always sets of two elements: 'from,thru' or 'text,multiplier'.
; - Use tupel (n,m) for extracting chars from sString out of columns n to m.
; - Use tupel (abc,n) to fill new text "abc" n-times into the composed string,
; as an alternative: use tupel ('a b c',n) to fill in new text "a b c" n-times.
;..........................................................................................................................................
; Detlev Dalitz.20010716
;..........................................................................................................................................
#EndFunction
:skip_udfstrcompose
;------------------------------------------------------------------------------------------------------------------------------------------
; --- test ---
:test1
sString = "All messages in all conferences have been marked as read."
sComposeList = "43,46,' --> ',2,5,12, ,2,33,36, ,2,44,44,2,3, ,2,38,41, ,2,38,38,55,56, ,1,<,1,-,5"
sComposeStr = udfStrCompose(sString,sComposeList,",")
sMsgTitle = "Demo udfStrCompose (sString, sComposeList, sDelimiter)"
sMsgText = StrCat('sString = "',sString,'"',@CRLF)
sMsgText = StrCat(sMsgText,'sComposeList = "',sComposeList,'"',@CRLF)
sMsgText = StrCat(sMsgText,'sComposeStr = "',sComposeStr,'"')
Message(sMsgTitle,sMsgText)
; ==> "mark --> --> messages have all been bad <-----"
:test2
sString = "Demo with bad formatted sComposeList."
; sComposeList is not ok: 3 and a half tupel, odd number of items
sComposeList = "1,5,33,34,10,13,."
; sComposeList is ok: 4 tupel, even number of items
; sComposeList = "1,5,33,34,10,13,.,1"
sComposeStr = udfStrCompose(sString,sComposeList,",")
sMsgTitle = "Demo udfStrCompose (sString, sComposeList, sDelimiter)"
sMsgText = StrCat('sString = "',sString,'"',@CRLF)
sMsgText = StrCat(sMsgText,'sComposeList = "',sComposeList,'"',@CRLF)
sMsgText = StrCat(sMsgText,'sComposeStr = "',sComposeStr,'"')
Message(sMsgTitle,sMsgText)
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
|
|
If you have questions about WinBatch, you are encouraged to use online WebBoard BBS at http://webboard.windowware.com
|
|
|
|
|
udfStrQuote (sStr, sLeft, sRight)
|
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfstrquote",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfstrquote
#DefineFunction udfStrQuote (sString, sLeft, sRight)
; If (sString == "") then return (sString)
If (""==sLeft)
If (""==sRight)
sQuote = """'`"
sClean = StrClean(sString,sQuote,"",@FALSE,2)
If (""==StrClean(sQuote,sClean,"",@FALSE,1))
sQuote = '"'
sString = StrReplace(sString,sQuote,StrCat(sQuote,sQuote))
Else
sClean = StrClean(sQuote,sClean,"",@FALSE,1)
sQuote = StrSub(sClean,1,1)
EndIf
sLeft = sQuote
sRight = sQuote
EndIf
EndIf
Return (StrCat(sLeft,sString,sRight))
;------------------------------------------------------------------------------------------------------------------------------------------
; With sLeft="" and sRight=""
; the function chooses a winbatch quote delimiter automagically
; and doubles the quotation char in sString if necessary.
;
; With sLeft="""" and sRight=""""
; the function allows quotation without doubling of quotation char in sString.
;
; With sLeft="(* " and sRight=" *)"
; the function encloses sString in pairs of pascal comments.
;
; DD.20010722.20020628
;------------------------------------------------------------------------------------------------------------------------------------------
#EndFunction
:skip_udfstrquote
;------------------------------------------------------------------------------------------------------------------------------------------
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfstrunquote",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfstrunquote
#DefineFunction udfStrUnQuote (sString, iMode)
If (sString=="") Then Return (sString)
If (iMode<0)||(iMode>5) Then Return (sString)
iLen = StrLen(sString)
c1 = StrSub(sString,1,1)
c2 = StrSub(sString,iLen,1)
cc = StrCat(c1,c2)
qq = """""''``"
bb = "(){}[]<>"
Select iMode
Case 0
dd = qq
Break
Case 1
dd = bb
Break
Case 2
dd = StrCat(qq,bb)
Break
Case 3
dd = StrCat(c1,c1)
Break
Case 4
dd = StrCat(bb,c1,c1)
Break
Case 5
dd = cc
Break
EndSelect
ib = @FALSE
ii = 1
While !(ib||(ii>StrLen(dd)/2))
ib = (""==StrClean(cc,StrSub(dd,ii+ii-1,2),"",@FALSE,1))
ii = ii + 1
EndWhile
If ib Then sString = StrSub(sString,2,iLen-2)
Return (sString)
;..........................................................................................................................................
; This udf removes quote delimiters, brackets or any first/last chars.
;
; iMode=0 ... Removes quotes only.
; iMode=1 ... Removes brackets only.
; iMode=2 ... Removes quotes and brackets.
; iMode=3 ... Removes first/last chars if equal and quotes.
; iMode=4 ... Removes first/last chars if equal and brackets.
; iMode=5 ... Removes any first/last chars.
;..........................................................................................................................................
; Detlev Dalitz.20010722
;..........................................................................................................................................
#EndFunction
;
:skip_udfstrunquote
;------------------------------------------------------------------------------------------------------------------------------------------
;--- test 1 ---
test1 = `udfStrQuote (str, '', '')`
test2 = `udfStrQuote (str, '"', '"')`
test3 = `udfStrQuote (str, '(', ')')`
test4 = `udfStrQuote (str, '/* ',' */')`
test5 = `udfStrQuote (str, '==> ','')`
For it=1 To 5
test = test%it%
str = "."" ab'c ""."
outstr = ""
For i=1 To 4
k = i+i
j = k-1
str%j% = str
str%k% = %test%
str = str%k%
line = StrCat(str%j%,@TAB,@TAB,"-->",@TAB,str%k%)
outstr = StrCat(outstr,line,@CR)
If (StrSub(str,1,1)=="""") Then str=StrReplace(str,"""""","""") ; Simulate quote substitution.
Next
outstr=StrCat(test,@CR,@CR,outstr)
Pause("",outstr)
Next
;--- test 2 ---
mode=2
test=StrCat("udfStrUnQuote (str, ",mode,")")
str="""'`[{<#test#>}]`'"""
steps=10
For i=1 To steps
str2=udfStrUnQuote(str,mode)
outstr=StrCat(str,@TAB,"-->",@TAB,str2)
outstr=StrCat(test,@CR,"step ",steps,"/",i,@CR,@CR,outstr)
Pause("",outstr)
str=str2
Next
;--- test 3 ---
mode=4
test=StrCat("udfStrUnQuote (str, ",mode,")")
str="""'`[{<#test#>}]`'"""
steps=10
For i=1 To steps
str2=udfStrUnQuote(str,mode)
outstr=StrCat(str,@TAB,"-->",@TAB,str2)
outstr=StrCat(test,@CR,"step ",steps,"/",i,@CR,@CR,outstr)
Pause("",outstr)
str=str2
Next
;--- test 4 ---
mode=5
test=StrCat("udfStrUnQuote (str, ",mode,")")
str="""'`[{<#test#>}]`'"""
steps=10
For i=1 To steps
str2=udfStrUnQuote(str,mode)
outstr=StrCat(str,@TAB,"-->",@TAB,str2)
outstr=StrCat(test,@CR,"step ",steps,"/",i,@CR,@CR,outstr)
Pause("",outstr)
str=str2
Next
:CANCEL
Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;*EOF*
|
|
If you have questions about WinBatch, you are encouraged to use online WebBoard BBS at http://webboard.windowware.com
|