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

Side by Side Diff: runtime/lib/string.cc

Issue 11368138: Add some support for the code-point code-unit distinction. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Implemented feedback from patch set 3 Created 8 years, 1 month 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
« no previous file with comments | « no previous file | runtime/lib/string_base.dart » ('j') | runtime/vm/scanner.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 #include "vm/bootstrap_natives.h" 5 #include "vm/bootstrap_natives.h"
6 6
7 #include "vm/exceptions.h" 7 #include "vm/exceptions.h"
8 #include "vm/native_entry.h" 8 #include "vm/native_entry.h"
9 #include "vm/object.h" 9 #include "vm/object.h"
10 #include "vm/symbols.h" 10 #include "vm/symbols.h"
11 11
12 namespace dart { 12 namespace dart {
13 13
14 DEFINE_NATIVE_ENTRY(StringBase_createFromCodePoints, 1) { 14 DEFINE_NATIVE_ENTRY(StringBase_createFromUtf16, 1) {
15 GET_NATIVE_ARGUMENT(Array, a, arguments->NativeArgAt(0)); 15 GET_NATIVE_ARGUMENT(Array, a, arguments->NativeArgAt(0));
16 // TODO(srdjan): Check that parameterized type is an int. 16 // TODO(srdjan): Check that parameterized type is an int.
17 Zone* zone = isolate->current_zone(); 17 Zone* zone = isolate->current_zone();
18 intptr_t array_len = a.Length(); 18 intptr_t array_len = a.Length();
19 19
20 // Unbox the array and determine the maximum element width. 20 // Unbox the array and determine the maximum element width.
21 bool is_one_byte_string = true; 21 bool is_one_byte_string = true;
22 intptr_t utf16_len = array_len; 22 int32_t* utf32_array = zone->Alloc<int32_t>(array_len);
23 uint32_t* utf32_array = zone->Alloc<uint32_t>(array_len);
24 Object& index_object = Object::Handle(isolate); 23 Object& index_object = Object::Handle(isolate);
25 for (intptr_t i = 0; i < array_len; i++) { 24 for (intptr_t i = 0; i < array_len; i++) {
26 index_object = a.At(i); 25 index_object = a.At(i);
27 if (!index_object.IsSmi()) { 26 if (!index_object.IsSmi()) {
28 GrowableArray<const Object*> args; 27 GrowableArray<const Object*> args;
29 args.Add(&index_object); 28 args.Add(&index_object);
30 Exceptions::ThrowByType(Exceptions::kArgument, args); 29 Exceptions::ThrowByType(Exceptions::kArgument, args);
31 } 30 }
32 intptr_t value = Smi::Cast(index_object).Value(); 31 intptr_t value = Smi::Cast(index_object).Value();
33 if (value < 0) { 32 if (value > Utf16::kMaxCodeUnit) {
34 GrowableArray<const Object*> args; 33 GrowableArray<const Object*> args;
35 Exceptions::ThrowByType(Exceptions::kArgument, args); 34 Exceptions::ThrowByType(Exceptions::kArgument, args);
36 } else { 35 } else {
37 if (value > 0x7F) { 36 if (value > 0x7F) {
38 is_one_byte_string = false; 37 is_one_byte_string = false;
39 } 38 }
40 if (value > 0xFFFF) {
41 utf16_len += 1;
42 }
43 } 39 }
44 utf32_array[i] = value; 40 utf32_array[i] = value;
45 } 41 }
46 if (is_one_byte_string) { 42 if (is_one_byte_string) {
47 return OneByteString::New(utf32_array, array_len, Heap::kNew); 43 return OneByteString::New(utf32_array, array_len, Heap::kNew);
48 } 44 }
49 return TwoByteString::New(utf16_len, utf32_array, array_len, Heap::kNew); 45 return TwoByteString::New(array_len, utf32_array, array_len, Heap::kNew);
50 } 46 }
51 47
52 48
53 DEFINE_NATIVE_ENTRY(StringBase_substringUnchecked, 3) { 49 DEFINE_NATIVE_ENTRY(StringBase_substringUnchecked, 3) {
54 GET_NATIVE_ARGUMENT(String, receiver, arguments->NativeArgAt(0)); 50 GET_NATIVE_ARGUMENT(String, receiver, arguments->NativeArgAt(0));
55 GET_NATIVE_ARGUMENT(Smi, start_obj, arguments->NativeArgAt(1)); 51 GET_NATIVE_ARGUMENT(Smi, start_obj, arguments->NativeArgAt(1));
56 GET_NATIVE_ARGUMENT(Smi, end_obj, arguments->NativeArgAt(2)); 52 GET_NATIVE_ARGUMENT(Smi, end_obj, arguments->NativeArgAt(2));
57 53
58 intptr_t start = start_obj.Value(); 54 intptr_t start = start_obj.Value();
59 intptr_t end = end_obj.Value(); 55 intptr_t end = end_obj.Value();
60 return String::SubString(receiver, start, (end - start)); 56 return String::SubString(receiver, start, (end - start));
61 } 57 }
62 58
63 59
64 DEFINE_NATIVE_ENTRY(String_getHashCode, 1) { 60 DEFINE_NATIVE_ENTRY(String_getHashCode, 1) {
65 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 61 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
66 intptr_t hash_val = receiver.Hash(); 62 intptr_t hash_val = receiver.Hash();
67 ASSERT(hash_val > 0); 63 ASSERT(hash_val > 0);
68 ASSERT(Smi::IsValid(hash_val)); 64 ASSERT(Smi::IsValid(hash_val));
69 return Smi::New(hash_val); 65 return Smi::New(hash_val);
70 } 66 }
71 67
72 68
73 DEFINE_NATIVE_ENTRY(String_getLength, 1) { 69 DEFINE_NATIVE_ENTRY(String_getLength, 1) {
74 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 70 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
75 return Smi::New(receiver.Length()); 71 return Smi::New(receiver.Length());
76 } 72 }
77 73
78 74
79 static int32_t StringValueAt(const String& str, const Integer& index) { 75 static int32_t StringCodeUnitAt(const String& str, const Integer& index) {
80 if (index.IsSmi()) { 76 if (index.IsSmi()) {
81 Smi& smi = Smi::Handle(); 77 Smi& smi = Smi::Handle();
82 smi ^= index.raw(); 78 smi ^= index.raw();
83 int32_t index = smi.Value(); 79 int32_t index = smi.Value();
84 if ((index < 0) || (index >= str.Length())) { 80 if ((index < 0) || (index >= str.Length())) {
85 GrowableArray<const Object*> arguments; 81 GrowableArray<const Object*> arguments;
86 arguments.Add(&smi); 82 arguments.Add(&smi);
87 Exceptions::ThrowByType(Exceptions::kRange, arguments); 83 Exceptions::ThrowByType(Exceptions::kRange, arguments);
88 } 84 }
89 return str.CharAt(index); 85 return str.CharAt(index);
90 } else { 86 } else {
91 // An index larger than Smi is always illegal. 87 // An index larger than Smi is always illegal.
92 GrowableArray<const Object*> arguments; 88 GrowableArray<const Object*> arguments;
93 arguments.Add(&index); 89 arguments.Add(&index);
94 Exceptions::ThrowByType(Exceptions::kRange, arguments); 90 Exceptions::ThrowByType(Exceptions::kRange, arguments);
95 return 0; 91 return 0;
96 } 92 }
97 } 93 }
98 94
99 95
100 DEFINE_NATIVE_ENTRY(String_charAt, 2) { 96 DEFINE_NATIVE_ENTRY(String_charAt, 2) {
101 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 97 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
102 GET_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1)); 98 GET_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1));
103 uint32_t value = StringValueAt(receiver, index); 99 uint16_t value = StringCodeUnitAt(receiver, index);
104 ASSERT(value <= 0x10FFFF); 100 // The VM does not GC interned strings, so we only create them for a
105 return Symbols::New(&value, 1); 101 // restricted range.
102 const int32_t kFrequentCharacterLimit = 0x7F;
103 if (value <= kFrequentCharacterLimit) {
104 return Symbols::New(&value, 1);
105 } else {
106 return String::New(&value, 1);
107 }
106 } 108 }
107 109
108 DEFINE_NATIVE_ENTRY(String_charCodeAt, 2) { 110
111 DEFINE_NATIVE_ENTRY(String_codeUnitAt, 2) {
109 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 112 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
110 GET_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1)); 113 GET_NATIVE_ARGUMENT(Integer, index, arguments->NativeArgAt(1));
111 114 int32_t value = StringCodeUnitAt(receiver, index);
112 int32_t value = StringValueAt(receiver, index); 115 ASSERT(value <= Utf16::kMaxCodeUnit);
113 ASSERT(value >= 0);
114 ASSERT(value <= 0x10FFFF);
115 return Smi::New(value); 116 return Smi::New(value);
116 } 117 }
117 118
118 119
119 DEFINE_NATIVE_ENTRY(String_concat, 2) { 120 DEFINE_NATIVE_ENTRY(String_concat, 2) {
120 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0)); 121 const String& receiver = String::CheckedHandle(arguments->NativeArgAt(0));
121 GET_NATIVE_ARGUMENT(String, b, arguments->NativeArgAt(1)); 122 GET_NATIVE_ARGUMENT(String, b, arguments->NativeArgAt(1));
122 return String::Concat(receiver, b); 123 return String::Concat(receiver, b);
123 } 124 }
124 125
(...skipping 26 matching lines...) Expand all
151 if (!elem.IsString()) { 152 if (!elem.IsString()) {
152 GrowableArray<const Object*> args; 153 GrowableArray<const Object*> args;
153 args.Add(&elem); 154 args.Add(&elem);
154 Exceptions::ThrowByType(Exceptions::kArgument, args); 155 Exceptions::ThrowByType(Exceptions::kArgument, args);
155 } 156 }
156 } 157 }
157 return String::ConcatAll(strings); 158 return String::ConcatAll(strings);
158 } 159 }
159 160
160 } // namespace dart 161 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/string_base.dart » ('j') | runtime/vm/scanner.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698