244 lines
3.8 KiB
Markdown
244 lines
3.8 KiB
Markdown
# Exemples / DO-DON'T — Chapitre 21 — Strings, Unicode et encodages
|
|
|
|
> Document compagnon. Les exemples illustrent les règles du chapitre ; la formulation normative reste dans le chapitre lui-même.
|
|
|
|
## Exemples extraits du chapitre
|
|
|
|
### Exemple 1
|
|
|
|
```text
|
|
String text = "abc";
|
|
text::append("def"); // OK
|
|
```
|
|
|
|
### Exemple 2
|
|
|
|
```text
|
|
'A'
|
|
'é'
|
|
'中'
|
|
'😀'
|
|
'\n'
|
|
'\u{1F600}'
|
|
```
|
|
|
|
### Exemple 3
|
|
|
|
```text
|
|
Utf8Char
|
|
Utf16Char
|
|
Utf32Char
|
|
|
|
Utf8String
|
|
Utf16String
|
|
Utf32String
|
|
```
|
|
|
|
### Exemple 4
|
|
|
|
```text
|
|
Utf8Char
|
|
exactement un Unicode scalar encodé en UTF-8
|
|
1 à 4 uint8
|
|
|
|
Utf16Char
|
|
exactement un Unicode scalar encodé en UTF-16
|
|
1 à 2 uint16
|
|
|
|
Utf32Char
|
|
exactement un Unicode scalar encodé en UTF-32
|
|
1 uint32
|
|
```
|
|
|
|
### Exemple 5
|
|
|
|
```text
|
|
Utf8String
|
|
séquence UTF-8 valide
|
|
|
|
Utf16String
|
|
séquence UTF-16 valide
|
|
|
|
Utf32String
|
|
séquence UTF-32 valide
|
|
```
|
|
|
|
### Exemple 6
|
|
|
|
```text
|
|
UTF-8 -> uint8
|
|
UTF-16 -> uint16
|
|
UTF-32 -> uint32
|
|
```
|
|
|
|
### Exemple 7
|
|
|
|
```text
|
|
char::toUtf8Char()
|
|
Utf8Char::toChar()
|
|
Utf8Char::toUtf16Char()
|
|
|
|
Utf8Char::from(uint8) faults UnicodeEncodingFault
|
|
Utf16Char::from(uint16) faults UnicodeEncodingFault
|
|
Utf32Char::from(uint32) faults UnicodeEncodingFault
|
|
|
|
Utf8Char::toUint8() faults UnicodeEncodingFault
|
|
Utf16Char::toUint16() faults UnicodeEncodingFault
|
|
Utf32Char::toUint32()
|
|
```
|
|
|
|
### Exemple 8
|
|
|
|
```text
|
|
Utf8String text = ...;
|
|
Utf16Char value = ...;
|
|
|
|
text::append(value); // ERROR
|
|
text::append(value::toUtf8Char()); // OK
|
|
```
|
|
|
|
### Exemple 9
|
|
|
|
```text
|
|
String text = ...;
|
|
text[5]; // ERROR
|
|
```
|
|
|
|
### Exemple 10
|
|
|
|
```text
|
|
text::scalarAt(index) -> char
|
|
text::scalarCount() -> uint64
|
|
```
|
|
|
|
### Exemple 11
|
|
|
|
```text
|
|
Utf8String[index] -> uint8
|
|
Utf16String[index] -> uint16
|
|
Utf32String[index] -> uint32
|
|
```
|
|
|
|
### Exemple 12
|
|
|
|
```text
|
|
encodedCharAt(scalarIndex)
|
|
retourne le UtfXChar correspondant au scalar ordinal demandé
|
|
|
|
scalarAt(scalarIndex)
|
|
retourne le char correspondant
|
|
```
|
|
|
|
### Exemple 13
|
|
|
|
```text
|
|
Utf8String::encodedCharAt(uint64) -> Utf8Char
|
|
Utf16String::encodedCharAt(uint64) -> Utf16Char
|
|
Utf32String::encodedCharAt(uint64) -> Utf32Char
|
|
|
|
Utf8String::scalarAt(uint64) -> char
|
|
Utf16String::scalarAt(uint64) -> char
|
|
Utf32String::scalarAt(uint64) -> char
|
|
```
|
|
|
|
### Exemple 14
|
|
|
|
```text
|
|
scalarCount()
|
|
```
|
|
|
|
### Exemple 15
|
|
|
|
```text
|
|
codeUnitCount()
|
|
scalarCount()
|
|
```
|
|
|
|
### Exemple 16
|
|
|
|
```text
|
|
String implements Iterable<char>
|
|
```
|
|
|
|
### Exemple 17
|
|
|
|
```text
|
|
code units
|
|
encoded chars
|
|
Unicode scalars
|
|
```
|
|
|
|
### Exemple 18
|
|
|
|
```text
|
|
codeUnits()
|
|
encodedChars()
|
|
scalars()
|
|
```
|
|
|
|
### Exemple 19
|
|
|
|
```text
|
|
String text = "abc";
|
|
text::append("def");
|
|
```
|
|
|
|
### Exemple 20
|
|
|
|
```text
|
|
Utf8String text = ...;
|
|
Utf8Char value = ...;
|
|
|
|
text::append(value); // OK
|
|
```
|
|
|
|
### Exemple 21
|
|
|
|
```text
|
|
text[index] = 0xFF; // ERROR
|
|
```
|
|
|
|
### Exemple 22
|
|
|
|
```text
|
|
"..." String normale, escapes actifs
|
|
"""...""" String normale multiligne
|
|
r"..." String brute
|
|
r"""...""" String brute multiligne
|
|
i"..." String interpolée
|
|
i"""...""" String interpolée multiligne
|
|
ir"..." interpolation + contenu brut, réservé
|
|
b"..." bytes, réservé
|
|
br"..." bytes bruts, réservé
|
|
u8"..." texte encodé UTF-8 explicitement, réservé
|
|
u16"..." texte encodé UTF-16 explicitement, réservé
|
|
u32"..." texte encodé UTF-32 explicitement, réservé
|
|
c"..." chaîne compatible C, réservé
|
|
cr"..." chaîne C brute, réservé
|
|
t"..." template structuré, réservé
|
|
```
|
|
|
|
### Exemple 23
|
|
|
|
```text
|
|
\\
|
|
\"
|
|
\'
|
|
\n
|
|
\r
|
|
\t
|
|
\0
|
|
\u{...}
|
|
```
|
|
|
|
### Exemple 24
|
|
|
|
```text
|
|
String a = "abcdef";
|
|
String b = a::substring(1..<4);
|
|
```
|
|
|
|
## DO / DON'T / WHY / compiler error / edge cases
|
|
|
|
À consolider progressivement avant la baseline publique V3 et à transformer, lorsque pertinent, en tests de conformité de la toolchain.
|