1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 #include "arrow/util/value_parsing.h"
19
20 #include <string>
21 #include <utility>
22
23 #include "arrow/vendored/fast_float/fast_float.h"
24
25 namespace arrow {
26 namespace internal {
27
StringToFloat(const char * s,size_t length,float * out)28 bool StringToFloat(const char* s, size_t length, float* out) {
29 const auto res = ::arrow_vendored::fast_float::from_chars(s, s + length, *out);
30 return res.ec == std::errc() && res.ptr == s + length;
31 }
32
StringToFloat(const char * s,size_t length,double * out)33 bool StringToFloat(const char* s, size_t length, double* out) {
34 const auto res = ::arrow_vendored::fast_float::from_chars(s, s + length, *out);
35 return res.ec == std::errc() && res.ptr == s + length;
36 }
37
38 // ----------------------------------------------------------------------
39 // strptime-like parsing
40
41 namespace {
42
43 class StrptimeTimestampParser : public TimestampParser {
44 public:
StrptimeTimestampParser(std::string format)45 explicit StrptimeTimestampParser(std::string format) : format_(std::move(format)) {}
46
operator ()(const char * s,size_t length,TimeUnit::type out_unit,int64_t * out) const47 bool operator()(const char* s, size_t length, TimeUnit::type out_unit,
48 int64_t* out) const override {
49 return ParseTimestampStrptime(s, length, format_.c_str(),
50 /*ignore_time_in_day=*/false,
51 /*allow_trailing_chars=*/false, out_unit, out);
52 }
53
kind() const54 const char* kind() const override { return "strptime"; }
55
format() const56 const char* format() const override { return format_.c_str(); }
57
58 private:
59 std::string format_;
60 };
61
62 class ISO8601Parser : public TimestampParser {
63 public:
ISO8601Parser()64 ISO8601Parser() {}
65
operator ()(const char * s,size_t length,TimeUnit::type out_unit,int64_t * out) const66 bool operator()(const char* s, size_t length, TimeUnit::type out_unit,
67 int64_t* out) const override {
68 return ParseTimestampISO8601(s, length, out_unit, out);
69 }
70
kind() const71 const char* kind() const override { return "iso8601"; }
72 };
73
74 } // namespace
75 } // namespace internal
76
format() const77 const char* TimestampParser::format() const { return ""; }
78
MakeStrptime(std::string format)79 std::shared_ptr<TimestampParser> TimestampParser::MakeStrptime(std::string format) {
80 return std::make_shared<internal::StrptimeTimestampParser>(std::move(format));
81 }
82
MakeISO8601()83 std::shared_ptr<TimestampParser> TimestampParser::MakeISO8601() {
84 return std::make_shared<internal::ISO8601Parser>();
85 }
86
87 } // namespace arrow
88