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

Side by Side Diff: src/dateparser.cc

Issue 7291022: Make date parser handle all ES5 Date Time Strings correctly. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Address review comments. Created 9 years, 5 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
« no previous file with comments | « src/dateparser.h ('k') | src/dateparser-inl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2008 the V8 project authors. All rights reserved. 1 // Copyright 2011 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
11 // with the distribution. 11 // with the distribution.
(...skipping 25 matching lines...) Expand all
37 // Day and month defaults to 1. 37 // Day and month defaults to 1.
38 while (index_ < kSize) { 38 while (index_ < kSize) {
39 comp_[index_++] = 1; 39 comp_[index_++] = 1;
40 } 40 }
41 41
42 int year = 0; // Default year is 0 (=> 2000) for KJS compatibility. 42 int year = 0; // Default year is 0 (=> 2000) for KJS compatibility.
43 int month = kNone; 43 int month = kNone;
44 int day = kNone; 44 int day = kNone;
45 45
46 if (named_month_ == kNone) { 46 if (named_month_ == kNone) {
47 if (index_ == 3 && !IsDay(comp_[0])) { 47 if (is_iso_date_ || (index_ == 3 && !IsDay(comp_[0]))) {
48 // YMD 48 // YMD
49 year = comp_[0]; 49 year = comp_[0];
50 month = comp_[1]; 50 month = comp_[1];
51 day = comp_[2]; 51 day = comp_[2];
52 } else { 52 } else {
53 // MD(Y) 53 // MD(Y)
54 month = comp_[0]; 54 month = comp_[0];
55 day = comp_[1]; 55 day = comp_[1];
56 if (index_ == 3) year = comp_[2]; 56 if (index_ == 3) year = comp_[2];
57 } 57 }
58 } else { 58 } else {
59 month = named_month_; 59 month = named_month_;
60 if (index_ == 1) { 60 if (index_ == 1) {
61 // MD or DM 61 // MD or DM
62 day = comp_[0]; 62 day = comp_[0];
63 } else if (!IsDay(comp_[0])) { 63 } else if (!IsDay(comp_[0])) {
64 // YMD, MYD, or YDM 64 // YMD, MYD, or YDM
65 year = comp_[0]; 65 year = comp_[0];
66 day = comp_[1]; 66 day = comp_[1];
67 } else { 67 } else {
68 // DMY, MDY, or DYM 68 // DMY, MDY, or DYM
69 day = comp_[0]; 69 day = comp_[0];
70 year = comp_[1]; 70 year = comp_[1];
71 } 71 }
72 } 72 }
73 73
74 if (Between(year, 0, 49)) year += 2000; 74 if (!is_iso_date_) {
75 else if (Between(year, 50, 99)) year += 1900; 75 if (Between(year, 0, 49)) year += 2000;
76 else if (Between(year, 50, 99)) year += 1900;
77 }
76 78
77 if (!Smi::IsValid(year) || !IsMonth(month) || !IsDay(day)) return false; 79 if (!Smi::IsValid(year) || !IsMonth(month) || !IsDay(day)) return false;
78 80
79 output->set(YEAR, Smi::FromInt(year)); 81 output->set(YEAR, Smi::FromInt(year));
80 output->set(MONTH, Smi::FromInt(month - 1)); // 0-based 82 output->set(MONTH, Smi::FromInt(month - 1)); // 0-based
81 output->set(DAY, Smi::FromInt(day)); 83 output->set(DAY, Smi::FromInt(day));
82 return true; 84 return true;
83 } 85 }
84 86
85 87
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 {'z', '\0', '\0', DateParser::TIME_ZONE_NAME, 0}, 146 {'z', '\0', '\0', DateParser::TIME_ZONE_NAME, 0},
145 {'g', 'm', 't', DateParser::TIME_ZONE_NAME, 0}, 147 {'g', 'm', 't', DateParser::TIME_ZONE_NAME, 0},
146 {'c', 'd', 't', DateParser::TIME_ZONE_NAME, -5}, 148 {'c', 'd', 't', DateParser::TIME_ZONE_NAME, -5},
147 {'c', 's', 't', DateParser::TIME_ZONE_NAME, -6}, 149 {'c', 's', 't', DateParser::TIME_ZONE_NAME, -6},
148 {'e', 'd', 't', DateParser::TIME_ZONE_NAME, -4}, 150 {'e', 'd', 't', DateParser::TIME_ZONE_NAME, -4},
149 {'e', 's', 't', DateParser::TIME_ZONE_NAME, -5}, 151 {'e', 's', 't', DateParser::TIME_ZONE_NAME, -5},
150 {'m', 'd', 't', DateParser::TIME_ZONE_NAME, -6}, 152 {'m', 'd', 't', DateParser::TIME_ZONE_NAME, -6},
151 {'m', 's', 't', DateParser::TIME_ZONE_NAME, -7}, 153 {'m', 's', 't', DateParser::TIME_ZONE_NAME, -7},
152 {'p', 'd', 't', DateParser::TIME_ZONE_NAME, -7}, 154 {'p', 'd', 't', DateParser::TIME_ZONE_NAME, -7},
153 {'p', 's', 't', DateParser::TIME_ZONE_NAME, -8}, 155 {'p', 's', 't', DateParser::TIME_ZONE_NAME, -8},
156 {'t', '\0', '\0', DateParser::TIME_SEPARATOR, 0},
154 {'\0', '\0', '\0', DateParser::INVALID, 0}, 157 {'\0', '\0', '\0', DateParser::INVALID, 0},
155 }; 158 };
156 159
157 160
158 // We could use perfect hashing here, but this is not a bottleneck. 161 // We could use perfect hashing here, but this is not a bottleneck.
159 int DateParser::KeywordTable::Lookup(const uint32_t* pre, int len) { 162 int DateParser::KeywordTable::Lookup(const uint32_t* pre, int len) {
160 int i; 163 int i;
161 for (i = 0; array[i][kTypeOffset] != INVALID; i++) { 164 for (i = 0; array[i][kTypeOffset] != INVALID; i++) {
162 int j = 0; 165 int j = 0;
163 while (j < kPrefixLength && 166 while (j < kPrefixLength &&
164 pre[j] == static_cast<uint32_t>(array[i][j])) { 167 pre[j] == static_cast<uint32_t>(array[i][j])) {
165 j++; 168 j++;
166 } 169 }
167 // Check if we have a match and the length is legal. 170 // Check if we have a match and the length is legal.
168 // Word longer than keyword is only allowed for month names. 171 // Word longer than keyword is only allowed for month names.
169 if (j == kPrefixLength && 172 if (j == kPrefixLength &&
170 (len <= kPrefixLength || array[i][kTypeOffset] == MONTH_NAME)) { 173 (len <= kPrefixLength || array[i][kTypeOffset] == MONTH_NAME)) {
171 return i; 174 return i;
172 } 175 }
173 } 176 }
174 return i; 177 return i;
175 } 178 }
176 179
177 180
181 int DateParser::ReadMilliseconds(DateToken token) {
182 // Read first three significant digits of the original numeral,
183 // as inferred from the value and the number of digits.
184 // I.e., use the number of digits to see if there were
185 // leading zeros.
186 int number = token.number();
187 int length = token.length();
188 if (length < 3) {
189 // Less than three digits. Multiply to put most significant digit
190 // in hundreds position.
191 if (length == 1) {
192 number *= 100;
193 } else if (length == 2) {
194 number *= 10;
195 }
196 } else if (length > 3) {
197 if (length > kMaxSignificantDigits) length = kMaxSignificantDigits;
198 // More than three digits. Divide by 10^(length - 3) to get three
199 // most significant digits.
200 int factor = 1;
201 do {
202 ASSERT(factor <= 100000000); // factor won't overflow.
203 factor *= 10;
204 length--;
205 } while (length > 3);
206 number /= factor;
207 }
208 return number;
209 }
210
211
178 } } // namespace v8::internal 212 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/dateparser.h ('k') | src/dateparser-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698