printf.h 3.8 KB
Newer Older
1
//  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Y
Yi Wang 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

// Compared with std::stringstream, there are primary purpose of
// string::Printf:
//
// 1. Type-safe printing, with why and how explained in
//    http://www.drdobbs.com/stringprintf-a-typesafe-printf-family-fo/184401999.
//    Implementation includes
//
//    https://github.com/c42f/tinyformat
//    boost::format
//    std::stringstream
//
//    std::stringstream is not convenient enough in many cases.  For example:
//
//      std::cout << std::setprecision(2) << std::fixed << 1.23456 << "\n";
//
//    boost::format is the most convenient one.  We can have
//
//      std::cout << format("%2% %1%") % 36 % 77;
//
//    or
//
//      format fmter("%2% %1%");
//      fmter % 36; fmter % 77;
//      std::cout << fmter.c_str();
//
//    But the overloading of % might be overkilling and it would be
//    more efficient if it can write to std::cout directly.
//
//    tinyformat has an interface compatible with the C-printf style,
//    and it can writes to a stream or returns a std::string:
//
//      std::cout << tfm::printf(
//                  "%s, %s %d, %.2d:%.2d\n",
//                  weekday, month, day, hour, min);
//
//    or
//
//      tfm::format(std::cout,
//                  "%s, %s %d, %.2d:%.2d\n",
//                  weekday, month, day, hour, min);
//
// 2. High-performance -- most printed strings are not too long and
//    doens't need dynamic memory allocation.  Many StringPrintf
//    implementations doesn't enforce type-safe, but are
//    high-performance, including
//
//    https://developers.google.com/optimization/reference/base/stringprintf/
//    https://github.com/adobe/chromium/blob/master/base/stringprintf.h
//    https://github.com/google/protobuf/blob/master/src/google/protobuf/stubs/stringprintf.h
//
// According to
// https://github.com/c42f/tinyformat#compile-time-and-code-bloat,
// boost::format runs too slow and results in large executable binary
// files.  So here we port tinyformat.

#pragma once

#include <iostream>
#include <sstream>
74
#include <string>
W
Wu Yi 已提交
75
#include <vector>
76

77
#include "tinyformat/tinyformat.h"  // https://github.com/c42f/tinyformat
Y
Yi Wang 已提交
78 79 80 81 82 83

namespace paddle {
namespace string {

template <typename... Args>
void Fprintf(std::ostream& out, const char* fmt, const Args&... args) {
84
  tinyformat::vformat(out, fmt, tinyformat::makeFormatList(args...));
Y
Yi Wang 已提交
85 86
}

S
sneaxiy 已提交
87 88
inline std::string Sprintf() { return ""; }

M
minqiyang 已提交
89 90 91
template <typename... Args>
std::string Sprintf(const Args&... args) {
  std::ostringstream oss;
M
minqiyang 已提交
92
  Fprintf(oss, "%s", args...);
M
minqiyang 已提交
93 94 95
  return oss.str();
}

Y
Yi Wang 已提交
96
template <typename... Args>
M
minqiyang 已提交
97
std::string Sprintf(const char* fmt, const Args&... args) {
Y
Yi Wang 已提交
98
  std::ostringstream oss;
99
  Fprintf(oss, fmt, args...);
Y
Yi Wang 已提交
100 101 102 103
  return oss.str();
}

template <typename... Args>
104 105
void Printf(const char* fmt, const Args&... args) {
  Fprintf(std::cout, fmt, args...);
Y
Yi Wang 已提交
106 107
}

W
Wu Yi 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
template <typename T>
std::string HumanReadableSize(T size) {
  size_t i = 0;
  double f_size = static_cast<double>(size);
  double orig = f_size;
  const std::vector<std::string> units(
      {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"});
  while (f_size > 1024) {
    f_size /= 1024;
    i++;
  }
  if (i >= units.size()) {
    return Sprintf("%fB", orig);
  }
  return Sprintf("%f%s", f_size, units[i]);
}

Y
Yi Wang 已提交
125 126
}  // namespace string
}  // namespace paddle