Character and string types/fr: what changed
Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 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 | 2 | ||
| 3 | 3 | ## Contents | |
| 4 | 4 | ||
| 5 | 5 | * 1 AnsiChar | |
| 6 | 6 | * 1.1 Référence | |
| 7 | 7 | * 2 WideChar | |
| 8 | 8 | * 2.1 Références | |
| 9 | 9 | * 3 Tableau de Char | |
| 10 | 10 | * 3.1 Tableau de Char statique | |
| 11 | 11 | * 3.2 Tableau de Char dynamique | |
| 12 | 12 | * 4 PChar | |
| 13 | 13 | * 4.1 Référence | |
| 14 | 14 | * 5 PWideChar | |
| 15 | 15 | * 5.1 Référence | |
| 16 | 16 | * 6 String | |
| 17 | 17 | * 6.1 Référence | |
| 18 | 18 | * 7 ShortString | |
| 19 | 19 | * 7.1 Référence | |
| 20 | 20 | * 8 AnsiString | |
| 21 | 21 | * 8.1 Référence | |
| 22 | 22 | * 9 UnicodeString | |
| 23 | 23 | * 9.1 Référence | |
| 24 | 24 | * 10 UTF8String | |
| 25 | 25 | * 10.1 Référence | |
| 26 | 26 | * 11 UTF16String | |
| 27 | 27 | * 11.1 Référence | |
| 28 | 28 | * 12 WideString | |
| 29 | 29 | * 12.1 Référence | |
| 30 | 30 | * 13 PShortString | |
| 31 | 31 | * 13.1 Référence | |
| 32 | 32 | * 14 PAnsiString | |
| 33 | 33 | * 14.1 Référence | |
| 34 | 34 | * 15 PUnicodeString | |
| 35 | 35 | * 15.1 Référence | |
| 36 | 36 | * 16 PWideString | |
| 37 | 37 | * 16.1 Référence | |
| 38 | 38 | * 17 Constantes String | |
| 39 | 39 | * 18 Voir aussi | |
| 40 | 40 | ||
| 41 | 41 | ## AnsiChar | |
| 42 | 42 | ||
| 43 | 43 | 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. | |
| 44 | 44 | ||
| 45 | 45 | a | |
| 46 | 46 | --- | |
| 47 | 47 | ||
| 48 | 48 | #### Référence | |
| 49 | 49 | ||
| 50 | 50 | * [Documentation FPC sur les AnsiChar](<http://www.freepascal.org/docs-html/ref/refsu7.html>) | |
| 51 | 51 | * [[Char/fr|Usage de Char]] | |
| 52 | 52 | ||
| 53 | 53 | ## WideChar | |
| 54 | 54 | ||
| 55 | 55 | 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. | |
| 56 | 56 | ||
| 57 | 57 | | a | |
| 58 | 58 | ---|--- | |
| 59 | 59 | ||
| 60 | 60 | #### Références | |
| 61 | 61 | ||
| 62 | 62 | * [Documentation FPC sur les WideChar](<http://www.freepascal.org/docs-html/ref/refsu8.html>) | |
| 63 | 63 | * [Information sur l'UTF-16 dans Wikipedia](<https://fr.wikipedia.org/wiki/UTF-16>) | |
| 64 | 64 | * [Documentation sur RTL UnicodeChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/unicodechar.html> "doc:rtl/system/unicodechar.html") | |
| 65 | 65 | ||
| 66 | 66 | ## Tableau de Char | |
| 67 | 67 | ||
| 68 | 68 | 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. | |
| 69 | 69 | ||
| 70 | 70 | ### Tableau de Char statique | |
| 71 | 71 | ``` | |
| 72 | 72 | type | |
| 73 | 73 | TOldString4 = array[0..3] of char; | |
| 74 | 74 | var | |
| 75 | 75 | aOldString4: TOldString4; | |
| 76 | 76 | begin | |
| 77 | 77 | aOldString4[0] := 'a'; | |
| 78 | 78 | aOldString4[1] := 'b'; | |
| 79 | 79 | aOldString4[2] := 'c'; | |
| 80 | 80 | aOldString4[3] := 'd'; | |
| 81 | 81 | end; | |
| 82 | 82 | ``` | |
| 83 | 83 | ||
| 84 | 84 | Le tableau de char statique contient dorénavant: | |
| 85 | 85 | ||
| 86 | 86 | a | b | c | d | |
| 87 | 87 | ---|---|---|--- | |
| 88 | 88 | ||
| 89 | 89 |  **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é). | |
| 90 | 90 | ||
| 91 | 91 | ### Tableau de Char dynamique | |
| 92 | 92 | ``` | |
| 93 | 93 | var | |
| 94 | 94 | aOldString: Array of Char; // pas de dimension!! | |
| 95 | 95 | begin | |
| 96 | 96 | SetLength(aOldString, 5); | |
| 97 | 97 | aOldString[0] := 'a'; | |
| 98 | 98 | aOldString[1] := 'b'; | |
| 99 | 99 | aOldString[2] := 'c'; | |
| 100 | 100 | aOldString[3] := 'd'; | |
| 101 | 101 | end; | |
| 102 | 102 | ``` | |
| 103 | 103 | ||
| 104 | 104 | Le tableau de char dynamique contient dorénavant: | |
| 105 | 105 | ||
| 106 | 106 | a | b | c | d | #0 | |
| 107 | 107 | ---|---|---|---|--- | |
| 108 | 108 | ||
| 109 | 109 |  **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 ...) | |
| 110 | 110 | ||
| 111 | 111 | ## PChar | |
| 112 | 112 | ||
| 113 | 113 | 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. | |
| 114 | 114 | ||
| 115 | 115 | a | b | c | #0 | |
| 116 | 116 | ---|---|---|--- | |
| 117 | 117 | ^ | |
| 118 | 118 | ||
| 119 | 119 | #### Référence | |
| 120 | 120 | ||
| 121 | 121 | * [Documentation FPC sur les PChar](<http://www.freepascal.org/docs-html/ref/refsu16.html>) | |
| 122 | 122 | * [Fonctions associées à PChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/pcharfunctions.html> "doc:rtl/sysutils/pcharfunctions.html") | |
| 123 | 123 | ||
| 124 | 124 | ## PWideChar | |
| 125 | 125 | ||
| 126 | 126 | Une variable de type **PWideChar** est un pointeur vers une variable [[Character and string types/fr|WideChar]]. | |
| 127 | 127 | ||
| 128 | 128 | | a | | b | | c | #0 | #0 | |
| 129 | 129 | ---|---|---|---|---|---|---|--- | |
| 130 | 130 | ^ | |
| 131 | 131 | ||
| 132 | 132 | #### Référence | |
| 133 | 133 | ||
| 134 | 134 | * [Documentation sur RTL PWideChar](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidechar.html> "doc:rtl/system/pwidechar.html") | |
| 135 | 135 | ||
| 136 | 136 | ## String | |
| 137 | 137 | ||
| 138 | 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**. | |
| 139 | 139 | ||
| 140 | 140 | #### Référence | |
| 141 | 141 | ||
| 142 | 142 | * [[String/fr|Usage des String]] | |
| 143 | 143 | * [Fonctions sur les chaînes](<http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/stringfunctions.html> "doc:rtl/sysutils/stringfunctions.html") | |
| 144 | 144 | * [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") | |
| 145 | 145 | ||
| 146 | 146 | ## ShortString | |
| 147 | 147 | ||
| 148 | 148 | 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. | |
| 149 | 149 | ||
| 150 | 150 | #3 | a | b | c | |
| 151 | 151 | ---|---|---|--- | |
| 152 | 152 | ||
| 153 | 153 | #### Référence | |
| 154 | 154 | ||
| 155 | 155 | * [Documentation FPC sur ShortString](<http://www.freepascal.org/docs-html/ref/refsu11.html#x34-370003.2.5>) | |
| 156 | 156 | ||
| 157 | 157 | ## AnsiString | |
| 158 | 158 | ||
| 159 | 159 | 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. | |
| 160 | 160 | ||
| 161 | 161 | | | | | | | | | a | b | c | #0 | |
| 162 | 162 | ---|---|---|---|---|---|---|---|---|---|---|--- | |
| 163 | 163 | RefCount | Length | |
| 164 | 164 | ||
| 165 | 165 | #### Référence | |
| 166 | 166 | ||
| 167 | 167 | * [Documentation FPC sur les AnsiString](<http://www.freepascal.org/docs-html/ref/refsu12.html>) | |
| 168 | 168 | ||
| 169 | 169 | ## UnicodeString | |
| 170 | 170 | ||
| 171 | 171 | 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. | |
| 172 | 172 | ||
| 173 | 173 |  **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)... | |
| 174 | 174 | ||
| 175 | 175 | | | | | | | | | | a | | b | | c | #0 | #0 | |
| 176 | 176 | ---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|--- | |
| 177 | 177 | RefCount | Length | |
| 178 | 178 | ||
| 179 | 179 | #### Référence | |
| 180 | 180 | ||
| 181 | 181 | * [Documentation FPC sur UnicodeString](<http://www.freepascal.org/docs-html/ref/refsu13.html>) | |
| 182 | 182 | ||
| 183 | 183 | ## UTF8String | |
| 184 | 184 | ||
| 185 | 185 | 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. | |
| 186 | 186 | ||
| 187 | 187 | #### Référence | |
| 188 | 188 | ||
| 189 | 189 | * [Documentation sur RTL UTF8String](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/utf8string.html> "doc:rtl/system/utf8string.html") | |
| 190 | 190 | ||
| 191 | 191 | ## UTF16String | |
| 192 | 192 | ||
| 193 | 193 | Le type **UTF16String** est un alias sur le type **WideString**. Dans l'unité LCL _lclproc_ , c'est un alias sur **UnicodeString**. | |
| 194 | 194 | ||
| 195 | 195 | #### Référence | |
| 196 | 196 | ||
| 197 | 197 | * [Documentation sur LCL UTF16String](<http://lazarus-ccr.sourceforge.net/docs/lcl/lclproc/utf16string.html> "doc:lcl/lclproc/utf16string.html") | |
| 198 | 198 | ||
| 199 | 199 | ## WideString | |
| 200 | 200 | ||
| 201 | 201 | 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. | |
| 202 | 202 | ||
| 203 | 203 | 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. | |
| 204 | 204 | ||
| 205 | 205 | | | | | | a | | b | | c | #0 | #0 | |
| 206 | 206 | ---|---|---|---|---|---|---|---|---|---|---|--- | |
| 207 | 207 | Length | |
| 208 | 208 | ||
| 209 | 209 | #### Référence | |
| 210 | 210 | ||
| 211 | 211 | * [Documentation FPC sur WideString](<http://www.freepascal.org/docs-html/ref/refsu14.html#x37-400003.2.8>) | |
| 212 | 212 | ||
| 213 | 213 | ## PShortString | |
| 214 | 214 | ||
| 215 | 215 | 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). | |
| 216 | 216 | ||
| 217 | 217 | #3 | a | b | c | |
| 218 | 218 | ---|---|---|--- | |
| 219 | 219 | ^ | |
| 220 | 220 | ||
| 221 | 221 | #### Référence | |
| 222 | 222 | ||
| 223 | 223 | * [Documentation RTL sur PShortString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pshortstring.html> "doc:rtl/system/pshortstring.html") | |
| 224 | 224 | ||
| 225 | 225 | ## PAnsiString | |
| 226 | 226 | ||
| 227 | 227 | 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**. | |
| 228 | 228 | ||
| 229 | 229 | | | | | | | | | a | b | c | #0 | |
| 230 | 230 | ---|---|---|---|---|---|---|---|---|---|---|--- | |
| 231 | 231 | RefCount | Length | ^ | |
| 232 | 232 | ||
| 233 | 233 | #### Référence | |
| 234 | 234 | ||
| 235 | 235 | * [Documentation RTL sur PAnsiString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pansistring.html> "doc:rtl/system/pansistring.html") | |
| 236 | 236 | ||
| 237 | 237 | ## PUnicodeString | |
| 238 | 238 | ||
| 239 | 239 | Les variables de type **PUnicodeString** sont des pointeurs vers les variables de type **UnicodeString**. | |
| 240 | 240 | ||
| 241 | 241 | | | | | | | | | | a | | b | | c | #0 | #0 | |
| 242 | 242 | ---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|--- | |
| 243 | 243 | RefCount | Length | ^ | |
| 244 | 244 | ||
| 245 | 245 | #### Référence | |
| 246 | 246 | ||
| 247 | 247 | * [Documentation RTL sur PUnicodeString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/punicodestring.html> "doc:rtl/system/punicodestring.html") | |
| 248 | 248 | ||
| 249 | 249 | ## PWideString | |
| 250 | 250 | ||
| 251 | 251 | Les variables du type **PWideString** sont des pointeurs. Elles pointent sur le premier caractère d'une variable de type **WideString**. | |
| 252 | 252 | ||
| 253 | 253 | | | | | | a | | b | | c | #0 | #0 | |
| 254 | 254 | ---|---|---|---|---|---|---|---|---|---|---|--- | |
| 255 | 255 | Length | ^ | |
| 256 | 256 | ||
| 257 | 257 | #### Référence | |
| 258 | 258 | ||
| 259 | 259 | * [Documentation RTL sur PWideString](<http://lazarus-ccr.sourceforge.net/docs/rtl/system/pwidestring.html> "doc:rtl/system/pwidestring.html") | |
| 260 | 260 | ||
| 261 | 261 | ## Constantes String | |
| 262 | 262 | ||
| 263 | 263 | 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. | |
| 264 | 264 | ||
| 265 | 265 | ||
| 266 | 266 | 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. | |
| 267 | 267 | ||
| 268 | 268 | 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 | |
| 269 | 269 | ---|---|---|---|--- | |
| 270 | 270 | 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. | |
| 271 | 271 | 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 | |
| 272 | 272 | AnUnicodeString:='ãü'; | Without | Wrong everywhere | Wrong everywhere | Wrong everywhere | |
| 273 | 273 | 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 | |
| 274 | 274 | AnUTF8String:='ãü'; | Without | Same as AnsiString | Wrong everywhere | Wrong everywhere | |
| 275 | 275 | 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. | |
| 276 | 276 | ||
| 277 | 277 | * W-WinAPI = Windows API "W" functions, UTF-16 | |
| 278 | 278 | * A-WinAPI = Windows API non "W" functions, 8bit system code page | |
| 279 | 279 | * System CP = The 8bit system code page of the OS. For example code page [1252](<http://en.wikipedia.org/wiki/Windows-1252>). | |
| 280 | 280 | ||
| 281 | 281 | ``` | |
| 282 | 282 | const | |
| 283 | 283 | c='ãü'; | |
| 284 | 284 | cstring: string = 'ãü'; // see AnAnsiString:='ãü'; | |
| 285 | 285 | var | |
| 286 | 286 | s: string; | |
| 287 | 287 | u: UnicodeString; | |
| 288 | 288 | begin | |
| 289 | 289 | s:=c; // same as s:='ãü'; | |
| 290 | 290 | s:=cstring; // does not change encoding | |
| 291 | 291 | u:=c; // same as u:='ãü'; | |
| 292 | 292 | u:=cstring; // fpc 2.6.1: converts from system cp to UTF-16, fpc 2.7.1+: depends on encoding of cstring | |
| 293 | 293 | end; | |
| 294 | 294 | ``` | |
| 295 | 295 | ||
| 296 | 296 | ## Voir aussi | |
| 297 | 297 | ||
| 298 | 298 | * [[FPC Unicode support/fr|Support de l'Unicode par FPC]] | |
| 299 | 299 | * [[LCL Unicode Support/fr|Support de l'Unicode par la LCL]] | |
| 300 | 300 | * [[TStringList-TStrings Tutorial/fr|Tutoriel TStringList-TStrings]] | |
| 301 | 301 | * [A Brief History of Strings](<http://www.codexterity.com/delphistrings.htm>) | |
| 302 | 302 | ||
| 303 | 303 | --- | |
| 304 | - | ||
| 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).* |
