Line data Source code
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 : import 'span.dart'; 6 : 7 : /// Returns the minimum of [obj1] and [obj2] according to 8 : /// [Comparable.compareTo]. 9 0 : T min<T extends Comparable>(T obj1, T obj2) => 10 0 : obj1.compareTo(obj2) > 0 ? obj2 : obj1; 11 : 12 : /// Returns the maximum of [obj1] and [obj2] according to 13 : /// [Comparable.compareTo]. 14 0 : T max<T extends Comparable>(T obj1, T obj2) => 15 0 : obj1.compareTo(obj2) > 0 ? obj1 : obj2; 16 : 17 : /// Returns whether all elements of [iter] are the same value, according to 18 : /// `==`. 19 0 : bool isAllTheSame(Iterable<Object?> iter) { 20 0 : if (iter.isEmpty) return true; 21 0 : final firstValue = iter.first; 22 0 : for (var value in iter.skip(1)) { 23 0 : if (value != firstValue) { 24 : return false; 25 : } 26 : } 27 : return true; 28 : } 29 : 30 : /// Returns whether [span] covers multiple lines. 31 0 : bool isMultiline(SourceSpan span) => span.start.line != span.end.line; 32 : 33 : /// Sets the first `null` element of [list] to [element]. 34 0 : void replaceFirstNull<E>(List<E?> list, E element) { 35 0 : final index = list.indexOf(null); 36 0 : if (index < 0) throw ArgumentError('$list contains no null elements.'); 37 0 : list[index] = element; 38 : } 39 : 40 : /// Sets the element of [list] that currently contains [element] to `null`. 41 0 : void replaceWithNull<E>(List<E?> list, E element) { 42 0 : final index = list.indexOf(element); 43 0 : if (index < 0) { 44 0 : throw ArgumentError('$list contains no elements matching $element.'); 45 : } 46 : 47 0 : list[index] = null; 48 : } 49 : 50 : /// Returns the number of instances of [codeUnit] in [string]. 51 0 : int countCodeUnits(String string, int codeUnit) { 52 : var count = 0; 53 0 : for (var codeUnitToCheck in string.codeUnits) { 54 0 : if (codeUnitToCheck == codeUnit) count++; 55 : } 56 : return count; 57 : } 58 : 59 : /// Finds a line in [context] containing [text] at the specified [column]. 60 : /// 61 : /// Returns the index in [context] where that line begins, or null if none 62 : /// exists. 63 0 : int? findLineStart(String context, String text, int column) { 64 : // If the text is empty, we just want to find the first line that has at least 65 : // [column] characters. 66 0 : if (text.isEmpty) { 67 : var beginningOfLine = 0; 68 : while (true) { 69 0 : final index = context.indexOf('\n', beginningOfLine); 70 0 : if (index == -1) { 71 0 : return context.length - beginningOfLine >= column 72 : ? beginningOfLine 73 : : null; 74 : } 75 : 76 0 : if (index - beginningOfLine >= column) return beginningOfLine; 77 0 : beginningOfLine = index + 1; 78 : } 79 : } 80 : 81 0 : var index = context.indexOf(text); 82 0 : while (index != -1) { 83 : // Start looking before [index] in case [text] starts with a newline. 84 0 : final lineStart = index == 0 ? 0 : context.lastIndexOf('\n', index - 1) + 1; 85 0 : final textColumn = index - lineStart; 86 0 : if (column == textColumn) return lineStart; 87 0 : index = context.indexOf(text, index + 1); 88 : } 89 : // ignore: avoid_returning_null 90 : return null; 91 : }