Files
ortools-clone/src/base/join.h
2014-01-08 12:01:58 +00:00

91 lines
3.6 KiB
C++

// Copyright 2010-2013 Google
// 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.
#ifndef OR_TOOLS_BASE_JOIN_H_
#define OR_TOOLS_BASE_JOIN_H_
#include <string>
#include <sstream>
#include "base/basictypes.h"
#include "base/integral_types.h"
#include "base/stringpiece.h"
namespace operations_research {
// A buffer size large enough for all FastToBuffer functions.
const int kFastToBufferSize = 32;
// Writes output to the beginning of the given buffer. Returns a pointer to the
// end of the std::string (i.e. to the NUL char). Buffer must be at least 12 bytes.
// Not actually fast, but maybe someday!
template <class T>
char* NumToBuffer(T i, char* buffer) {
std::stringstream ss;
ss << i;
const std::string s = ss.str();
strcpy(buffer, s.c_str()); // NOLINT
return buffer + s.size();
}
struct AlphaNum {
StringPiece piece;
char digits[kFastToBufferSize];
// No bool ctor -- bools convert to an integral type.
// A bool ctor would also convert incoming pointers (bletch).
AlphaNum(int32 i32) // NOLINT(runtime/explicit)
: piece(digits, NumToBuffer(i32, digits) - &digits[0]) {}
AlphaNum(uint32 u32) // NOLINT(runtime/explicit)
: piece(digits, NumToBuffer(u32, digits) - &digits[0]) {}
AlphaNum(int64 i64) // NOLINT(runtime/explicit)
: piece(digits, NumToBuffer(i64, digits) - &digits[0]) {}
AlphaNum(uint64 u64) // NOLINT(runtime/explicit)
: piece(digits, NumToBuffer(u64, digits) - &digits[0]) {}
AlphaNum(float f) // NOLINT(runtime/explicit)
: piece(digits, strlen(NumToBuffer(f, digits))) {}
AlphaNum(double f) // NOLINT(runtime/explicit)
: piece(digits, strlen(NumToBuffer(f, digits))) {}
AlphaNum(const char* c_str) : piece(c_str) {} // NOLINT(runtime/explicit)
AlphaNum(const StringPiece& pc) : piece(pc) {} // NOLINT(runtime/explicit)
AlphaNum(const std::string& s) : piece(s) {} // NOLINT(runtime/explicit)
StringPiece::size_type size() const { return piece.size(); }
const char* data() const { return piece.data(); }
private:
// Use ":" not ':'
AlphaNum(char c); // NOLINT(runtime/explicit)
};
extern AlphaNum gEmptyAlphaNum;
std::string StrCat(const AlphaNum& a);
std::string StrCat(const AlphaNum& a, const AlphaNum& b);
std::string StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c);
std::string StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c,
const AlphaNum& d);
std::string StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c,
const AlphaNum& d, const AlphaNum& e);
std::string StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c,
const AlphaNum& d, const AlphaNum& e, const AlphaNum& f);
std::string StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c,
const AlphaNum& d, const AlphaNum& e, const AlphaNum& f,
const AlphaNum& g);
std::string StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c,
const AlphaNum& d, const AlphaNum& e, const AlphaNum& f,
const AlphaNum& g, const AlphaNum& h);
} // namespace operations_research
#endif // OR_TOOLS_BASE_JOIN_H_