Difference between revisions of "Character and string types/fr"

From Free Pascal wiki
Jump to navigationJump to search
m (Fixed syntax highlighting; deleted category already in page template)
 
(42 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{Character and string types}}
 
{{Character and string types}}
  
Free Pascal supportes plusieurs types de caractères et de chaînes. Il s'étendent du simple caractère ANSI aux chaînes Unicode et incluent aussi les types pointer. Les différence s'appliquent aussi aux encodages et au comptage de référence.
+
Free Pascal supportes plusieurs '''types de [[Char/fr|caractères]] et de [[String/fr|chaîne de caractères]]'''. Il s'étendent du simple caractère ANSI aux chaînes Unicode et incluent aussi les types pointer. Les différence s'appliquent aussi aux encodages et au comptage de référence.
  
 
== AnsiChar ==
 
== AnsiChar ==
  
Une variable de type '''AnsiChar''', aussi mentionnée comme '''char''', a une taille d'exactement un octet et contient un caractère ANSI.
+
Une variable de type '''AnsiChar''', aussi mentionnée comme '''[[Character_and_string_types/fr#AnsiChar|Char]]''', a une taille d'exactement un octet et contient un caractère ANSI.
  
 
{| class="wikitable" style="text-align:center; width:25px"
 
{| class="wikitable" style="text-align:center; width:25px"
Line 12: Line 12:
 
|}
 
|}
  
