Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 31 | #include "dateparser.h" |
| 32 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | template <typename Char> |
| 37 | bool 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 Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 57 | in.Skip('.'); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 59 | } 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | } else if (tz.IsExpecting(n)) { |
| 65 | tz.SetAbsoluteMinute(n); |
| 66 | } else if (time.IsExpecting(n)) { |
| 67 | time.AddFinal(n); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 68 | // Require end, white space or Z immediately after finalizing time. |
| 69 | if (!in.IsEnd() && !in.SkipWhiteSpace() && !in.Is('Z')) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | } else { |
| 71 | if (!day.Add(n)) return false; |
| 72 | in.Skip('-'); // Ignore suffix '-' for year, month, or day. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 73 | // Skip trailing 'T' for ECMAScript 5 date string format but make |
| 74 | // sure that it is followed by a digit (for the time). |
| 75 | if (in.Skip('T') && !in.IsAsciiDigit()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 76 | } |
| 77 | } else if (in.IsAsciiAlphaOrAbove()) { |
| 78 | // Parse a "word" (sequence of chars. >= 'A'). |
| 79 | uint32_t pre[KeywordTable::kPrefixLength]; |
| 80 | int len = in.ReadWord(pre, KeywordTable::kPrefixLength); |
| 81 | int index = KeywordTable::Lookup(pre, len); |
| 82 | KeywordType type = KeywordTable::GetType(index); |
| 83 | |
| 84 | if (type == AM_PM && !time.IsEmpty()) { |
| 85 | time.SetHourOffset(KeywordTable::GetValue(index)); |
| 86 | } else if (type == MONTH_NAME) { |
| 87 | day.SetNamedMonth(KeywordTable::GetValue(index)); |
| 88 | in.Skip('-'); // Ignore suffix '-' for month names |
| 89 | } else if (type == TIME_ZONE_NAME && in.HasReadNumber()) { |
| 90 | tz.Set(KeywordTable::GetValue(index)); |
| 91 | } else { |
| 92 | // Garbage words are illegal if a number has been read. |
| 93 | if (in.HasReadNumber()) return false; |
| 94 | } |
| 95 | } else if (in.IsAsciiSign() && (tz.IsUTC() || !time.IsEmpty())) { |
| 96 | // Parse UTC offset (only after UTC or time). |
| 97 | tz.SetSign(in.GetAsciiSignValue()); |
| 98 | in.Next(); |
| 99 | int n = in.ReadUnsignedNumber(); |
| 100 | if (in.Skip(':')) { |
| 101 | tz.SetAbsoluteHour(n); |
| 102 | tz.SetAbsoluteMinute(kNone); |
| 103 | } else { |
| 104 | tz.SetAbsoluteHour(n / 100); |
| 105 | tz.SetAbsoluteMinute(n % 100); |
| 106 | } |
| 107 | } else if (in.Is('(')) { |
| 108 | // Ignore anything from '(' to a matching ')' or end of string. |
| 109 | in.SkipParentheses(); |
| 110 | } else if ((in.IsAsciiSign() || in.Is(')')) && in.HasReadNumber()) { |
| 111 | // Extra sign or ')' is illegal if a number has been read. |
| 112 | return false; |
| 113 | } else { |
| 114 | // Ignore other characters. |
| 115 | in.Next(); |
| 116 | } |
| 117 | } |
| 118 | return day.Write(out) && time.Write(out) && tz.Write(out); |
| 119 | } |
| 120 | |
| 121 | } } // namespace v8::internal |
| 122 | |
| 123 | #endif // V8_DATEPARSER_INL_H_ |