string.h 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 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
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#pragma once
#include <stdarg.h>  // For va_start, etc.
#include <algorithm>
#include <cstring>
#include <memory>  // For std::unique_ptr
#include <sstream>
#include <string>
#include <vector>

namespace paddle {
namespace lite {

static std::string string_format(const std::string fmt_str, ...) {
  /* Reserve two times as much as the length of the fmt_str */
  int final_n, n = (static_cast<int>(fmt_str.size())) * 2;
  std::unique_ptr<char[]> formatted;
  va_list ap;
  while (1) {
    formatted.reset(
        new char[n]); /* Wrap the plain char array into the unique_ptr */
    std::strcpy(&formatted[0], fmt_str.c_str());  // NOLINT
    va_start(ap, fmt_str);
    final_n = vsnprintf(&formatted[0], n, fmt_str.c_str(), ap);
    va_end(ap);
    if (final_n < 0 || final_n >= n)
      n += abs(final_n - n + 1);
    else
      break;
  }
  return std::string(formatted.get());
}

template <typename T>
static std::string to_string_with_precision(const T& v, const int n = 6) {
  std::stringstream ss;
  ss.precision(n);
  ss << std::fixed << v;
  return ss.str();
}

static std::string Join(const std::vector<std::string>& vec,
                        const std::string& delim) {
  if (vec.empty()) return "";

  std::stringstream ss;
  for (size_t i = 0; i < vec.size() - 1; i++) ss << vec[i] << delim;
  if (!vec.empty()) {
    ss << vec.back();
  }
Y
Yan Chunwei 已提交
64

65 66 67 68 69 70 71 72 73 74 75 76
  return ss.str();
}

static std::string Repr(const std::string& x) { return "\"" + x + "\""; }

static std::string Repr(const std::vector<std::string>& v) {
  std::vector<std::string> tmp;
  std::transform(v.begin(), v.end(), std::back_inserter(tmp),
                 [](const std::string& x) { return Repr(x); });
  return "{" + Join(tmp, ",") + "}";
}

C
Chunwei 已提交
77 78 79 80 81 82 83 84 85 86
static std::vector<std::string> Split(const std::string& s, char delim) {
  std::stringstream ss(s);
  std::string line;
  std::vector<std::string> res;
  while (std::getline(ss, line, delim)) {
    res.push_back(line);
  }
  return res;
}

87 88
}  // namespace lite
}  // namespace paddle