==== Reference ====
+
==== Référence ====
* [http://www.freepascal.org/docs-html/ref/refsu7.html FPC AnsiChar documentation]
+
* [http://www.freepascal.org/docs-html/ref/refsu7.html Documentation FPC sur les AnsiChar]
* [[Char|Usage Char]]
+
* [[Char/fr|Usage de Char]]
  
 
== WideChar ==
 
== WideChar ==
  
A variable of type '''WideChar''', also referred to as '''UnicodeChar''', is exactly 2 bytes in size, and contains one (part of) [[LCL Unicode Support/fr|Unicode]] character in UTF-16 encoding.
+
Une variable du type '''WideChar''', aussi mentionnée comme'''UnicodeChar''', est exactement d'une taille de 2 octets, et contient une partie des caractères [[LCL Unicode Support/fr|Unicode]] dans l'encodage UTF-16.
Note: it is impossible to encode all Unicode code points in 2 bytes. Therefore, 2 WideChars may be needed to encode a single code point.
+
Note : il est impossible d'encodes tous les points de code Unicode dans 2 octets. En conséquence, 2 WideChars pourraient être nécessaires pour encoder un simple point de code.
  
 
{| class="wikitable" style="text-align:center; width:50px"
 
{| class="wikitable" style="text-align:center; width:50px"
Line 26: Line 26:
 
|}
 
|}
  
==== References ====
+
==== Références ====
* [http://www.freepascal.org/docs-html/ref/refsu8.html FPC WideChar documentation]
+
* [http://www.freepascal.org/docs-html/ref/refsu8.html Documentation FPC sur les WideChar]
* [https://en.wikipedia.org/wiki/UTF-16 UTF-16 information on Wikipedia]
+
* [https://fr.wikipedia.org/wiki/UTF-16 Information sur l'UTF-16 dans Wikipedia]
* [[doc:rtl/system/unicodechar.html|RTL UnicodeChar documentation]]
+
* [[doc:rtl/system/unicodechar.html|Documentation sur RTL UnicodeChar]]
  
== Array of Char ==
+
== Tableau de Char ==
  
Early Pascal implementations that were in use before 1978 didn't support a string type (with the exception of string constants). The only possibility to store strings in variables was the use of arrays of char. This approach has many disadvantages and is no longer recommended. It is, however, still supported to ensure backward-compatibility with ancient code.
+
Les premières implémentations de Pascal, utilisées avant 1978, ne supportaient pas le type chaîne (exception faite des constantes chaînes). La seule possibilité pour stocker des chaînes dans des variables consistaient à utiliser des tableaux de char. Cette approche a de nombreux inconvénients et n'est plus recommandée. C'est néanmoins toujours supporté pour assurer la compatibilité descendante avec l'ancien code.
  
===Static Array of Char===
+
===Tableau de Char statique===
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
type
 
type
 
   TOldString4 = array[0..3] of char;
 
   TOldString4 = array[0..3] of char;
Line 48: Line 48:
 
   aOldString4[3] := 'd';
 
   aOldString4[3] := 'd';
 
end;
 
end;
</syntaxhighlight> The static array of char has now the content:
+
</syntaxhighlight> Le tableau de char statique contient dorénavant:
  
 
{| class="wikitable" style="text-align:center; width:100px"
 
{| class="wikitable" style="text-align:center; width:100px"
Line 55: Line 55:
 
|}
 
|}
  
{{Note|Unassigned chars can have any content, depending on what was just in memory when the memory for the array was made available.}}
+
{{Note|Les chars non affectés contiennent n'importe quoi, en fonction de ce qui se trouvait en mémoire quand la mémoire pour le tableau est devenue disponible (le contenu du tableau n'est pas initialisé).}}
  
===Dynamic Array of Char===
+
===Tableau de Char dynamique===
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
var
 
var
   aOldString: Array of Char;  
+
   aOldString: Array of Char; // pas de dimension!!
 
begin
 
begin
 
   SetLength(aOldString, 5);
 
   SetLength(aOldString, 5);
Line 69: Line 69:
 
   aOldString[3] := 'd';
 
   aOldString[3] := 'd';
 
end;
 
end;
</syntaxhighlight> The dynamic array of char has now the content:
+
</syntaxhighlight> Le tableau de char dynamique contient dorénavant:
  
 
{| class="wikitable" style="text-align:center; width:100px"
 
{| class="wikitable" style="text-align:center; width:100px"
Line 76: Line 76:
 
|}
 
|}
  
{{Note|Unassigned chars in dynamic arrays have a content #0, cause empty positions of all dynamic arrays are initially initialised with 0 (or #0, or nil, or ...)}}
+
{{Note|Les chars non affectés dans les tableaux dynamiques contiennent des #0, parce que les positions vides de tous les tableaux dynamiques sont dès le départ initialisées avec 0 (ou #0, ou nil, ou ...)}}
  
 
== PChar ==
 
== PChar ==
  
A variable of type '''PChar''' is basically a pointer to a '''[[Character_and_string_types#AnsiChar|Char]]''' type, but allows additional operations. PChars can be used to access C-style [http://en.wikipedia.org/wiki/Null-terminated_string null-terminated strings], e.g. in interaction with certain OS libraries or third-party software.
+
Une variable de type '''PChar''' est simplement un pointeur vers un type '''[[#AnsiChar|Char]]''', mais permet des opérations supplémentaires. Les PChars peuvent être utilisé pour accéder à des [http://en.wikipedia.org/wiki/Null-terminated_string chaînes C à zéro terminal], par exemple en interaction avec certaines API systèmes ou des logiciels tierce partie.
  
 
{| class="wikitable" style="text-align:center; width:100px"
 
{| class="wikitable" style="text-align:center; width:100px"
Line 89: Line 89:
 
|}
 
|}
  
==== Reference ====
+
==== Référence ====
* [http://www.freepascal.org/docs-html/ref/refsu16.html FPC PChar documentation]
+
* [http://www.freepascal.org/docs-html/ref/refsu16.html Documentation FPC sur les PChar]
* [[doc:rtl/sysutils/pcharfunctions.html|PChar related functions]]
+
* [[doc:rtl/sysutils/pcharfunctions.html|Fonctions associées à PChar]]
 
 
  
 
== PWideChar ==
 
== PWideChar ==
  
A variable of type '''PWideChar''' is a pointer to a [[#WideChar|WideChar]] variable.
+
Une variable de type '''PWideChar''' est un pointeur vers une variable [[Character_and_string_types/fr#WideChar|WideChar]].
  
 
{| class="wikitable" style="text-align:center; width:200px"
 
{| class="wikitable" style="text-align:center; width:200px"
Line 105: Line 104:
 
|}
 
|}
  
==== Reference ====
+
==== Référence ====
* [[doc:rtl/system/pwidechar.html|RTL PWideChar documentation]]
+
* [[doc:rtl/system/pwidechar.html|Documentation sur RTL PWideChar]]
  
 
== String ==
 
== String ==
  
The type '''String''' may refer to '''[[#ShortString|ShortString]]''' or '''[[#AnsiString|AnsiString]]''', depending from the [http://www.freepascal.org/docs-html/prog/progsu25.html#x32-310001.2.25 {$H} switch]. If the switch is off ({$H-}) then any string declaration will define a '''ShortString'''. It size will be 255 chars, if not otherwise specified. If it is on ({$H+}) '''string''' without length specifier will define an '''AnsiString''', otherwise a '''ShortString''' with specified length.
+
Le type '''String''' peut faire référence à '''[[#ShortString|ShortString]]''' ou '''[[#AnsiString|AnsiString]]''', selon la  [http://www.freepascal.org/docs-html/prog/progsu25.html#x32-310001.2.25 bascule {$H}]. Si la bascule est off ({$H-}) alors toute déclaration string va définir une '''ShortString'''. Sa taille sera de 255 caractères, si elle  n'est pas spécifiée autrement. Si la bascule est on ({$H+}) '''string''' sans spécifier la taille va définir une '''AnsiString''', sinon une '''ShortString''' avec une taille spécifiée. Dans le '''mode delphiunicode''' '''String'' est '''[[#UnicodeString|UnicodeString]]'''.
  
==== Reference ====
+
==== Référence ====
* [[String|Usage String]]
+
* [[String/fr|Usage des String]]
* [[doc:rtl/sysutils/stringfunctions.html|String functions]]
+
* [[doc:rtl/sysutils/stringfunctions.html|Fonctions sur les chaînes]]
* [[doc:rtl/strutils/index-5.html|Reference for unit 'strutils': Procedures and functions]]
+
* [[doc:rtl/strutils/index-5.html|Référence sur l'unité 'strutils': Procédures et fonctions]]
  
 
== ShortString ==
 
== ShortString ==
  
Short strings have a maximum length of 255 characters with the implicit [[FPC Unicode support#Codepages|codepage]] CP_ACP. The length is stored in the character at index 0.
+
Les chaînes courtes ont une taille maximale de 255 caractères avec la [[FPC Unicode support#Codepages|page de code]] CP_ACP implicite. La longueur effective de la chaîne est conservée dans le caractère à l'index 0.
 
 
 
{| class="wikitable" style="text-align:center; width:100px"
 
{| class="wikitable" style="text-align:center; width:100px"
 
|-  
 
|-  
Line 126: Line 124:
 
|}
 
|}
  
==== Reference ====
+
==== Référence ====
* [[doc:ref/refsu12.html|FPC AnsiString documentation]]
+
* [http://www.freepascal.org/docs-html/ref/refsu11.html#x34-370003.2.5 Documentation FPC sur ShortString]
  
 
== AnsiString ==
 
== AnsiString ==
  
Ansistrings or UTF8Strings are strings that have no length limit. They are [http://en.wikipedia.org/wiki/Reference_counting reference counted] and are guaranteed to be [http://en.wikipedia.org/wiki/Null-terminated_string null terminated]. Internally, a variable of type '''AnsiString''' is treated as a pointer: the actual content of the string is stored on the heap, as much memory as needed to store the string content is allocated.
+
Les Ansistrings ou les UTF8Strings sont des chaînes qui n'ont pas de limite de taille. Le [http://en.wikipedia.org/wiki/Reference_counting comptage de référence] s'applique à elle et il est garanti qu'elles soient à [http://en.wikipedia.org/wiki/Null-terminated_string zéro terminal]. En interne, une variable de type '''AnsiString''' est traitée comme un pointeur: le contenu réel de la chaîne se trouve dans le tas, la mémoire nécessaire à son contenu étant allouée.
  
 
{| class="wikitable" style="text-align:center; width:300px"
 
{| class="wikitable" style="text-align:center; width:300px"
Line 140: Line 138:
 
|}
 
|}
  
==== Reference ====
+
==== Référence ====
* [http://www.freepascal.org/docs-html/ref/refsu12.html FPC AnsiString documentation]
+
* [http://www.freepascal.org/docs-html/ref/refsu12.html Documentation FPC sur les AnsiString]
  
 
== UnicodeString ==
 
== UnicodeString ==
  
Like '''AnsiStrings''', '''UnicodeStrings''' are reference counted, null-terminated arrays, but they are implemented as arrays of '''[[#WideChar|WideChars]]''' instead of regular '''[[#Char|Chars]]'''.
+
Telles les '''AnsiStrings''', '''UnicodeStrings''' sont sujette au comptage de référence, sont des tableau à zéro terminal, mais elles sont implémentées comme des tableaux de '''[[#WideChar|WideChars]]''' au lieu de '''[[#AnsiChar|Chars]]''' ordinaires.
  
{{Note|The UnicodeString naming is a bit ambiguous but probably due to its use in Delphi on Windows, where the OS uses UTF16 encoding; it's not the only string type that can hold Unicode string data (see also UTF8String)...}}
+
{{Note|L'appelation UnicodeString est un peu ambiguë mais provient sans doute de son emploi dans Delphi sur Windows, où le système d'exploitation utilise l'encodage UTF-16 ; ce n'est pas le seul type de chaîne qui permet de contenir des données Unicode (voir aussi UTF8String)...}}
  
 
{| class="wikitable" style="text-align:center; width:390px"
 
{| class="wikitable" style="text-align:center; width:390px"
Line 156: Line 154:
 
|}
 
|}
  
==== Reference ====
+
==== Référence ====
* [http://www.freepascal.org/docs-html/ref/refsu13.html FPC UnicodeString documentation]
+
* [http://www.freepascal.org/docs-html/ref/refsu13.html Documentation FPC sur UnicodeString]
  
 
== UTF8String ==
 
== UTF8String ==
  
Currently, the type '''UTF8String''' is an alias to the type '''[[#AnsiString|AnsiString]]'''. It is meant to contain UTF8 encoded strings (i.e. unicode data ranging from 1..4 bytes per character). UTF8String is the default string in Lazarus and LCL.
+
Actuellement, le type '''UTF8String''' est un alias sur le type '''[[#AnsiString|AnsiString]]'''. Il est destiné à contenir des chaînes encodées en UTF-8 (i.e. des données Unicode) allant de 1 à 4 octets par caractère. UTF8String est le type par défaut dans Lazarus et la  LCL.
 +
UTF8String est le type de chaîne par défaut dans Lazarus et la LCL.
  
==== Reference ====
+
==== Référence ====
* [[doc:rtl/system/utf8string.html|RTL UTF8String documentation]]
+
* [[doc:rtl/system/utf8string.html|Documentation sur RTL UTF8String]]
  
 
== UTF16String ==
 
== UTF16String ==
  
The type '''UTF16String''' is an alias to the type '''[[#WideString|WideString]]'''. In the LCL unit ''lclproc'' it is an alias to '''[[#UnicodeString|UnicodeString]]'''.
+
Le type '''UTF16String''' est un alias sur le type '''[[#WideString|WideString]]'''. Dans l'unité LCL ''lclproc'', c'est un alias sur '''[[#UnicodeString|UnicodeString]]'''.
  
==== Reference ====
+
==== Référence ====
* [[doc:lcl/lclproc/utf16string.html|LCL UTF16String documentation]]
+
* [[doc:lcl/lclproc/utf16string.html|Documentation sur LCL UTF16String]]
  
 
== WideString ==
 
== WideString ==
  
Variables of type '''[[Widestrings|WideString]]''' (used to represent unicode character strings in COM applications) resemble those of type '''UnicodeString''', but unlike them they are not reference-counted. On Windows they are allocated with a special windows function which allows them to be used for OLE automation.
+
Les variables du type '''[[Widestrings/fr|WideString]]''' (utilisées pour représenter les chaînes de caractères Unicode dans les applications COM) ressemblent à celle du type '''UnicodeString''', mais contrairement à elles, elles ne bénéficient pas du comptage de références. Dans Windows, elles sont allouées une fonction spéciale de Windows qui leur permet d'être utilisées dans OLE Automation.
  
WideStrings consist of COM compatible UTF16 encoded bytes on Windows machines (UCS2 on Windows 2000), and they are encoded as plain UTF16 on Linux, Mac OS X and iOS.
+
Les WideStrings consistent en octets encodés UTF-16 comppatible COM sur les machines Windows (UCS2 sur Windows 2000) et elles sont encodées en UTF-16 pur sur Linux, Mac OS et iOS.
  
 
{| class="wikitable" style="text-align:center; width:300px"
 
{| class="wikitable" style="text-align:center; width:300px"
Line 186: Line 185:
 
|}
 
|}
  
==== Reference ====
+
==== Référence ====
* [http://www.freepascal.org/docs-html/ref/refsu14.html#x37-400003.2.8 FPC WideString documentation]
+
* [http://www.freepascal.org/docs-html/ref/refsu14.html#x37-400003.2.8 Documentation FPC sur WideString]
  
 
== PShortString ==
 
== PShortString ==
  
A variable of type '''PShortString''' is a pointer that points to the first byte of a '''[[#ShortString|ShortString]]'''-type variable (which defines the length of the ShortString).
+
Une variable du type '''PShortString''' est un pointeur qui désigne le premier octet d'une variable de type '''[[#ShortString|ShortString]]''' (lequel définit la longueur de la ShortString).
  
 
{| class="wikitable" style="text-align:center; width:100px"
 
{| class="wikitable" style="text-align:center; width:100px"
Line 200: Line 199:
 
|}
 
|}
  
==== Reference ====
+
==== Référence ====
* [[doc:rtl/system/pshortstring.html|RTL PShortString documentation]]
+
* [[doc:rtl/system/pshortstring.html|Documentation RTL sur PShortString]]
  
 
== PAnsiString ==
 
== PAnsiString ==
  
Variables of type '''PAnsiString''' are pointers to '''[[#AnsiString|AnsiString]]'''-type variables. However, unlike '''PShortString'''-type variables they don't point to the first byte of the header, but to the first '''char''' of the '''AnsiString'''.
+
Les variables de type '''PAnsiString''' sont des pointeurs sur des variables de type '''[[#AnsiString|AnsiString]]'''. Néanmoins, contrairement aux variables de type '''PShortString''', ils ne pointent pas sur le premier octet d'un en-tête mais sur le premier '''char''' de l''''AnsiString'''.
  
 
{| class="wikitable" style="text-align:center; width:300px"
 
{| class="wikitable" style="text-align:center; width:300px"
Line 214: Line 213:
 
|}
 
|}
  
==== Reference ====
+
==== Référence ====
* [[doc:rtl/system/pansistring.html|RTL PAnsiString documentation]]
+
* [[doc:rtl/system/pansistring.html|Documentation RTL sur PAnsiString]]
  
 
== PUnicodeString ==
 
== PUnicodeString ==
  
Variables of type '''PUnicodeString''' are pointers to variables of type '''[[#UnicodeString|UnicodeString]]'''.
+
Les variables de type '''PUnicodeString''' sont des pointeurs vers les variables de type '''[[#UnicodeString|UnicodeString]]'''.
  
 
{| class="wikitable" style="text-align:center; width:390px"
 
{| class="wikitable" style="text-align:center; width:390px"
Line 228: Line 227:
 
|}
 
|}
  
==== Reference ====
+
==== Référence ====
* [[doc:rtl/system/punicodestring.html|RTL PUnicodeString documentation]]
+
* [[doc:rtl/system/punicodestring.html|Documentation RTL sur PUnicodeString]]
  
 
== PWideString ==
 
== PWideString ==
  
Variables of type '''PWideString''' are pointers. They point to the first char of a '''[[#WideString|WideString]]'''-typed variable.
+
Les variables du type '''PWideString''' sont des pointeurs. Elles pointent sur le premier caractère d'une variable de type '''[[#WideString|WideString]]'''.
  
 
{| class="wikitable" style="text-align:center; width:300px"
 
{| class="wikitable" style="text-align:center; width:300px"
Line 242: Line 241:
 
|}
 
|}
  
==== Reference ====
+
==== Référence ====
* [[doc:rtl/system/pwidestring.html|RTL PWideString documentation]]
+
* [[doc:rtl/system/pwidestring.html|Documentation RTL sur PWideString]]
 +
 
 +
== Constantes String ==
 +
Si vous utilisez des constantes an Anglais, vos chaînes fonctionnent de la même façon avec tous les types, sur toutes les plateformes et toutes les versions du compilateur. Les chaînes 'non anglaises' peuvent eêtre chargées depuis des chaînes de ressource ou depuis des fichiers. Si vous utilisez des chaînes non anglaises dans votre code, vous devriez lire ce qui suit.
  
== See also ==
+
 
* [[FPC Unicode support]]
+
There are various encodings for non English strings. By default Lazarus saves Pascal files as '''UTF-8 without BOM'''. UTF-8 supports the full Unicode range. That means all string constants are stored in UTF-8. Lazarus also supports to change the encoding of a file to other encoding, for example under Windows your local codepage. The Windows codepage is limited to your current language group.
 +
 
 +
{| class="wikitable sortable"
 +
! String Type, UTF-8 Source !! With or without {$codepage utf8} !! FPC 2.6.5 and below !! FPC 2.7.1 and above !! FPC 2.7.1+ with UTF8 as default CodePage
 +
|----
 +
|AnAnsiString:='ãü';||Without||Needs UTF8ToAnsi in RTL/WinAPI. Ok in LCL||Needs UTF8ToAnsi in RTL/WinAPI. Ok in LCL||Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI.
 +
|----
 +
|AnAnsiString:='ãü';||With||System cp ok in RTL/WinAPI. Needs SysToUTF8 in LCL||Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp||Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI
 +
|----
 +
|AnUnicodeString:='ãü';||Without||Wrong everywhere||Wrong everywhere||Wrong everywhere
 +
|----
 +
|AnUnicodeString:='ãü';||With||System cp ok in RTL/WinAPI. Needs UTF8Encode in LCL||Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp ||Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI
 +
|----
 +
|AnUTF8String:='ãü';||Without||Same as AnsiString||Wrong everywhere||Wrong everywhere
 +
|----
 +
|AnUTF8String:='ãü';||With||Same as AnsiString||Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp ||Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI.
 +
|}
 +
 
 +
*W-WinAPI = Windows API "W" functions, UTF-16
 +
*A-WinAPI = Windows API non "W" functions, 8bit system code page
 +
*System CP = The 8bit system code page of the OS. For example code page [http://en.wikipedia.org/wiki/Windows-1252 1252].
 +
 
 +
<syntaxhighlight lang="pascal">
 +
const
 +
  c='ãü';
 +
  cstring: string = 'ãü'; // see AnAnsiString:='ãü';
 +
var
 +
  s: string;
 +
  u: UnicodeString;
 +
begin
 +
  s:=c; // same as s:='ãü';
 +
  s:=cstring; // does not change encoding
 +
  u:=c; // same as u:='ãü';
 +
  u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring
 +
end;
 +
</syntaxhighlight>
 +
 
 +
== Voir aussi ==
 +
* [[FPC Unicode support/fr|Support de l'Unicode par FPC]]
 
* [[LCL_Unicode_Support/fr|Support de l'Unicode par la LCL]]
 
* [[LCL_Unicode_Support/fr|Support de l'Unicode par la LCL]]
* [[TStringList-TStrings Tutorial]]
+
* [[TStringList-TStrings Tutorial/fr|Tutoriel TStringList-TStrings]]
 
+
* [http://www.codexterity.com/delphistrings.htm A Brief History of Strings]
[[Category: FPC]]
 
[[Category: RTL]]
 
[[Category: Data types]]
 
[[Category: Unicode]]
 

Latest revision as of 01:24, 11 February 2020

Deutsch (de) English (en) español (es) français (fr) русский (ru) 中文(中国大陆)‎ (zh_CN)

Free Pascal supportes plusieurs types de caractères et de chaîne de caractères. Il s'étendent du simple caractère ANSI aux chaînes Unicode et incluent aussi les types pointer. Les différence s'appliquent aussi aux encodages et au comptage de référence.

AnsiChar

Une variable de type AnsiChar, aussi mentionnée comme Char, a une taille d'exactement un octet et contient un caractère ANSI.

a

Référence

WideChar

Une variable du type WideChar, aussi mentionnée commeUnicodeChar, est exactement d'une taille de 2 octets, et contient une partie des caractères Unicode dans l'encodage UTF-16. Note : il est impossible d'encodes tous les points de code Unicode dans 2 octets. En conséquence, 2 WideChars pourraient être nécessaires pour encoder un simple point de code.

a

Références

Tableau de Char

Les premières implémentations de Pascal, utilisées avant 1978, ne supportaient pas le type chaîne (exception faite des constantes chaînes). La seule possibilité pour stocker des chaînes dans des variables consistaient à utiliser des tableaux de char. Cette approche a de nombreux inconvénients et n'est plus recommandée. C'est néanmoins toujours supporté pour assurer la compatibilité descendante avec l'ancien code.

Tableau de Char statique

type
  TOldString4 = array[0..3] of char;
var
  aOldString4: TOldString4; 
begin
  aOldString4[0] := 'a';
  aOldString4[1] := 'b';
  aOldString4[2] := 'c';
  aOldString4[3] := 'd';
end;

Le tableau de char statique contient dorénavant:

a b c d
Light bulb  Remarque: Les chars non affectés contiennent n'importe quoi, en fonction de ce qui se trouvait en mémoire quand la mémoire pour le tableau est devenue disponible (le contenu du tableau n'est pas initialisé).

Tableau de Char dynamique

var
  aOldString: Array of Char; // pas de dimension!!
begin
  SetLength(aOldString, 5);
  aOldString[0] := 'a';
  aOldString[1] := 'b';
  aOldString[2] := 'c';
  aOldString[3] := 'd';
end;

Le tableau de char dynamique contient dorénavant:

a b c d #0
Light bulb  Remarque: Les chars non affectés dans les tableaux dynamiques contiennent des #0, parce que les positions vides de tous les tableaux dynamiques sont dès le départ initialisées avec 0 (ou #0, ou nil, ou ...)

PChar

Une variable de type PChar est simplement un pointeur vers un type Char, mais permet des opérations supplémentaires. Les PChars peuvent être utilisé pour accéder à des chaînes C à zéro terminal, par exemple en interaction avec certaines API systèmes ou des logiciels tierce partie.

a b c #0
^

Référence

PWideChar

Une variable de type PWideChar est un pointeur vers une variable WideChar.

a b c #0 #0
^

Référence

String

Le type String' peut faire référence à ShortString ou AnsiString, selon la bascule {$H}. Si la bascule est off ({$H-}) alors toute déclaration string va définir une ShortString. Sa taille sera de 255 caractères, si elle n'est pas spécifiée autrement. Si la bascule est on ({$H+}) string sans spécifier la taille va définir une AnsiString, sinon une ShortString avec une taille spécifiée. Dans le mode delphiunicode String est UnicodeString.

Référence

ShortString

Les chaînes courtes ont une taille maximale de 255 caractères avec la page de code CP_ACP implicite. La longueur effective de la chaîne est conservée dans le caractère à l'index 0.

#3 a b c

Référence

AnsiString

Les Ansistrings ou les UTF8Strings sont des chaînes qui n'ont pas de limite de taille. Le comptage de référence s'applique à elle et il est garanti qu'elles soient à zéro terminal. En interne, une variable de type AnsiString est traitée comme un pointeur: le contenu réel de la chaîne se trouve dans le tas, la mémoire nécessaire à son contenu étant allouée.

a b c #0
RefCount Length

Référence

UnicodeString

Telles les AnsiStrings, UnicodeStrings sont sujette au comptage de référence, sont des tableau à zéro terminal, mais elles sont implémentées comme des tableaux de WideChars au lieu de Chars ordinaires.

Light bulb  Remarque: L'appelation UnicodeString est un peu ambiguë mais provient sans doute de son emploi dans Delphi sur Windows, où le système d'exploitation utilise l'encodage UTF-16 ; ce n'est pas le seul type de chaîne qui permet de contenir des données Unicode (voir aussi UTF8String)...
a b c #0 #0
RefCount Length

Référence

UTF8String

Actuellement, le type UTF8String est un alias sur le type AnsiString. Il est destiné à contenir des chaînes encodées en UTF-8 (i.e. des données Unicode) allant de 1 à 4 octets par caractère. UTF8String est le type par défaut dans Lazarus et la LCL. UTF8String est le type de chaîne par défaut dans Lazarus et la LCL.

Référence

UTF16String

Le type UTF16String est un alias sur le type WideString. Dans l'unité LCL lclproc, c'est un alias sur UnicodeString.

Référence

WideString

Les variables du type WideString (utilisées pour représenter les chaînes de caractères Unicode dans les applications COM) ressemblent à celle du type UnicodeString, mais contrairement à elles, elles ne bénéficient pas du comptage de références. Dans Windows, elles sont allouées une fonction spéciale de Windows qui leur permet d'être utilisées dans OLE Automation.

Les WideStrings consistent en octets encodés UTF-16 comppatible COM sur les machines Windows (UCS2 sur Windows 2000) et elles sont encodées en UTF-16 pur sur Linux, Mac OS et iOS.

a b c #0 #0
Length

Référence

PShortString

Une variable du type PShortString est un pointeur qui désigne le premier octet d'une variable de type ShortString (lequel définit la longueur de la ShortString).

#3 a b c
^

Référence

PAnsiString

Les variables de type PAnsiString sont des pointeurs sur des variables de type AnsiString. Néanmoins, contrairement aux variables de type PShortString, ils ne pointent pas sur le premier octet d'un en-tête mais sur le premier char de l'AnsiString.

a b c #0
RefCount Length ^

Référence

PUnicodeString

Les variables de type PUnicodeString sont des pointeurs vers les variables de type UnicodeString.

a b c #0 #0
RefCount Length ^

Référence

PWideString

Les variables du type PWideString sont des pointeurs. Elles pointent sur le premier caractère d'une variable de type WideString.

a b c #0 #0
Length ^

Référence

Constantes String

Si vous utilisez des constantes an Anglais, vos chaînes fonctionnent de la même façon avec tous les types, sur toutes les plateformes et toutes les versions du compilateur. Les chaînes 'non anglaises' peuvent eêtre chargées depuis des chaînes de ressource ou depuis des fichiers. Si vous utilisez des chaînes non anglaises dans votre code, vous devriez lire ce qui suit.


There are various encodings for non English strings. By default Lazarus saves Pascal files as UTF-8 without BOM. UTF-8 supports the full Unicode range. That means all string constants are stored in UTF-8. Lazarus also supports to change the encoding of a file to other encoding, for example under Windows your local codepage. The Windows codepage is limited to your current language group.

String Type, UTF-8 Source With or without {$codepage utf8} FPC 2.6.5 and below FPC 2.7.1 and above FPC 2.7.1+ with UTF8 as default CodePage
AnAnsiString:='ãü'; Without Needs UTF8ToAnsi in RTL/WinAPI. Ok in LCL Needs UTF8ToAnsi in RTL/WinAPI. Ok in LCL Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI.
AnAnsiString:='ãü'; With System cp ok in RTL/WinAPI. Needs SysToUTF8 in LCL Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI
AnUnicodeString:='ãü'; Without Wrong everywhere Wrong everywhere Wrong everywhere
AnUnicodeString:='ãü'; With System cp ok in RTL/WinAPI. Needs UTF8Encode in LCL Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI
AnUTF8String:='ãü'; Without Same as AnsiString Wrong everywhere Wrong everywhere
AnUTF8String:='ãü'; With Same as AnsiString Ok in RTL/WinAPI/LCL. Mixing with other strings converts to system cp Ok in RTL/W-WinAPI/LCL. Needs UTF8ToWinCP in A-WinAPI.
  • W-WinAPI = Windows API "W" functions, UTF-16
  • A-WinAPI = Windows API non "W" functions, 8bit system code page
  • System CP = The 8bit system code page of the OS. For example code page 1252.
const 
  c='ãü';
  cstring: string = 'ãü'; // see AnAnsiString:='ãü';
var
  s: string;
  u: UnicodeString;
begin
  s:=c; // same as s:='ãü';
  s:=cstring; // does not change encoding
  u:=c; // same as u:='ãü';
  u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring
end;

Voir aussi