Public Function IsGoodReplace(Optional fLigaturesToo As Boolean) As Boolean
' verify correct Replace returns, 20020929
' returns True if all tests are passed
  Dim fFailed As Boolean
  
  ' replace "Replace01" with the name of your function to test
  '
  ' ! note the differences to VB6's native Replace function!
  With New CStrProcessorV2
  'With New CStrProcessor
  
  If Replace01("aaa", "a", "baa") <> "baabaabaa" Then Stop: fFailed = True
  If Replace01("", "b", "XX") <> "" Then Stop: fFailed = True
  If Replace01("abc", "", "XX") <> "abc" Then Stop: fFailed = True
  If Replace01("abc", "b", "") <> "ac" Then Stop: fFailed = True
  If Replace01("abc", "c", "") <> "ab" Then Stop: fFailed = True
  If Replace01("abc", "b", "X") <> "aXc" Then Stop: fFailed = True
  If Replace01("abc", "b", "XX") <> "aXXc" Then Stop: fFailed = True
  If Replace01("blah", "blah", "ha") <> "ha" Then Stop: fFailed = True
  
  ' text compare
  If Replace01("abc", "B", "X", , , vbTextCompare) <> "aXc" Then Stop: fFailed = True
  If Replace01("aBc", "b", "XX", , , vbTextCompare) <> "aXXc" Then Stop: fFailed = True
  If Replace01("abc", "B", "XX", , , vbTextCompare) <> "aXXc" Then Stop: fFailed = True
  If Replace01("ac", "", "XX", , , vbTextCompare) <> "aXXc" Then Stop: fFailed = True
  If Replace01("ac", "", "XX", , , vbTextCompare) <> "aXXc" Then Stop: fFailed = True
  
  ' the 4 stooges: /, /, /, / (154/138, 156/140, 158/142, 255/159)
  If Replace01("Hai", "", "sch", , , vbTextCompare) <> "Haschisch" Then Stop: fFailed = True
  ' ligatures  textcompare (VBspeed entries do NOT have to pass this test)
  If fLigaturesToo Then
    ' ligatures, a digraphemic fun house: ss/, ae/, oe/, th/
    If Replace01("Strae", "ss", "f", , , vbTextCompare) <> "Strafe" Then Stop: fFailed = True
  End If
  
  ' non-textual chars in vbTextCompare mode:
  ' using &HDF (223) to convert to uppercase is problematic:
  ' it works with textual chars like "a"
    ' chr$(97) -> "a"           97=01100001
    '                      AND 223=11011111
    ' chr$(97 and 223) -> "A"   65=01000001
  ' but it fails with these for example:
    ' "[" = 91,   91 And 223 = 91
    ' "{" = 123, 123 And 223 = 91
  If Replace01("[[{{", "{", "x", , , vbTextCompare) <> "[[xx" Then Stop: fFailed = True
  If Replace01("[[{{", "[", "x", , , vbTextCompare) <> "xx{{" Then Stop: fFailed = True
  
  
  ' unicode
  If Replace01("", "", "x") <> "x" Then Stop: fFailed = True
  If Replace01("x", "x", "") <> "" Then Stop: fFailed = True
  
  ' hard core unicode + text compare
  ' high unicode textwise, test too hard for now => TestReplace
  ''If Replace01(ChrW$(400) & " Tag!", ChrW$(603), "Guten", , , vbTextCompare) <> "Guten Tag!" Then Stop: fFailed = True
  
  
  ' Start param
  If Replace01("abc", "a", "XX", 1) <> "XXbc" Then Stop: fFailed = True
  ' ! VB6 Replace returns "bc":
  If Replace01("abc", "a", "XX", 2) <> "abc" Then Stop: fFailed = True
  ' ! VB6 Replace returns "c":
  If Replace01("abc", "a", "XX", 3) <> "abc" Then Stop: fFailed = True
  ' ! VB6 Replace returns "":
  If Replace01("abc", "a", "XX", 4) <> "abc" Then Stop: fFailed = True
  ' ! VB6 Replace returns "bcXabcXabcabc":
  If Replace01("abcabcabcabc", "a", "Xa", 2, 2) <> "abcXabcXabcabc" Then Stop: fFailed = True
  
  ' very large inputs
  If Replace01("x" & Space$(10000) & "x", "x", Space$(10000)) <> Space$(30000) Then Stop: fFailed = True
  
  End With
  
  ' well done
  IsGoodReplace = Not fFailed
  
End Function

Back to Replace