• 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 #if defined(PLATFORM_GOOGLE) && defined(HAS_GLOBAL_STRING)
40 // Overloaded case where we return string.
IdentityOrConvertStringToRaw(const string & foo)41 inline const char* IdentityOrConvertStringToRaw(const string& foo) {
42   return foo.c_str();
43 }
44 #endif  // PLATFORM_GOOGLE
45 // Delegate to TensorFlow Appendf function until absl has an equivalent.
46 template <typename... Args>
AppendFHelper(string * destination,const char * fmt,Args &&...args)47 inline void AppendFHelper(string* destination, const char* fmt,
48                           Args&&... args) {
49   tensorflow::strings::Appendf(destination, fmt, args...);
50 }
51 
52 // Specialization for no argument format string (avoid security bug).
AppendFHelper(string * destination,const char * fmt)53 inline void AppendFHelper(string* destination, const char* fmt) {
54   tensorflow::strings::Appendf(destination, "%s", fmt);
55 }
56 
57 // Append formatted string (with format fmt and args args) to the string
58 // pointed to by destination. fmt follows C printf semantics.
59 // One departure is that %s can be driven by a std::string or string.
60 template <typename... Args>
AppendF(string * destination,const char * fmt,Args &&...args)61 inline void AppendF(string* destination, const char* fmt, Args&&... args) {
62   AppendFHelper(destination, fmt, IdentityOrConvertStringToRaw(args)...);
63 }
64 
65 // Return formatted string (with format fmt and args args). fmt follows C printf
66 // semantics. One departure is that %s can be driven by a std::string or string.
67 template <typename... Args>
StringF(const char * fmt,Args &&...args)68 inline string StringF(const char* fmt, Args&&... args) {
69   string result;
70   AppendFHelper(&result, fmt, IdentityOrConvertStringToRaw(args)...);
71   return result;
72 }
73 
74 }  // namespace port
75 }  // namespace toco
76 
77 #endif  // TENSORFLOW_LITE_TOCO_FORMAT_PORT_H_
78