| 203 | }; |
| 204 | |
| 205 | DateParseResult tryParseDateString( |
| 206 | const char* buf, |
| 207 | size_t len, |
| 208 | size_t& pos, |
| 209 | int64_t& daysSinceEpoch, |
| 210 | int32_t mode, |
| 211 | bool sparkCompatible = false) { |
| 212 | bool isValid = true; |
| 213 | pos = 0; |
| 214 | if (len == 0) { |
| 215 | return DateParseResult::kEmptyInput; |
| 216 | } |
| 217 | |
| 218 | int32_t day = 0; |
| 219 | int32_t month = -1; |
| 220 | int32_t year = 0; |
| 221 | bool yearneg = false; |
| 222 | int sep; |
| 223 | |
| 224 | // Skip leading spaces. |
| 225 | while (pos < len && characterIsSpace(buf[pos])) { |
| 226 | pos++; |
| 227 | } |
| 228 | |
| 229 | if (pos >= len) { |
| 230 | return DateParseResult::kEmptyInput; |
| 231 | } |
| 232 | if (buf[pos] == '-') { |
| 233 | yearneg = true; |
| 234 | pos++; |
| 235 | if (pos >= len) { |
| 236 | return DateParseResult::kEmptyYear; |
| 237 | } |
| 238 | } else if (buf[pos] == '+') { |
| 239 | pos++; |
| 240 | if (pos >= len) { |
| 241 | return DateParseResult::kEmptyYear; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if (!characterIsDigit(buf[pos])) { |
| 246 | return DateParseResult::kInvalidYear; |
| 247 | } |
| 248 | // First parse the year. |
| 249 | int yearDigitNum = 0; |
| 250 | for (; pos < len && characterIsDigit(buf[pos]); pos++, yearDigitNum++) { |
| 251 | year = checkedPlus((buf[pos] - '0'), checkedMultiply(year, 10)); |
| 252 | if (year > kMaxYear) { |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | if ((mode & ParseMode::kNonStandardCast) && yearDigitNum < 4) { |
| 257 | return DateParseResult::kYearTooShort; |
| 258 | } |
| 259 | if (yearneg) { |
| 260 | year = checkedNegate(year); |
| 261 | if (year < kMinYear) { |
| 262 | return DateParseResult::kInvalidYear; |
no test coverage detected