Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(399)

Side by Side Diff: sdk/lib/typeddata/typeddata.dart

Issue 14332002: Add endian parameter to the get/set functions in ByteData. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 dart.typeddata; 5 library dart.typeddata;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 import 'dart:_collection-dev'; 8 import 'dart:_collection-dev';
9 9
10 /** 10 /**
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 int get lengthInBytes; 42 int get lengthInBytes;
43 43
44 /** 44 /**
45 * Returns the byte buffer associated with this object. 45 * Returns the byte buffer associated with this object.
46 */ 46 */
47 ByteBuffer get buffer; 47 ByteBuffer get buffer;
48 } 48 }
49 49
50 50
51 /** 51 /**
52 * Describes endianness to be used when accessing or updating a
53 * sequence of bytes.
54 */
55 class Endianness {
56 const Endianness(this.little_endian);
Mads Ager (google) 2013/04/18 08:10:44 Maybe we should make sure that the user cannot cre
siva 2013/04/18 17:17:47 Done.
57
58 static const Endianness BIG_ENDIAN = const Endianness(false);
59 static const Endianness LITTLE_ENDIAN = const Endianness(true);
60 static final Endianness HOST_ENDIAN =
61 (new ByteData.view(new Uint16List.fromList([1]).buffer)).getInt8(0) == 1 ?
62 LITTLE_ENDIAN : BIG_ENDIAN;
63
64 final bool little_endian;
vsm 2013/04/18 04:06:56 Can we make this private? Also, standard Dart sty
siva 2013/04/18 17:17:47 Done.
65 }
66
67
68 /**
52 * A fixed-length, random-access sequence of bytes that also provides random 69 * A fixed-length, random-access sequence of bytes that also provides random
53 * and unaligned access to the fixed-width integers and floating point 70 * and unaligned access to the fixed-width integers and floating point
54 * numbers represented by those bytes. 71 * numbers represented by those bytes.
55 * ByteData may be used to pack and unpack data from external sources 72 * ByteData may be used to pack and unpack data from external sources
56 * (such as networks or files systems), and to process large quantities 73 * (such as networks or files systems), and to process large quantities
57 * of numerical data more efficiently than would be possible 74 * of numerical data more efficiently than would be possible
58 * with ordinary [List] implementations. ByteData can save space, by 75 * with ordinary [List] implementations. ByteData can save space, by
59 * eliminating the need for object headers, and time, by eliminating the 76 * eliminating the need for object headers, and time, by eliminating the
60 * need for data copies. Finally, ByteData may be used to intentionally 77 * need for data copies. Finally, ByteData may be used to intentionally
61 * reinterpret the bytes representing one arithmetic type as another. 78 * reinterpret the bytes representing one arithmetic type as another.
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 /** 151 /**
135 * Returns the (possibly negative) integer represented by the two bytes at 152 * Returns the (possibly negative) integer represented by the two bytes at
136 * the specified [byteOffset] in this object, in two's complement binary 153 * the specified [byteOffset] in this object, in two's complement binary
137 * form. 154 * form.
138 * The return value will be between 2<sup>15</sup> and 2<sup>15</sup> - 1, 155 * The return value will be between 2<sup>15</sup> and 2<sup>15</sup> - 1,
139 * inclusive. 156 * inclusive.
140 * 157 *
141 * Throws [RangeError] if [byteOffset] is negative, or 158 * Throws [RangeError] if [byteOffset] is negative, or
142 * `byteOffset + 2` is greater than the length of this object. 159 * `byteOffset + 2` is greater than the length of this object.
143 */ 160 */
144 int getInt16(int byteOffset); 161 int getInt16(int byteOffset, [Endianness endian = Endianness.BIG_ENDIAN]);
Søren Gjesse 2013/04/18 07:04:36 How about adding a property endianess to ByteData
siva 2013/04/18 17:17:47 Seems like a good idea. I will defer this proposal
145 162
146 /** 163 /**
147 * Sets the two bytes starting at the specified [byteOffset] in this 164 * Sets the two bytes starting at the specified [byteOffset] in this
148 * object to the two's complement binary representation of the specified 165 * object to the two's complement binary representation of the specified
149 * [value], which must fit in two bytes. In other words, [value] must lie 166 * [value], which must fit in two bytes. In other words, [value] must lie
150 * between 2<sup>15</sup> and 2<sup>15</sup> - 1, inclusive. 167 * between 2<sup>15</sup> and 2<sup>15</sup> - 1, inclusive.
151 * 168 *
152 * Throws [RangeError] if [byteOffset] is negative, or 169 * Throws [RangeError] if [byteOffset] is negative, or
153 * `byteOffset + 2` is greater than the length of this object. 170 * `byteOffset + 2` is greater than the length of this object.
154 */ 171 */
155 void setInt16(int byteOffset, int value); 172 void setInt16(int byteOffset,
173 int value,
174 [Endianness endian = Endianness.BIG_ENDIAN]);
156 175
157 /** 176 /**
158 * Returns the positive integer represented by the two bytes starting 177 * Returns the positive integer represented by the two bytes starting
159 * at the specified [byteOffset] in this object, in unsigned binary 178 * at the specified [byteOffset] in this object, in unsigned binary
160 * form. 179 * form.
161 * The return value will be between 0 and 2<sup>16</sup> - 1, inclusive. 180 * The return value will be between 0 and 2<sup>16</sup> - 1, inclusive.
162 * 181 *
163 * Throws [RangeError] if [byteOffset] is negative, or 182 * Throws [RangeError] if [byteOffset] is negative, or
164 * `byteOffset + 2` is greater than the length of this object. 183 * `byteOffset + 2` is greater than the length of this object.
165 */ 184 */
166 int getUint16(int byteOffset); 185 int getUint16(int byteOffset, [Endianness endian = Endianness.BIG_ENDIAN]);
167 186
168 /** 187 /**
169 * Sets the two bytes starting at the specified [byteOffset] in this object 188 * Sets the two bytes starting at the specified [byteOffset] in this object
170 * to the unsigned binary representation of the specified [value], 189 * to the unsigned binary representation of the specified [value],
171 * which must fit in two bytes. in other words, [value] must be between 190 * which must fit in two bytes. in other words, [value] must be between
172 * 0 and 2<sup>16</sup> - 1, inclusive. 191 * 0 and 2<sup>16</sup> - 1, inclusive.
173 * 192 *
174 * Throws [RangeError] if [byteOffset] is negative, or 193 * Throws [RangeError] if [byteOffset] is negative, or
175 * `byteOffset + 2` is greater than the length of this object. 194 * `byteOffset + 2` is greater than the length of this object.
176 */ 195 */
177 void setUint16(int byteOffset, int value); 196 void setUint16(int byteOffset,
197 int value,
198 [Endianness endian = Endianness.BIG_ENDIAN]);
178 199
179 /** 200 /**
180 * Returns the (possibly negative) integer represented by the four bytes at 201 * Returns the (possibly negative) integer represented by the four bytes at
181 * the specified [byteOffset] in this object, in two's complement binary 202 * the specified [byteOffset] in this object, in two's complement binary
182 * form. 203 * form.
183 * The return value will be between 2<sup>31</sup> and 2<sup>31</sup> - 1, 204 * The return value will be between 2<sup>31</sup> and 2<sup>31</sup> - 1,
184 * inclusive. 205 * inclusive.
185 * 206 *
186 * Throws [RangeError] if [byteOffset] is negative, or 207 * Throws [RangeError] if [byteOffset] is negative, or
187 * `byteOffset + 4` is greater than the length of this object. 208 * `byteOffset + 4` is greater than the length of this object.
188 */ 209 */
189 int getInt32(int byteOffset); 210 int getInt32(int byteOffset, [Endianness endian = Endianness.BIG_ENDIAN]);
190 211
191 /** 212 /**
192 * Sets the four bytes starting at the specified [byteOffset] in this 213 * Sets the four bytes starting at the specified [byteOffset] in this
193 * object to the two's complement binary representation of the specified 214 * object to the two's complement binary representation of the specified
194 * [value], which must fit in four bytes. In other words, [value] must lie 215 * [value], which must fit in four bytes. In other words, [value] must lie
195 * between 2<sup>31</sup> and 2<sup>31</sup> - 1, inclusive. 216 * between 2<sup>31</sup> and 2<sup>31</sup> - 1, inclusive.
196 * 217 *
197 * Throws [RangeError] if [byteOffset] is negative, or 218 * Throws [RangeError] if [byteOffset] is negative, or
198 * `byteOffset + 4` is greater than the length of this object. 219 * `byteOffset + 4` is greater than the length of this object.
199 */ 220 */
200 void setInt32(int byteOffset, int value); 221 void setInt32(int byteOffset,
222 int value,
223 [Endianness endian = Endianness.BIG_ENDIAN]);
201 224
202 /** 225 /**
203 * Returns the positive integer represented by the four bytes starting 226 * Returns the positive integer represented by the four bytes starting
204 * at the specified [byteOffset] in this object, in unsigned binary 227 * at the specified [byteOffset] in this object, in unsigned binary
205 * form. 228 * form.
206 * The return value will be between 0 and 2<sup>32</sup> - 1, inclusive. 229 * The return value will be between 0 and 2<sup>32</sup> - 1, inclusive.
207 * 230 *
208 */ 231 */
209 int getUint32(int byteOffset); 232 int getUint32(int byteOffset, [Endianness endian = Endianness.BIG_ENDIAN]);
210 233
211 /** 234 /**
212 * Sets the four bytes starting at the specified [byteOffset] in this object 235 * Sets the four bytes starting at the specified [byteOffset] in this object
213 * to the unsigned binary representation of the specified [value], 236 * to the unsigned binary representation of the specified [value],
214 * which must fit in four bytes. in other words, [value] must be between 237 * which must fit in four bytes. in other words, [value] must be between
215 * 0 and 2<sup>32</sup> - 1, inclusive. 238 * 0 and 2<sup>32</sup> - 1, inclusive.
216 * 239 *
217 * Throws [RangeError] if [byteOffset] is negative, or 240 * Throws [RangeError] if [byteOffset] is negative, or
218 * `byteOffset + 4` is greater than the length of this object. 241 * `byteOffset + 4` is greater than the length of this object.
219 */ 242 */
220 void setUint32(int byteOffset, int value); 243 void setUint32(int byteOffset,
244 int value,
245 [Endianness endian = Endianness.BIG_ENDIAN]);
221 246
222 /** 247 /**
223 * Returns the (possibly negative) integer represented by the eight bytes at 248 * Returns the (possibly negative) integer represented by the eight bytes at
224 * the specified [byteOffset] in this object, in two's complement binary 249 * the specified [byteOffset] in this object, in two's complement binary
225 * form. 250 * form.
226 * The return value will be between 2<sup>63</sup> and 2<sup>63</sup> - 1, 251 * The return value will be between 2<sup>63</sup> and 2<sup>63</sup> - 1,
227 * inclusive. 252 * inclusive.
228 * 253 *
229 * Throws [RangeError] if [byteOffset] is negative, or 254 * Throws [RangeError] if [byteOffset] is negative, or
230 * `byteOffset + 8` is greater than the length of this object. 255 * `byteOffset + 8` is greater than the length of this object.
231 */ 256 */
232 int getInt64(int byteOffset); 257 int getInt64(int byteOffset, [Endianness endian = Endianness.BIG_ENDIAN]);
233 258
234 /** 259 /**
235 * Sets the eight bytes starting at the specified [byteOffset] in this 260 * Sets the eight bytes starting at the specified [byteOffset] in this
236 * object to the two's complement binary representation of the specified 261 * object to the two's complement binary representation of the specified
237 * [value], which must fit in eight bytes. In other words, [value] must lie 262 * [value], which must fit in eight bytes. In other words, [value] must lie
238 * between 2<sup>63</sup> and 2<sup>63</sup> - 1, inclusive. 263 * between 2<sup>63</sup> and 2<sup>63</sup> - 1, inclusive.
239 * 264 *
240 * Throws [RangeError] if [byteOffset] is negative, or 265 * Throws [RangeError] if [byteOffset] is negative, or
241 * `byteOffset + 8` is greater than the length of this object. 266 * `byteOffset + 8` is greater than the length of this object.
242 */ 267 */
243 void setInt64(int byteOffset, int value); 268 void setInt64(int byteOffset,
269 int value,
270 [Endianness endian = Endianness.BIG_ENDIAN]);
244 271
245 /** 272 /**
246 * Returns the positive integer represented by the eight bytes starting 273 * Returns the positive integer represented by the eight bytes starting
247 * at the specified [byteOffset] in this object, in unsigned binary 274 * at the specified [byteOffset] in this object, in unsigned binary
248 * form. 275 * form.
249 * The return value will be between 0 and 2<sup>64</sup> - 1, inclusive. 276 * The return value will be between 0 and 2<sup>64</sup> - 1, inclusive.
250 * 277 *
251 * Throws [RangeError] if [byteOffset] is negative, or 278 * Throws [RangeError] if [byteOffset] is negative, or
252 * `byteOffset + 8` is greater than the length of this object. 279 * `byteOffset + 8` is greater than the length of this object.
253 */ 280 */
254 int getUint64(int byteOffset); 281 int getUint64(int byteOffset, [Endianness endian = Endianness.BIG_ENDIAN]);
255 282
256 /** 283 /**
257 * Sets the eight bytes starting at the specified [byteOffset] in this object 284 * Sets the eight bytes starting at the specified [byteOffset] in this object
258 * to the unsigned binary representation of the specified [value], 285 * to the unsigned binary representation of the specified [value],
259 * which must fit in eight bytes. in other words, [value] must be between 286 * which must fit in eight bytes. in other words, [value] must be between
260 * 0 and 2<sup>64</sup> - 1, inclusive. 287 * 0 and 2<sup>64</sup> - 1, inclusive.
261 * 288 *
262 * Throws [RangeError] if [byteOffset] is negative, or 289 * Throws [RangeError] if [byteOffset] is negative, or
263 * `byteOffset + 8` is greater than the length of this object. 290 * `byteOffset + 8` is greater than the length of this object.
264 */ 291 */
265 void setUint64(int byteOffset, int value); 292 void setUint64(int byteOffset,
293 int value,
294 [Endianness endian = Endianness.BIG_ENDIAN]);
266 295
267 /** 296 /**
268 * Returns the floating point number represented by the four bytes at 297 * Returns the floating point number represented by the four bytes at
269 * the specified [byteOffset] in this object, in IEEE 754 298 * the specified [byteOffset] in this object, in IEEE 754
270 * single-precision binary floating-point format (binary32). 299 * single-precision binary floating-point format (binary32).
271 * 300 *
272 * Throws [RangeError] if [byteOffset] is negative, or 301 * Throws [RangeError] if [byteOffset] is negative, or
273 * `byteOffset + 4` is greater than the length of this object. 302 * `byteOffset + 4` is greater than the length of this object.
274 */ 303 */
275 double getFloat32(int byteOffset); 304 double getFloat32(int byteOffset,
305 [Endianness endian = Endianness.BIG_ENDIAN]);
276 306
277 /** 307 /**
278 * Sets the four bytes starting at the specified [byteOffset] in this 308 * Sets the four bytes starting at the specified [byteOffset] in this
279 * object to the IEEE 754 single-precision binary floating-point 309 * object to the IEEE 754 single-precision binary floating-point
280 * (binary32) representation of the specified [value]. 310 * (binary32) representation of the specified [value].
281 * 311 *
282 * **Note that this method can lose precision.** The input [value] is 312 * **Note that this method can lose precision.** The input [value] is
283 * a 64-bit floating point value, which will be converted to 32-bit 313 * a 64-bit floating point value, which will be converted to 32-bit
284 * floating point value by IEEE 754 rounding rules before it is stored. 314 * floating point value by IEEE 754 rounding rules before it is stored.
285 * If [value] cannot be represented exactly as a binary32, it will be 315 * If [value] cannot be represented exactly as a binary32, it will be
286 * converted to the nearest binary32 value. If two binary32 values are 316 * converted to the nearest binary32 value. If two binary32 values are
287 * equally close, the one whose least significant bit is zero will be used. 317 * equally close, the one whose least significant bit is zero will be used.
288 * Note that finite (but large) values can be converted to infinity, and 318 * Note that finite (but large) values can be converted to infinity, and
289 * small non-zero values can be converted to zero. 319 * small non-zero values can be converted to zero.
290 * 320 *
291 * Throws [RangeError] if [byteOffset] is negative, or 321 * Throws [RangeError] if [byteOffset] is negative, or
292 * `byteOffset + 4` is greater than the length of this object. 322 * `byteOffset + 4` is greater than the length of this object.
293 */ 323 */
294 void setFloat32(int byteOffset, double value); 324 void setFloat32(int byteOffset,
325 double value,
326 [Endianness endian = Endianness.BIG_ENDIAN]);
295 327
296 /** 328 /**
297 * Returns the floating point number represented by the eight bytes at 329 * Returns the floating point number represented by the eight bytes at
298 * the specified [byteOffset] in this object, in IEEE 754 330 * the specified [byteOffset] in this object, in IEEE 754
299 * double-precision binary floating-point format (binary64). 331 * double-precision binary floating-point format (binary64).
300 * 332 *
301 * Throws [RangeError] if [byteOffset] is negative, or 333 * Throws [RangeError] if [byteOffset] is negative, or
302 * `byteOffset + 8` is greater than the length of this object. 334 * `byteOffset + 8` is greater than the length of this object.
303 */ 335 */
304 double getFloat64(int byteOffset); 336 double getFloat64(int byteOffset,
337 [Endianness endian = Endianness.BIG_ENDIAN]);
305 338
306 /** 339 /**
307 * Sets the eight bytes starting at the specified [byteOffset] in this 340 * Sets the eight bytes starting at the specified [byteOffset] in this
308 * object to the IEEE 754 double-precision binary floating-point 341 * object to the IEEE 754 double-precision binary floating-point
309 * (binary64) representation of the specified [value]. 342 * (binary64) representation of the specified [value].
310 * 343 *
311 * Throws [RangeError] if [byteOffset] is negative, or 344 * Throws [RangeError] if [byteOffset] is negative, or
312 * `byteOffset + 8` is greater than the length of this object. 345 * `byteOffset + 8` is greater than the length of this object.
313 */ 346 */
314 void setFloat64(int byteOffset, double value); 347 void setFloat64(int byteOffset,
348 double value,
349 [Endianness endian = Endianness.BIG_ENDIAN]);
315 } 350 }
316 351
317 352
318 /** 353 /**
319 * A fixed-length list of 8-bit signed integers. 354 * A fixed-length list of 8-bit signed integers.
320 * For long lists, this implementation can be considerably 355 * For long lists, this implementation can be considerably
321 * more space- and time-efficient than the default [List] implementation. 356 * more space- and time-efficient than the default [List] implementation.
322 */ 357 */
323 abstract class Int8List implements List<int>, TypedData { 358 abstract class Int8List implements List<int>, TypedData {
324 /** 359 /**
(...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 Uint32x4 withFlagW(bool w); 961 Uint32x4 withFlagW(bool w);
927 962
928 /// Merge [trueValue] and [falseValue] based on [this]' bit mask: 963 /// Merge [trueValue] and [falseValue] based on [this]' bit mask:
929 /// Select bit from [trueValue] when bit in [this] is on. 964 /// Select bit from [trueValue] when bit in [this] is on.
930 /// Select bit from [falseValue] when bit in [this] is off. 965 /// Select bit from [falseValue] when bit in [this] is off.
931 Float32x4 select(Float32x4 trueValue, Float32x4 falseValue); 966 Float32x4 select(Float32x4 trueValue, Float32x4 falseValue);
932 967
933 /// Returns a bit-wise copy of [this] as a [Float32x4]. 968 /// Returns a bit-wise copy of [this] as a [Float32x4].
934 Float32x4 toFloat32x4(); 969 Float32x4 toFloat32x4();
935 } 970 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698