Apollo 10.0
自动驾驶开放平台
string_util.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <cmath>
20#include <vector>
21
22#include "absl/strings/str_cat.h"
23
24namespace apollo {
25namespace common {
26namespace util {
27namespace {
28
29static const char kBase64Array[] =
30 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
31
32std::string Base64Piece(const char in0, const char in1, const char in2) {
33 const int triplet = in0 << 16 | in1 << 8 | in2;
34 std::string out(4, '=');
35 out[0] = kBase64Array[(triplet >> 18) & 0x3f];
36 out[1] = kBase64Array[(triplet >> 12) & 0x3f];
37 if (in1) {
38 out[2] = kBase64Array[(triplet >> 6) & 0x3f];
39 }
40 if (in2) {
41 out[3] = kBase64Array[triplet & 0x3f];
42 }
43 return out;
44}
45
46} // namespace
47
48std::string EncodeBase64(std::string_view in) {
49 std::string out;
50 if (in.empty()) {
51 return out;
52 }
53
54 const size_t in_size = in.length();
55 out.reserve(((in_size - 1) / 3 + 1) * 4);
56 for (size_t i = 0; i + 2 < in_size; i += 3) {
57 absl::StrAppend(&out, Base64Piece(in[i], in[i + 1], in[i + 2]));
58 }
59 if (in_size % 3 == 1) {
60 absl::StrAppend(&out, Base64Piece(in[in_size - 1], 0, 0));
61 }
62 if (in_size % 3 == 2) {
63 absl::StrAppend(&out, Base64Piece(in[in_size - 2], in[in_size - 1], 0));
64 }
65 return out;
66}
67
68} // namespace util
69} // namespace common
70} // namespace apollo
std::string EncodeBase64(std::string_view in)
class register implement
Definition arena_queue.h:37
Some string util functions.