GuessEncoding function
Tries to determine the encoding used for the specified value.
Declaration
function GuessEncoding(const s: string): stringArguments
s | String with the content examined in the routine. |
Function result
Encoding name detected, or a default value.
Description
GuessEncoding is a
String function which tries to
determine the encoding used for the value specified in
S. The
return value contains an encoding name, like 'utf8' or 'ISO-8859-1'. It may
contain an empty string ('') when S is also an empty string.
GuessEncoding checks S for various Byte Order Marks at the start of the
value, including: UTF8BOM, UTF16LEBOM, and
UTF16BEBOM. When present, the BOM determines the encoding used for
the value.
Finally, it checks for a valid UTF-8 encoding (which includes ASCII characters). All characters in S are examined until a character whose UTF-8 code point is not valid is encountered.
If encoding cannot be determined, the default encoding for the platform is used.
When it is EncodingUTF8, it is changed to 'ISO-8859-1'.
This is done because the system may use the UTF-8 encoding, but the value in S does not.
ISO 8859-1 has a full mapping to Unicode, and this prevents data loss in encoding conversions.
An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
