LCOV - code coverage report
Current view: top level - http-0.11.3+15/lib/src - utils.dart (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 0 41 0.0 %
Date: 2017-10-10 20:17:03 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
       2             : // for details. All rights reserved. Use of this source code is governed by a
       3             : // BSD-style license that can be found in the LICENSE file.
       4             : 
       5             : import 'dart:async';
       6             : import 'dart:convert';
       7             : import 'dart:typed_data';
       8             : 
       9             : import 'byte_stream.dart';
      10             : 
      11             : /// Converts a [Map] from parameter names to values to a URL query string.
      12             : ///
      13             : ///     mapToQuery({"foo": "bar", "baz": "bang"});
      14             : ///     //=> "foo=bar&baz=bang"
      15             : String mapToQuery(Map<String, String> map, {Encoding encoding}) {
      16           0 :   var pairs = <List<String>>[];
      17           0 :   map.forEach((key, value) =>
      18           0 :       pairs.add([Uri.encodeQueryComponent(key, encoding: encoding),
      19           0 :                  Uri.encodeQueryComponent(value, encoding: encoding)]));
      20           0 :   return pairs.map((pair) => "${pair[0]}=${pair[1]}").join("&");
      21             : }
      22             : 
      23             : /// Like [String.split], but only splits on the first occurrence of the pattern.
      24             : /// This will always return an array of two elements or fewer.
      25             : ///
      26             : ///     split1("foo,bar,baz", ","); //=> ["foo", "bar,baz"]
      27             : ///     split1("foo", ","); //=> ["foo"]
      28             : ///     split1("", ","); //=> []
      29             : List<String> split1(String toSplit, String pattern) {
      30           0 :   if (toSplit.isEmpty) return <String>[];
      31             : 
      32           0 :   var index = toSplit.indexOf(pattern);
      33           0 :   if (index == -1) return [toSplit];
      34           0 :   return [
      35           0 :     toSplit.substring(0, index),
      36           0 :     toSplit.substring(index + pattern.length)
      37             :   ];
      38             : }
      39             : 
      40             : /// Returns the [Encoding] that corresponds to [charset]. Returns [fallback] if
      41             : /// [charset] is null or if no [Encoding] was found that corresponds to
      42             : /// [charset].
      43             : Encoding encodingForCharset(String charset, [Encoding fallback = LATIN1]) {
      44             :   if (charset == null) return fallback;
      45           0 :   var encoding = Encoding.getByName(charset);
      46             :   return encoding == null ? fallback : encoding;
      47             : }
      48             : 
      49             : 
      50             : /// Returns the [Encoding] that corresponds to [charset]. Throws a
      51             : /// [FormatException] if no [Encoding] was found that corresponds to [charset].
      52             : /// [charset] may not be null.
      53             : Encoding requiredEncodingForCharset(String charset) {
      54           0 :   var encoding = Encoding.getByName(charset);
      55             :   if (encoding != null) return encoding;
      56           0 :   throw new FormatException('Unsupported encoding "$charset".');
      57             : }
      58             : 
      59             : /// A regular expression that matches strings that are composed entirely of
      60             : /// ASCII-compatible characters.
      61             : final RegExp _ASCII_ONLY = new RegExp(r"^[\x00-\x7F]+$");
      62             : 
      63             : /// Returns whether [string] is composed entirely of ASCII-compatible
      64             : /// characters.
      65           0 : bool isPlainAscii(String string) => _ASCII_ONLY.hasMatch(string);
      66             : 
      67             : /// Converts [input] into a [Uint8List].
      68             : ///
      69             : /// If [input] is a [TypedData], this just returns a view on [input].
      70             : Uint8List toUint8List(List<int> input) {
      71           0 :   if (input is Uint8List) return input;
      72           0 :   if (input is TypedData) {
      73             :     // TODO(nweiz): remove "as" when issue 11080 is fixed.
      74           0 :     return new Uint8List.view((input as TypedData).buffer);
      75             :   }
      76           0 :   return new Uint8List.fromList(input);
      77             : }
      78             : 
      79             : /// If [stream] is already a [ByteStream], returns it. Otherwise, wraps it in a
      80             : /// [ByteStream].
      81             : ByteStream toByteStream(Stream<List<int>> stream) {
      82           0 :   if (stream is ByteStream) return stream;
      83           0 :   return new ByteStream(stream);
      84             : }
      85             : 
      86             : /// Calls [onDone] once [stream] (a single-subscription [Stream]) is finished.
      87             : /// The return value, also a single-subscription [Stream] should be used in
      88             : /// place of [stream] after calling this method.
      89             : Stream<T> onDone<T>(Stream<T> stream, void onDone()) =>
      90           0 :     stream.transform(new StreamTransformer.fromHandlers(handleDone: (sink) {
      91           0 :       sink.close();
      92           0 :       onDone();
      93             :     }));
      94             : 
      95             : // TODO(nweiz): remove this when issue 7786 is fixed.
      96             : /// Pipes all data and errors from [stream] into [sink]. When [stream] is done,
      97             : /// [sink] is closed and the returned [Future] is completed.
      98             : Future store(Stream stream, EventSink sink) {
      99           0 :   var completer = new Completer();
     100           0 :   stream.listen(sink.add,
     101           0 :       onError: sink.addError,
     102             :       onDone: () {
     103           0 :         sink.close();
     104           0 :         completer.complete();
     105             :       });
     106           0 :   return completer.future;
     107             : }
     108             : 
     109             : /// Pipes all data and errors from [stream] into [sink]. Completes [Future] once
     110             : /// [stream] is done. Unlike [store], [sink] remains open after [stream] is
     111             : /// done.
     112             : Future writeStreamToSink(Stream stream, EventSink sink) {
     113           0 :   var completer = new Completer();
     114           0 :   stream.listen(sink.add,
     115           0 :       onError: sink.addError,
     116           0 :       onDone: () => completer.complete());
     117           0 :   return completer.future;
     118             : }
     119             : 
     120             : /// A pair of values.
     121             : class Pair<E, F> {
     122             :   E first;
     123             :   F last;
     124             : 
     125           0 :   Pair(this.first, this.last);
     126             : 
     127           0 :   String toString() => '($first, $last)';
     128             : 
     129             :   bool operator==(other) {
     130           0 :     if (other is! Pair) return false;
     131           0 :     return other.first == first && other.last == last;
     132             :   }
     133             : 
     134           0 :   int get hashCode => first.hashCode ^ last.hashCode;
     135             : }
     136             : 
     137             : /// Configures [future] so that its result (success or exception) is passed on
     138             : /// to [completer].
     139             : void chainToCompleter(Future future, Completer completer) {
     140           0 :   future.then(completer.complete, onError: completer.completeError);
     141             : }

Generated by: LCOV version 1.13