1 // 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
31 namespace v8 {
32 namespace internal {
33
34 template <typename Char>
Parse(Vector<Char> str,FixedArray * out)35 bool DateParser::Parse(Vector<Char> str, FixedArray* out) {
36 ASSERT(out->length() >= OUTPUT_SIZE);
37 InputReader<Char> in(str);
38 TimeZoneComposer tz;
39 TimeComposer time;
40 DayComposer day;
41
42 while (!in.IsEnd()) {
43 if (in.IsAsciiDigit()) {
44 // Parse a number (possibly with 1 or 2 trailing colons).
45 int n = in.ReadUnsignedNumber();
46 if (in.Skip(':')) {
47 if (in.Skip(':')) {
48 // n + "::"
49 if (!time.IsEmpty()) return false;
50 time.Add(n);
51 time.Add(0);
52 } else {
53 // n + ":"
54 if (!time.Add(n)) return false;
55 }
56 } else if (tz.IsExpecting(n)) {
57 tz.SetAbsoluteMinute(n);
58 } else if (time.IsExpecting(n)) {
59 time.AddFinal(n);
60 // Require end or white space immediately after finalizing time.
61 if (!in.IsEnd() && !in.SkipWhiteSpace()) return false;
62 } else {
63 if (!day.Add(n)) return false;
64 in.Skip('-'); // Ignore suffix '-' for year, month, or day.
65 }
66 } else if (in.IsAsciiAlphaOrAbove()) {
67 // Parse a "word" (sequence of chars. >= 'A').
68 uint32_t pre[KeywordTable::kPrefixLength];
69 int len = in.ReadWord(pre, KeywordTable::kPrefixLength);
70 int index = KeywordTable::Lookup(pre, len);
71 KeywordType type = KeywordTable::GetType(index);
72
73 if (type == AM_PM && !time.IsEmpty()) {
74 time.SetHourOffset(KeywordTable::GetValue(index));
75 } else if (type == MONTH_NAME) {
76 day.SetNamedMonth(KeywordTable::GetValue(index));
77 in.Skip('-'); // Ignore suffix '-' for month names
78 } else if (type == TIME_ZONE_NAME && in.HasReadNumber()) {
79 tz.Set(KeywordTable::GetValue(index));
80 } else {
81 // Garbage words are illegal if a number has been read.
82 if (in.HasReadNumber()) return false;
83 }
84 } else if (in.IsAsciiSign() && (tz.IsUTC() || !time.IsEmpty())) {
85 // Parse UTC offset (only after UTC or time).
86 tz.SetSign(in.GetAsciiSignValue());
87 in.Next();
88 int n = in.ReadUnsignedNumber();
89 if (in.Skip(':')) {
90 tz.SetAbsoluteHour(n);
91 tz.SetAbsoluteMinute(kNone);
92 } else {
93 tz.SetAbsoluteHour(n / 100);
94 tz.SetAbsoluteMinute(n % 100);
95 }
96 } else if (in.Is('(')) {
97 // Ignore anything from '(' to a matching ')' or end of string.
98 in.SkipParentheses();
99 } else if ((in.IsAsciiSign() || in.Is(')')) && in.HasReadNumber()) {
100 // Extra sign or ')' is illegal if a number has been read.
101 return false;
102 } else {
103 // Ignore other characters.
104 in.Next();
105 }
106 }
107 return day.Write(out) && time.Write(out) && tz.Write(out);
108 }
109
110 } } // namespace v8::internal
111
112 #endif // V8_DATEPARSER_INL_H_
113