Sign in Join
Wiki copytemporary

Character and string types/fr: what changed

Revision 2 to 3. 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
33## Contents
44
55 * 1 AnsiChar
66 * 1.1 Référence
77 * 2 WideChar
88 * 2.1 Références
99 * 3 Tableau de Char
1010 * 3.1 Tableau de Char statique
1111 * 3.2 Tableau de Char dynamique
1212 * 4 PChar
1313 * 4.1 Référence
1414 * 5 PWideChar
1515 * 5.1 Référence
1616 * 6 String
1717 * 6.1 Référence
1818 * 7 ShortString
1919 * 7.1 Référence
2020 * 8 AnsiString
2121 * 8.1 Référence
2222 * 9 UnicodeString
2323 * 9.1 Référence
2424 * 10 UTF8String
2525 * 10.1 Référence
2626 * 11 UTF16String
2727 * 11.1 Référence
2828 * 12 WideString
2929 * 12.1 Référence
3030 * 13 PShortString
3131 * 13.1 Référence
3232 * 14 PAnsiString
3333 * 14.1 Référence
3434 * 15 PUnicodeString
3535 * 15.1 Référence
3636 * 16 PWideString
3737 * 16.1 Référence
3838 * 17 Constantes String
3939 * 18 Voir aussi
4040
4141## AnsiChar
4242
4343Une 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.
4444
4545a
4646---
4747
4848#### Référence
4949
5050 * [Documentation FPC sur les AnsiChar](<http://www.freepascal.org/docs-html/ref/refsu7.html>)
5151 * [[Char/fr|Usage de Char]]
5252
5353## WideChar
5454
5555Une 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.
5656
5757| a
5858---|---
5959
6060#### Références
6161
6262 * [Documentation FPC sur les WideChar](<http://www.freepascal.org/docs-html/ref/refsu8.html>)
6363 * [Information sur l'UTF-16 dans Wikipedia](<https://fr.wikipedia.org/wiki/UTF-16>)
6464 * [Documentation sur RTL UnicodeChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/unicodechar.html> "doc:rtl/system/unicodechar.html")
6565
6666## Tableau de Char
6767
6868Les 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.
6969
7070### Tableau de Char statique
7171```
7272type
7373 TOldString4 = array[0..3] of char;
7474 var
7575 aOldString4: TOldString4;
7676 begin
7777 aOldString4[0] := 'a';
7878 aOldString4[1] := 'b';
7979 aOldString4[2] := 'c';
8080 aOldString4[3] := 'd';
8181 end;
8282```
8383
8484Le tableau de char statique contient dorénavant:
8585
8686a | b | c | d
8787---|---|---|---
8888
8989![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é).
9090
9191### Tableau de Char dynamique
9292```
9393var
9494 aOldString: Array of Char; // pas de dimension!!
9595 begin
9696 SetLength(aOldString, 5);
9797 aOldString[0] := 'a';
9898 aOldString[1] := 'b';
9999 aOldString[2] := 'c';
100100 aOldString[3] := 'd';
101101 end;
102102```
103103
104104Le tableau de char dynamique contient dorénavant:
105105
106106a | b | c | d | #0
107107---|---|---|---|---
108108
109109![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 ...)
110110
111111## PChar
112112
113113Une 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.
114114
115115a | b | c | #0
116116---|---|---|---
117117^
118118
119119#### Référence
120120
121121 * [Documentation FPC sur les PChar](<http://www.freepascal.org/docs-html/ref/refsu16.html>)
122122 * [Fonctions associées à PChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/pcharfunctions.html> "doc:rtl/sysutils/pcharfunctions.html")
123123
124124## PWideChar
125125
126126Une variable de type **PWideChar** est un pointeur vers une variable [[Character and string types/fr|WideChar]].
127127
128128| a | | b | | c | #0 | #0
129129---|---|---|---|---|---|---|---
130130^
131131
132132#### Référence
133133
134134 * [Documentation sur RTL PWideChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidechar.html> "doc:rtl/system/pwidechar.html")
135135
136136## String
137137
138138Le 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**.
139139
140140#### Référence
141141
142142 * [[String/fr|Usage des String]]
143143 * [Fonctions sur les chaînes](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/stringfunctions.html> "doc:rtl/sysutils/stringfunctions.html")
144144 * [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")
145145
146146## ShortString
147147
148148Les 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.
149149
150150#3 | a | b | c
151151---|---|---|---
152152
153153#### Référence
154154
155155 * [Documentation FPC sur ShortString](<http://www.freepascal.org/docs-html/ref/refsu11.html#x34-370003.2.5>)
156156
157157## AnsiString
158158
159159Les 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.
160160
161161| | | | | | | | a | b | c | #0
162162---|---|---|---|---|---|---|---|---|---|---|---
163163RefCount | Length
164164
165165#### Référence
166166
167167 * [Documentation FPC sur les AnsiString](<http://www.freepascal.org/docs-html/ref/refsu12.html>)
168168
169169## UnicodeString
170170
171171Telles 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.
172172
173173![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)...
174174
175175| | | | | | | | | a | | b | | c | #0 | #0
176176---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
177177RefCount | Length
178178
179179#### Référence
180180
181181 * [Documentation FPC sur UnicodeString](<http://www.freepascal.org/docs-html/ref/refsu13.html>)
182182
183183## UTF8String
184184
185185Actuellement, 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.
186186
187187#### Référence
188188
189189 * [Documentation sur RTL UTF8String](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/utf8string.html> "doc:rtl/system/utf8string.html")
190190
191191## UTF16String
192192
193193Le type **UTF16String** est un alias sur le type **WideString**. Dans l'unité LCL _lclproc_ , c'est un alias sur **UnicodeString**.
194194
195195#### Référence
196196
197197 * [Documentation sur LCL UTF16String](<http://lazarus-ccr.sourceforge.net/docs/lcl/lclproc/utf16string.html> "doc:lcl/lclproc/utf16string.html")
198198
199199## WideString
200200
201201Les 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.
202202
203203Les 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.
204204
205205| | | | | a | | b | | c | #0 | #0
206206---|---|---|---|---|---|---|---|---|---|---|---
207207Length
208208
209209#### Référence
210210
211211 * [Documentation FPC sur WideString](<http://www.freepascal.org/docs-html/ref/refsu14.html#x37-400003.2.8>)
212212
213213## PShortString
214214
215215Une 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).
216216
217217#3 | a | b | c
218218---|---|---|---
219219^
220220
221221#### Référence
222222
223223 * [Documentation RTL sur PShortString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pshortstring.html> "doc:rtl/system/pshortstring.html")
224224
225225## PAnsiString
226226
227227Les 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**.
228228
229229| | | | | | | | a | b | c | #0
230230---|---|---|---|---|---|---|---|---|---|---|---
231231RefCount | Length | ^
232232
233233#### Référence
234234
235235 * [Documentation RTL sur PAnsiString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pansistring.html> "doc:rtl/system/pansistring.html")
236236
237237## PUnicodeString
238238
239239Les variables de type **PUnicodeString** sont des pointeurs vers les variables de type **UnicodeString**.
240240
241241| | | | | | | | | a | | b | | c | #0 | #0
242242---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---
243243RefCount | Length | ^
244244
245245#### Référence
246246
247247 * [Documentation RTL sur PUnicodeString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/punicodestring.html> "doc:rtl/system/punicodestring.html")
248248
249249## PWideString
250250
251251Les variables du type **PWideString** sont des pointeurs. Elles pointent sur le premier caractère d'une variable de type **WideString**.
252252
253253| | | | | a | | b | | c | #0 | #0
254254---|---|---|---|---|---|---|---|---|---|---|---
255255Length | ^
256256
257257#### Référence
258258
259259 * [Documentation RTL sur PWideString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidestring.html> "doc:rtl/system/pwidestring.html")
260260
261261## Constantes String
262262
263263Si 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.
264264
265265
266266There 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.
267267
268268String 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
269269---|---|---|---|---
270270AnAnsiString:='ãü'; | 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.
271271AnAnsiString:='ãü'; | 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
272272AnUnicodeString:='ãü'; | Without | Wrong everywhere | Wrong everywhere | Wrong everywhere
273273AnUnicodeString:='ãü'; | 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
274274AnUTF8String:='ãü'; | Without | Same as AnsiString | Wrong everywhere | Wrong everywhere
275275AnUTF8String:='ãü'; | 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.
276276
277277 * W-WinAPI = Windows API "W" functions, UTF-16
278278 * A-WinAPI = Windows API non "W" functions, 8bit system code page
279279 * System CP = The 8bit system code page of the OS. For example code page [1252](<http://en.wikipedia.org/wiki/Windows-1252>).
280280
281281```
282282const
283283 c='ãü';
284284 cstring: string = 'ãü'; // see AnAnsiString:='ãü';
285285 var
286286 s: string;
287287 u: UnicodeString;
288288 begin
289289 s:=c; // same as s:='ãü';
290290 s:=cstring; // does not change encoding
291291 u:=c; // same as u:='ãü';
292292 u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring
293293 end;
294294```
295295
296296## Voir aussi
297297
298298 * [[FPC Unicode support/fr|Support de l'Unicode par FPC]]
299299 * [[LCL Unicode Support/fr|Support de l'Unicode par la LCL]]
300300 * [[TStringList-TStrings Tutorial/fr|Tutoriel TStringList-TStrings]]
301301 * [A Brief History of Strings](<http://www.codexterity.com/delphistrings.htm>)
302302
303303---
304-*From the Free Pascal wiki, page "Character and string types/fr", as the Internet Archive kept it on 2024-09-08. 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).*
304+*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