| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library engine.utilities.general; | 5 library analyzer.src.generated.utilities_general; |
| 6 | 6 |
| 7 import 'dart:collection'; |
| 7 import 'dart:developer' show UserTag; | 8 import 'dart:developer' show UserTag; |
| 8 | 9 |
| 9 /** | 10 /** |
| 11 * Test if the given [value] is `false` or the string "false" (case-insensitive)
. |
| 12 */ |
| 13 bool isFalse(Object value) => |
| 14 value is bool ? !value : toLowerCase(value) == 'false'; |
| 15 |
| 16 /** |
| 17 * Test if the given [value] is `true` or the string "true" (case-insensitive). |
| 18 */ |
| 19 bool isTrue(Object value) => |
| 20 value is bool ? value : toLowerCase(value) == 'true'; |
| 21 |
| 22 /** |
| 23 * Safely convert the given [value] to a bool value, or return `null` if the |
| 24 * value coult not be converted. |
| 25 */ |
| 26 bool toBool(Object value) { |
| 27 if (value is bool) { |
| 28 return value; |
| 29 } |
| 30 String string = toLowerCase(value); |
| 31 if (string == 'true') { |
| 32 return true; |
| 33 } |
| 34 if (string == 'false') { |
| 35 return false; |
| 36 } |
| 37 return null; |
| 38 } |
| 39 |
| 40 /** |
| 41 * Safely convert this [value] to lower case, returning `null` if [value] is |
| 42 * null. |
| 43 */ |
| 44 String toLowerCase(Object value) => value?.toString()?.toLowerCase(); |
| 45 |
| 46 /** |
| 47 * Safely convert this [value] to upper case, returning `null` if [value] is |
| 48 * null. |
| 49 */ |
| 50 String toUpperCase(Object value) => value?.toString()?.toUpperCase(); |
| 51 |
| 52 /** |
| 10 * Jenkins hash function, optimized for small integers. | 53 * Jenkins hash function, optimized for small integers. |
| 11 * Borrowed from sdk/lib/math/jenkins_smi_hash.dart. | 54 * Borrowed from sdk/lib/math/jenkins_smi_hash.dart. |
| 12 */ | 55 */ |
| 13 class JenkinsSmiHash { | 56 class JenkinsSmiHash { |
| 14 static int combine(int hash, int value) { | 57 static int combine(int hash, int value) { |
| 15 hash = 0x1fffffff & (hash + value); | 58 hash = 0x1fffffff & (hash + value); |
| 16 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); | 59 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); |
| 17 return hash ^ (hash >> 6); | 60 return hash ^ (hash >> 6); |
| 18 } | 61 } |
| 19 | 62 |
| 20 static int finish(int hash) { | 63 static int finish(int hash) { |
| 21 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); | 64 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); |
| 22 hash = hash ^ (hash >> 11); | 65 hash = hash ^ (hash >> 11); |
| 23 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); | 66 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); |
| 24 } | 67 } |
| 25 | 68 |
| 26 static int hash2(a, b) => finish(combine(combine(0, a), b)); | 69 static int hash2(a, b) => finish(combine(combine(0, a), b)); |
| 27 | 70 |
| 28 static int hash3(a, b, c) => finish(combine(combine(combine(0, a), b), c)); | 71 static int hash3(a, b, c) => finish(combine(combine(combine(0, a), b), c)); |
| 29 | 72 |
| 30 static int hash4(a, b, c, d) => | 73 static int hash4(a, b, c, d) => |
| 31 finish(combine(combine(combine(combine(0, a), b), c), d)); | 74 finish(combine(combine(combine(combine(0, a), b), c), d)); |
| 32 } | 75 } |
| 33 | 76 |
| 34 /** | 77 /** |
| 78 * A simple limited queue. |
| 79 */ |
| 80 class LimitedQueue<E> extends ListQueue<E> { |
| 81 final int limit; |
| 82 |
| 83 /** |
| 84 * Create a queue with [limit] items. |
| 85 */ |
| 86 LimitedQueue(this.limit); |
| 87 |
| 88 @override |
| 89 void add(E o) { |
| 90 super.add(o); |
| 91 while (length > limit) { |
| 92 remove(first); |
| 93 } |
| 94 } |
| 95 } |
| 96 |
| 97 /** |
| 35 * Helper class for gathering performance statistics. This class is modeled on | 98 * Helper class for gathering performance statistics. This class is modeled on |
| 36 * the UserTag class in dart:developer so that it can interoperate easily with | 99 * the UserTag class in dart:developer so that it can interoperate easily with |
| 37 * it. | 100 * it. |
| 38 */ | 101 */ |
| 39 abstract class PerformanceTag { | 102 abstract class PerformanceTag { |
| 40 /** | 103 /** |
| 41 * Return a list of all [PerformanceTag]s which have been created. | 104 * Return a list of all [PerformanceTag]s which have been created. |
| 42 */ | 105 */ |
| 43 static List<PerformanceTag> get all => _PerformanceTagImpl.all.toList(); | 106 static List<PerformanceTag> get all => _PerformanceTagImpl.all.toList(); |
| 44 | 107 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 75 | 138 |
| 76 /** | 139 /** |
| 77 * Make this the current tag for the isolate, and return the previous tag. | 140 * Make this the current tag for the isolate, and return the previous tag. |
| 78 */ | 141 */ |
| 79 PerformanceTag makeCurrent(); | 142 PerformanceTag makeCurrent(); |
| 80 | 143 |
| 81 /** | 144 /** |
| 82 * Make this the current tag for the isolate, run [f], and restore the | 145 * Make this the current tag for the isolate, run [f], and restore the |
| 83 * previous tag. Returns the result of invoking [f]. | 146 * previous tag. Returns the result of invoking [f]. |
| 84 */ | 147 */ |
| 85 makeCurrentWhile(f()); | 148 dynamic/*=E*/ makeCurrentWhile/*<E>*/(dynamic/*=E*/ f()); |
| 86 | 149 |
| 87 /** | 150 /** |
| 88 * Reset the total time tracked by all [PerformanceTag]s to zero. | 151 * Reset the total time tracked by all [PerformanceTag]s to zero. |
| 89 */ | 152 */ |
| 90 static void reset() { | 153 static void reset() { |
| 91 for (_PerformanceTagImpl tag in _PerformanceTagImpl.all) { | 154 for (_PerformanceTagImpl tag in _PerformanceTagImpl.all) { |
| 92 tag.stopwatch.reset(); | 155 tag.stopwatch.reset(); |
| 93 } | 156 } |
| 94 } | 157 } |
| 95 } | 158 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 return current; | 199 return current; |
| 137 } | 200 } |
| 138 _PerformanceTagImpl previous = current; | 201 _PerformanceTagImpl previous = current; |
| 139 previous.stopwatch.stop(); | 202 previous.stopwatch.stop(); |
| 140 stopwatch.start(); | 203 stopwatch.start(); |
| 141 current = this; | 204 current = this; |
| 142 userTag.makeCurrent(); | 205 userTag.makeCurrent(); |
| 143 return previous; | 206 return previous; |
| 144 } | 207 } |
| 145 | 208 |
| 146 makeCurrentWhile(f()) { | 209 dynamic/*=E*/ makeCurrentWhile/*<E>*/(dynamic/*=E*/ f()) { |
| 147 PerformanceTag prevTag = makeCurrent(); | 210 PerformanceTag prevTag = makeCurrent(); |
| 148 try { | 211 try { |
| 149 return f(); | 212 return f(); |
| 150 } finally { | 213 } finally { |
| 151 prevTag.makeCurrent(); | 214 prevTag.makeCurrent(); |
| 152 } | 215 } |
| 153 } | 216 } |
| 154 } | 217 } |
| OLD | NEW |