MYNT EYE D SDK  1.7.1
http://www.myntai.com/mynteye/depth
strings.h
1 // Copyright 2018 Slightech Co., Ltd. All rights reserved.
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 #ifndef MYNTEYE_UTIL_STRINGS_H_
15 #define MYNTEYE_UTIL_STRINGS_H_
16 #pragma once
17 
18 #include <cstdio>
19 #include <memory>
20 #include <sstream>
21 #include <stdexcept>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include "mynteyed/stubs/global.h"
27 
28 MYNTEYE_BEGIN_NAMESPACE
29 
31 class MYNTEYE_API strings_error : public std::runtime_error {
32  public:
33  explicit strings_error(const std::string &what_arg) noexcept
34  : std::runtime_error(std::move(what_arg)) {}
35  explicit strings_error(const char *what_arg) noexcept
36  : std::runtime_error(std::move(what_arg)) {}
37 };
38 
39 namespace strings {
40 
41 // http://stackoverflow.com/questions/22774009/android-ndk-stdto-string-support
42 template <typename T>
43 std::string to_string(const T& value) {
44  std::ostringstream os;
45  os << value;
46  return os.str();
47 }
48 
49 template <typename T>
50 T Argument(T value) noexcept {
51  return value;
52 }
53 
54 template <typename T>
55 T const* Argument(std::basic_string<T> const& value) noexcept {
56  return value.c_str();
57 }
58 
59 inline const char* Argument(bool value) noexcept {
60  return value ? "true" : "false";
61 }
62 
63 // http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
64 // Note: could not pass C++ string
65 template <typename... Args>
66 std::string format_cstring(const std::string& format, const Args&... args) {
67  // Extra space for '\0'
68  std::size_t size = snprintf(nullptr, 0, format.c_str(), args...) + 1;
69  std::unique_ptr<char[]> buf(new char[size]);
70  snprintf(buf.get(), size, format.c_str(), args...);
71  // We don't want the '\0' inside
72  return std::string(buf.get(), buf.get() + size - 1);
73 }
74 
75 template <>
76 inline std::string format_cstring(const std::string& format) {
77  return format;
78 }
79 
80 // https://msdn.microsoft.com/en-us/magazine/dn913181.aspx
81 template <typename... Args>
82 std::string format_string(const std::string& format, const Args&... args) {
83  return format_cstring(format, Argument(args)...);
84 }
85 
86 template <>
87 inline std::string format_string(const std::string& format) {
88  return format;
89 }
90 
91 MYNTEYE_API
92 int hex2int(const std::string &text);
93 
94 MYNTEYE_API
95 bool starts_with(const std::string &text, const std::string &prefix);
96 
97 MYNTEYE_API
98 bool ends_with(const std::string &text, const std::string &suffix);
99 
100 MYNTEYE_API
101 std::vector<std::string> split(
102  const std::string &text, const std::string &delimiters);
103 
104 MYNTEYE_API void ltrim(std::string &s); // NOLINT
105 MYNTEYE_API void rtrim(std::string &s); // NOLINT
106 MYNTEYE_API void trim(std::string &s); // NOLINT
107 
108 MYNTEYE_API
109 std::string trim_copy(const std::string &text);
110 
111 } // namespace strings
112 
113 MYNTEYE_END_NAMESPACE
114 
115 #endif // MYNTEYE_UTIL_STRINGS_H_
The strings error.
Definition: strings.h:31