/src/sentencepiece/third_party/absl/strings/str_join.h
Line | Count | Source |
1 | | // |
2 | | // Copyright 2017 The Abseil Authors. |
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 | | #ifndef ABSL_STRINGS_STR_JOIN_H_ |
17 | | #define ABSL_STRINGS_STR_JOIN_H_ |
18 | | |
19 | | #include <string> |
20 | | |
21 | | #include "third_party/absl/strings/string_view.h" |
22 | | |
23 | | namespace absl { |
24 | | namespace { |
25 | | template <typename T> |
26 | 0 | inline size_t Itoa(T val, char *s) { |
27 | 0 | char *org = s; |
28 | 0 |
|
29 | 0 | if (val < 0) { |
30 | 0 | *s++ = '-'; |
31 | 0 | val = -val; |
32 | 0 | } |
33 | 0 | char *t = s; |
34 | 0 |
|
35 | 0 | T mod = 0; |
36 | 0 | while (val) { |
37 | 0 | mod = val % 10; |
38 | 0 | *t++ = static_cast<char>(mod) + '0'; |
39 | 0 | val /= 10; |
40 | 0 | } |
41 | 0 |
|
42 | 0 | if (s == t) { |
43 | 0 | *t++ = '0'; |
44 | 0 | } |
45 | 0 |
|
46 | 0 | *t = '\0'; |
47 | 0 | std::reverse(s, t); |
48 | 0 | return static_cast<size_t>(t - org); |
49 | 0 | } |
50 | | } // namespace |
51 | | |
52 | | inline std::string StrJoin(const std::vector<std::string> &tokens, |
53 | 0 | absl::string_view delim) { |
54 | 0 | std::string result; |
55 | 0 | if (!tokens.empty()) { |
56 | 0 | result.append(tokens[0]); |
57 | 0 | } |
58 | 0 | for (size_t i = 1; i < tokens.size(); ++i) { |
59 | 0 | result.append(delim.data(), delim.size()); |
60 | 0 | result.append(tokens[i]); |
61 | 0 | } |
62 | 0 | return result; |
63 | 0 | } |
64 | | |
65 | | inline std::string StrJoin(const std::vector<absl::string_view> &tokens, |
66 | 0 | absl::string_view delim) { |
67 | 0 | std::string result; |
68 | 0 | if (!tokens.empty()) { |
69 | 0 | result.append(tokens[0].data(), tokens[0].size()); |
70 | 0 | } |
71 | 0 | for (size_t i = 1; i < tokens.size(); ++i) { |
72 | 0 | result.append(delim.data(), delim.size()); |
73 | 0 | result.append(tokens[i].data(), tokens[i].size()); |
74 | 0 | } |
75 | 0 | return result; |
76 | 0 | } |
77 | | |
78 | | inline std::string StrJoin(const std::vector<int> &tokens, |
79 | 0 | absl::string_view delim) { |
80 | 0 | std::string result; |
81 | 0 | char buf[32]; |
82 | 0 | if (!tokens.empty()) { |
83 | 0 | const size_t len = Itoa(tokens[0], buf); |
84 | 0 | result.append(buf, len); |
85 | 0 | } |
86 | 0 | for (size_t i = 1; i < tokens.size(); ++i) { |
87 | 0 | result.append(delim.data(), delim.size()); |
88 | 0 | const size_t len = Itoa(tokens[i], buf); |
89 | 0 | result.append(buf, len); |
90 | 0 | } |
91 | 0 | return result; |
92 | 0 | } |
93 | | |
94 | | } // namespace absl |
95 | | #endif // ABSL_STRINGS_STR_CAT_H_ |