iceberg-cpp
Loading...
Searching...
No Matches
truncate_util.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
22#include <cstdint>
23#include <string>
24#include <utility>
25
26#include "iceberg/iceberg_export.h"
27#include "iceberg/result.h"
28#include "iceberg/type_fwd.h"
29
30namespace iceberg {
31
32class ICEBERG_EXPORT TruncateUtils {
33 public:
41 static std::string TruncateUTF8(std::string source, size_t L) {
42 size_t code_point_count = 0;
43 size_t safe_point = 0;
44
45 for (size_t i = 0; i < source.size(); ++i) {
46 // Start of a new UTF-8 code point
47 if ((source[i] & 0xC0) != 0x80) {
48 code_point_count++;
49 if (code_point_count > static_cast<size_t>(L)) {
50 safe_point = i;
51 break;
52 }
53 }
54 }
55
56 if (safe_point != 0) {
57 // Resize the string to the safe point
58 source.resize(safe_point);
59 }
60
61 return source;
62 }
63
76 static Result<std::string> TruncateUTF8Max(const std::string& source, size_t L);
77
82 template <typename T>
83 requires std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t>
84 static inline T TruncateInteger(T v, int32_t W) {
85 return v - (((v % W) + W) % W);
86 }
87
92 static Decimal TruncateDecimal(const Decimal& decimal, int32_t width);
93
102 static Result<Literal> TruncateLiteral(const Literal& literal, int32_t width);
103
115 static Result<Literal> TruncateLiteralMax(const Literal& value, int32_t width);
116};
117
118} // namespace iceberg
Represents 128-bit fixed-point decimal numbers. The max decimal precision that can be safely represen...
Definition decimal.h:46
Literal is a literal value that is associated with a primitive type.
Definition literal.h:39
Definition truncate_util.h:32
static T TruncateInteger(T v, int32_t W)
Truncate an integer v, either int32_t or int64_t, to v - (v % W).
Definition truncate_util.h:84
static std::string TruncateUTF8(std::string source, size_t L)
Truncate a UTF-8 string to a specified number of code points.
Definition truncate_util.h:41