forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf8.h
More file actions
82 lines (71 loc) · 3.39 KB
/
Copy pathutf8.h
File metadata and controls
82 lines (71 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_CEL_CPP_INTERNAL_UTF8_H_
#define THIRD_PARTY_CEL_CPP_INTERNAL_UTF8_H_
#include <cstddef>
#include <string>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/nullability.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
namespace cel::internal {
// Returns true if the given UTF-8 encoded string is not malformed, false
// otherwise.
bool Utf8IsValid(absl::string_view str);
bool Utf8IsValid(const absl::Cord& str);
// Returns the number of Unicode code points in the UTF-8 encoded string.
//
// If there are any invalid bytes, they will each be counted as an invalid code
// point.
size_t Utf8CodePointCount(absl::string_view str);
size_t Utf8CodePointCount(const absl::Cord& str);
// Validates the given UTF-8 encoded string. The first return value is the
// number of code points and its meaning depends on the second return value. If
// the second return value is true the entire string is not malformed and the
// first return value is the number of code points. If the second return value
// is false the string is malformed and the first return value is the number of
// code points up until the malformed sequence was encountered.
std::pair<size_t, bool> Utf8Validate(absl::string_view str);
std::pair<size_t, bool> Utf8Validate(const absl::Cord& str);
// Decodes the next code point, returning the decoded code point and the number
// of code units (a.k.a. bytes) consumed. In the event that an invalid code unit
// sequence is returned the replacement character, U+FFFD, is returned with a
// code unit count of 1. As U+FFFD requires 3 code units when encoded, this can
// be used to differentiate valid input from malformed input.
size_t Utf8Decode(absl::string_view str, char32_t* absl_nullable code_point);
size_t Utf8Decode(const absl::Cord::CharIterator& it,
char32_t* absl_nullable code_point);
inline std::pair<char32_t, size_t> Utf8Decode(absl::string_view str) {
char32_t code_point;
size_t code_units = Utf8Decode(str, &code_point);
return std::pair{code_point, code_units};
}
inline std::pair<char32_t, size_t> Utf8Decode(
const absl::Cord::CharIterator& it) {
char32_t code_point;
size_t code_units = Utf8Decode(it, &code_point);
return std::pair{code_point, code_units};
}
// Encodes the given code point and appends it to the buffer. If the code point
// is an unpaired surrogate or outside of the valid Unicode range it is replaced
// with the replacement character, U+FFFD.
size_t Utf8Encode(char32_t code_point, std::string* absl_nonnull buffer);
size_t Utf8Encode(char32_t code_point, char* absl_nonnull buffer);
ABSL_DEPRECATED("Use other overload")
inline size_t Utf8Encode(std::string& buffer, char32_t code_point) {
return Utf8Encode(code_point, &buffer);
}
} // namespace cel::internal
#endif // THIRD_PARTY_CEL_CPP_INTERNAL_UTF8_H_