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

Side by Side Diff: src/runtime.cc

Issue 7300: * StringSearch now tests aginst non-ascii needle on an ascii string as the first thing. (Closed)
Patch Set: Rebase to SVN version Created 12 years, 2 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 978 matching lines...) Expand 10 before | Expand all | Expand 10 after
989 DISALLOW_COPY_AND_ASSIGN(BMGoodSuffixBuffers); 989 DISALLOW_COPY_AND_ASSIGN(BMGoodSuffixBuffers);
990 }; 990 };
991 991
992 // buffers reused by BoyerMoore 992 // buffers reused by BoyerMoore
993 static int bad_char_occurence[kBMAlphabetSize]; 993 static int bad_char_occurence[kBMAlphabetSize];
994 static BMGoodSuffixBuffers bmgs_buffers; 994 static BMGoodSuffixBuffers bmgs_buffers;
995 995
996 // Compute the bad-char table for Boyer-Moore in the static buffer. 996 // Compute the bad-char table for Boyer-Moore in the static buffer.
997 // Return false if the pattern contains non-ASCII characters that cannot be 997 // Return false if the pattern contains non-ASCII characters that cannot be
998 // in the searched string. 998 // in the searched string.
999 template <typename pchar, bool check_ascii> 999 template <typename pchar>
1000 static bool BoyerMoorePopulateBadCharTable(Vector<const pchar> pattern, 1000 static void BoyerMoorePopulateBadCharTable(Vector<const pchar> pattern,
1001 int start) { 1001 int start) {
1002 // Run forwards to populate bad_char_table, so that *last* instance 1002 // Run forwards to populate bad_char_table, so that *last* instance
1003 // of character equivalence class is the one registered. 1003 // of character equivalence class is the one registered.
1004 // Notice: Doesn't include the last character. 1004 // Notice: Doesn't include the last character.
1005 for (int i = 0; i < kBMAlphabetSize; i++) { 1005 for (int i = 0; i < kBMAlphabetSize; i++) {
1006 bad_char_occurence[i] = start - 1; 1006 bad_char_occurence[i] = start - 1;
1007 } 1007 }
1008 for (int i = start; i < pattern.length(); i++) { 1008 for (int i = start; i < pattern.length(); i++) {
1009 uc32 c = pattern[i]; 1009 bad_char_occurence[pattern[i] % kBMAlphabetSize] = i;
1010 bad_char_occurence[c % kBMAlphabetSize] = i;
1011 if (check_ascii &&
1012 c > String::kMaxAsciiCharCode) {
1013 return false;
1014 }
1015 } 1010 }
1016 return true;
1017 } 1011 }
1018 1012
1019 template <typename pchar> 1013 template <typename pchar>
1020 static void BoyerMoorePopulateGoodSuffixTable(Vector<const pchar> pattern, 1014 static void BoyerMoorePopulateGoodSuffixTable(Vector<const pchar> pattern,
1021 int start, 1015 int start,
1022 int len) { 1016 int len) {
1023 int m = pattern.length(); 1017 int m = pattern.length();
1024 // Compute Good Suffix tables. 1018 // Compute Good Suffix tables.
1025 bmgs_buffers.init(m); 1019 bmgs_buffers.init(m);
1026 1020
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1074 static int BoyerMooreIndexOf(Vector<const schar> subject, 1068 static int BoyerMooreIndexOf(Vector<const schar> subject,
1075 Vector<const pchar> pattern, 1069 Vector<const pchar> pattern,
1076 int start_index) { 1070 int start_index) {
1077 int m = pattern.length(); 1071 int m = pattern.length();
1078 int n = subject.length(); 1072 int n = subject.length();
1079 1073
1080 // Only preprocess at most kBMMaxShift last characters of pattern. 1074 // Only preprocess at most kBMMaxShift last characters of pattern.
1081 int start = m < kBMMaxShift ? 0 : m - kBMMaxShift; 1075 int start = m < kBMMaxShift ? 0 : m - kBMMaxShift;
1082 int len = m - start; 1076 int len = m - start;
1083 1077
1084 if (sizeof(pchar) > 1 && sizeof(schar) == 1) { 1078 BoyerMoorePopulateBadCharTable(pattern, start);
1085 BoyerMoorePopulateBadCharTable<pchar, true>(pattern, start);
1086 } else {
1087 if (!BoyerMoorePopulateBadCharTable<pchar, false>(pattern, start)) {
1088 return -1;
1089 }
1090 }
1091 1079
1092 int badness = 0; // How bad we are doing without a good-suffix table. 1080 int badness = 0; // How bad we are doing without a good-suffix table.
1093 int idx; // No matches found prior to this index. 1081 int idx; // No matches found prior to this index.
1094 // Perform search 1082 // Perform search
1095 for (idx = start_index; idx <= n - m;) { 1083 for (idx = start_index; idx <= n - m;) {
1096 int j = m - 1; 1084 int j = m - 1;
1097 schar c; 1085 schar c;
1098 while (j >= 0 && pattern[j] == (c = subject[idx + j])) j--; 1086 while (j >= 0 && pattern[j] == (c = subject[idx + j])) j--;
1099 if (j < 0) { 1087 if (j < 0) {
1100 return idx; 1088 return idx;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
1194 return -1; 1182 return -1;
1195 } 1183 }
1196 1184
1197 // Dispatch to different algorithms for different length of pattern/subject 1185 // Dispatch to different algorithms for different length of pattern/subject
1198 template <typename schar, typename pchar> 1186 template <typename schar, typename pchar>
1199 static int StringMatchStrategy(Vector<const schar> sub, 1187 static int StringMatchStrategy(Vector<const schar> sub,
1200 Vector<const pchar> pat, 1188 Vector<const pchar> pat,
1201 int start_index) { 1189 int start_index) {
1202 ASSERT(pat.length() > 1); 1190 ASSERT(pat.length() > 1);
1203 1191
1192 // We have an ASCII haystack and a non-ASCII needle. Check if there
1193 // really is a non-ASCII character in the needle and bail out if there
1194 // is.
1195 if (sizeof(pchar) > 1 && sizeof(schar) == 1) {
1196 for (int i = 0; i < pat.length(); i++) {
1197 uc16 c = pat[i];
1198 if (c > String::kMaxAsciiCharCode) {
1199 return -1;
1200 }
1201 }
1202 }
1204 // For small searches, a complex sort is not worth the setup overhead. 1203 // For small searches, a complex sort is not worth the setup overhead.
1205 bool complete; 1204 bool complete;
1206 int idx = SimpleIndexOf(sub, pat, start_index, complete); 1205 int idx = SimpleIndexOf(sub, pat, start_index, complete);
1207 if (complete) return idx; 1206 if (complete) return idx;
1208 return BoyerMooreIndexOf(sub, pat, idx); 1207 return BoyerMooreIndexOf(sub, pat, idx);
1209 } 1208 }
1210 1209
1211 // Perform string match of pattern on subject, starting at start index. 1210 // Perform string match of pattern on subject, starting at start index.
1212 // Caller must ensure that 0 <= start_index <= sub->length(), 1211 // Caller must ensure that 0 <= start_index <= sub->length(),
1213 // and should check that pat->length() + start_index <= sub->length() 1212 // and should check that pat->length() + start_index <= sub->length()
(...skipping 4117 matching lines...) Expand 10 before | Expand all | Expand 10 after
5331 5330
5332 void Runtime::PerformGC(Object* result) { 5331 void Runtime::PerformGC(Object* result) {
5333 Failure* failure = Failure::cast(result); 5332 Failure* failure = Failure::cast(result);
5334 // Try to do a garbage collection; ignore it if it fails. The C 5333 // Try to do a garbage collection; ignore it if it fails. The C
5335 // entry stub will throw an out-of-memory exception in that case. 5334 // entry stub will throw an out-of-memory exception in that case.
5336 Heap::CollectGarbage(failure->requested(), failure->allocation_space()); 5335 Heap::CollectGarbage(failure->requested(), failure->allocation_space());
5337 } 5336 }
5338 5337
5339 5338
5340 } } // namespace v8::internal 5339 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698