Page 1 of 1

Error when open file in UTF-8 w/o BOM

Posted: Sun Dec 12, 2021 3:21 pm
by Allen
Hi,

I found error when load file stored in UTF-8 w/o BOM. Please copy and paste below code in window note, save the file as test.pb in UTF-8 (not UTF-8 BOM). Load and run. Please confirm if this is a feature? can IDE accept file in UTF-8 format only?

Code: Select all

Global.s No$="零一二三四五六七八九十百千萬"
Debug No$
Thanks

Allen

Re: Error when open file in UTF-8 w/o BOM

Posted: Sun Dec 12, 2021 3:46 pm
by mk-soft
In the PB settings you must set the source file text encoding under Compiler -> Defaults.
By default, the file is always interpreted as UTF8. (Which is also better)

Re: Error when open file in UTF-8 w/o BOM

Posted: Sun Dec 12, 2021 4:02 pm
by Allen
Thanks for the advise.

Under File>Preferences>Complier>Defaults>SourceFile Text Encoding>

there is two choices, Plain Text and UTF-8. I already choose UTF-8. It did not work.

I make two identical files in UTF-8, one with BOM and one w/o, loaded in IDE, the files look exactly the same but the one w/o BOM did not run properly. Even use file compare function, the files look identical.

Any suggestions ?

Re: Error when open file in UTF-8 w/o BOM

Posted: Sun Dec 12, 2021 4:48 pm
by STARGÅTE
A file w/o BOM is always interpreted as ASCII.
If no BOM is in the file, how the string format should be stored?

UTF-8 files must have a BOM to load correctly as UTF-8 in IDE, independent from the default configuration settings.

Re: Error when open file in UTF-8 w/o BOM

Posted: Sun Dec 12, 2021 4:54 pm
by mk-soft
Any file without BOM is open alway as ascii, because the file can save as plain text

Small Helper ... Only for UTF8 file with missing BOM (invalid file format)

Code: Select all

;-TOP

Procedure.s AddUTF8BOM(FileName.s)
  Protected file, newfile, NewFileName.s, ft, context.s
  file = ReadFile(#PB_Any, FileName)
  If file
    ft = ReadStringFormat(file)
    If ft = #PB_Ascii
      NewFileName = FileName + ".utf8"
      newfile = CreateFile(#PB_Any, NewFileName)
      If newfile
        WriteStringFormat(newfile, #PB_UTF8)
        context = ReadString(file, #PB_File_IgnoreEOL)
        WriteStringN(newfile, context)
        CloseFile(newfile)
      EndIf
    EndIf
    CloseFile(file)
  EndIf
  ProcedureReturn NewFileName
EndProcedure

file.s = OpenFileRequester("Textfile", "", "", 0)
r1.s = AddUTF8BOM(file.s)
Debug r1


Re: Error when open file in UTF-8 w/o BOM

Posted: Mon Dec 13, 2021 1:54 am
by Allen
Thanks STARGÅTE for the clarification.
Thanks mk-soft for the example.

Allen