nndeploy C++ API  0.2.0
nndeploy C++ API
string.h
Go to the documentation of this file.
1 
2 #ifndef _NNDEPLOY_BASE_STRING_H_
3 #define _NNDEPLOY_BASE_STRING_H_
4 
5 #include <functional>
6 
7 #include "nndeploy/base/common.h"
9 #include "nndeploy/base/macro.h"
10 
11 namespace nndeploy {
12 namespace base {
13 
14 extern NNDEPLOY_CC_API std::string ucharToString(const unsigned char *buffer,
15  int length);
16 
17 extern NNDEPLOY_CC_API std::string wstringToString(const std::wstring &wstr);
18 
19 extern NNDEPLOY_CC_API std::wstring stringToWstring(const std::string &str);
20 
21 extern NNDEPLOY_CC_API std::vector<std::string> splitString(
22  const std::string &str, const std::string &spstr);
23 
24 template <typename T>
25 std::string toString(T value) {
26  std::ostringstream os;
27  os << value;
28  return os.str();
29 }
30 
31 template <typename T>
32 std::string vectorToString(std::vector<T> val) {
33  if (val.empty()) {
34  return "";
35  }
36 
37  std::stringstream stream;
38  stream << "[";
39  for (int i = 0; i < val.size(); ++i) {
40  stream << val[i];
41  if (i != val.size() - 1) stream << ",";
42  }
43  stream << "]";
44  return stream.str();
45 }
46 
53 inline bool isSpace(char c) {
54  return (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f');
55 }
56 
63 inline bool isBlank(char c) { return (c == ' ' || c == '\t'); }
64 
71 inline bool isDigit(char c) { return (c >= '0' && c <= '9'); }
72 
79 inline bool isAlpha(char c) {
80  static_assert(
81  static_cast<int>('A') == 65 && static_cast<int>('Z' - 'A') == 25,
82  "Only system with ASCII character set is supported");
83  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
84 }
85 
93 inline bool isDigitchars(char c) {
94  return (c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.' ||
95  c == 'e' || c == 'E';
96 }
97 
98 extern NNDEPLOY_CC_API bool isNumeric(const std::string &str);
99 
100 template <typename T>
101 void printData(T *data, size_t size, std::ostream &stream = std::cout) {
102  if (data == nullptr) {
103  return;
104  }
105 
106  stream << "[";
107  for (int i = 0; i < size; ++i) {
108  stream << data[i];
109  if (i != size - 1) stream << ",";
110  }
111  stream << "]";
112 
113  stream << std::endl;
114 }
115 
116 template <typename T>
118  std::ostream &stream = std::cout) {
119  if (data == nullptr || shape.empty()) {
120  return;
121  }
122 
123  size_t total_size = 1;
124  for (int dim : shape) {
125  total_size *= dim;
126  }
127 
128  std::function<void(size_t, size_t)> print_recursive = [&](size_t depth,
129  size_t offset) {
130  if (depth == shape.size() - 1) {
131  // stream << std::endl;
132  for (int i = 0; i < shape[depth]; ++i) {
133  // std::cout << (float)data[offset + i] << ",";
134  // 当为uint8类型时,数据为0时,无法打印数据
135  stream << (float)data[offset + i];
136  // stream << data[offset + i];
137  if (i != shape[depth] - 1) stream << ",";
138  }
139  stream << std::endl;
140  } else {
141  // stream << std::endl;
142  size_t stride = 1;
143  for (size_t i = depth + 1; i < shape.size(); ++i) {
144  stride *= shape[i];
145  }
146  for (int i = 0; i < shape[depth]; ++i) {
147  print_recursive(depth + 1, offset + i * stride);
148  if (i != shape[depth] - 1) stream << ",";
149  }
150  // stream << std::endl;
151  }
152  };
153 
154  print_recursive(0, 0);
155  stream << std::endl;
156 }
157 
158 extern NNDEPLOY_CC_API std::string getUniqueString();
159 
160 } // namespace base
161 } // namespace nndeploy
162 
163 #endif
#define NNDEPLOY_CC_API
api
Definition: macro.h:29
std::string vectorToString(std::vector< T > val)
Definition: string.h:32
std::vector< int > IntVector
Definition: common.h:379
std::vector< std::string > splitString(const std::string &str, const std::string &spstr)
bool isDigit(char c)
Inline implementation of isDigit(). Tests whether the given character is a decimal digit.
Definition: string.h:71
std::wstring stringToWstring(const std::string &str)
bool isBlank(char c)
Inline implementation of isBlank(). Tests whether the given character is a space or tab character.
Definition: string.h:63
std::string toString(T value)
Definition: string.h:25
std::string getUniqueString()
bool isDigitchars(char c)
Tests whether the given character is a valid letter in the string representation of a floating-point ...
Definition: string.h:93
std::string ucharToString(const unsigned char *buffer, int length)
bool isNumeric(const std::string &str)
bool isAlpha(char c)
Inline implementation of isAlpha(). Tests whether the given character is an alphabet letter.
Definition: string.h:79
std::string wstringToString(const std::wstring &wstr)
bool isSpace(char c)
Inline implementation of isSpace(). Tests whether the given character is a whitespace letter.
Definition: string.h:53
void printData(T *data, size_t size, std::ostream &stream=std::cout)
Definition: string.h:101
base::Status shape(device::Tensor *input, std::shared_ptr< ir::ShapeParam > param, device::Tensor *output)