xref: /aosp_15_r20/external/OpenCL-CTS/test_common/harness/stringHelpers.h (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
1 //
2 // Copyright (c) 2023 The Khronos Group Inc.
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 
17 #ifndef STRING_HELPERS_H
18 #define STRING_HELPERS_H
19 
20 #include <memory>
21 #include <stdexcept>
22 #include <string>
23 
concat_kernel(const char * sstr[],int num)24 inline std::string concat_kernel(const char *sstr[], int num)
25 {
26     std::string res;
27     for (int i = 0; i < num; i++) res += std::string(sstr[i]);
28     return res;
29 }
30 
31 template <typename... Args>
str_sprintf(const std::string & str,Args...args)32 inline std::string str_sprintf(const std::string &str, Args... args)
33 {
34     int str_size = std::snprintf(nullptr, 0, str.c_str(), args...) + 1;
35     if (str_size <= 0) throw std::runtime_error("Formatting error.");
36     size_t s = static_cast<size_t>(str_size);
37     std::unique_ptr<char[]> buffer(new char[s]);
38     std::snprintf(buffer.get(), s, str.c_str(), args...);
39     return std::string(buffer.get(), buffer.get() + s - 1);
40 }
41 
42 #endif // STRING_HELPERS_H
43