The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / v8 / src / dateparser-inl.h @ 40c0f755
History | View | Annotate | Download (4.26 KB)
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 |
namespace v8 { namespace internal { |
29 |
|
30 |
template <typename Char> |
31 |
bool DateParser::Parse(Vector<Char> str, FixedArray* out) {
|
32 |
ASSERT(out->length() >= OUTPUT_SIZE); |
33 |
InputReader<Char> in(str); |
34 |
TimeZoneComposer tz; |
35 |
TimeComposer time; |
36 |
DayComposer day; |
37 |
|
38 |
while (!in.IsEnd()) {
|
39 |
if (in.IsAsciiDigit()) {
|
40 |
// Parse a number (possibly with 1 or 2 trailing colons).
|
41 |
int n = in.ReadUnsignedNumber();
|
42 |
if (in.Skip(':')) { |
43 |
if (in.Skip(':')) { |
44 |
// n + "::"
|
45 |
if (!time.IsEmpty()) return false; |
46 |
time.Add(n); |
47 |
time.Add(0);
|
48 |
} else {
|
49 |
// n + ":"
|
50 |
if (!time.Add(n)) return false; |
51 |
} |
52 |
} else if (tz.IsExpecting(n)) { |
53 |
tz.SetAbsoluteMinute(n); |
54 |
} else if (time.IsExpecting(n)) { |
55 |
time.AddFinal(n); |
56 |
// Require end or white space immediately after finalizing time.
|
57 |
if (!in.IsEnd() && !in.SkipWhiteSpace()) return false; |
58 |
} else {
|
59 |
if (!day.Add(n)) return false; |
60 |
in.Skip('-'); // Ignore suffix '-' for year, month, or day. |
61 |
} |
62 |
} else if (in.IsAsciiAlphaOrAbove()) { |
63 |
// Parse a "word" (sequence of chars. >= 'A').
|
64 |
uint32_t pre[KeywordTable::kPrefixLength]; |
65 |
int len = in.ReadWord(pre, KeywordTable::kPrefixLength);
|
66 |
int index = KeywordTable::Lookup(pre, len);
|
67 |
KeywordType type = KeywordTable::GetType(index); |
68 |
|
69 |
if (type == AM_PM && !time.IsEmpty()) {
|
70 |
time.SetHourOffset(KeywordTable::GetValue(index)); |
71 |
} else if (type == MONTH_NAME) { |
72 |
day.SetNamedMonth(KeywordTable::GetValue(index)); |
73 |
in.Skip('-'); // Ignore suffix '-' for month names |
74 |
} else if (type == TIME_ZONE_NAME && in.HasReadNumber()) { |
75 |
tz.Set(KeywordTable::GetValue(index)); |
76 |
} else {
|
77 |
// Garbage words are illegal if a number has been read.
|
78 |
if (in.HasReadNumber()) return false; |
79 |
} |
80 |
} else if (in.IsAsciiSign() && (tz.IsUTC() || !time.IsEmpty())) { |
81 |
// Parse UTC offset (only after UTC or time).
|
82 |
tz.SetSign(in.GetAsciiSignValue()); |
83 |
in.Next(); |
84 |
int n = in.ReadUnsignedNumber();
|
85 |
if (in.Skip(':')) { |
86 |
tz.SetAbsoluteHour(n); |
87 |
tz.SetAbsoluteMinute(kNone); |
88 |
} else {
|
89 |
tz.SetAbsoluteHour(n / 100);
|
90 |
tz.SetAbsoluteMinute(n % 100);
|
91 |
} |
92 |
} else if (in.Is('(')) { |
93 |
// Ignore anything from '(' to a matching ')' or end of string.
|
94 |
in.SkipParentheses(); |
95 |
} else if ((in.IsAsciiSign() || in.Is(')')) && in.HasReadNumber()) { |
96 |
// Extra sign or ')' is illegal if a number has been read.
|
97 |
return false; |
98 |
} else {
|
99 |
// Ignore other characters.
|
100 |
in.Next(); |
101 |
} |
102 |
} |
103 |
return day.Write(out) && time.Write(out) && tz.Write(out);
|
104 |
} |
105 |
|
106 |
} } // namespace v8::internal
|