xref: /aosp_15_r20/external/OpenCL-CTS/test_common/harness/stringHelpers.h (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
1*6467f958SSadaf Ebrahimi //
2*6467f958SSadaf Ebrahimi // Copyright (c) 2023 The Khronos Group Inc.
3*6467f958SSadaf Ebrahimi //
4*6467f958SSadaf Ebrahimi // Licensed under the Apache License, Version 2.0 (the "License");
5*6467f958SSadaf Ebrahimi // you may not use this file except in compliance with the License.
6*6467f958SSadaf Ebrahimi // You may obtain a copy of the License at
7*6467f958SSadaf Ebrahimi //
8*6467f958SSadaf Ebrahimi //    http://www.apache.org/licenses/LICENSE-2.0
9*6467f958SSadaf Ebrahimi //
10*6467f958SSadaf Ebrahimi // Unless required by applicable law or agreed to in writing, software
11*6467f958SSadaf Ebrahimi // distributed under the License is distributed on an "AS IS" BASIS,
12*6467f958SSadaf Ebrahimi // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*6467f958SSadaf Ebrahimi // See the License for the specific language governing permissions and
14*6467f958SSadaf Ebrahimi // limitations under the License.
15*6467f958SSadaf Ebrahimi //
16*6467f958SSadaf Ebrahimi 
17*6467f958SSadaf Ebrahimi #ifndef STRING_HELPERS_H
18*6467f958SSadaf Ebrahimi #define STRING_HELPERS_H
19*6467f958SSadaf Ebrahimi 
20*6467f958SSadaf Ebrahimi #include <memory>
21*6467f958SSadaf Ebrahimi #include <stdexcept>
22*6467f958SSadaf Ebrahimi #include <string>
23*6467f958SSadaf Ebrahimi 
concat_kernel(const char * sstr[],int num)24*6467f958SSadaf Ebrahimi inline std::string concat_kernel(const char *sstr[], int num)
25*6467f958SSadaf Ebrahimi {
26*6467f958SSadaf Ebrahimi     std::string res;
27*6467f958SSadaf Ebrahimi     for (int i = 0; i < num; i++) res += std::string(sstr[i]);
28*6467f958SSadaf Ebrahimi     return res;
29*6467f958SSadaf Ebrahimi }
30*6467f958SSadaf Ebrahimi 
31*6467f958SSadaf Ebrahimi template <typename... Args>
str_sprintf(const std::string & str,Args...args)32*6467f958SSadaf Ebrahimi inline std::string str_sprintf(const std::string &str, Args... args)
33*6467f958SSadaf Ebrahimi {
34*6467f958SSadaf Ebrahimi     int str_size = std::snprintf(nullptr, 0, str.c_str(), args...) + 1;
35*6467f958SSadaf Ebrahimi     if (str_size <= 0) throw std::runtime_error("Formatting error.");
36*6467f958SSadaf Ebrahimi     size_t s = static_cast<size_t>(str_size);
37*6467f958SSadaf Ebrahimi     std::unique_ptr<char[]> buffer(new char[s]);
38*6467f958SSadaf Ebrahimi     std::snprintf(buffer.get(), s, str.c_str(), args...);
39*6467f958SSadaf Ebrahimi     return std::string(buffer.get(), buffer.get() + s - 1);
40*6467f958SSadaf Ebrahimi }
41*6467f958SSadaf Ebrahimi 
42*6467f958SSadaf Ebrahimi #endif // STRING_HELPERS_H
43