/proc/self/cwd/common/constant.cc
Line | Count | Source |
1 | | // Copyright 2024 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "common/constant.h" |
16 | | |
17 | | #include <cmath> |
18 | | #include <cstdint> |
19 | | #include <string> |
20 | | |
21 | | #include "absl/base/no_destructor.h" |
22 | | #include "absl/strings/match.h" |
23 | | #include "absl/strings/str_cat.h" |
24 | | #include "absl/strings/string_view.h" |
25 | | #include "absl/time/time.h" |
26 | | #include "internal/strings.h" |
27 | | |
28 | | namespace cel { |
29 | | |
30 | 0 | const BytesConstant& BytesConstant::default_instance() { |
31 | 0 | static const absl::NoDestructor<BytesConstant> instance; |
32 | 0 | return *instance; |
33 | 0 | } |
34 | | |
35 | 0 | const StringConstant& StringConstant::default_instance() { |
36 | 0 | static const absl::NoDestructor<StringConstant> instance; |
37 | 0 | return *instance; |
38 | 0 | } |
39 | | |
40 | 0 | const Constant& Constant::default_instance() { |
41 | 0 | static const absl::NoDestructor<Constant> instance; |
42 | 0 | return *instance; |
43 | 0 | } |
44 | | |
45 | 0 | std::string FormatNullConstant() { return "null"; } |
46 | | |
47 | 0 | std::string FormatBoolConstant(bool value) { |
48 | 0 | return value ? std::string("true") : std::string("false"); |
49 | 0 | } |
50 | | |
51 | 0 | std::string FormatIntConstant(int64_t value) { return absl::StrCat(value); } |
52 | | |
53 | 0 | std::string FormatUintConstant(uint64_t value) { |
54 | 0 | return absl::StrCat(value, "u"); |
55 | 0 | } |
56 | | |
57 | 0 | std::string FormatDoubleConstant(double value) { |
58 | 0 | if (std::isfinite(value)) { |
59 | 0 | if (std::floor(value) != value) { |
60 | | // The double is not representable as a whole number, so use |
61 | | // absl::StrCat which will add decimal places. |
62 | 0 | return absl::StrCat(value); |
63 | 0 | } |
64 | | // absl::StrCat historically would represent 0.0 as 0, and we want the |
65 | | // decimal places so ZetaSQL correctly assumes the type as double |
66 | | // instead of int64. |
67 | 0 | std::string stringified = absl::StrCat(value); |
68 | 0 | if (!absl::StrContains(stringified, '.')) { |
69 | 0 | absl::StrAppend(&stringified, ".0"); |
70 | 0 | } |
71 | 0 | return stringified; |
72 | 0 | } |
73 | 0 | if (std::isnan(value)) { |
74 | 0 | return "nan"; |
75 | 0 | } |
76 | 0 | if (std::signbit(value)) { |
77 | 0 | return "-infinity"; |
78 | 0 | } |
79 | 0 | return "+infinity"; |
80 | 0 | } |
81 | | |
82 | 0 | std::string FormatBytesConstant(absl::string_view value) { |
83 | 0 | return internal::FormatBytesLiteral(value); |
84 | 0 | } |
85 | | |
86 | 0 | std::string FormatStringConstant(absl::string_view value) { |
87 | 0 | return internal::FormatStringLiteral(value); |
88 | 0 | } |
89 | | |
90 | 0 | std::string FormatDurationConstant(absl::Duration value) { |
91 | 0 | return absl::StrCat("duration(\"", absl::FormatDuration(value), "\")"); |
92 | 0 | } |
93 | | |
94 | 0 | std::string FormatTimestampConstant(absl::Time value) { |
95 | 0 | return absl::StrCat( |
96 | 0 | "timestamp(\"", |
97 | 0 | absl::FormatTime("%Y-%m-%d%ET%H:%M:%E*SZ", value, absl::UTCTimeZone()), |
98 | 0 | "\")"); |
99 | 0 | } |
100 | | |
101 | | } // namespace cel |