Latest post of the previous page:
To may way of thinking,
if we work on editor selection, it can be only text data, or supposed to be text data, not binary.
I would say what those first two bytes of text have very easy detectable signature.
It is always first two bytes, and always only very certain bytes.
Detecting of UTF8 strings is necessary for any text handling nowadays.
Here is the Lua-code for it, can be easily translated to Pascal.
it is simple and bug-free (er.. well, should be),
handling any kind of unicode (not only UTF8):
Code: Select all
local fnc_det_bom_str = function(str_head)
local boms = {
{name = "ASCI", code = ""},
{name = "32LE", code = '\255\254\0\0'}, -- utf32le
{name = "32BE", code = '\0\0\254\255'}, -- utf32be
{name = "16LE", code = '\255\254'}, -- utf16le
{name = "16BE", code = '\254\255'}, -- utf16be
{name = "UTF8", code = '\239\187\191'} -- utf8
}
local str_bom
local res = 1
for ii = #boms, 3, -1
do str_bom = string.sub(str_head, 1, #(boms[ii].code))
if str_bom == boms[ii].code
then if ii == 4 and str_bom == boms[2].code then ii = 2 end
res = ii
break
end
end
return boms[res].name, boms[res].code
end
local fnc_read_header_text = function(file_name)
local fp = io.open(filename)
if not fp then return end
local s = fp:read(512)
fp:close()
return s:gsub("%s+", " "):match("[%w_].*")
end
local fnc_det_bom_file = function(file_name)
local str_head, bom_name, bom_code
local fHnd = io.open(file_name, "rb")
if fHnd
then str_head = fHnd:read(4) or ''
fHnd:close()
bom_name, bom_code = Xer0X.fnc_det_bom_str(str_head)
end
return bom_name, bom_code
end