;========================================================================================================================================== ; ; Collection of different udfStrCap () algorithms. ; ; Coded by Guido sedar@yahoo.com, George Vagenas gvag@shaw.ca, Detlev Dalitz dd@hpdd.de. ; I don't know what algorithm from what programmer. ; ; (c)Detlev Dalitz.20100306. ;========================================================================================================================================== intPrevIC50 = IntControl (50, 0, 0, 0, 0) ; Remove "Go to web page" button from error boxes. DirChange (DirScript ()) ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfStrCapV1 (strString) ; This algorithm seems to be the fastest, when capitalizing is needed only following space character. If strString == "" Then Return "" strOut = "" intCount = ItemCount (strString, " ") For intI = 1 To intCount strItem = ItemExtract (intI, strString, " ") strOut = strOut : " " : StrUpper (StrSub (strItem, 1, 1)) : StrLower (StrSub (strItem, 2, -1)) Next strOut = StrSub (strOut, 2, -1) Return strOut ;.......................................................................................................................................... ; (c)Detlev Dalitz.20030210.20100306. ;.......................................................................................................................................... #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. strIn = "To be OR NOT to be." strOut = udfStrCapV1 (strIn) ; "To be Or Not to be." ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfStrCapV2 (strString) If strString == "" Then Return "" strOut = "" While strString != "" strOut = ItemInsert (StrUpper (StrSub (ItemExtract (1, strString, " "), 1, 1)) : StrSub (StrLower (ItemExtract (1, strString, " ")), 2, -1), -1, strOut, " ") strString = ItemRemove (1, strString, " ") EndWhile Return strOut #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. strIn = "To be OR NOT to be." strOut = udfStrCapV2 (strIn) ; "To be Or Not to be." ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfStrCapV3 (strString) ; Same algorithm as udfStrCapV4 but each calculation coded on separate line. If strString == "" Then Return "" strOut = "" While strString != "" strItem = ItemExtract (1, strString, " ") strFirst = StrSub (strItem, 1, 1) strFirst = StrUpper (strFirst) strOther = StrSub (strItem, 2, -1) strOther = StrLower (strOther) strItem = strFirst : strOther strOut = ItemInsert (strItem, -1, strOut, " ") strString = ItemRemove (1, strString, " ") EndWhile Return strOut #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. strIn = "To be OR NOT to be." strOut = udfStrCapV3 (strIn) ; "To be Or Not to be." ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfStrCapV4 (strString) ; This function keeps already upper cased characters. If strString == "" Then Return "" intLen = StrLen (strString) blnFlag = @TRUE strCap = "" For intI = 1 To intLen strChar = StrSub (strString, intI, 1) If blnFlag strCap = strCap : StrUpper (strChar) Else strCap = strCap : strChar EndIf blnFlag = (strChar == " ") Next Return strCap #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. strIn = "To be OR NOT to be." ; "To be OR NOT to be." strOut = udfStrCapV4 (strIn) ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfStrCapV5 (strString) ; This function keeps already upper cased characters. If strString == "" Then Return "" intLen = StrLen (strString) intX = 1 While @TRUE While @TRUE strChar = StrSub (strString, intX, 1) If strChar != " " || intX == intLen Then Break intX = intX + 1 EndWhile strString = StrFixChars (strString, "", intX - 1) : StrUpper (strChar) : StrFixCharsL (strString, "", intLen - intX) While @TRUE strChar = StrSub (strString, intX, 1) If strChar == " " || intX == intLen Then Break intX = intX + 1 EndWhile If intX == intLen Then Break EndWhile Return strString #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. strIn = "To be OR NOT to be." strOut = udfStrCapV5 (strIn) ; "To be OR NOT to be." ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfStrCapV6 (strString) ; This function keeps already upper cased characters. If strString == "" Then Return "" intX = 0 intLen = StrLen (strString) hdlBB = BinaryAlloc (intLen + 1) BinaryPokeStr (hdlBB, 0, strString) intY = 0 While @TRUE While @TRUE strTmp1 = BinaryPeekStr (hdlBB, intX, 1) strTmp2 = BinaryIndexEx (hdlBB, intY, @CRLF, @FWDSCAN, 0) If strTmp2 != -1 BinaryPokeStr (hdlBB, strTmp2 + 2, StrUpper (BinaryPeekStr (hdlBB, strTmp2 + 2, 1))) intY = strTmp2 + 2 EndIf If strTmp1 != " " || intX == intLen Then Break intX = intX + 1 EndWhile BinaryPokeStr (hdlBB, intX, StrUpper (strTmp1)) While @TRUE strTmp3 = BinaryPeekStr (hdlBB, intX, 1) If strTmp3 == " " || intX == intLen Then Break intX = intX + 1 EndWhile If intX == intLen Then Break EndWhile strString = BinaryPeekStr (hdlBB, 0, intLen) hdlBB = BinaryFree (hdlBB) Return strString #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. strIn = "To be OR NOT to be." strOut = udfStrCapV6 (strIn) ; "To be OR NOT to be." ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfStrCapV7 (strString) ; This function keeps already upper cased characters. If strString == "" Then Return "" strTC = " ,.:;-+/*=<>?!()[]{}#%%@""'`" : @TAB : @CR : @LF ; Trigger characters. intLen = StrLen (strString) strOut = StrUpper (StrSub (strString, 1, 1)) For intI = 2 To intLen If StrIndex (strTC, StrSub (strString, intI - 1, 1), 1, @FWDSCAN) Then strOut = strOut : StrUpper (StrSub (strString, intI, 1)) Else strOut = strOut : StrSub (strString, intI, 1) Next Return strOut #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. strIn = "To:be,OR NOT--to+++be?" strOut = udfStrCapV7 (strIn) ; "To:Be,OR NOT--To+++Be?" ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfFileCap (strFileIn, strFileOut) ; This function does not keep already upper cased characters. intFS = FileSize (strFileIn) If intFS == 0 Then Return "" hdlBB = BinaryAlloc (intFS + 2) BinaryPokeStr (hdlBB, 0, " ") ; Prepend one blank character. BinaryPokeStr (hdlBB, intFS + 1, " ") ; Append one blank character. BinaryReadEx (hdlBB, 1, strFileIn, 0, intFS) BinaryConvert (hdlBB, 0, 0, 0, 2) ; Convert to lowercase. arrTC = ArrayFromStr (" ,.:;-+/*=<>?!()[]{}#%%@""'`" : @TAB : @CR : @LF) ; Trigger characters. intTCLast = ArrInfo (arrTC, 1) - 1 For intC = 65 To 90 ; "A" to "Z". strChar = Num2Char (intC) For intTC = 0 To intTCLast strSR = arrTC[intTC] : strChar BinaryReplace (hdlBB, strSR, strSR, @FALSE) Next Next BinaryWriteEx (0, 0, strFileOut, 0, -1) ; Create new file. intBytesWritten = BinaryWriteEx (hdlBB, 1, strFileOut, 0, intFS) ; Discard surrounding blanks. hdlBB = BinaryFree (hdlBB) Return intBytesWritten ;.......................................................................................................................................... ; (c)Detlev Dalitz.20030210.20100306. ;.......................................................................................................................................... #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. strFileIn = IntControl (1004, 0, 0, 0, 0) ; Use this script as test input. strFileOut = FileCreateTemp ("TMP") intBytesWritten = udfFileCap (strFileIn, strFileOut) intResult = RunZoomWait ("Notepad.exe", strFileOut) blnResult = FileDelete (strFileOut) ;------------------------------------------------------------------------------------------------------------------------------------------ ;========================================================================================================================================== ; ; Profiling Performance Test. ; ;========================================================================================================================================== strTest = "To be, or not to be, that is the question: Whether it is nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles, and by opposing end them?" intTestMin = 1 intTestMax = 7 intLoopMax = 100 For intTest = intTestMin To intTestMax Display (1, "Performance Test udfStrCap ()", "Please wait ... running test: " : intTest) strName%intTest% = "udfStrCapV" : intTest Exclusive (@ON) intStart = GetTickCount () For intL = 1 To intLoopMax strResult = udfStrCapV%intTest% (strTest) Next intTicks%intTest% = GetTickCount () - intStart Exclusive (@OFF) Next ;------------------------------------------------------------------------------------------------------------------------------------------ ; Result. ;------------------------------------------------------------------------------------------------------------------------------------------ Decimals (1) intTicksSum = 0 For intTest = intTestMin To intTestMax intTicksSum = intTicksSum + intTicks%intTest% Next If intTicksSum < 1 Then intTicksSum = 1 ; To prevent dividing by zero. For intTest = intTestMin To intTestMax intPct%intTest% = 100.0 * intTicks%intTest% / intTicksSum Next ; Format output. strTest = "Test" strTicks = "Ticks" strPct = "Pct" strName = "Name" strSep = " " strPre = "; " intLenTest = Max (StrLen (strTest), StrLen (intTestMax)) intLenTicks = StrLen (strTicks) intLenPct = StrLen (strPct) intLenName = StrLen (strName) For intTest = intTestMin To intTestMax intLenTicks = Max (intLenTicks, StrLen (intTicks%intTest%)) intLenPct = Max (intLenPct, StrLen (intPct%intTest%)) intLenName = Max (intLenName, StrLen (strName%intTest%)) Next strTest = StrFixLeft (strTest, " ", intLenTest) strTicks = StrFixLeft (strTicks, " ", intLenTicks) strPct = StrFixLeft (strPct, " ", intLenPct) strResult = "" strResult = ItemInsert (strPre : "Iterations: " : intLoopMax, -1, strResult, @LF) strResult = ItemInsert (strPre : strTest : strSep : strTicks : strSep : strPct : strSep : strName, -1, strResult, @LF) For intTest = intTestMin To intTestMax strTest = StrFixLeft (intTest, " ", intLenTest) strTicks = StrFixLeft (intTicks%intTest%, " ", intLenTicks) strPct = StrFixLeft (intPct%intTest%, " ", intLenPct) strName = StrFix (strName%intTest%, " ", intLenName) strResult = ItemInsert (strPre : strTest : strSep : strTicks : strSep : strPct : strSep : strName, -1, strResult, @LF) Next strResult = strResult : @LF Message ("Performance Test Result", strResult) ClipPut (strResult) :CANCEL Exit ;------------------------------------------------------------------------------------------------------------------------------------------ ; Iterations: 100 ; Test Ticks Pct Name ; 1 2000 3.5 udfStrCapV1 <== The winner, when capitalizing is needed only following space character. ; 2 2422 4.3 udfStrCapV2 ; 3 4297 7.6 udfStrCapV3 ; 4 10437 18.5 udfStrCapV4 ; 5 12828 22.7 udfStrCapV5 ; 6 14672 26.0 udfStrCapV6 ; 7 9797 17.4 udfStrCapV7 <== The winner, when capitalizing is needed following dedicated characters. ;------------------------------------------------------------------------------------------------------------------------------------------ ;==========================================================================================================================================