Ticket #3137: FileRead_UTF-8_Bug.au3

File FileRead_UTF-8_Bug.au3, 1.7 KB (added by miraged, 9 years ago)

Repro

Line 
1Opt("MustDeclareVars", 1)
2
3Global $sResults
4
5$sResults &= UTFx_Test(True) ;## UTF-8 - Funky
6$sResults &= UTFx_Test(True, True) ;## UTF-8 - Worse
7$sResults &= UTFx_Test(False) ;## UTF-16 - Works as expected
8
9MsgBox(0, "Results", $sResults)
10
11Func UTF8_Test($bUseUTF8, $bPartial = False)
12        Local Const $iCharsToRead = 7
13        Local $hFile, $iStartPos, $iEndPos, $iStrLen, $iBinLen, $sChars
14        Local $sToWrite = ($bPartial ? "123456" & ChrW(185) & "789" : ChrW(185) & "23456789") ;## ChrW(185) = Superscript One
15
16        $hFile = FileOpen(@TempDir & '\utf8_test.txt', ($bUseUTF8 ? 130 : 34)) ;## 130 = ($FO_UTF8 + $FO_OVERWRITE), 34 = ($FO_UTF16_LE + $FO_OVERWRITE)
17        If $hFile <> -1 Then
18                FileWrite($hFile, $sToWrite)
19                FileClose($hFile)
20        Else
21                MsgBox(16, "UTF-" & ($bUseUTF8 ? "8" : "16") & " Test", "Failed to create test file")
22                Exit (1)
23        EndIf
24
25        $hFile = FileOpen(@TempDir & '\utf8_test.txt', ($bUseUTF8 ? 128 : 32)) ;## 128 = $FO_UTF8, 32 = $FO_UTF16_LE
26        If $hFile <> -1 Then
27                $iStartPos = FileGetPos($hFile)
28                $sChars = FileRead($hFile, $iCharsToRead)
29                $iEndPos = FileGetPos($hFile)
30                $iStrLen = StringLen($sChars)
31                $iBinLen = BinaryLen(StringToBinary($sChars, ($bUseUTF8 ? 4 : 2))) ;## 4 = UTF-8, 2 = UTF-16LE
32                FileClose($hFile)
33        Else
34                MsgBox(16, "UTF-" & ($bUseUTF8 ? "8" : "16") & " Test", "Failed to open test file")
35                Exit (2)
36        EndIf
37
38        FileDelete(@TempDir & '\utf8_test.txt')
39        Return StringFormat("UTF-%s Test\r\n-------\r\nString read: %s\r\nStarting offset: %i\r\nEnding offset: %i\r\n" & _
40                                                "Characters read: %i\r\nExpected characters read: %i\r\nBytes read: %i\r\n\r\n", _
41                                                ($bUseUTF8 ? "8" : "16") & ($bPartial ? " - Odd offset" : ""), $sChars, $iStartPos, $iEndPos, $iStrLen, $iCharsToRead, $iBinLen)
42EndFunc