Sign in Join
Wiki copytemporary

Character and string types/fr: what changed

Revision 3 to 4. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11Free 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.
22
3-## Contents
4-
5- * 1 AnsiChar
6- * 1.1 Référence
7- * 2 WideChar
8- * 2.1 Références
9- * 3 Tableau de Char
10- * 3.1 Tableau de Char statique
11- * 3.2 Tableau de Char dynamique
12- * 4 PChar
13- * 4.1 Référence
14- * 5 PWideChar
15- * 5.1 Référence
16- * 6 String
17- * 6.1 Référence
18- * 7 ShortString
19- * 7.1 Référence
20- * 8 AnsiString
21- * 8.1 Référence
22- * 9 UnicodeString
23- * 9.1 Référence
24- * 10 UTF8String
25- * 10.1 Référence
26- * 11 UTF16String
27- * 11.1 Référence
28- * 12 WideString
29- * 12.1 Référence
30- * 13 PShortString
31- * 13.1 Référence
32- * 14 PAnsiString
33- * 14.1 Référence
34- * 15 PUnicodeString
35- * 15.1 Référence
36- * 16 PWideString
37- * 16.1 Référence
38- * 17 Constantes String
39- * 18 Voir aussi
40-
413## AnsiChar
424
435Une variable de type **AnsiChar** , aussi mentionnée comme **[[Character and string types/fr|Char]]** , a une taille d'exactement un octet et contient un caractère ANSI.
446
457a
468---
479
4810#### Référence
4911
5012 * [Documentation FPC sur les AnsiChar](<http://www.freepascal.org/docs-html/ref/refsu7.html>)
5113 * [[Char/fr|Usage de Char]]
5214
5315## WideChar
5416
5517Une 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 : 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.
5618
5719| a
5820---|---
5921
6022#### Références
6123
6224 * [Documentation FPC sur les WideChar](<http://www.freepascal.org/docs-html/ref/refsu8.html>)
6325 * [Information sur l'UTF-16 dans Wikipedia](<https://fr.wikipedia.org/wiki/UTF-16>)
6426 * [Documentation sur RTL UnicodeChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/unicodechar.html> "doc:rtl/system/unicodechar.html")
6527
6628## Tableau de Char
6729
6830Les 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.
6931
7032### Tableau de Char statique
7133```
7234type
7335 TOldString4 = array[0..3] of char;
7436 var
7537 aOldString4: TOldString4;
7638 begin
7739 aOldString4[0] := 'a';
7840 aOldString4[1] := 'b';
7941 aOldString4[2] := 'c';
8042 aOldString4[3] := 'd';
8143 end;
8244```
8345
8446Le tableau de char statique contient dorénavant:
8547
8648a | b | c | d
8749---|---|---|---
8850
8951![Light bulb](https://upload.wikimedia.org/wikipedia/commons/d/d8/Nuvola_apps_ktip.png) **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é).
9052
9153### Tableau de Char dynamique
9254```
9355var
9456 aOldString: Array of Char; // pas de dimension!!
9557 begin
9658 SetLength(aOldString, 5);
9759 aOldString[0] := 'a';
9860 aOldString[1] := 'b';
9961 aOldString[2] := 'c';
10062 aOldString[3] := 'd';
10163 end;
10264```
10365
10466Le tableau de char dynamique contient dorénavant:
10567
10668a | b | c | d | #0
10769---|---|---|---|---
10870
10971![Light bulb](https://upload.wikimedia.org/wikipedia/commons/d/d8/Nuvola_apps_ktip.png) **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 ...)
11072
11173## PChar
11274
11375Une 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](<http://en.wikipedia.org/wiki/Null-terminated_string>), par exemple en interaction avec certaines API systèmes ou des logiciels tierce partie.
11476
11577a | b | c | #0
11678---|---|---|---
11779^
11880
11981#### Référence
12082
12183 * [Documentation FPC sur les PChar](<http://www.freepascal.org/docs-html/ref/refsu16.html>)
12284 * [Fonctions associées à PChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/pcharfunctions.html> "doc:rtl/sysutils/pcharfunctions.html")
12385
12486## PWideChar
12587
12688Une variable de type **PWideChar** est un pointeur vers une variable [[Character and string types/fr|WideChar]].
12789
12890| a | | b | | c | #0 | #0
12991---|---|---|---|---|---|---|---
13092^
13193
13294#### Référence
13395
13496 * [Documentation sur RTL PWideChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidechar.html> "doc:rtl/system/pwidechar.html")
13597
13698## String
13799
138-Le type **String'_peut faire référence à_** _ShortString**ou**AnsiString**, selon la[bascule {$H}](<http://www.freepascal.org/docs-html/prog/progsu25.html#x32-310001.2.25>). 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**.
100+Le type **String'_peut faire référence à_** _ShortString**ou**AnsiString**, selon la[bascule {$H}](<http://www.freepascal.org/docs-html/prog/progsu25.html#x32-310001.2.25>). 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 delphiunicodeString_ est **UnicodeString**.
139101
140102#### Référence
141103
142104 * [[String/fr|Usage des String]]
143105 * [Fonctions sur les chaînes](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/stringfunctions.html> "doc:rtl/sysutils/stringfunctions.html")
144106 * [Référence sur l'unité 'strutils': Procédures et fonctions](<http://lazarus-ccr.sourceforge.net/docs/rtl/strutils/index-5.html> "doc:rtl/strutils/index-5.html")
145107
146108## ShortString
147109
148110Les chaînes courtes ont une taille maximale de 255 caractères avec la [[FPC Unicode support|page de code]] CP_ACP implicite. La longueur effective de la chaîne est conservée dans le caractère à l'index 0.
149111
150112#3 | a | b | c
151113---|---|---|---
152114
153115#### Référence
154116
155117 * [Documentation FPC sur ShortString](<http://www.freepascal.org/docs-html/ref/refsu11.html#x34-370003.2.5>)
156118
157119## AnsiString
158120
159121Les Ansistrings ou les UTF8Strings sont des chaînes qui n'ont pas de limite de taille. Le [comptage de référence](<http://en.wikipedia.org/wiki/Reference_counting>) s'applique à elle et il est garanti qu'elles soient à [zéro terminal](<http://en.wikipedia.org/wiki/Null-terminated_string>). 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.
160122
161123| | | | | | | | a | b | c | #0
162124---|---|---|---|---|---|---|---|---|---|---|---
163125RefCount | Length
164126
165127#### Référence
166128
167129 * [Documentation FPC sur les AnsiString](<http://www.freepascal.org/docs-html/ref/refsu12.html>)
168130
169131## UnicodeString
170132
171133Telles 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.
172134
173135![Light bulb](https://upload.wikimedia.org/wikipedia/commons/d/d8/Nuvola_apps_ktip.png) **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)...
174136
175137| | | | | | | | | a | | b | | c | #0 | #0
176138---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
177139RefCount | Length
178140
179141#### Référence
180142
181143 * [Documentation FPC sur UnicodeString](<http://www.freepascal.org/docs-html/ref/refsu13.html>)
182144
183145## UTF8String
184146
185147Actuellement, 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.
186148
187149#### Référence
188150
189151 * [Documentation sur RTL UTF8String](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/utf8string.html> "doc:rtl/system/utf8string.html")
190152
191153## UTF16String
192154
193155Le type **UTF16String** est un alias sur le type **WideString**. Dans l'unité LCL _lclproc_ , c'est un alias sur **UnicodeString**.
194156
195157#### Référence
196158
197159 * [Documentation sur LCL UTF16String](<http://lazarus-ccr.sourceforge.net/docs/lcl/lclproc/utf16string.html> "doc:lcl/lclproc/utf16string.html")
198160
199161## WideString
200162
201163Les 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.
202164
203165Les 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.
204166
205167| | | | | a | | b | | c | #0 | #0
206168---|---|---|---|---|---|---|---|---|---|---|---
207169Length
208170
209171#### Référence
210172
211173 * [Documentation FPC sur WideString](<http://www.freepascal.org/docs-html/ref/refsu14.html#x37-400003.2.8>)
212174
213175## PShortString
214176
215177Une 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).
216178
217179#3 | a | b | c
218180---|---|---|---
219181^
220182
221183#### Référence
222184
223185 * [Documentation RTL sur PShortString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pshortstring.html> "doc:rtl/system/pshortstring.html")
224186
225187## PAnsiString
226188
227189Les 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**.
228190
229191| | | | | | | | a | b | c | #0
230192---|---|---|---|---|---|---|---|---|---|---|---
231193RefCount | Length | ^
232194
233195#### Référence
234196
235197 * [Documentation RTL sur PAnsiString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pansistring.html> "doc:rtl/system/pansistring.html")
236198
237199## PUnicodeString
238200
239201Les variables de type **PUnicodeString** sont des pointeurs vers les variables de type **UnicodeString**.
240202
241203| | | | | | | | | a | | b | | c | #0 | #0
242204---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
243205RefCount | Length | ^
244206
245207#### Référence
246208
247209 * [Documentation RTL sur PUnicodeString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/punicodestring.html> "doc:rtl/system/punicodestring.html")
248210
249211## PWideString
250212
251213Les variables du type **PWideString** sont des pointeurs. Elles pointent sur le premier caractère d'une variable de type **WideString**.
252214
253215| | | | | a | | b | | c | #0 | #0
254216---|---|---|---|---|---|---|---|---|---|---|---
255217Length | ^
256218
257219#### Référence
258220
259221 * [Documentation RTL sur PWideString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidestring.html> "doc:rtl/system/pwidestring.html")
260222
261223## Constantes String
262224
263225Si 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.
264226
265227
266228There 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.
267229
268230String 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
269231---|---|---|---|---
270232AnAnsiString:='ãü'; | 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.
271233AnAnsiString:='ãü'; | 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
272234AnUnicodeString:='ãü'; | Without | Wrong everywhere | Wrong everywhere | Wrong everywhere
273235AnUnicodeString:='ãü'; | 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
274236AnUTF8String:='ãü'; | Without | Same as AnsiString | Wrong everywhere | Wrong everywhere
275237AnUTF8String:='ãü'; | 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.
276238
277239 * W-WinAPI = Windows API "W" functions, UTF-16
278240 * A-WinAPI = Windows API non "W" functions, 8bit system code page
279241 * System CP = The 8bit system code page of the OS. For example code page [1252](<http://en.wikipedia.org/wiki/Windows-1252>).
280242
281243```
282244const
283245 c='ãü';
284246 cstring: string = 'ãü'; // see AnAnsiString:='ãü';
285247 var
286248 s: string;
287249 u: UnicodeString;
288250 begin
289251 s:=c; // same as s:='ãü';
290252 s:=cstring; // does not change encoding
291253 u:=c; // same as u:='ãü';
292254 u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring
293255 end;
294256```
295257
296258## Voir aussi
297259
298260 * [[FPC Unicode support/fr|Support de l'Unicode par FPC]]
299261 * [[LCL Unicode Support/fr|Support de l'Unicode par la LCL]]
300262 * [[TStringList-TStrings Tutorial/fr|Tutoriel TStringList-TStrings]]
301263 * [A Brief History of Strings](<http://www.codexterity.com/delphistrings.htm>)
302264
303265---
304266*From the Free Pascal wiki, page "Character and string types/fr", as the Internet Archive kept it on 2024-09-08, last changed on 2020-02-11. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history