Skip to content

Commit 56035a7

Browse files
jpsiitonencommit-bot@chromium.org
authored andcommitted
Documentation update for Utf8Encoder and Utf8Decoder
Examples for Utf8Encoder and Utf8Decoder usage. Some small text updates done. Closes #47622 #47622 GitOrigin-RevId: 8fa3688 Change-Id: I5fa545128bdbec7b27a1bc35c01d5764ddd2a432 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/219281 Reviewed-by: Lasse R.H. Nielsen <[email protected]> Commit-Queue: Lasse R.H. Nielsen <[email protected]>
1 parent 9703f4a commit 56035a7

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

sdk/lib/convert/utf.dart

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Utf8Codec extends Encoding {
4040
const Utf8Codec({bool allowMalformed = false})
4141
: _allowMalformed = allowMalformed;
4242

43-
/// The name of this codec, "utf-8".
43+
/// The name of this codec is "utf-8".
4444
String get name => "utf-8";
4545

4646
/// Decodes the UTF-8 [codeUnits] (a list of unsigned 8-bit integers) to the
@@ -49,7 +49,7 @@ class Utf8Codec extends Encoding {
4949
/// If the [codeUnits] start with the encoding of a
5050
/// [unicodeBomCharacterRune], that character is discarded.
5151
///
52-
/// If [allowMalformed] is `true` the decoder replaces invalid (or
52+
/// If [allowMalformed] is `true`, the decoder replaces invalid (or
5353
/// unterminated) character sequences with the Unicode Replacement character
5454
/// `U+FFFD` (�). Otherwise it throws a [FormatException].
5555
///
@@ -74,6 +74,14 @@ class Utf8Codec extends Encoding {
7474

7575
/// This class converts strings to their UTF-8 code units (a list of
7676
/// unsigned 8-bit integers).
77+
///
78+
/// Example:
79+
/// ```dart
80+
/// final utf8Encoder = utf8.encoder;
81+
/// const sample = 'Îñţérñåţîöñåļîžåţîờñ';
82+
/// final encodedSample = utf8Encoder.convert(sample);
83+
/// print(encodedSample);
84+
/// ```
7785
class Utf8Encoder extends Converter<String, List<int>> {
7886
const Utf8Encoder();
7987

@@ -148,10 +156,10 @@ class _Utf8Encoder {
148156
/// writes it to [_buffer].
149157
///
150158
/// Returns true if the [nextCodeUnit] was combined with the
151-
/// [leadingSurrogate]. If it wasn't then nextCodeUnit was not a trailing
159+
/// [leadingSurrogate]. If it wasn't, then nextCodeUnit was not a trailing
152160
/// surrogate and has not been written yet.
153161
///
154-
/// It is safe to pass 0 for [nextCodeUnit] in which case a replacement
162+
/// It is safe to pass 0 for [nextCodeUnit], in which case a replacement
155163
/// character is written to represent the unpaired lead surrogate.
156164
bool _writeSurrogate(int leadingSurrogate, int nextCodeUnit) {
157165
if (_isTailSurrogate(nextCodeUnit)) {
@@ -285,6 +293,31 @@ class _Utf8EncoderSink extends _Utf8Encoder with StringConversionSinkMixin {
285293

286294
/// This class converts UTF-8 code units (lists of unsigned 8-bit integers)
287295
/// to a string.
296+
///
297+
/// Example:
298+
/// ```dart
299+
/// final utf8Decoder = utf8.decoder;
300+
/// const encodedBytes = [
301+
/// 195, 142, 195, 177, 197, 163, 195, 169, 114, 195, 177, 195, 165, 197,
302+
/// 163, 195, 174, 195, 182, 195, 177, 195, 165, 196, 188, 195, 174, 197,
303+
/// 190, 195, 165, 197, 163, 195, 174, 225, 187, 157, 195, 177];
304+
///
305+
/// final decodedBytes = utf8Decoder.convert(encodedBytes);
306+
/// print(decodedBytes); // Îñţérñåţîöñåļîžåţîờñ
307+
/// ```
308+
/// Throws a [FormatException] if the encoded input contains
309+
/// invalid UTF-8 byte sequences and [allowMalformed] is `false` (the default).
310+
///
311+
/// If [allowMalformed] is `true`, invalid byte sequences are converted into
312+
/// one or more Unicode replacement characters, U+FFFD ('�').
313+
///
314+
/// Example with `allowMalformed` set to true:
315+
/// ```dart
316+
/// const utf8Decoder = Utf8Decoder(allowMalformed: true);
317+
/// const encodedBytes = [0xFF];
318+
/// final decodedBytes = utf8Decoder.convert(encodedBytes);
319+
/// print(decodedBytes); // �
320+
/// ```
288321
class Utf8Decoder extends Converter<List<int>, String> {
289322
final bool _allowMalformed;
290323

@@ -293,7 +326,7 @@ class Utf8Decoder extends Converter<List<int>, String> {
293326
/// The optional [allowMalformed] argument defines how [convert] deals
294327
/// with invalid or unterminated character sequences.
295328
///
296-
/// If it is `true` [convert] replaces invalid (or unterminated) character
329+
/// If it is `true`, [convert] replaces invalid (or unterminated) character
297330
/// sequences with the Unicode Replacement character `U+FFFD` (�). Otherwise
298331
/// it throws a [FormatException].
299332
const Utf8Decoder({bool allowMalformed = false})
@@ -302,7 +335,7 @@ class Utf8Decoder extends Converter<List<int>, String> {
302335
/// Converts the UTF-8 [codeUnits] (a list of unsigned 8-bit integers) to the
303336
/// corresponding string.
304337
///
305-
/// Uses the code units from [start] to, but no including, [end].
338+
/// Uses the code units from [start] to, but not including, [end].
306339
/// If [end] is omitted, it defaults to `codeUnits.length`.
307340
///
308341
/// If the [codeUnits] start with the encoding of a

0 commit comments

Comments
 (0)