Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef HCCLV2_STRING_UTIL_H
12 : #define HCCLV2_STRING_UTIL_H
13 :
14 : #include <string>
15 : #include <sstream>
16 : #include <iomanip>
17 : #include <vector>
18 : #include "securec.h"
19 : #include "log.h"
20 :
21 : namespace Hccl {
22 :
23 : template <typename... Args>
24 59479 : inline std::string StringFormat(const char* format, Args... args)
25 : {
26 : using namespace std;
27 59479 : constexpr size_t bufSize = BUFSIZ;
28 : char buffer[bufSize];
29 59479 : int result = snprintf_s(&buffer[0], bufSize, bufSize, format, args...);
30 59479 : if (result < 0) {
31 0 : HCCL_ERROR("[StringFormat] data snprintf_s failed.");
32 0 : return "";
33 : }
34 59479 : size_t actualSize = static_cast<size_t>(result);
35 59479 : if (actualSize + 1 > bufSize) {
36 0 : actualSize++;
37 0 : std::vector<char> newbuffer(actualSize);
38 0 : auto ret = snprintf_s(newbuffer.data(), actualSize, actualSize, format, args...);
39 0 : if (ret != EOK) {
40 0 : HCCL_ERROR("[StringFormat] data snprintf_s failed.");
41 0 : return "";
42 : }
43 0 : return newbuffer.data();
44 0 : }
45 118958 : return buffer;
46 : }
47 :
48 : template <typename I>
49 528 : std::string Dec2hex(I i)
50 : {
51 : static_assert(std::is_integral<I>::value, "type I is not a integral");
52 528 : std::stringstream ss;
53 528 : ss << std::hex << "0x" << i;
54 1056 : return ss.str();
55 528 : }
56 :
57 1 : inline std::vector<std::string> SplitString(const std::string& str, const char c)
58 : {
59 1 : std::string::size_type startPos = 0;
60 1 : std::string::size_type foundPos = str.find(c);
61 :
62 1 : std::vector<std::string> strVector;
63 1 : while (foundPos != std::string::npos) {
64 0 : strVector.push_back(str.substr(startPos, foundPos - startPos));
65 0 : startPos = foundPos + 1;
66 0 : foundPos = str.find(c, startPos);
67 : }
68 1 : if (startPos != str.length()) {
69 1 : strVector.push_back(str.substr(startPos));
70 : }
71 1 : return strVector;
72 0 : }
73 :
74 : template <class T>
75 50 : T String2T(const std::string& s)
76 : {
77 : // T must support >>
78 : T t;
79 50 : std::istringstream ist(s);
80 50 : ist >> t;
81 50 : return t;
82 50 : }
83 :
84 356 : inline std::string Bytes2hex(const void* data, int size)
85 : {
86 356 : constexpr int hexWidth = 2;
87 356 : std::stringstream ss;
88 356 : ss << std::hex << std::setfill('0');
89 356 : const auto* tmp = static_cast<const unsigned char*>(data);
90 7374 : for (int i = 0; i < size; i++) {
91 7018 : ss << std::setw(hexWidth) << static_cast<int>(*tmp);
92 7018 : tmp++;
93 : }
94 712 : return ss.str();
95 356 : }
96 :
97 : } // namespace Hccl
98 :
99 : #endif // HCCLV2_STRING_UTIL_H
|