substitute.h 8.3 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

W
wangguibao 已提交
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
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Author: kenton@google.com (Kenton Varda)
// from google3/strings/substitute.h

#include <google/protobuf/stubs/common.h>
W
wangguibao 已提交
49
#include <string>
W
wangguibao 已提交
50
// hmmm...
W
wangguibao 已提交
51
// #include <google/protobuf/stubs/strutil.h>
W
wangguibao 已提交
52 53
#include "plugin/strutil.h"

W
wangguibao 已提交
54
#pragma once
W
wangguibao 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
namespace google {
namespace protobuf {
namespace strings {

// ----------------------------------------------------------------------
// strings::Substitute()
// strings::SubstituteAndAppend()
//   Kind of like StringPrintf, but different.
//
//   Example:
//     string GetMessage(string first_name, string last_name, int age) {
//       return strings::Substitute("My name is $0 $1 and I am $2 years old.",
//                                  first_name, last_name, age);
//     }
//
//   Differences from StringPrintf:
//   * The format string does not identify the types of arguments.
//     Instead, the magic of C++ deals with this for us.  See below
//     for a list of accepted types.
//   * Substitutions in the format string are identified by a '$'
//     followed by a digit.  So, you can use arguments out-of-order and
//     use the same argument multiple times.
//   * It's much faster than StringPrintf.
//
//   Supported types:
//   * Strings (const char*, const string&)
//     * Note that this means you do not have to add .c_str() to all of
//       your strings.  In fact, you shouldn't; it will be slower.
//   * int32, int64, uint32, uint64:  Formatted using SimpleItoa().
//   * float, double:  Formatted using SimpleFtoa() and SimpleDtoa().
//   * bool:  Printed as "true" or "false".
//
//   SubstituteAndAppend() is like Substitute() but appends the result to
//   *output.  Example:
//
//     string str;
//     strings::SubstituteAndAppend(&str,
//                                  "My name is $0 $1 and I am $2 years old.",
//                                  first_name, last_name, age);
//
//   Substitute() is significantly faster than StringPrintf().  For very
//   large strings, it may be orders of magnitude faster.
// ----------------------------------------------------------------------

namespace internal {  // Implementation details.

class SubstituteArg {
 public:
W
wangguibao 已提交
103 104 105 106
  explicit inline SubstituteArg(const char* value)
      : text_(value), size_(strlen(text_)) {}
  explicit inline SubstituteArg(const string& value)
      : text_(value.data()), size_(value.size()) {}
W
wangguibao 已提交
107 108

  // Indicates that no argument was given.
W
wangguibao 已提交
109
  inline SubstituteArg() : text_(NULL), size_(-1) {}
W
wangguibao 已提交
110 111 112 113 114 115 116

  // Primitives
  // We don't overload for signed and unsigned char because if people are
  // explicitly declaring their chars as signed or unsigned then they are
  // probably actually using them as 8-bit integers and would probably
  // prefer an integer representation.  But, we don't really know.  So, we
  // make the caller decide what to do.
W
wangguibao 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  explicit inline SubstituteArg(char value) : text_(scratch_), size_(1) {
    scratch_[0] = value;
  }
  explicit inline SubstituteArg(short value)  // NOLINT
      : text_(FastInt32ToBuffer(value, scratch_)),
        size_(strlen(text_)) {}
  explicit inline SubstituteArg(unsigned short value)  // NOLINT
      : text_(FastUInt32ToBuffer(value, scratch_)),
        size_(strlen(text_)) {}
  explicit inline SubstituteArg(int value)
      : text_(FastInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
  explicit inline SubstituteArg(unsigned int value)
      : text_(FastUInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
  explicit inline SubstituteArg(long value)  // NOLINT
      : text_(FastLongToBuffer(value, scratch_)),
        size_(strlen(text_)) {}
  explicit inline SubstituteArg(unsigned long value)  // NOLINT
      : text_(FastULongToBuffer(value, scratch_)),
        size_(strlen(text_)) {}
  explicit inline SubstituteArg(long long value)  // NOLINT
      : text_(FastInt64ToBuffer(value, scratch_)),
        size_(strlen(text_)) {}
  explicit inline SubstituteArg(unsigned long long value)  // NOLINT
      : text_(FastUInt64ToBuffer(value, scratch_)),
        size_(strlen(text_)) {}
  explicit inline SubstituteArg(float value)
      : text_(FloatToBuffer(value, scratch_)), size_(strlen(text_)) {}
  explicit inline SubstituteArg(double value)
      : text_(DoubleToBuffer(value, scratch_)), size_(strlen(text_)) {}
  explicit inline SubstituteArg(bool value)
      : text_(value ? "true" : "false"), size_(strlen(text_)) {}
W
wangguibao 已提交
148 149 150 151 152 153 154 155 156 157 158 159

  inline const char* data() const { return text_; }
  inline int size() const { return size_; }

 private:
  const char* text_;
  int size_;
  char scratch_[kFastToBufferSize];
};

}  // namespace internal

W
wangguibao 已提交
160 161 162 163 164 165 166 167 168 169 170 171
LIBPROTOBUF_EXPORT string
Substitute(const char* format,
           const internal::SubstituteArg& arg0 = internal::SubstituteArg(),
           const internal::SubstituteArg& arg1 = internal::SubstituteArg(),
           const internal::SubstituteArg& arg2 = internal::SubstituteArg(),
           const internal::SubstituteArg& arg3 = internal::SubstituteArg(),
           const internal::SubstituteArg& arg4 = internal::SubstituteArg(),
           const internal::SubstituteArg& arg5 = internal::SubstituteArg(),
           const internal::SubstituteArg& arg6 = internal::SubstituteArg(),
           const internal::SubstituteArg& arg7 = internal::SubstituteArg(),
           const internal::SubstituteArg& arg8 = internal::SubstituteArg(),
           const internal::SubstituteArg& arg9 = internal::SubstituteArg());
W
wangguibao 已提交
172 173

LIBPROTOBUF_EXPORT void SubstituteAndAppend(
W
wangguibao 已提交
174 175 176 177 178 179 180 181 182 183 184 185
    string* output,
    const char* format,
    const internal::SubstituteArg& arg0 = internal::SubstituteArg(),
    const internal::SubstituteArg& arg1 = internal::SubstituteArg(),
    const internal::SubstituteArg& arg2 = internal::SubstituteArg(),
    const internal::SubstituteArg& arg3 = internal::SubstituteArg(),
    const internal::SubstituteArg& arg4 = internal::SubstituteArg(),
    const internal::SubstituteArg& arg5 = internal::SubstituteArg(),
    const internal::SubstituteArg& arg6 = internal::SubstituteArg(),
    const internal::SubstituteArg& arg7 = internal::SubstituteArg(),
    const internal::SubstituteArg& arg8 = internal::SubstituteArg(),
    const internal::SubstituteArg& arg9 = internal::SubstituteArg());
W
wangguibao 已提交
186 187 188 189

}  // namespace strings
}  // namespace protobuf
}  // namespace google