Sign in Join
Wiki copytemporary

Character and string types/fr: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Character and string types/fr

OldNewLine
1+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.
2+
3+## AnsiChar
4+
5+Une 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.
6+
7+| |
8+|-----|
9+| a |
10+
11+#### Référence
12+
13+- [Documentation FPC sur les AnsiChar](http://www.freepascal.org/docs-html/ref/refsu7.html)
14+- [[Char/fr|Usage de Char]]
15+
16+## WideChar
17+
18+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 : 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.
19+
20+| | |
21+|-----|-----|
22+| | a |
23+
24+#### Références
25+
26+- [Documentation FPC sur les WideChar](http://www.freepascal.org/docs-html/ref/refsu8.html)
27+- [Information sur l'UTF-16 dans Wikipedia](https://fr.wikipedia.org/wiki/UTF-16)
28+- [[doc:rtl/system/unicodechar.html|Documentation sur RTL UnicodeChar]]
29+
30+## Tableau de Char
31+
32+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.
33+
34+### Tableau de Char statique
35+
36+``` pascal
37+type
38+ TOldString4 = array[0..3] of char;
39+var
40+ aOldString4: TOldString4;
41+begin
42+ aOldString4[0] := 'a';
43+ aOldString4[1] := 'b';
44+ aOldString4[2] := 'c';
45+ aOldString4[3] := 'd';
46+end;
47+```
48+
49+Le tableau de char statique contient dorénavant:
50+
51+| | | | |
52+|-----|-----|-----|-----|
53+| a | b | c | d |
54+
55+> **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é).
56+
57+### Tableau de Char dynamique
58+
59+``` pascal
60+var
61+ aOldString: Array of Char; // pas de dimension!!
62+begin
63+ SetLength(aOldString, 5);
64+ aOldString[0] := 'a';
65+ aOldString[1] := 'b';
66+ aOldString[2] := 'c';
67+ aOldString[3] := 'd';
68+end;
69+```
70+
71+Le tableau de char dynamique contient dorénavant:
72+
73+| | | | | |
74+|-----|-----|-----|-----|-----|
75+| a | b | c | d | \#0 |
76+
77+> **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 ...)
78+
79+## PChar
80+
81+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](http://en.wikipedia.org/wiki/Null-terminated_string), par exemple en interaction avec certaines API systèmes ou des logiciels tierce partie.
82+
83+| | | | |
84+|-----|-----|-----|-----|
85+| a | b | c | \#0 |
86+| ^ | | | |
87+
88+#### Référence
89+
90+- [Documentation FPC sur les PChar](http://www.freepascal.org/docs-html/ref/refsu16.html)
91+- [[doc:rtl/sysutils/pcharfunctions.html|Fonctions associées à PChar]]
92+
93+## PWideChar
94+
95+Une variable de type **PWideChar** est un pointeur vers une variable [[Character and string types/fr|WideChar]].
96+
97+| | | | | | | | |
98+|-----|-----|-----|-----|-----|-----|-----|-----|
99+| | a | | b | | c | \#0 | \#0 |
100+| ^ | | | | | | | |
101+
102+#### Référence
103+
104+- [[doc:rtl/system/pwidechar.html|Documentation sur RTL PWideChar]]
105+
106+## String
107+
108+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'''.
109+
110+#### Référence
111+
112+- [[String/fr|Usage des String]]
113+- [[doc:rtl/sysutils/stringfunctions.html|Fonctions sur les chaînes]]
114+- [[doc:rtl/strutils/index-5.html|Référence sur l'unité 'strutils': Procédures et fonctions]]
115+
116+## ShortString
117+
118+Les 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.
119+
120+| | | | |
121+|-----|-----|-----|-----|
122+| \#3 | a | b | c |
123+
124+#### Référence
125+
126+- [Documentation FPC sur ShortString](http://www.freepascal.org/docs-html/ref/refsu11.html#x34-370003.2.5)
127+
128+## AnsiString
129+
130+Les 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.
131+
132+<table>
133+<tbody>
134+<tr>
135+<td style="width: 8%;"></td>
136+<td style="width: 8%;"></td>
137+<td style="width: 8%;"></td>
138+<td style="width: 8%;"></td>
139+<td style="width: 8%;"></td>
140+<td style="width: 8%;"></td>
141+<td style="width: 8%;"></td>
142+<td style="width: 8%;"></td>
143+<td style="width: 8%;">a</td>
144+<td style="width: 8%;">b</td>
145+<td style="width: 8%;">c</td>
146+<td style="width: 8%;">#0</td>
147+</tr>
148+<tr>
149+<td colspan="4" style="width: 16%;">RefCount</td>
150+<td colspan="4" style="width: 16%;">Length</td>
151+<td></td>
152+<td></td>
153+<td></td>
154+<td></td>
155+</tr>
156+</tbody>
157+</table>
158+
159+#### Référence
160+
161+- [Documentation FPC sur les AnsiString](http://www.freepascal.org/docs-html/ref/refsu12.html)
162+
163+## UnicodeString
164+
165+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.
166+
167+> **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)...
168+
169+<table>
170+<tbody>
171+<tr>
172+<td style="width: 7%;"></td>
173+<td style="width: 7%;"></td>
174+<td style="width: 7%;"></td>
175+<td style="width: 7%;"></td>
176+<td style="width: 7%;"></td>
177+<td style="width: 7%;"></td>
178+<td style="width: 7%;"></td>
179+<td style="width: 7%;"></td>
180+<td style="width: 7%;"></td>
181+<td style="width: 7%;">a</td>
182+<td style="width: 7%;"></td>
183+<td style="width: 7%;">b</td>
184+<td style="width: 7%;"></td>
185+<td style="width: 7%;">c</td>
186+<td style="width: 7%;">#0</td>
187+<td style="width: 7%;">#0</td>
188+</tr>
189+<tr>
190+<td colspan="4" style="width: 16%;">RefCount</td>
191+<td colspan="4" style="width: 16%;">Length</td>
192+<td></td>
193+<td></td>
194+<td></td>
195+<td></td>
196+<td></td>
197+<td></td>
198+<td></td>
199+<td></td>
200+</tr>
201+</tbody>
202+</table>
203+
204+#### Référence
205+
206+- [Documentation FPC sur UnicodeString](http://www.freepascal.org/docs-html/ref/refsu13.html)
207+
208+## UTF8String
209+
210+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.
211+
212+#### Référence
213+
214+- [[doc:rtl/system/utf8string.html|Documentation sur RTL UTF8String]]
215+
216+## UTF16String
217+
218+Le type **UTF16String** est un alias sur le type **WideString**. Dans l'unité LCL *lclproc*, c'est un alias sur **UnicodeString**.
219+
220+#### Référence
221+
222+- [[doc:lcl/lclproc/utf16string.html|Documentation sur LCL UTF16String]]
223+
224+## WideString
225+
226+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.
227+
228+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.
229+
230+<table>
231+<tbody>
232+<tr>
233+<td style="width: 8%;"></td>
234+<td style="width: 8%;"></td>
235+<td style="width: 8%;"></td>
236+<td style="width: 8%;"></td>
237+<td style="width: 8%;"></td>
238+<td style="width: 8%;">a</td>
239+<td style="width: 8%;"></td>
240+<td style="width: 8%;">b</td>
241+<td style="width: 8%;"></td>
242+<td style="width: 8%;">c</td>
243+<td style="width: 8%;">#0</td>
244+<td style="width: 8%;">#0</td>
245+</tr>
246+<tr>
247+<td colspan="4" style="width: 16%;">Length</td>
248+<td></td>
249+<td></td>
250+<td></td>
251+<td></td>
252+<td></td>
253+<td></td>
254+<td></td>
255+<td></td>
256+</tr>
257+</tbody>
258+</table>
259+
260+#### Référence
261+
262+- [Documentation FPC sur WideString](http://www.freepascal.org/docs-html/ref/refsu14.html#x37-400003.2.8)
263+
264+## PShortString
265+
266+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).
267+
268+| | | | |
269+|-----|-----|-----|-----|
270+| \#3 | a | b | c |
271+| ^ | | | |
272+
273+#### Référence
274+
275+- [[doc:rtl/system/pshortstring.html|Documentation RTL sur PShortString]]
276+
277+## PAnsiString
278+
279+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**.
280+
281+<table>
282+<tbody>
283+<tr>
284+<td style="width: 8%;"></td>
285+<td style="width: 8%;"></td>
286+<td style="width: 8%;"></td>
287+<td style="width: 8%;"></td>
288+<td style="width: 8%;"></td>
289+<td style="width: 8%;"></td>
290+<td style="width: 8%;"></td>
291+<td style="width: 8%;"></td>
292+<td style="width: 8%;">a</td>
293+<td style="width: 8%;">b</td>
294+<td style="width: 8%;">c</td>
295+<td style="width: 8%;">#0</td>
296+</tr>
297+<tr>
298+<td colspan="4" style="width: 16%;">RefCount</td>
299+<td colspan="4" style="width: 16%;">Length</td>
300+<td style="width: 8%;">^</td>
301+<td></td>
302+<td></td>
303+<td></td>
304+</tr>
305+</tbody>
306+</table>
307+
308+#### Référence
309+
310+- [[doc:rtl/system/pansistring.html|Documentation RTL sur PAnsiString]]
311+
312+## PUnicodeString
313+
314+Les variables de type **PUnicodeString** sont des pointeurs vers les variables de type **UnicodeString**.
315+
316+<table>
317+<tbody>
318+<tr>
319+<td style="width: 7%;"></td>
320+<td style="width: 7%;"></td>
321+<td style="width: 7%;"></td>
322+<td style="width: 7%;"></td>
323+<td style="width: 7%;"></td>
324+<td style="width: 7%;"></td>
325+<td style="width: 7%;"></td>
326+<td style="width: 7%;"></td>
327+<td style="width: 7%;"></td>
328+<td style="width: 7%;">a</td>
329+<td style="width: 7%;"></td>
330+<td style="width: 7%;">b</td>
331+<td style="width: 7%;"></td>
332+<td style="width: 7%;">c</td>
333+<td style="width: 7%;">#0</td>
334+<td style="width: 7%;">#0</td>
335+</tr>
336+<tr>
337+<td colspan="4" style="width: 16%;">RefCount</td>
338+<td colspan="4" style="width: 16%;">Length</td>
339+<td style="width: 7%;">^</td>
340+<td></td>
341+<td></td>
342+<td></td>
343+<td></td>
344+<td></td>
345+<td></td>
346+<td></td>
347+</tr>
348+</tbody>
349+</table>
350+
351+#### Référence
352+
353+- [[doc:rtl/system/punicodestring.html|Documentation RTL sur PUnicodeString]]
354+
355+## PWideString
356+
357+Les variables du type **PWideString** sont des pointeurs. Elles pointent sur le premier caractère d'une variable de type **WideString**.
358+
359+<table>
360+<tbody>
361+<tr>
362+<td style="width: 8%;"></td>
363+<td style="width: 8%;"></td>
364+<td style="width: 8%;"></td>
365+<td style="width: 8%;"></td>
366+<td style="width: 8%;"></td>
367+<td style="width: 8%;">a</td>
368+<td style="width: 8%;"></td>
369+<td style="width: 8%;">b</td>
370+<td style="width: 8%;"></td>
371+<td style="width: 8%;">c</td>
372+<td style="width: 8%;">#0</td>
373+<td style="width: 8%;">#0</td>
374+</tr>
375+<tr>
376+<td colspan="4" style="width: 16%;">Length</td>
377+<td style="width: 8%;">^</td>
378+<td></td>
379+<td></td>
380+<td></td>
381+<td></td>
382+<td></td>
383+<td></td>
384+<td></td>
385+</tr>
386+</tbody>
387+</table>
388+
389+#### Référence
390+
391+- [[doc:rtl/system/pwidestring.html|Documentation RTL sur PWideString]]
392+
393+## Constantes String
394+
395+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.
396+
397+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.
398+
399+| 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 |
400+|----|----|----|----|----|
401+| \|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. | |
402+| \|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 | |
403+| \|Without | Wrong everywhere | Wrong everywhere | Wrong everywhere | |
404+| \|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 | |
405+| \|Without | Same as AnsiString | Wrong everywhere | Wrong everywhere | |
406+| \|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. | |
407+
408+- W-WinAPI = Windows API "W" functions, UTF-16
409+- A-WinAPI = Windows API non "W" functions, 8bit system code page
410+- System CP = The 8bit system code page of the OS. For example code page [1252](http://en.wikipedia.org/wiki/Windows-1252).
411+
412+``` pascal
413+const
414+ c='ãü';
415+ cstring: string = 'ãü'; // see AnAnsiString:='ãü';
416+var
417+ s: string;
418+ u: UnicodeString;
419+begin
420+ s:=c; // same as s:='ãü';
421+ s:=cstring; // does not change encoding
422+ u:=c; // same as u:='ãü';
423+ u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring
424+end;
425+```
426+
427+## Voir aussi
428+
429+- [[FPC Unicode support/fr|Support de l'Unicode par FPC]]
430+- [[LCL Unicode Support/fr|Support de l'Unicode par la LCL]]
431+- [[TStringList-TStrings Tutorial/fr|Tutoriel TStringList-TStrings]]
432+- [A Brief History of Strings](http://www.codexterity.com/delphistrings.htm)
433+
434+---
435+*From the Free Pascal wiki, page "Character and string types/fr", as it stood on 2020-02-10 after a change by Trev, out of the wiki's dump of 2024-05-26. 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