• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tensorflow/lite/toco/toco_types.h"
23 #include "tensorflow/core/lib/strings/stringprintf.h"
24 
25 namespace toco {
26 namespace port {
27 
28 /// Identity (default case)
29 template <class T>
IdentityOrConvertStringToRaw(T foo)30 T IdentityOrConvertStringToRaw(T foo) {
31   return foo;
32 }
33 
34 // Overloaded case where we return std::string.
IdentityOrConvertStringToRaw(const std::string & foo)35 inline const char* IdentityOrConvertStringToRaw(const std::string& foo) {
36   return foo.c_str();
37 }
38 
39 // Delegate to TensorFlow Appendf function until absl has an equivalent.
40 template <typename... Args>
AppendFHelper(std::string * destination,const char * fmt,Args &&...args)41 inline void AppendFHelper(std::string* destination, const char* fmt,
42                           Args&&... args) {
43   tensorflow::strings::Appendf(destination, fmt, args...);
44 }
45 
46 // Specialization for no argument format string (avoid security bug).
AppendFHelper(std::string * destination,const char * fmt)47 inline void AppendFHelper(std::string* destination, const char* fmt) {
48   tensorflow::strings::Appendf(destination, "%s", fmt);
49 }
50 
51 // Append formatted string (with format fmt and args args) to the string
52 // pointed to by destination. fmt follows C printf semantics.
53 // One departure is that %s can be driven by a std::string or string.
54 template <typename... Args>
AppendF(std::string * destination,const char * fmt,Args &&...args)55 inline void AppendF(std::string* destination, const char* fmt, Args&&... args) {
56   AppendFHelper(destination, fmt, IdentityOrConvertStringToRaw(args)...);
57 }
58 
59 // Return formatted string (with format fmt and args args). fmt follows C printf
60 // semantics. One departure is that %s can be driven by a std::string or string.
61 template <typename... Args>
StringF(const char * fmt,Args &&...args)62 inline std::string StringF(const char* fmt, Args&&... args) {
63   std::string result;
64   AppendFHelper(&result, fmt, IdentityOrConvertStringToRaw(args)...);
65   return result;
66 }
67 
68 }  // namespace port
69 }  // namespace toco
70 
71 #endif  // TENSORFLOW_LITE_TOCO_FORMAT_PORT_H_
72