• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 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 //      http://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 #ifndef ICING_ABSL_PORTS_STR_CAT_H_
16 #define ICING_ABSL_PORTS_STR_CAT_H_
17 
18 #include <cstdarg>
19 #include <cstring>
20 #include <string>
21 #include <string_view>
22 #include <vector>
23 
24 namespace icing {
25 namespace lib {
26 namespace absl_ports {
27 
28 // Appends the content of s to the char buffer starting at out and returns the
29 // address of the first character after the content copied from s.
30 // REQUIRES: out is large enough to hold all content from s.
31 char* Append(char* out, std::string_view s);
32 
33 // A port of absl::StrCat.
34 //
35 // Merges given strings or numbers, using no delimiter(s), returning the merged
36 // result as a string.
37 //
38 // Unlike absl::StrCat, this version only accepts string_views. For converting
39 // numerics to strings, use StringPrintf.
40 //
41 // Separate implementations for 2-4 arguments are provided separately from the
42 // variadic definition, just like absl does. This is a minor optimization to
43 // avoid constructing a vector and copying all string_view params.
44 std::string StrCat(std::string_view a, std::string_view b);
45 std::string StrCat(std::string_view a, std::string_view b, std::string_view c);
46 std::string StrCat(std::string_view a, std::string_view b, std::string_view c,
47                    std::string_view d);
48 
49 std::string StrCatPieces(std::vector<std::string_view> pieces);
50 
51 template <typename... AV>
StrCat(const AV &...args)52 std::string StrCat(const AV&... args) {
53   return StrCatPieces({static_cast<const std::string_view&>(args)...});
54 }
55 
56 // A port of absl::StrAppend.
57 //
58 // Appends a string or set of strings to an existing string, in a similar
59 // fashion to `StrCat()`.
60 //
61 // Unlike absl::StrAppend, this version only accepts string_views. For
62 // converting numerics to strings, use StringPrintf.
63 void StrAppend(std::string* dest, std::string_view a);
64 void StrAppend(std::string* dest, std::string_view a, std::string_view b);
65 void StrAppend(std::string* dest, std::string_view a, std::string_view b,
66                std::string_view c);
67 void StrAppend(std::string* dest, std::string_view a, std::string_view b,
68                std::string_view c, std::string_view d);
69 
70 void StrAppendPieces(std::string* dest, std::vector<std::string_view> pieces);
71 
72 template <typename... AV>
StrAppend(std::string * dest,const AV &...args)73 void StrAppend(std::string* dest, const AV&... args) {
74   StrAppendPieces(dest, {static_cast<const std::string_view&>(args)...});
75 }
76 
77 }  // namespace absl_ports
78 }  // namespace lib
79 }  // namespace icing
80 
81 #endif  // ICING_ABSL_PORTS_STR_CAT_H_
82