OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 library trydart.source_update; |
| 6 |
| 7 /// Returns [updates] expanded to full compilation units/source files. |
| 8 /// |
| 9 /// [updates] is a convenient way to write updates/patches to a single source |
| 10 /// file without repeating common parts. |
| 11 /// |
| 12 /// For example: |
| 13 /// ["head ", ["v1", "v2"], " tail"] |
| 14 /// expands to: |
| 15 /// ["head v1 tail", "head v2 tail"] |
| 16 List<String> expandUpdates(List updates) { |
| 17 int outputCount = updates.firstWhere((e) => e is Iterable).length; |
| 18 List<StringBuffer> result = new List<StringBuffer>(outputCount); |
| 19 for (int i = 0; i < outputCount; i++) { |
| 20 result[i] = new StringBuffer(); |
| 21 } |
| 22 for (var chunk in updates) { |
| 23 if (chunk is Iterable) { |
| 24 int segmentCount = 0; |
| 25 for (var segment in chunk) { |
| 26 result[segmentCount++].write(segment); |
| 27 } |
| 28 if (segmentCount != outputCount) { |
| 29 throw new ArgumentError( |
| 30 "Expected ${outputCount} segments, " |
| 31 "but found ${segmentCount} in $chunk"); |
| 32 } |
| 33 } else { |
| 34 for (StringBuffer buffer in result) { |
| 35 buffer.write(chunk); |
| 36 } |
| 37 } |
| 38 } |
| 39 |
| 40 return result.map((e) => '$e').toList(); |
| 41 } |
OLD | NEW |