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

Side by Side Diff: runtime/lib/typed_data.dart

Issue 856053002: Let Uint16List.setRange detect a String.codeUnits object, and copy directly from the string. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Move CodeUnits.cid to VM-only patch file. It makes no sense for dart2js Created 5 years, 10 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 // patch classes for Int8List ..... Float64List and ByteData implementations. 5 // patch classes for Int8List ..... Float64List and ByteData implementations.
6 6
7 import "dart:_internal"; 7 import "dart:_internal";
8 import 'dart:math' show Random; 8 import 'dart:math' show Random;
9 9
10 patch class Int8List { 10 patch class Int8List {
(...skipping 811 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 void _setFloat32x4(int offsetInBytes, Float32x4 value) 822 void _setFloat32x4(int offsetInBytes, Float32x4 value)
823 native "TypedData_SetFloat32x4"; 823 native "TypedData_SetFloat32x4";
824 824
825 Int32x4 _getInt32x4(int offsetInBytes) native "TypedData_GetInt32x4"; 825 Int32x4 _getInt32x4(int offsetInBytes) native "TypedData_GetInt32x4";
826 void _setInt32x4(int offsetInBytes, Int32x4 value) 826 void _setInt32x4(int offsetInBytes, Int32x4 value)
827 native "TypedData_SetInt32x4"; 827 native "TypedData_SetInt32x4";
828 828
829 Float64x2 _getFloat64x2(int offsetInBytes) native "TypedData_GetFloat64x2"; 829 Float64x2 _getFloat64x2(int offsetInBytes) native "TypedData_GetFloat64x2";
830 void _setFloat64x2(int offsetInBytes, Float64x2 value) 830 void _setFloat64x2(int offsetInBytes, Float64x2 value)
831 native "TypedData_SetFloat64x2"; 831 native "TypedData_SetFloat64x2";
832
833 /** Writes the code units of `string.substring(start, end)` as Uint16. */
834 void _setStringAt(int byteOffset, String string, int start, int end) {
835 // This can be optimized to a mem-move if string is two-byte.
836 int length = end - start;
837 assert(byteOffset + length * Uint16List.BYTES_PER_ELEMENT <=
838 this.lengthInBytes);
839 for (int i = 0; i < length; i++) {
840 _setUint16(byteOffset + i * Uint16List.BYTES_PER_ELEMENT,
841 string.codeUnitAt(start + i));
842 }
843 }
844
845 void _setCodeUnits(CodeUnits units, int start, int end, int skipCount) {
846 String string = CodeUnits.stringOf(units);
847 end = RangeError.checkValidRange(start, end, length);
848 int sliceLength = end - start;
849 int sliceEnd = skipCount + sliceLength;
850 RangeError.checkValidRange(skipCount, sliceEnd,
851 string.length,
852 "skipCount", "skipCount + length");
853 this._setStringAt(start * elementSizeInBytes, string, skipCount, sliceEnd);
854 }
832 } 855 }
833 856
834 857
835 class _Int8Array extends _TypedList with _IntListMixin implements Int8List { 858 class _Int8Array extends _TypedList with _IntListMixin implements Int8List {
836 // Factory constructors. 859 // Factory constructors.
837 860
838 factory _Int8Array(int length) { 861 factory _Int8Array(int length) {
839 return _new(length); 862 return _new(length);
840 } 863 }
841 864
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
1022 return _getIndexedUint16(index); 1045 return _getIndexedUint16(index);
1023 } 1046 }
1024 1047
1025 void operator[]=(int index, int value) { 1048 void operator[]=(int index, int value) {
1026 if (index < 0 || index >= length) { 1049 if (index < 0 || index >= length) {
1027 throw _newRangeError(index, length); 1050 throw _newRangeError(index, length);
1028 } 1051 }
1029 _setIndexedUint16(index, _toUint16(value)); 1052 _setIndexedUint16(index, _toUint16(value));
1030 } 1053 }
1031 1054
1055 void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) {
1056 if (ClassID.getID(iterable) != CodeUnits.cid) {
1057 super.setRange(start, end, iterable, skipCount);
1058 } else {
1059 _setCodeUnits(iterable, start, end, skipCount);
Vyacheslav Egorov (Google) 2015/01/29 13:04:54 Maybe we should also have an optimization for Int8
Lasse Reichstein Nielsen 2015/01/29 13:13:27 I was planning to do that in a later CL.
1060 }
1061 }
1032 1062
1033 // Method(s) implementing the TypedData interface. 1063 // Method(s) implementing the TypedData interface.
1034 1064
1035 int get elementSizeInBytes { 1065 int get elementSizeInBytes {
1036 return Uint16List.BYTES_PER_ELEMENT; 1066 return Uint16List.BYTES_PER_ELEMENT;
1037 } 1067 }
1038 1068
1039 1069
1040 // Internal utility methods. 1070 // Internal utility methods.
1041 1071
(...skipping 1404 matching lines...) Expand 10 before | Expand all | Expand 10 after
2446 // Method(s) implementing the TypedData interface. 2476 // Method(s) implementing the TypedData interface.
2447 2477
2448 int get lengthInBytes { 2478 int get lengthInBytes {
2449 return length * elementSizeInBytes; 2479 return length * elementSizeInBytes;
2450 } 2480 }
2451 2481
2452 ByteBuffer get buffer { 2482 ByteBuffer get buffer {
2453 return _typedData.buffer; 2483 return _typedData.buffer;
2454 } 2484 }
2455 2485
2456 final TypedData _typedData; 2486 final _TypedList _typedData;
2457 final int offsetInBytes; 2487 final int offsetInBytes;
2458 final int length; 2488 final int length;
2459 } 2489 }
2460 2490
2461 2491
2462 class _Int8ArrayView extends _TypedListView with _IntListMixin implements Int8Li st { 2492 class _Int8ArrayView extends _TypedListView with _IntListMixin implements Int8Li st {
2463 // Constructor. 2493 // Constructor.
2464 _Int8ArrayView(ByteBuffer buffer, [int _offsetInBytes = 0, int _length]) 2494 _Int8ArrayView(ByteBuffer buffer, [int _offsetInBytes = 0, int _length])
2465 : super(buffer, _offsetInBytes, 2495 : super(buffer, _offsetInBytes,
2466 _defaultIfNull(_length, 2496 _defaultIfNull(_length,
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
2674 } 2704 }
2675 2705
2676 void operator[]=(int index, int value) { 2706 void operator[]=(int index, int value) {
2677 if (index < 0 || index >= length) { 2707 if (index < 0 || index >= length) {
2678 throw _newRangeError(index, length); 2708 throw _newRangeError(index, length);
2679 } 2709 }
2680 _typedData._setUint16(offsetInBytes + (index * Uint16List.BYTES_PER_ELEMENT) , 2710 _typedData._setUint16(offsetInBytes + (index * Uint16List.BYTES_PER_ELEMENT) ,
2681 _toUint16(value)); 2711 _toUint16(value));
2682 } 2712 }
2683 2713
2714 void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) {
2715 if (ClassID.getID(iterable) != CodeUnits.cid) {
2716 super.setRange(start, end, iterable, skipCount);
2717 } else {
2718 _typed_Data._setCodeUnits(iterable, start, end, skipCount);
2719 }
2720 }
2684 2721
2685 // Method(s) implementing TypedData interface. 2722 // Method(s) implementing TypedData interface.
2686 2723
2687 int get elementSizeInBytes { 2724 int get elementSizeInBytes {
2688 return Uint16List.BYTES_PER_ELEMENT; 2725 return Uint16List.BYTES_PER_ELEMENT;
2689 } 2726 }
2690 2727
2691
2692 // Internal utility methods. 2728 // Internal utility methods.
2693 2729
2694 Uint16List _createList(int length) { 2730 Uint16List _createList(int length) {
2695 return new Uint16List(length); 2731 return new Uint16List(length);
2696 } 2732 }
2697 } 2733 }
2698 2734
2699 2735
2700 class _Int32ArrayView extends _TypedListView with _IntListMixin implements Int32 List { 2736 class _Int32ArrayView extends _TypedListView with _IntListMixin implements Int32 List {
2701 // Constructor. 2737 // Constructor.
(...skipping 784 matching lines...) Expand 10 before | Expand all | Expand 10 after
3486 return value; 3522 return value;
3487 } 3523 }
3488 return object; 3524 return object;
3489 } 3525 }
3490 3526
3491 3527
3492 _newRangeError(int index, int length) { 3528 _newRangeError(int index, int length) {
3493 String message = "$index must be in the range [0..$length)"; 3529 String message = "$index must be in the range [0..$length)";
3494 return new RangeError(message); 3530 return new RangeError(message);
3495 } 3531 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698