Skip to content

Commit 07cd8dd

Browse files
jpsiitonencommit-bot@chromium.org
authored andcommitted
Documentation update for AsciiDecoder and AsciiEncoder
Example codes added for AsciiDecoder and AsciiEncoder Closes #47619 #47619 GitOrigin-RevId: fd3a4cf Change-Id: I63c685013edc913c0783621bfe53635e35e741b6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/219260 Reviewed-by: Lasse R.H. Nielsen <[email protected]> Commit-Queue: Lasse R.H. Nielsen <[email protected]>
1 parent 93c2aad commit 07cd8dd

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

sdk/lib/convert/ascii.dart

+33-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class AsciiCodec extends Encoding {
3535
/// Encoders will not accept invalid (non ASCII) characters.
3636
const AsciiCodec({bool allowInvalid = false}) : _allowInvalid = allowInvalid;
3737

38-
/// The name of this codec, "us-ascii".
38+
/// The name of this codec is "us-ascii".
3939
String get name => "us-ascii";
4040

4141
Uint8List encode(String source) => encoder.convert(source);
@@ -103,7 +103,15 @@ class _UnicodeSubsetEncoder extends Converter<String, List<int>> {
103103
Stream<List<int>> bind(Stream<String> stream) => super.bind(stream);
104104
}
105105

106-
/// This class converts strings of only ASCII characters to bytes.
106+
/// Converts strings of only ASCII characters to bytes.
107+
///
108+
/// Example:
109+
/// ```dart import:typed_data
110+
/// const asciiEncoder = AsciiEncoder();
111+
/// const sample = 'Dart';
112+
/// final asciiValues = asciiEncoder.convert(sample);
113+
/// print(asciiValues); // [68, 97, 114, 116]
114+
/// ```
107115
class AsciiEncoder extends _UnicodeSubsetEncoder {
108116
const AsciiEncoder() : super(_asciiMask);
109117
}
@@ -195,6 +203,29 @@ abstract class _UnicodeSubsetDecoder extends Converter<List<int>, String> {
195203
Stream<String> bind(Stream<List<int>> stream) => super.bind(stream);
196204
}
197205

206+
/// Converts ASCII bytes to string.
207+
///
208+
/// Example:
209+
/// ```dart
210+
/// const asciiDecoder = AsciiDecoder();
211+
/// final asciiValues = [68, 97, 114, 116];
212+
/// final result = asciiDecoder.convert(asciiValues);
213+
/// print(result); // Dart
214+
/// ```
215+
/// Throws a [FormatException] if [bytes] contains values that are not
216+
/// in the range 0 .. 127, and [allowInvalid] is `false` (the default).
217+
///
218+
/// If [allowInvalid] is `true`, any byte outside the range 0..127 is replaced
219+
/// by the Unicode replacement character, U+FFFD ('�').
220+
///
221+
/// Example with `allowInvalid` set to true:
222+
/// ```dart
223+
/// const asciiDecoder = AsciiDecoder(allowInvalid: true);
224+
/// final asciiValues = [68, 97, 114, 116, 20, 0xFF];
225+
/// final result = asciiDecoder.convert(asciiValues);
226+
/// print(result); // Dart �
227+
/// print(result.codeUnits.last.toRadixString(16)); // fffd
228+
/// ```
198229
class AsciiDecoder extends _UnicodeSubsetDecoder {
199230
const AsciiDecoder({bool allowInvalid = false})
200231
: super(allowInvalid, _asciiMask);

0 commit comments

Comments
 (0)