blob: e52cc94a4869d729f96c759ca67819f6ecdc0110 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_DATEPARSER_INL_H_
29#define V8_DATEPARSER_INL_H_
30
Steve Block3ce2e202009-11-05 08:53:23 +000031#include "dateparser.h"
32
Steve Blocka7e24c12009-10-30 11:49:00 +000033namespace v8 {
34namespace internal {
35
36template <typename Char>
37bool DateParser::Parse(Vector<Char> str, FixedArray* out) {
38 ASSERT(out->length() >= OUTPUT_SIZE);
39 InputReader<Char> in(str);
40 TimeZoneComposer tz;
41 TimeComposer time;
42 DayComposer day;
43
44 while (!in.IsEnd()) {
45 if (in.IsAsciiDigit()) {
46 // Parse a number (possibly with 1 or 2 trailing colons).
47 int n = in.ReadUnsignedNumber();
48 if (in.Skip(':')) {
49 if (in.Skip(':')) {
50 // n + "::"
51 if (!time.IsEmpty()) return false;
52 time.Add(n);
53 time.Add(0);
54 } else {
55 // n + ":"
56 if (!time.Add(n)) return false;
Steve Block6ded16b2010-05-10 14:33:55 +010057 in.Skip('.');
Steve Blocka7e24c12009-10-30 11:49:00 +000058 }
Steve Block6ded16b2010-05-10 14:33:55 +010059 } else if (in.Skip('.') && time.IsExpecting(n)) {
60 time.Add(n);
61 if (!in.IsAsciiDigit()) return false;
62 int n = in.ReadUnsignedNumber();
63 time.AddFinal(n);
Steve Blocka7e24c12009-10-30 11:49:00 +000064 } else if (tz.IsExpecting(n)) {
65 tz.SetAbsoluteMinute(n);
66 } else if (time.IsExpecting(n)) {
67 time.AddFinal(n);
Steve Block59151502010-09-22 15:07:15 +010068 // Require end, white space, "Z", "+" or "-" immediately after
69 // finalizing time.
70 if (!in.IsEnd() && !in.SkipWhiteSpace() && !in.Is('Z') &&
71 !in.IsAsciiSign()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +000072 } else {
73 if (!day.Add(n)) return false;
74 in.Skip('-'); // Ignore suffix '-' for year, month, or day.
Steve Block6ded16b2010-05-10 14:33:55 +010075 // Skip trailing 'T' for ECMAScript 5 date string format but make
76 // sure that it is followed by a digit (for the time).
77 if (in.Skip('T') && !in.IsAsciiDigit()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +000078 }
79 } else if (in.IsAsciiAlphaOrAbove()) {
80 // Parse a "word" (sequence of chars. >= 'A').
81 uint32_t pre[KeywordTable::kPrefixLength];
82 int len = in.ReadWord(pre, KeywordTable::kPrefixLength);
83 int index = KeywordTable::Lookup(pre, len);
84 KeywordType type = KeywordTable::GetType(index);
85
86 if (type == AM_PM && !time.IsEmpty()) {
87 time.SetHourOffset(KeywordTable::GetValue(index));
88 } else if (type == MONTH_NAME) {
89 day.SetNamedMonth(KeywordTable::GetValue(index));
90 in.Skip('-'); // Ignore suffix '-' for month names
91 } else if (type == TIME_ZONE_NAME && in.HasReadNumber()) {
92 tz.Set(KeywordTable::GetValue(index));
93 } else {
94 // Garbage words are illegal if a number has been read.
95 if (in.HasReadNumber()) return false;
96 }
97 } else if (in.IsAsciiSign() && (tz.IsUTC() || !time.IsEmpty())) {
98 // Parse UTC offset (only after UTC or time).
99 tz.SetSign(in.GetAsciiSignValue());
100 in.Next();
101 int n = in.ReadUnsignedNumber();
102 if (in.Skip(':')) {
103 tz.SetAbsoluteHour(n);
104 tz.SetAbsoluteMinute(kNone);
105 } else {
106 tz.SetAbsoluteHour(n / 100);
107 tz.SetAbsoluteMinute(n % 100);
108 }
109 } else if (in.Is('(')) {
110 // Ignore anything from '(' to a matching ')' or end of string.
111 in.SkipParentheses();
112 } else if ((in.IsAsciiSign() || in.Is(')')) && in.HasReadNumber()) {
113 // Extra sign or ')' is illegal if a number has been read.
114 return false;
115 } else {
116 // Ignore other characters.
117 in.Next();
118 }
119 }
120 return day.Write(out) && time.Write(out) && tz.Write(out);
121}
122
123} } // namespace v8::internal
124
125#endif // V8_DATEPARSER_INL_H_