@@ -40,7 +40,7 @@ class Utf8Codec extends Encoding {
40
40
const Utf8Codec ({bool allowMalformed = false })
41
41
: _allowMalformed = allowMalformed;
42
42
43
- /// The name of this codec, "utf-8".
43
+ /// The name of this codec is "utf-8".
44
44
String get name => "utf-8" ;
45
45
46
46
/// Decodes the UTF-8 [codeUnits] (a list of unsigned 8-bit integers) to the
@@ -49,7 +49,7 @@ class Utf8Codec extends Encoding {
49
49
/// If the [codeUnits] start with the encoding of a
50
50
/// [unicodeBomCharacterRune] , that character is discarded.
51
51
///
52
- /// If [allowMalformed] is `true` the decoder replaces invalid (or
52
+ /// If [allowMalformed] is `true` , the decoder replaces invalid (or
53
53
/// unterminated) character sequences with the Unicode Replacement character
54
54
/// `U+FFFD` (�). Otherwise it throws a [FormatException] .
55
55
///
@@ -74,6 +74,14 @@ class Utf8Codec extends Encoding {
74
74
75
75
/// This class converts strings to their UTF-8 code units (a list of
76
76
/// 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
+ /// ```
77
85
class Utf8Encoder extends Converter <String , List <int >> {
78
86
const Utf8Encoder ();
79
87
@@ -148,10 +156,10 @@ class _Utf8Encoder {
148
156
/// writes it to [_buffer] .
149
157
///
150
158
/// 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
152
160
/// surrogate and has not been written yet.
153
161
///
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
155
163
/// character is written to represent the unpaired lead surrogate.
156
164
bool _writeSurrogate (int leadingSurrogate, int nextCodeUnit) {
157
165
if (_isTailSurrogate (nextCodeUnit)) {
@@ -285,6 +293,31 @@ class _Utf8EncoderSink extends _Utf8Encoder with StringConversionSinkMixin {
285
293
286
294
/// This class converts UTF-8 code units (lists of unsigned 8-bit integers)
287
295
/// 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
+ /// ```
288
321
class Utf8Decoder extends Converter <List <int >, String > {
289
322
final bool _allowMalformed;
290
323
@@ -293,7 +326,7 @@ class Utf8Decoder extends Converter<List<int>, String> {
293
326
/// The optional [allowMalformed] argument defines how [convert] deals
294
327
/// with invalid or unterminated character sequences.
295
328
///
296
- /// If it is `true` [convert] replaces invalid (or unterminated) character
329
+ /// If it is `true` , [convert] replaces invalid (or unterminated) character
297
330
/// sequences with the Unicode Replacement character `U+FFFD` (�). Otherwise
298
331
/// it throws a [FormatException] .
299
332
const Utf8Decoder ({bool allowMalformed = false })
@@ -302,7 +335,7 @@ class Utf8Decoder extends Converter<List<int>, String> {
302
335
/// Converts the UTF-8 [codeUnits] (a list of unsigned 8-bit integers) to the
303
336
/// corresponding string.
304
337
///
305
- /// Uses the code units from [start] to, but no including, [end] .
338
+ /// Uses the code units from [start] to, but not including, [end] .
306
339
/// If [end] is omitted, it defaults to `codeUnits.length` .
307
340
///
308
341
/// If the [codeUnits] start with the encoding of a
0 commit comments