1 | /* Copyright 2017 The TensorFlow Authors. 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 | ==============================================================================*/ |
15 | // This file is used to provide equivalents of internal absl::FormatF |
16 | // and absl::StrAppendFormat. Unfortunately, type safety is not as good as a |
17 | // a full C++ example. |
18 | // TODO(aselle): When absl adds support for StrFormat, use that instead. |
19 | #ifndef TENSORFLOW_LITE_TOCO_FORMAT_PORT_H_ |
20 | #define TENSORFLOW_LITE_TOCO_FORMAT_PORT_H_ |
21 | |
22 | #include <string> |
23 | |
24 | #include "tensorflow/core/lib/strings/stringprintf.h" |
25 | #include "tensorflow/lite/toco/toco_types.h" |
26 | |
27 | namespace toco { |
28 | namespace port { |
29 | |
30 | /// Identity (default case) |
31 | template <class T> |
32 | T IdentityOrConvertStringToRaw(T foo) { |
33 | return foo; |
34 | } |
35 | |
36 | // Overloaded case where we return std::string. |
37 | inline const char* IdentityOrConvertStringToRaw(const std::string& foo) { |
38 | return foo.c_str(); |
39 | } |
40 | |
41 | // Delegate to TensorFlow Appendf function until absl has an equivalent. |
42 | template <typename... Args> |
43 | inline void AppendFHelper(std::string* destination, const char* fmt, |
44 | Args&&... args) { |
45 | tensorflow::strings::Appendf(destination, fmt, args...); |
46 | } |
47 | |
48 | // Specialization for no argument format string (avoid security bug). |
49 | inline void AppendFHelper(std::string* destination, const char* fmt) { |
50 | tensorflow::strings::Appendf(destination, "%s" , fmt); |
51 | } |
52 | |
53 | // Append formatted string (with format fmt and args args) to the string |
54 | // pointed to by destination. fmt follows C printf semantics. |
55 | // One departure is that %s can be driven by a std::string or string. |
56 | template <typename... Args> |
57 | inline void AppendF(std::string* destination, const char* fmt, Args&&... args) { |
58 | AppendFHelper(destination, fmt, IdentityOrConvertStringToRaw(args)...); |
59 | } |
60 | |
61 | // Return formatted string (with format fmt and args args). fmt follows C printf |
62 | // semantics. One departure is that %s can be driven by a std::string or string. |
63 | template <typename... Args> |
64 | inline std::string StringF(const char* fmt, Args&&... args) { |
65 | std::string result; |
66 | AppendFHelper(&result, fmt, IdentityOrConvertStringToRaw(args)...); |
67 | return result; |
68 | } |
69 | |
70 | } // namespace port |
71 | } // namespace toco |
72 | |
73 | #endif // TENSORFLOW_LITE_TOCO_FORMAT_PORT_H_ |
74 | |