[1.1.34.04] FileAppend default encoding with an empty string Topic is solved

Report problems with documented functionality
tester
Posts: 84
Joined: 10 Jun 2021, 23:03

[1.1.34.04] FileAppend default encoding with an empty string

Post by tester » 21 Sep 2022, 02:48

Hello,

Please run the code below. The second function, FileAppend_EncodingEmptyString(), produces a broken character. The function passes an empty string to the third parameter of FileAppend. I expected that it uses the same value as A_FileEncoding when an empty string is passed. But it seems to use the system default's encoding as the documentation for FileEncoding says as below
One of the following values (if omitted, it defaults to the system default ANSI code page, which is also the default setting):
It also says,
The default encoding is not used if a UTF-8 or UTF-16 byte order mark is present in the file, unless the file is being opened with write-only access (i.e. the previous contents of the file are being discarded).
But it doesn't apply to a case that the parameter is omitted seen in FileAppend_EncodingOmitted(). So the documentation doesn't explain why characters get broken with an empty string parameter value.

Code: Select all

FileEncoding, UTF-8

sPath := A_ScriptDir "\test-01.txt"
For _i, sFuncName in [ "FileAppend_EncodingOmitted", "FileAppend_EncodingEmptyString", "FileAPpend_EncodingSpecified" ] {
    FileDelete, % sPath
    FileAppend, % "✔️", % sPath
    FileRead, sRaw, % sPath
    sModified := StrReplace(sRaw, "✔️", "✔️ Modified")
    %sFuncName%(sModified, sPath) ; performs FileAppend
    FileRead, sResult, % sPath
    sExpected := "✔️ Modified"
    bPassed   := sExpected == sResult
    msgbox, % bPassed ? "" : 16, % sFuncName ": " (bPassed ? "Passed" : "Failed"), % ""
        . "Expected: " sExpected "`n"
        . "Actual: " sResult "`n"    
}

FileAppend_EncodingOmitted(sText, sPathFile) {
    FileDelete, % sPathFile
    FileAppend, % sText, % sPathFile
}
FileAppend_EncodingEmptyString(sText, sPathFile, sEncoding:="") {
    FileDelete, % sPathFile    
    FileAppend, % sText, % sPathFile, % sEncoding
}
FileAppend_EncodingSpecified(sText, sPathFile, sEncoding:="") {
    FileDelete, % sPathFile
    FileAppend, % sText, % sPathFile, % sEncoding ? sEncoding : A_FileEncoding
}

lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: [1.1.34.04] FileAppend default encoding with an empty string

Post by lexikos » 21 Sep 2022, 04:19

Your code and words are convoluted and confusing. The issue is essentially very simple:

Code: Select all

FileEncoding UTF-8 ; This line is irrelevant
FileAppend ✔️, test-02.txt, % "" ; This is the same as "CP0"
I expected that it uses the same value as A_FileEncoding when an empty string is passed.
The documentation merely says that the parameter overrides the default setting. You specified the parameter, and the default setting was overridden. Go figure.

With the current documentation, FileAppend using A_FileEncoding, or CP0, or throwing an error, would all be reasonable. Relying on any particular behaviour is unsafe, because the documentation has given you no guarantee.

The documentation was reworded for v2 when FileAppend's syntax was changed. Compare:
v1 wrote:Overrides the default encoding set by FileEncoding, where Encoding follows the same format.
v2 wrote:Encoding: Specify any of the encoding names accepted by FileEncoding (excluding the empty string) to use that encoding if the file lacks a UTF-8 or UTF-16 byte order mark. If omitted, it defaults to A_FileEncoding ...
For v2, there is a clear difference between omitting a parameter and passing a blank value.

For v1, many commands (and fewer functions) treat the two conditions as the same, but those are (or should be) documented with "If blank or omitted".

The third parameter is interpreted like so:

Code: Select all

		UINT codepage = mArgc > 2 ? ConvertFileEncoding(ARG3) : g->Encoding;

Code: Select all

	static UINT ConvertFileEncoding(LPTSTR aBuf)
	// Returns the encoding with possible CP_AHKNOBOM flag, or (UINT)-1 if invalid.
	{
		if (!aBuf || !*aBuf)
			// Active codepage, equivalent to specifying CP0.
			return CP_ACP;
	...
Thus, there is no difference between passing an empty string and passing CP0.

It will be changed.


tester
Posts: 84
Joined: 10 Jun 2021, 23:03

Re: [1.1.34.04] FileAppend default encoding with an empty string

Post by tester » 12 Dec 2022, 23:52

Thank you!

Post Reply

Return to “Bug Reports”