OLD | NEW |
| (Empty) |
1 part of dart.convert; | |
2 abstract class StringConversionSink extends ChunkedConversionSink<String> {Stri
ngConversionSink(); | |
3 factory StringConversionSink.withCallback(void callback(String accumulated)) =
_StringCallbackSink; | |
4 factory StringConversionSink.from(Sink<String> sink) = _StringAdapterSink; | |
5 factory StringConversionSink.fromStringSink(StringSink sink) = _StringSinkConve
rsionSink; | |
6 void addSlice(String chunk, int start, int end, bool isLast); | |
7 ByteConversionSink asUtf8Sink(bool allowMalformed); | |
8 ClosableStringSink asStringSink(); | |
9 } | |
10 abstract class ClosableStringSink extends StringSink {factory ClosableStringSin
k.fromStringSink(StringSink sink, void onClose()) = _ClosableStringSink; | |
11 void close(); | |
12 } | |
13 typedef void _StringSinkCloseCallback(); | |
14 class _ClosableStringSink implements ClosableStringSink {final _StringSinkClose
Callback _callback; | |
15 final StringSink _sink; | |
16 _ClosableStringSink(this._sink, this._callback); | |
17 void close() => _callback(); | |
18 void writeCharCode(int charCode) => _sink.writeCharCode(charCode); | |
19 void write(Object o) => _sink.write(o); | |
20 void writeln([Object o = ""]) => _sink.writeln(o); | |
21 void writeAll(Iterable objects, [String separator = ""]) => _sink.writeAll(obje
cts, separator); | |
22 } | |
23 class _StringConversionSinkAsStringSinkAdapter implements ClosableStringSink {s
tatic const _MIN_STRING_SIZE = 16; | |
24 StringBuffer _buffer; | |
25 StringConversionSink _chunkedSink; | |
26 _StringConversionSinkAsStringSinkAdapter(this._chunkedSink) : _buffer = new Str
ingBuffer(); | |
27 void close() { | |
28 if (_buffer.isNotEmpty) _flush(); | |
29 _chunkedSink.close(); | |
30 } | |
31 void writeCharCode(int charCode) { | |
32 _buffer.writeCharCode(charCode); | |
33 if (_buffer.length > _MIN_STRING_SIZE) _flush(); | |
34 } | |
35 void write(Object o) { | |
36 if (_buffer.isNotEmpty) _flush(); | |
37 String str = o.toString(); | |
38 _chunkedSink.add(o.toString()); | |
39 } | |
40 void writeln([Object o = ""]) { | |
41 _buffer.writeln(o); | |
42 if (_buffer.length > _MIN_STRING_SIZE) _flush(); | |
43 } | |
44 void writeAll(Iterable objects, [String separator = ""]) { | |
45 if (_buffer.isNotEmpty) _flush(); | |
46 Iterator iterator = objects.iterator; | |
47 if (!iterator.moveNext()) return; if (separator.isEmpty) { | |
48 do { | |
49 _chunkedSink.add(iterator.current.toString()); | |
50 } | |
51 while (iterator.moveNext());} | |
52 else { | |
53 _chunkedSink.add(iterator.current.toString()); | |
54 while (iterator.moveNext()) { | |
55 write(separator); | |
56 _chunkedSink.add(iterator.current.toString()); | |
57 } | |
58 } | |
59 } | |
60 void _flush() { | |
61 String accumulated = _buffer.toString(); | |
62 _buffer.clear(); | |
63 _chunkedSink.add(accumulated); | |
64 } | |
65 } | |
66 abstract class StringConversionSinkBase extends StringConversionSinkMixin {} | |
67 abstract class StringConversionSinkMixin implements StringConversionSink {void
addSlice(String str, int start, int end, bool isLast); | |
68 void close(); | |
69 void add(String str) => addSlice(str, 0, str.length, false); | |
70 ByteConversionSink asUtf8Sink(bool allowMalformed) { | |
71 return new _Utf8ConversionSink(this, allowMalformed); | |
72 } | |
73 ClosableStringSink asStringSink() { | |
74 return new _StringConversionSinkAsStringSinkAdapter(this); | |
75 } | |
76 } | |
77 class _StringSinkConversionSink extends StringConversionSinkBase {StringSink _s
tringSink; | |
78 _StringSinkConversionSink(StringSink this._stringSink); | |
79 void close() { | |
80 } | |
81 void addSlice(String str, int start, int end, bool isLast) { | |
82 if (start != 0 || end != str.length) { | |
83 for (int i = start; i < end; i++) { | |
84 _stringSink.writeCharCode(str.codeUnitAt(i)); | |
85 } | |
86 } | |
87 else { | |
88 _stringSink.write(str); | |
89 } | |
90 if (isLast) close(); | |
91 } | |
92 void add(String str) => _stringSink.write(str); | |
93 ByteConversionSink asUtf8Sink(bool allowMalformed) { | |
94 return new _Utf8StringSinkAdapter(this, _stringSink, allowMalformed); | |
95 } | |
96 ClosableStringSink asStringSink() { | |
97 return new ClosableStringSink.fromStringSink(_stringSink, this.close); | |
98 } | |
99 } | |
100 class _StringCallbackSink extends _StringSinkConversionSink {final _ChunkedConv
ersionCallback<String> _callback; | |
101 _StringCallbackSink(this._callback) : super(new StringBuffer()); | |
102 void close() { | |
103 StringBuffer buffer = DEVC$RT.cast(_stringSink, StringSink, StringBuffer, "Assig
nmentCast", """line 233, column 27 of dart:convert/string_conversion.dart: """,
_stringSink is StringBuffer, true); | |
104 String accumulated = buffer.toString(); | |
105 buffer.clear(); | |
106 _callback(accumulated); | |
107 } | |
108 ByteConversionSink asUtf8Sink(bool allowMalformed) { | |
109 return new _Utf8StringSinkAdapter(this, _stringSink, allowMalformed); | |
110 } | |
111 } | |
112 class _StringAdapterSink extends StringConversionSinkBase {final Sink<String> _
sink; | |
113 _StringAdapterSink(this._sink); | |
114 void add(String str) => _sink.add(str); | |
115 void addSlice(String str, int start, int end, bool isLast) { | |
116 if (start == 0 && end == str.length) { | |
117 add(str); | |
118 } | |
119 else { | |
120 add(str.substring(start, end)); | |
121 } | |
122 if (isLast) close(); | |
123 } | |
124 void close() => _sink.close(); | |
125 } | |
126 class _Utf8StringSinkAdapter extends ByteConversionSink {final _Utf8Decoder _de
coder; | |
127 final Sink _sink; | |
128 _Utf8StringSinkAdapter(this._sink, StringSink stringSink, bool allowMalformed)
: _decoder = new _Utf8Decoder(stringSink, allowMalformed); | |
129 void close() { | |
130 _decoder.close(); | |
131 if (_sink != null) _sink.close(); | |
132 } | |
133 void add(List<int> chunk) { | |
134 addSlice(chunk, 0, chunk.length, false); | |
135 } | |
136 void addSlice(List<int> codeUnits, int startIndex, int endIndex, bool isLast) { | |
137 _decoder.convert(codeUnits, startIndex, endIndex); | |
138 if (isLast) close(); | |
139 } | |
140 } | |
141 class _Utf8ConversionSink extends ByteConversionSink {final _Utf8Decoder _decod
er; | |
142 final StringConversionSink _chunkedSink; | |
143 final StringBuffer _buffer; | |
144 _Utf8ConversionSink(StringConversionSink sink, bool allowMalformed) : this._(si
nk, new StringBuffer(), allowMalformed); | |
145 _Utf8ConversionSink._(this._chunkedSink, StringBuffer stringBuffer, bool allowM
alformed) : _decoder = new _Utf8Decoder(stringBuffer, allowMalformed), _buffer =
stringBuffer; | |
146 void close() { | |
147 _decoder.close(); | |
148 if (_buffer.isNotEmpty) { | |
149 String accumulated = _buffer.toString(); | |
150 _buffer.clear(); | |
151 _chunkedSink.addSlice(accumulated, 0, accumulated.length, true); | |
152 } | |
153 else { | |
154 _chunkedSink.close(); | |
155 } | |
156 } | |
157 void add(List<int> chunk) { | |
158 addSlice(chunk, 0, chunk.length, false); | |
159 } | |
160 void addSlice(List<int> chunk, int startIndex, int endIndex, bool isLast) { | |
161 _decoder.convert(chunk, startIndex, endIndex); | |
162 if (_buffer.isNotEmpty) { | |
163 String accumulated = _buffer.toString(); | |
164 _chunkedSink.addSlice(accumulated, 0, accumulated.length, isLast); | |
165 _buffer.clear(); | |
166 return;} | |
167 if (isLast) close(); | |
168 } | |
169 } | |
OLD | NEW |