| OLD | NEW |
| (Empty) |
| 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 part of dart.core; | |
| 6 | |
| 7 /** | |
| 8 * An arbitrarily large integer. | |
| 9 * | |
| 10 * **Note:** When compiling to JavaScript, integers are | |
| 11 * implemented as JavaScript numbers. When compiling to JavaScript, | |
| 12 * integers are therefore restricted to 53 significant bits because | |
| 13 * all JavaScript numbers are double-precision floating point | |
| 14 * values. The behavior of the operators and methods in the [int] | |
| 15 * class therefore sometimes differs between the Dart VM and Dart code | |
| 16 * compiled to JavaScript. | |
| 17 * | |
| 18 * It is a compile-time error for a class to attempt to extend or implement int. | |
| 19 */ | |
| 20 abstract class int extends num { | |
| 21 /** | |
| 22 * Returns the integer value of the given environment declaration [name]. | |
| 23 * | |
| 24 * The result is the same as would be returned by: | |
| 25 * | |
| 26 * int.parse(const String.fromEnvironment(name, defaultValue: ""), | |
| 27 * (_) => defaultValue) | |
| 28 * | |
| 29 * Example: | |
| 30 * | |
| 31 * const int.fromEnvironment("defaultPort", defaultValue: 80) | |
| 32 */ | |
| 33 factory int.fromEnvironment(String name, {int defaultValue}) { | |
| 34 throw new UnsupportedError( | |
| 35 'int.fromEnvironment can only be used as a const constructor'); | |
| 36 } | |
| 37 | |
| 38 /** | |
| 39 * Bit-wise and operator. | |
| 40 * | |
| 41 * Treating both `this` and [other] as sufficiently large two's component | |
| 42 * integers, the result is a number with only the bits set that are set in | |
| 43 * both `this` and [other] | |
| 44 * | |
| 45 * Of both operands are negative, the result is negative, otherwise | |
| 46 * the result is non-negative. | |
| 47 */ | |
| 48 int operator &(int other); | |
| 49 | |
| 50 /** | |
| 51 * Bit-wise or operator. | |
| 52 * | |
| 53 * Treating both `this` and [other] as sufficiently large two's component | |
| 54 * integers, the result is a number with the bits set that are set in either | |
| 55 * of `this` and [other] | |
| 56 * | |
| 57 * If both operands are non-negative, the result is non-negative, | |
| 58 * otherwise the result us negative. | |
| 59 */ | |
| 60 int operator |(int other); | |
| 61 | |
| 62 /** | |
| 63 * Bit-wise exclusive-or operator. | |
| 64 * | |
| 65 * Treating both `this` and [other] as sufficiently large two's component | |
| 66 * integers, the result is a number with the bits set that are set in one, | |
| 67 * but not both, of `this` and [other] | |
| 68 * | |
| 69 * If the operands have the same sign, the result is non-negative, | |
| 70 * otherwise the result is negative. | |
| 71 */ | |
| 72 int operator ^(int other); | |
| 73 | |
| 74 /** | |
| 75 * The bit-wise negate operator. | |
| 76 * | |
| 77 * Treating `this` as a sufficiently large two's component integer, | |
| 78 * the result is a number with the opposite bits set. | |
| 79 * | |
| 80 * This maps any integer `x` to `-x - 1`. | |
| 81 */ | |
| 82 int operator ~(); | |
| 83 | |
| 84 /** | |
| 85 * Shift the bits of this integer to the left by [shiftAmount]. | |
| 86 * | |
| 87 * Shifting to the left makes the number larger, effectively multiplying | |
| 88 * the number by `pow(2, shiftIndex)`. | |
| 89 * | |
| 90 * There is no limit on the size of the result. It may be relevant to | |
| 91 * limit intermediate values by using the "and" operator with a suitable | |
| 92 * mask. | |
| 93 * | |
| 94 * It is an error of [shiftAmount] is negative. | |
| 95 */ | |
| 96 int operator <<(int shiftAmount); | |
| 97 | |
| 98 /** | |
| 99 * Shift the bits of this integer to the right by [shiftAmount]. | |
| 100 * | |
| 101 * Shifting to the right makes the number smaller and drops the least | |
| 102 * significant bits, effectively doing an integer division by | |
| 103 *`pow(2, shiftIndex)`. | |
| 104 * | |
| 105 * It is an error of [shiftAmount] is negative. | |
| 106 */ | |
| 107 int operator >>(int shiftAmount); | |
| 108 | |
| 109 /** Returns true if and only if this integer is even. */ | |
| 110 bool get isEven; | |
| 111 | |
| 112 /** Returns true if and only if this integer is odd. */ | |
| 113 bool get isOdd; | |
| 114 | |
| 115 /** | |
| 116 * Returns the minimum number of bits required to store this integer. | |
| 117 * | |
| 118 * The number of bits excludes the sign bit, which gives the natural length | |
| 119 * for non-negative (unsigned) values. Negative values are complemented to | |
| 120 * return the bit position of the first bit that differs from the sign bit. | |
| 121 * | |
| 122 * To find the the number of bits needed to store the value as a signed value, | |
| 123 * add one, i.e. use `x.bitLength + 1`. | |
| 124 * | |
| 125 * x.bitLength == (-x-1).bitLength | |
| 126 * | |
| 127 * 3.bitLength == 2; // 00000011 | |
| 128 * 2.bitLength == 2; // 00000010 | |
| 129 * 1.bitLength == 1; // 00000001 | |
| 130 * 0.bitLength == 0; // 00000000 | |
| 131 * (-1).bitLength == 0; // 11111111 | |
| 132 * (-2).bitLength == 1; // 11111110 | |
| 133 * (-3).bitLength == 2; // 11111101 | |
| 134 * (-4).bitLength == 2; // 11111100 | |
| 135 */ | |
| 136 int get bitLength; | |
| 137 | |
| 138 /** | |
| 139 * Returns the least significant [width] bits of this integer as a | |
| 140 * non-negative number (i.e. unsigned representation). The returned value has | |
| 141 * zeros in all bit positions higher than [width]. | |
| 142 * | |
| 143 * (-1).toUnsigned(5) == 32 // 11111111 -> 00011111 | |
| 144 * | |
| 145 * This operation can be used to simulate arithmetic from low level languages. | |
| 146 * For example, to increment an 8 bit quantity: | |
| 147 * | |
| 148 * q = (q + 1).toUnsigned(8); | |
| 149 * | |
| 150 * `q` will count from `0` up to `255` and then wrap around to `0`. | |
| 151 * | |
| 152 * If the input fits in [width] bits without truncation, the result is the | |
| 153 * same as the input. The minimum width needed to avoid truncation of `x` is | |
| 154 * given by `x.bitLength`, i.e. | |
| 155 * | |
| 156 * x == x.toUnsigned(x.bitLength); | |
| 157 */ | |
| 158 int toUnsigned(int width); | |
| 159 | |
| 160 /** | |
| 161 * Returns the least significant [width] bits of this integer, extending the | |
| 162 * highest retained bit to the sign. This is the same as truncating the value | |
| 163 * to fit in [width] bits using an signed 2-s complement representation. The | |
| 164 * returned value has the same bit value in all positions higher than [width]. | |
| 165 * | |
| 166 * V--sign bit-V | |
| 167 * 16.toSigned(5) == -16 // 00010000 -> 11110000 | |
| 168 * 239.toSigned(5) == 15 // 11101111 -> 00001111 | |
| 169 * ^ ^ | |
| 170 * | |
| 171 * This operation can be used to simulate arithmetic from low level languages. | |
| 172 * For example, to increment an 8 bit signed quantity: | |
| 173 * | |
| 174 * q = (q + 1).toSigned(8); | |
| 175 * | |
| 176 * `q` will count from `0` up to `127`, wrap to `-128` and count back up to | |
| 177 * `127`. | |
| 178 * | |
| 179 * If the input value fits in [width] bits without truncation, the result is | |
| 180 * the same as the input. The minimum width needed to avoid truncation of `x` | |
| 181 * is `x.bitLength + 1`, i.e. | |
| 182 * | |
| 183 * x == x.toSigned(x.bitLength + 1); | |
| 184 */ | |
| 185 int toSigned(int width); | |
| 186 | |
| 187 /** | |
| 188 * Return the negative value of this integer. | |
| 189 * | |
| 190 * The result of negating an integer always has the opposite sign, except | |
| 191 * for zero, which is its own negation. | |
| 192 */ | |
| 193 int operator -(); | |
| 194 | |
| 195 /** | |
| 196 * Returns the absolute value of this integer. | |
| 197 * | |
| 198 * For any integer `x`, the result is the same as `x < 0 ? -x : x`. | |
| 199 */ | |
| 200 int abs(); | |
| 201 | |
| 202 /** | |
| 203 * Returns the sign of this integer. | |
| 204 * | |
| 205 * Returns 0 for zero, -1 for values less than zero and | |
| 206 * +1 for values greater than zero. | |
| 207 */ | |
| 208 int get sign; | |
| 209 | |
| 210 /** Returns `this`. */ | |
| 211 int round(); | |
| 212 | |
| 213 /** Returns `this`. */ | |
| 214 int floor(); | |
| 215 | |
| 216 /** Returns `this`. */ | |
| 217 int ceil(); | |
| 218 | |
| 219 /** Returns `this`. */ | |
| 220 int truncate(); | |
| 221 | |
| 222 /** Returns `this.toDouble()`. */ | |
| 223 double roundToDouble(); | |
| 224 | |
| 225 /** Returns `this.toDouble()`. */ | |
| 226 double floorToDouble(); | |
| 227 | |
| 228 /** Returns `this.toDouble()`. */ | |
| 229 double ceilToDouble(); | |
| 230 | |
| 231 /** Returns `this.toDouble()`. */ | |
| 232 double truncateToDouble(); | |
| 233 | |
| 234 /** | |
| 235 * Returns a String-representation of this integer. | |
| 236 * | |
| 237 * The returned string is parsable by [parse]. | |
| 238 * For any `int` [:i:], it is guaranteed that | |
| 239 * [:i == int.parse(i.toString()):]. | |
| 240 */ | |
| 241 String toString(); | |
| 242 | |
| 243 /** | |
| 244 * Converts [this] to a string representation in the given [radix]. | |
| 245 * | |
| 246 * In the string representation, lower-case letters are used for digits above | |
| 247 * '9'. | |
| 248 * | |
| 249 * The [radix] argument must be an integer in the range 2 to 36. | |
| 250 */ | |
| 251 String toRadixString(int radix); | |
| 252 | |
| 253 /** | |
| 254 * Parse [source] as an integer literal and return its value. | |
| 255 * | |
| 256 * The [radix] must be in the range 2..36. The digits used are | |
| 257 * first the decimal digits 0..9, and then the letters 'a'..'z'. | |
| 258 * Accepts capital letters as well. | |
| 259 * | |
| 260 * If no [radix] is given then it defaults to 10, unless the string starts | |
| 261 * with "0x", "-0x" or "+0x", in which case the radix is set to 16 and the | |
| 262 * "0x" is ignored. | |
| 263 * | |
| 264 * The [source] must be a non-empty sequence of base-[radix] digits, | |
| 265 * optionally prefixed with a minus or plus sign ('-' or '+'). | |
| 266 * | |
| 267 * It must always be the case for an int [:n:] and radix [:r:] that | |
| 268 * [:n == int.parse(n.toRadixString(r), radix: r):]. | |
| 269 * | |
| 270 * If the [source] is not a valid integer literal, optionally prefixed by a | |
| 271 * sign, the [onError] is called with the [source] as argument, and its return | |
| 272 * value is used instead. If no [onError] is provided, a [FormatException] | |
| 273 * is thrown. | |
| 274 * | |
| 275 * The [onError] function is only invoked if [source] is a [String]. It is | |
| 276 * not invoked if the [source] is, for example, `null`. | |
| 277 */ | |
| 278 static int parse(String source, | |
| 279 { int radix, | |
| 280 int onError(String source) }) { | |
| 281 return Primitives.parseInt(source, radix, onError); | |
| 282 } | |
| 283 } | |
| OLD | NEW |