ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +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 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 28 | #ifndef V8_DATEPARSER_INL_H_ |
| 29 | #define V8_DATEPARSER_INL_H_ |
| 30 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 31 | #include "dateparser.h" |
| 32 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 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; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 57 | in.Skip('.'); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 58 | } |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [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); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 64 | } else if (tz.IsExpecting(n)) { |
| 65 | tz.SetAbsoluteMinute(n); |
| 66 | } else if (time.IsExpecting(n)) { |
| 67 | time.AddFinal(n); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 68 | // 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; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 72 | } else { |
| 73 | if (!day.Add(n)) return false; |
| 74 | in.Skip('-'); // Ignore suffix '-' for year, month, or day. |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 75 | // 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; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 78 | } |
| 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 |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 124 | |
| 125 | #endif // V8_DATEPARSER_INL_H_ |