/
string_transformer.dart
126 lines (100 loc) · 3.44 KB
/
string_transformer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
part of dart.io;
/// The current system encoding.
///
/// This is used for converting from bytes to and from Strings when
/// communicating on stdin, stdout and stderr.
///
/// On Windows this will use the currently active code page for the conversion.
/// On all other systems it will always use UTF-8.
const SystemEncoding systemEncoding = const SystemEncoding();
@Deprecated("Use systemEncoding instead")
const SystemEncoding SYSTEM_ENCODING = const SystemEncoding();
/// The system encoding is the current code page on Windows and UTF-8 on Linux
/// and Mac.
class SystemEncoding extends Encoding {
/// Creates a const SystemEncoding.
///
/// Users should use the top-level constant, [systemEncoding].
const SystemEncoding();
String get name => 'system';
List<int> encode(String input) => encoder.convert(input);
String decode(List<int> encoded) => decoder.convert(encoded);
Converter<String, List<int>> get encoder {
if (Platform.operatingSystem == "windows") {
return const _WindowsCodePageEncoder();
} else {
return const Utf8Encoder();
}
}
Converter<List<int>, String> get decoder {
if (Platform.operatingSystem == "windows") {
return const _WindowsCodePageDecoder();
} else {
return const Utf8Decoder();
}
}
}
class _WindowsCodePageEncoder extends Converter<String, List<int>> {
const _WindowsCodePageEncoder();
List<int> convert(String input) {
List<int> encoded = _encodeString(input);
if (encoded == null) {
throw new FormatException("Invalid character for encoding");
}
return encoded;
}
/// Starts a chunked conversion.
StringConversionSink startChunkedConversion(Sink<List<int>> sink) {
return new _WindowsCodePageEncoderSink(sink);
}
external static List<int> _encodeString(String string);
}
class _WindowsCodePageEncoderSink extends StringConversionSinkBase {
// TODO(floitsch): provide more efficient conversions when the input is
// not a String.
final Sink<List<int>> _sink;
_WindowsCodePageEncoderSink(this._sink);
void close() {
_sink.close();
}
void add(String string) {
List<int> encoded = _WindowsCodePageEncoder._encodeString(string);
if (encoded == null) {
throw new FormatException("Invalid character for encoding");
}
_sink.add(encoded);
}
void addSlice(String source, int start, int end, bool isLast) {
if (start != 0 || end != source.length) {
source = source.substring(start, end);
}
add(source);
if (isLast) close();
}
}
class _WindowsCodePageDecoder extends Converter<List<int>, String> {
const _WindowsCodePageDecoder();
String convert(List<int> input) {
return _decodeBytes(input);
}
/// Starts a chunked conversion.
ByteConversionSink startChunkedConversion(Sink<String> sink) {
return new _WindowsCodePageDecoderSink(sink);
}
external static String _decodeBytes(List<int> bytes);
}
class _WindowsCodePageDecoderSink extends ByteConversionSinkBase {
// TODO(floitsch): provide more efficient conversions when the input is
// a slice.
final Sink<String> _sink;
_WindowsCodePageDecoderSink(this._sink);
void close() {
_sink.close();
}
void add(List<int> bytes) {
_sink.add(_WindowsCodePageDecoder._decodeBytes(bytes));
}
}