StringPiece -> string_view
This commit is contained in:
@@ -10,7 +10,7 @@ BASE_DEPS = \
|
||||
$(SRC_DIR)/ortools/base/logging.h \
|
||||
$(SRC_DIR)/ortools/base/macros.h \
|
||||
$(SRC_DIR)/ortools/base/status.h \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h \
|
||||
$(SRC_DIR)/ortools/base/string_view.h \
|
||||
$(SRC_DIR)/ortools/base/thorough_hash.h \
|
||||
$(SRC_DIR)/ortools/base/time_support.h
|
||||
|
||||
@@ -25,7 +25,7 @@ BASE_LIB_OBJS = \
|
||||
$(OBJ_DIR)/base/random.$O \
|
||||
$(OBJ_DIR)/base/recordio.$O \
|
||||
$(OBJ_DIR)/base/split.$O \
|
||||
$(OBJ_DIR)/base/stringpiece.$O \
|
||||
$(OBJ_DIR)/base/string_view.$O \
|
||||
$(OBJ_DIR)/base/stringprintf.$O \
|
||||
$(OBJ_DIR)/base/sysinfo.$O \
|
||||
$(OBJ_DIR)/base/threadpool.$O \
|
||||
@@ -76,7 +76,7 @@ $(SRC_DIR)/ortools/base/jniutil.h: \
|
||||
$(SRC_DIR)/ortools/base/join.h: \
|
||||
$(SRC_DIR)/ortools/base/basictypes.h \
|
||||
$(SRC_DIR)/ortools/base/integral_types.h \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h
|
||||
$(SRC_DIR)/ortools/base/string_view.h
|
||||
|
||||
$(SRC_DIR)/ortools/base/logging.h: \
|
||||
$(SRC_DIR)/ortools/base/integral_types.h \
|
||||
@@ -114,7 +114,7 @@ $(SRC_DIR)/ortools/base/sparsetable.h: \
|
||||
$(SRC_DIR)/ortools/base/split.h: \
|
||||
$(SRC_DIR)/ortools/base/integral_types.h \
|
||||
$(SRC_DIR)/ortools/base/logging.h \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h
|
||||
$(SRC_DIR)/ortools/base/string_view.h
|
||||
|
||||
$(SRC_DIR)/ortools/base/status.h: \
|
||||
$(SRC_DIR)/ortools/base/join.h \
|
||||
@@ -124,10 +124,10 @@ $(SRC_DIR)/ortools/base/statusor.h: \
|
||||
$(SRC_DIR)/ortools/base/status.h
|
||||
|
||||
$(SRC_DIR)/ortools/base/stringpiece_utils.h: \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h
|
||||
$(SRC_DIR)/ortools/base/string_view.h
|
||||
|
||||
$(SRC_DIR)/ortools/base/stringprintf.h: \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h
|
||||
$(SRC_DIR)/ortools/base/string_view.h
|
||||
|
||||
$(SRC_DIR)/ortools/base/strongly_connected_components.h: \
|
||||
$(SRC_DIR)/ortools/base/logging.h \
|
||||
@@ -137,7 +137,7 @@ $(SRC_DIR)/ortools/base/strtoint.h: \
|
||||
$(SRC_DIR)/ortools/base/basictypes.h
|
||||
|
||||
$(SRC_DIR)/ortools/base/strutil.h: \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h
|
||||
$(SRC_DIR)/ortools/base/string_view.h
|
||||
|
||||
$(SRC_DIR)/ortools/base/synchronization.h: \
|
||||
$(SRC_DIR)/ortools/base/logging.h
|
||||
@@ -186,7 +186,7 @@ $(OBJ_DIR)/base/join.$O: \
|
||||
$(SRC_DIR)/ortools/base/join.cc \
|
||||
$(SRC_DIR)/ortools/base/basictypes.h \
|
||||
$(SRC_DIR)/ortools/base/join.h \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h \
|
||||
$(SRC_DIR)/ortools/base/string_view.h \
|
||||
$(SRC_DIR)/ortools/base/stringprintf.h
|
||||
$(CCC) $(CFLAGS) -c $(SRC_DIR)$Sortools$Sbase$Sjoin.cc $(OBJ_OUT)$(OBJ_DIR)$Sbase$Sjoin.$O
|
||||
|
||||
@@ -218,10 +218,10 @@ $(OBJ_DIR)/base/split.$O: \
|
||||
$(SRC_DIR)/ortools/base/split.h
|
||||
$(CCC) $(CFLAGS) -c $(SRC_DIR)$Sortools$Sbase$Ssplit.cc $(OBJ_OUT)$(OBJ_DIR)$Sbase$Ssplit.$O
|
||||
|
||||
$(OBJ_DIR)/base/stringpiece.$O: \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.cc \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h
|
||||
$(CCC) $(CFLAGS) -c $(SRC_DIR)$Sortools$Sbase$Sstringpiece.cc $(OBJ_OUT)$(OBJ_DIR)$Sbase$Sstringpiece.$O
|
||||
$(OBJ_DIR)/base/string_view.$O: \
|
||||
$(SRC_DIR)/ortools/base/string_view.cc \
|
||||
$(SRC_DIR)/ortools/base/string_view.h
|
||||
$(CCC) $(CFLAGS) -c $(SRC_DIR)$Sortools$Sbase$Sstring_view.cc $(OBJ_OUT)$(OBJ_DIR)$Sbase$Sstring_view.$O
|
||||
|
||||
$(OBJ_DIR)/base/stringprintf.$O: \
|
||||
$(SRC_DIR)/ortools/base/stringprintf.cc \
|
||||
@@ -230,7 +230,7 @@ $(OBJ_DIR)/base/stringprintf.$O: \
|
||||
|
||||
$(OBJ_DIR)/base/sysinfo.$O: \
|
||||
$(SRC_DIR)/ortools/base/sysinfo.cc \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h \
|
||||
$(SRC_DIR)/ortools/base/string_view.h \
|
||||
$(SRC_DIR)/ortools/base/sysinfo.h
|
||||
$(CCC) $(CFLAGS) -c $(SRC_DIR)$Sortools$Sbase$Ssysinfo.cc $(OBJ_OUT)$(OBJ_DIR)$Sbase$Ssysinfo.$O
|
||||
|
||||
@@ -264,7 +264,7 @@ UTIL_DEPS = \
|
||||
$(SRC_DIR)/ortools/base/logging.h \
|
||||
$(SRC_DIR)/ortools/base/macros.h \
|
||||
$(SRC_DIR)/ortools/base/status.h \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h \
|
||||
$(SRC_DIR)/ortools/base/string_view.h \
|
||||
$(SRC_DIR)/ortools/base/thorough_hash.h \
|
||||
$(SRC_DIR)/ortools/base/time_support.h
|
||||
|
||||
@@ -524,7 +524,7 @@ LP_DATA_DEPS = \
|
||||
$(SRC_DIR)/ortools/base/logging.h \
|
||||
$(SRC_DIR)/ortools/base/macros.h \
|
||||
$(SRC_DIR)/ortools/base/status.h \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h \
|
||||
$(SRC_DIR)/ortools/base/string_view.h \
|
||||
$(SRC_DIR)/ortools/base/thorough_hash.h \
|
||||
$(SRC_DIR)/ortools/base/time_support.h \
|
||||
$(SRC_DIR)/ortools/algorithms/dynamic_partition.h \
|
||||
@@ -748,7 +748,7 @@ GLOP_DEPS = \
|
||||
$(SRC_DIR)/ortools/base/logging.h \
|
||||
$(SRC_DIR)/ortools/base/macros.h \
|
||||
$(SRC_DIR)/ortools/base/status.h \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h \
|
||||
$(SRC_DIR)/ortools/base/string_view.h \
|
||||
$(SRC_DIR)/ortools/base/thorough_hash.h \
|
||||
$(SRC_DIR)/ortools/base/time_support.h \
|
||||
$(SRC_DIR)/ortools/lp_data/lp_data.h \
|
||||
@@ -1065,7 +1065,7 @@ GRAPH_DEPS = \
|
||||
$(SRC_DIR)/ortools/base/logging.h \
|
||||
$(SRC_DIR)/ortools/base/macros.h \
|
||||
$(SRC_DIR)/ortools/base/status.h \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h \
|
||||
$(SRC_DIR)/ortools/base/string_view.h \
|
||||
$(SRC_DIR)/ortools/base/thorough_hash.h \
|
||||
$(SRC_DIR)/ortools/base/time_support.h \
|
||||
$(SRC_DIR)/ortools/util/bitset.h \
|
||||
@@ -1288,7 +1288,7 @@ ALGORITHMS_DEPS = \
|
||||
$(SRC_DIR)/ortools/base/logging.h \
|
||||
$(SRC_DIR)/ortools/base/macros.h \
|
||||
$(SRC_DIR)/ortools/base/status.h \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h \
|
||||
$(SRC_DIR)/ortools/base/string_view.h \
|
||||
$(SRC_DIR)/ortools/base/thorough_hash.h \
|
||||
$(SRC_DIR)/ortools/base/time_support.h \
|
||||
$(SRC_DIR)/ortools/util/bitset.h \
|
||||
@@ -1420,7 +1420,7 @@ SAT_DEPS = \
|
||||
$(SRC_DIR)/ortools/base/logging.h \
|
||||
$(SRC_DIR)/ortools/base/macros.h \
|
||||
$(SRC_DIR)/ortools/base/status.h \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h \
|
||||
$(SRC_DIR)/ortools/base/string_view.h \
|
||||
$(SRC_DIR)/ortools/base/thorough_hash.h \
|
||||
$(SRC_DIR)/ortools/base/time_support.h \
|
||||
$(SRC_DIR)/ortools/util/bitset.h \
|
||||
@@ -2010,7 +2010,7 @@ BOP_DEPS = \
|
||||
$(SRC_DIR)/ortools/base/logging.h \
|
||||
$(SRC_DIR)/ortools/base/macros.h \
|
||||
$(SRC_DIR)/ortools/base/status.h \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h \
|
||||
$(SRC_DIR)/ortools/base/string_view.h \
|
||||
$(SRC_DIR)/ortools/base/thorough_hash.h \
|
||||
$(SRC_DIR)/ortools/base/time_support.h \
|
||||
$(SRC_DIR)/ortools/util/bitset.h \
|
||||
@@ -2318,7 +2318,7 @@ LP_DEPS = \
|
||||
$(SRC_DIR)/ortools/base/logging.h \
|
||||
$(SRC_DIR)/ortools/base/macros.h \
|
||||
$(SRC_DIR)/ortools/base/status.h \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h \
|
||||
$(SRC_DIR)/ortools/base/string_view.h \
|
||||
$(SRC_DIR)/ortools/base/thorough_hash.h \
|
||||
$(SRC_DIR)/ortools/base/time_support.h \
|
||||
$(SRC_DIR)/ortools/util/bitset.h \
|
||||
@@ -2563,7 +2563,7 @@ CP_DEPS = \
|
||||
$(SRC_DIR)/ortools/base/logging.h \
|
||||
$(SRC_DIR)/ortools/base/macros.h \
|
||||
$(SRC_DIR)/ortools/base/status.h \
|
||||
$(SRC_DIR)/ortools/base/stringpiece.h \
|
||||
$(SRC_DIR)/ortools/base/string_view.h \
|
||||
$(SRC_DIR)/ortools/base/thorough_hash.h \
|
||||
$(SRC_DIR)/ortools/base/time_support.h \
|
||||
$(SRC_DIR)/ortools/util/bitset.h \
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
#include "ortools/base/file.h"
|
||||
#include "ortools/base/logging.h"
|
||||
|
||||
File::File(FILE* const f_des, const std::string& name) : f_(f_des), name_(name) {}
|
||||
File::File(FILE* const f_des, const operations_research::string_view& name) : f_(f_des), name_(name) {}
|
||||
|
||||
bool File::Delete(const char* const name) { return remove(name) == 0; }
|
||||
|
||||
@@ -37,7 +37,7 @@ bool File::Exists(const char* const name) { return access(name, F_OK) == 0; }
|
||||
|
||||
size_t File::Size() {
|
||||
struct stat f_stat;
|
||||
stat(name_.c_str(), &f_stat);
|
||||
stat(name_.data(), &f_stat);
|
||||
return f_stat.st_size;
|
||||
}
|
||||
|
||||
@@ -128,17 +128,17 @@ bool File::WriteLine(const std::string& line) {
|
||||
return Write("\n", 1) == 1;
|
||||
}
|
||||
|
||||
std::string File::filename() const { return name_; }
|
||||
operations_research::string_view File::filename() const { return name_; }
|
||||
|
||||
bool File::Open() const { return f_ != NULL; }
|
||||
|
||||
void File::Init() {}
|
||||
|
||||
namespace file {
|
||||
util::Status Open(const std::string& filename, const std::string& mode,
|
||||
util::Status Open(const operations_research::string_view& filename, const operations_research::string_view& mode,
|
||||
File** f, int flags) {
|
||||
if (flags == Defaults()) {
|
||||
*f = File::Open(filename, mode.c_str());
|
||||
*f = File::Open(filename, mode.data());
|
||||
if (*f != nullptr) {
|
||||
return util::Status::OK;
|
||||
}
|
||||
@@ -147,7 +147,7 @@ util::Status Open(const std::string& filename, const std::string& mode,
|
||||
StrCat("Could not open '", filename, "'"));
|
||||
}
|
||||
|
||||
util::Status GetContents(const std::string& filename, std::string* output, int flags) {
|
||||
util::Status GetContents(const operations_research::string_view& filename, std::string* output, int flags) {
|
||||
if (flags == Defaults()) {
|
||||
File* file = File::Open(filename, "r");
|
||||
if (file != NULL) {
|
||||
@@ -169,16 +169,16 @@ util::Status WriteString(File* file, const std::string& contents, int flags) {
|
||||
StrCat("Could not write ", contents.size(), " bytes"));
|
||||
}
|
||||
|
||||
util::Status SetContents(const std::string& filename, const std::string& contents,
|
||||
util::Status SetContents(const operations_research::string_view& filename, const std::string& contents,
|
||||
int flags) {
|
||||
return WriteString(File::Open(filename, "w"), contents, flags);
|
||||
}
|
||||
|
||||
bool ReadFileToString(const std::string& file_name, std::string* output) {
|
||||
bool ReadFileToString(const operations_research::string_view& file_name, std::string* output) {
|
||||
return GetContents(file_name, output, file::Defaults()).ok();
|
||||
}
|
||||
|
||||
bool WriteStringToFile(const std::string& data, const std::string& file_name) {
|
||||
bool WriteStringToFile(const std::string& data, const operations_research::string_view& file_name) {
|
||||
return SetContents(file_name, data, file::Defaults()).ok();
|
||||
}
|
||||
|
||||
@@ -189,7 +189,7 @@ class NoOpErrorCollector : public google::protobuf::io::ErrorCollector {
|
||||
};
|
||||
} // namespace
|
||||
|
||||
bool ReadFileToProto(const std::string& file_name, google::protobuf::Message* proto) {
|
||||
bool ReadFileToProto(const operations_research::string_view& file_name, google::protobuf::Message* proto) {
|
||||
std::string str;
|
||||
if (!ReadFileToString(file_name, &str)) {
|
||||
LOG(INFO) << "Could not read " << file_name;
|
||||
@@ -217,34 +217,34 @@ bool ReadFileToProto(const std::string& file_name, google::protobuf::Message* pr
|
||||
return false;
|
||||
}
|
||||
|
||||
void ReadFileToProtoOrDie(const std::string& file_name, google::protobuf::Message* proto) {
|
||||
void ReadFileToProtoOrDie(const operations_research::string_view& file_name, google::protobuf::Message* proto) {
|
||||
CHECK(ReadFileToProto(file_name, proto)) << "file_name: " << file_name;
|
||||
}
|
||||
|
||||
bool WriteProtoToASCIIFile(const google::protobuf::Message& proto,
|
||||
const std::string& file_name) {
|
||||
const operations_research::string_view& file_name) {
|
||||
std::string proto_string;
|
||||
return google::protobuf::TextFormat::PrintToString(proto, &proto_string) &&
|
||||
WriteStringToFile(proto_string, file_name);
|
||||
}
|
||||
|
||||
void WriteProtoToASCIIFileOrDie(const google::protobuf::Message& proto,
|
||||
const std::string& file_name) {
|
||||
const operations_research::string_view& file_name) {
|
||||
CHECK(WriteProtoToASCIIFile(proto, file_name)) << "file_name: " << file_name;
|
||||
}
|
||||
|
||||
bool WriteProtoToFile(const google::protobuf::Message& proto, const std::string& file_name) {
|
||||
bool WriteProtoToFile(const google::protobuf::Message& proto, const operations_research::string_view& file_name) {
|
||||
std::string proto_string;
|
||||
return proto.AppendToString(&proto_string) &&
|
||||
WriteStringToFile(proto_string, file_name);
|
||||
}
|
||||
|
||||
void WriteProtoToFileOrDie(const google::protobuf::Message& proto,
|
||||
const std::string& file_name) {
|
||||
const operations_research::string_view& file_name) {
|
||||
CHECK(WriteProtoToFile(proto, file_name)) << "file_name: " << file_name;
|
||||
}
|
||||
|
||||
util::Status SetTextProto(const std::string& filename, const google::protobuf::Message& proto,
|
||||
util::Status SetTextProto(const operations_research::string_view& filename, const google::protobuf::Message& proto,
|
||||
int flags) {
|
||||
if (flags == Defaults()) {
|
||||
if (WriteProtoToASCIIFile(proto, filename)) return util::Status::OK;
|
||||
@@ -253,7 +253,7 @@ util::Status SetTextProto(const std::string& filename, const google::protobuf::M
|
||||
StrCat("Could not write proto to '", filename, "'."));
|
||||
}
|
||||
|
||||
util::Status SetBinaryProto(const std::string& filename,
|
||||
util::Status SetBinaryProto(const operations_research::string_view& filename,
|
||||
const google::protobuf::Message& proto, int flags) {
|
||||
if (flags == Defaults()) {
|
||||
if (WriteProtoToFile(proto, filename)) return util::Status::OK;
|
||||
@@ -262,9 +262,9 @@ util::Status SetBinaryProto(const std::string& filename,
|
||||
StrCat("Could not write proto to '", filename, "'."));
|
||||
}
|
||||
|
||||
util::Status Delete(const std::string& path, int flags) {
|
||||
util::Status Delete(const operations_research::string_view& path, int flags) {
|
||||
if (flags == Defaults()) {
|
||||
if (remove(path.c_str())) return util::Status::OK;
|
||||
if (remove(path.data())) return util::Status::OK;
|
||||
}
|
||||
return util::Status(util::error::INVALID_ARGUMENT,
|
||||
StrCat("Could not delete '", path, "'."));
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "ortools/base/integral_types.h"
|
||||
#include "ortools/base/logging.h"
|
||||
#include "ortools/base/string_view.h"
|
||||
#include "google/protobuf/descriptor.h"
|
||||
#include "google/protobuf/message.h"
|
||||
#include "google/protobuf/text_format.h"
|
||||
@@ -35,8 +36,8 @@ class File {
|
||||
static File* Open(const char* const name, const char* const flag);
|
||||
|
||||
#ifndef SWIG // no overloading
|
||||
inline static File* Open(const std::string& name, const char* const mode) {
|
||||
return Open(name.c_str(), mode);
|
||||
inline static File* Open(const operations_research::string_view& name, const char* const mode) {
|
||||
return Open(name.data(), mode);
|
||||
}
|
||||
#endif // SWIG
|
||||
|
||||
@@ -45,8 +46,8 @@ class File {
|
||||
static File* OpenOrDie(const char* const name, const char* const flag);
|
||||
|
||||
#ifndef SWIG // no overloading
|
||||
inline static File* OpenOrDie(const std::string& name, const char* const flag) {
|
||||
return OpenOrDie(name.c_str(), flag);
|
||||
inline static File* OpenOrDie(const operations_research::string_view& name, const char* const flag) {
|
||||
return OpenOrDie(name.data(), flag);
|
||||
}
|
||||
#endif // SWIG
|
||||
|
||||
@@ -92,12 +93,12 @@ class File {
|
||||
static void Init();
|
||||
|
||||
// Returns the file name.
|
||||
std::string filename() const;
|
||||
operations_research::string_view filename() const;
|
||||
|
||||
// Deletes a file.
|
||||
static bool Delete(const char* const name);
|
||||
static bool Delete(const std::string& name) {
|
||||
return Delete(name.c_str());
|
||||
static bool Delete(const operations_research::string_view& name) {
|
||||
return Delete(name.data());
|
||||
}
|
||||
|
||||
// Tests if a file exists.
|
||||
@@ -106,40 +107,40 @@ class File {
|
||||
bool Open() const;
|
||||
|
||||
private:
|
||||
File(FILE* const descriptor, const std::string& name);
|
||||
File(FILE* const descriptor, const operations_research::string_view& name);
|
||||
|
||||
FILE* f_;
|
||||
const std::string name_;
|
||||
const operations_research::string_view name_;
|
||||
};
|
||||
|
||||
namespace file {
|
||||
inline int Defaults() { return 0xBABA; }
|
||||
|
||||
// As of 2016-01, these methods can only be used with flags = file::Defaults().
|
||||
util::Status Open(const std::string& filename, const std::string& mode,
|
||||
util::Status Open(const operations_research::string_view& filename, const operations_research::string_view& mode,
|
||||
File** f, int flags);
|
||||
util::Status SetTextProto(const std::string& filename, const google::protobuf::Message& proto,
|
||||
util::Status SetTextProto(const operations_research::string_view& filename, const google::protobuf::Message& proto,
|
||||
int flags);
|
||||
util::Status SetBinaryProto(const std::string& filename,
|
||||
util::Status SetBinaryProto(const operations_research::string_view& filename,
|
||||
const google::protobuf::Message& proto, int flags);
|
||||
util::Status SetContents(const std::string& filename, const std::string& contents,
|
||||
util::Status SetContents(const operations_research::string_view& filename, const std::string& contents,
|
||||
int flags);
|
||||
util::Status GetContents(const std::string& filename, std::string* output, int flags);
|
||||
util::Status GetContents(const operations_research::string_view& filename, std::string* output, int flags);
|
||||
util::Status WriteString(File* file, const std::string& contents, int flags);
|
||||
|
||||
bool ReadFileToString(const std::string& file_name, std::string* output);
|
||||
bool WriteStringToFile(const std::string& data, const std::string& file_name);
|
||||
bool ReadFileToProto(const std::string& file_name, google::protobuf::Message* proto);
|
||||
void ReadFileToProtoOrDie(const std::string& file_name, google::protobuf::Message* proto);
|
||||
bool ReadFileToString(const operations_research::string_view& file_name, std::string* output);
|
||||
bool WriteStringToFile(const std::string& data, const operations_research::string_view& file_name);
|
||||
bool ReadFileToProto(const operations_research::string_view& file_name, google::protobuf::Message* proto);
|
||||
void ReadFileToProtoOrDie(const operations_research::string_view& file_name, google::protobuf::Message* proto);
|
||||
bool WriteProtoToASCIIFile(const google::protobuf::Message& proto,
|
||||
const std::string& file_name);
|
||||
const operations_research::string_view& file_name);
|
||||
void WriteProtoToASCIIFileOrDie(const google::protobuf::Message& proto,
|
||||
const std::string& file_name);
|
||||
bool WriteProtoToFile(const google::protobuf::Message& proto, const std::string& file_name);
|
||||
const operations_research::string_view& file_name);
|
||||
bool WriteProtoToFile(const google::protobuf::Message& proto, const operations_research::string_view& file_name);
|
||||
void WriteProtoToFileOrDie(const google::protobuf::Message& proto,
|
||||
const std::string& file_name);
|
||||
const operations_research::string_view& file_name);
|
||||
|
||||
util::Status Delete(const std::string& path, int flags);
|
||||
util::Status Delete(const operations_research::string_view& path, int flags);
|
||||
|
||||
} // namespace file
|
||||
|
||||
|
||||
@@ -15,14 +15,14 @@
|
||||
#ifndef OR_TOOLS_BASE_INLINED_VECTOR_H_
|
||||
#define OR_TOOLS_BASE_INLINED_VECTOR_H_
|
||||
|
||||
// An gtl::InlinedVector<T,N,A> is like a std::vector<T,A>, except that storage
|
||||
// An InlinedVector<T,N,A> is like a std::vector<T,A>, except that storage
|
||||
// for sequences of length <= N are provided inline without requiring
|
||||
// any heap allocation. Typically N is very small (e.g., 4) so that
|
||||
// sequences that are expected to be short do not require allocations.
|
||||
//
|
||||
// Only some of the std::vector<> operations are currently implemented.
|
||||
// Other operations may be added as needed to facilitate migrating
|
||||
// code that uses std::vector<> to gtl::InlinedVector<>.
|
||||
// code that uses std::vector<> to InlinedVector<>.
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
@@ -125,7 +125,7 @@ class InlinedVector {
|
||||
}
|
||||
}
|
||||
const_pointer data() const {
|
||||
return const_cast<gtl::InlinedVector<T, N>*>(this)->data();
|
||||
return const_cast<InlinedVector<T, N>*>(this)->data();
|
||||
}
|
||||
|
||||
// Remove all elements
|
||||
@@ -454,54 +454,54 @@ class InlinedVector {
|
||||
|
||||
// Provide linkage for constants.
|
||||
template <typename T, int N>
|
||||
const size_t gtl::InlinedVector<T, N>::kSizeUnaligned;
|
||||
const size_t InlinedVector<T, N>::kSizeUnaligned;
|
||||
template <typename T, int N>
|
||||
const size_t gtl::InlinedVector<T, N>::kSize;
|
||||
const size_t InlinedVector<T, N>::kSize;
|
||||
template <typename T, int N>
|
||||
const unsigned int gtl::InlinedVector<T, N>::kSentinel;
|
||||
const unsigned int InlinedVector<T, N>::kSentinel;
|
||||
template <typename T, int N>
|
||||
const size_t gtl::InlinedVector<T, N>::kFit1;
|
||||
const size_t InlinedVector<T, N>::kFit1;
|
||||
template <typename T, int N>
|
||||
const size_t gtl::InlinedVector<T, N>::kFit;
|
||||
const size_t InlinedVector<T, N>::kFit;
|
||||
|
||||
template <typename T, int N>
|
||||
inline void swap(gtl::InlinedVector<T, N>& a, gtl::InlinedVector<T, N>& b) {
|
||||
inline void swap(InlinedVector<T, N>& a, InlinedVector<T, N>& b) {
|
||||
a.swap(b);
|
||||
}
|
||||
|
||||
template <typename T, int N>
|
||||
inline bool operator==(const gtl::InlinedVector<T, N>& a,
|
||||
const gtl::InlinedVector<T, N>& b) {
|
||||
inline bool operator==(const InlinedVector<T, N>& a,
|
||||
const InlinedVector<T, N>& b) {
|
||||
return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin());
|
||||
}
|
||||
|
||||
template <typename T, int N>
|
||||
inline bool operator!=(const gtl::InlinedVector<T, N>& a,
|
||||
const gtl::InlinedVector<T, N>& b) {
|
||||
inline bool operator!=(const InlinedVector<T, N>& a,
|
||||
const InlinedVector<T, N>& b) {
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
template <typename T, int N>
|
||||
inline bool operator<(const gtl::InlinedVector<T, N>& a,
|
||||
const gtl::InlinedVector<T, N>& b) {
|
||||
inline bool operator<(const InlinedVector<T, N>& a,
|
||||
const InlinedVector<T, N>& b) {
|
||||
return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
|
||||
}
|
||||
|
||||
template <typename T, int N>
|
||||
inline bool operator>(const gtl::InlinedVector<T, N>& a,
|
||||
const gtl::InlinedVector<T, N>& b) {
|
||||
inline bool operator>(const InlinedVector<T, N>& a,
|
||||
const InlinedVector<T, N>& b) {
|
||||
return b < a;
|
||||
}
|
||||
|
||||
template <typename T, int N>
|
||||
inline bool operator<=(const gtl::InlinedVector<T, N>& a,
|
||||
const gtl::InlinedVector<T, N>& b) {
|
||||
inline bool operator<=(const InlinedVector<T, N>& a,
|
||||
const InlinedVector<T, N>& b) {
|
||||
return !(b < a);
|
||||
}
|
||||
|
||||
template <typename T, int N>
|
||||
inline bool operator>=(const gtl::InlinedVector<T, N>& a,
|
||||
const gtl::InlinedVector<T, N>& b) {
|
||||
inline bool operator>=(const InlinedVector<T, N>& a,
|
||||
const InlinedVector<T, N>& b) {
|
||||
return !(a < b);
|
||||
}
|
||||
|
||||
@@ -509,12 +509,12 @@ inline bool operator>=(const gtl::InlinedVector<T, N>& a,
|
||||
// Implementation
|
||||
|
||||
template <typename T, int N>
|
||||
inline gtl::InlinedVector<T, N>::InlinedVector() {
|
||||
inline InlinedVector<T, N>::InlinedVector() {
|
||||
InitRep();
|
||||
}
|
||||
|
||||
template <typename T, int N>
|
||||
inline gtl::InlinedVector<T, N>::InlinedVector(size_t n) {
|
||||
inline InlinedVector<T, N>::InlinedVector(size_t n) {
|
||||
InitRep();
|
||||
if (n > capacity()) {
|
||||
Grow<Nop>(n); // Must use Nop in case T is not copyable
|
||||
@@ -524,7 +524,7 @@ inline gtl::InlinedVector<T, N>::InlinedVector(size_t n) {
|
||||
}
|
||||
|
||||
template <typename T, int N>
|
||||
inline gtl::InlinedVector<T, N>::InlinedVector(size_t n, const value_type& elem) {
|
||||
inline InlinedVector<T, N>::InlinedVector(size_t n, const value_type& elem) {
|
||||
InitRep();
|
||||
if (n > capacity()) {
|
||||
Grow<Nop>(n); // Can use Nop since we know we have nothing to copy
|
||||
@@ -534,13 +534,13 @@ inline gtl::InlinedVector<T, N>::InlinedVector(size_t n, const value_type& elem)
|
||||
}
|
||||
|
||||
template <typename T, int N>
|
||||
inline gtl::InlinedVector<T, N>::InlinedVector(const InlinedVector& v) {
|
||||
inline InlinedVector<T, N>::InlinedVector(const InlinedVector& v) {
|
||||
InitRep();
|
||||
*this = v;
|
||||
}
|
||||
|
||||
template <typename T, int N>
|
||||
typename gtl::InlinedVector<T, N>::iterator gtl::InlinedVector<T, N>::insert(
|
||||
typename InlinedVector<T, N>::iterator InlinedVector<T, N>::insert(
|
||||
iterator pos, const value_type& v) {
|
||||
DCHECK_GE(pos, begin());
|
||||
DCHECK_LE(pos, end());
|
||||
@@ -564,7 +564,7 @@ typename gtl::InlinedVector<T, N>::iterator gtl::InlinedVector<T, N>::insert(
|
||||
}
|
||||
|
||||
template <typename T, int N>
|
||||
typename gtl::InlinedVector<T, N>::iterator gtl::InlinedVector<T, N>::erase(
|
||||
typename InlinedVector<T, N>::iterator InlinedVector<T, N>::erase(
|
||||
iterator first, iterator last) {
|
||||
DCHECK_LE(begin(), first);
|
||||
DCHECK_LE(first, last);
|
||||
@@ -579,7 +579,7 @@ typename gtl::InlinedVector<T, N>::iterator gtl::InlinedVector<T, N>::erase(
|
||||
}
|
||||
|
||||
template <typename T, int N>
|
||||
void gtl::InlinedVector<T, N>::swap(InlinedVector& other) {
|
||||
void InlinedVector<T, N>::swap(InlinedVector& other) {
|
||||
using std::swap; // Augment ADL with std::swap.
|
||||
if (&other == this) {
|
||||
return;
|
||||
@@ -632,14 +632,14 @@ void gtl::InlinedVector<T, N>::swap(InlinedVector& other) {
|
||||
|
||||
template <typename T, int N>
|
||||
template <typename Iter>
|
||||
inline void gtl::InlinedVector<T, N>::AppendRange(Iter first, Iter last,
|
||||
inline void InlinedVector<T, N>::AppendRange(Iter first, Iter last,
|
||||
std::input_iterator_tag) {
|
||||
std::copy(first, last, std::back_inserter(*this));
|
||||
}
|
||||
|
||||
template <typename T, int N>
|
||||
template <typename Iter>
|
||||
inline void gtl::InlinedVector<T, N>::AppendRange(Iter first, Iter last,
|
||||
inline void InlinedVector<T, N>::AppendRange(Iter first, Iter last,
|
||||
std::forward_iterator_tag) {
|
||||
typedef typename std::iterator_traits<Iter>::difference_type Length;
|
||||
Length length = std::distance(first, last);
|
||||
@@ -651,7 +651,7 @@ inline void gtl::InlinedVector<T, N>::AppendRange(Iter first, Iter last,
|
||||
|
||||
template <typename T, int N>
|
||||
template <typename Iter>
|
||||
inline void gtl::InlinedVector<T, N>::AppendRange(Iter first, Iter last) {
|
||||
inline void InlinedVector<T, N>::AppendRange(Iter first, Iter last) {
|
||||
typedef typename std::iterator_traits<Iter>::iterator_category IterTag;
|
||||
AppendRange(first, last, IterTag());
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
#include <string>
|
||||
#include "ortools/base/basictypes.h"
|
||||
#include "ortools/base/join.h"
|
||||
#include "ortools/base/stringpiece.h"
|
||||
#include "ortools/base/string_view.h"
|
||||
#include "ortools/base/stringprintf.h"
|
||||
|
||||
void StrAppend(std::string* s, const AlphaNum& a) { s->append(a.data(), a.size()); }
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
#include "ortools/base/basictypes.h"
|
||||
#include "ortools/base/integral_types.h"
|
||||
#include "ortools/base/stringpiece.h"
|
||||
#include "ortools/base/string_view.h"
|
||||
|
||||
// A buffer size large enough for all FastToBuffer functions.
|
||||
const int kFastToBufferSize = 32;
|
||||
@@ -37,7 +37,7 @@ char* NumToBuffer(T i, char* buffer) {
|
||||
}
|
||||
|
||||
struct AlphaNum {
|
||||
StringPiece piece;
|
||||
operations_research::string_view piece;
|
||||
char digits[kFastToBufferSize];
|
||||
|
||||
// No bool ctor -- bools convert to an integral type.
|
||||
@@ -60,10 +60,13 @@ struct AlphaNum {
|
||||
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 operations_research::string_view& pc)
|
||||
: piece(pc) {} // NOLINT(runtime/explicit)
|
||||
AlphaNum(const std::string& s) : piece(s) {} // NOLINT(runtime/explicit)
|
||||
|
||||
StringPiece::size_type size() const { return piece.size(); }
|
||||
operations_research::string_view::size_type size() const {
|
||||
return piece.size();
|
||||
}
|
||||
const char* data() const { return piece.data(); }
|
||||
|
||||
private:
|
||||
|
||||
@@ -360,7 +360,7 @@ class Span {
|
||||
: impl_(a, N) {}
|
||||
|
||||
template <int N>
|
||||
Span(const gtl::InlinedVector<value_type, N>& v) // NOLINT(runtime/explicit)
|
||||
Span(const InlinedVector<value_type, N>& v) // NOLINT(runtime/explicit)
|
||||
: impl_(v.data(), v.size()) {}
|
||||
|
||||
// The constructor for any class supplying 'data() const' that returns either
|
||||
@@ -469,7 +469,7 @@ class MutableSpan {
|
||||
: impl_(a, N) {}
|
||||
|
||||
template <int N>
|
||||
MutableSpan(gtl::InlinedVector<value_type, N>* v) // NOLINT(runtime/explicit)
|
||||
MutableSpan(InlinedVector<value_type, N>* v) // NOLINT(runtime/explicit)
|
||||
: impl_(v->data(), v->size()) {}
|
||||
|
||||
// The constructor for any class supplying 'T* data()' or 'T* mutable_data()'
|
||||
|
||||
@@ -87,10 +87,11 @@ std::vector<std::string> Split(const std::string& full, const char* delim, int f
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<StringPiece> Split(const std::string& full, const char* delim,
|
||||
int64 flags) {
|
||||
std::vector<operations_research::string_view> Split(const std::string& full,
|
||||
const char* delim,
|
||||
int64 flags) {
|
||||
CHECK_EQ(SkipEmpty(), flags);
|
||||
std::vector<StringPiece> out;
|
||||
std::vector<operations_research::string_view> out;
|
||||
InternalSplitStringUsing(full, delim, &out);
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -21,21 +21,22 @@
|
||||
|
||||
#include "ortools/base/integral_types.h"
|
||||
#include "ortools/base/logging.h"
|
||||
#include "ortools/base/stringpiece.h"
|
||||
#include "ortools/base/string_view.h"
|
||||
|
||||
namespace strings {
|
||||
std::vector<std::string> Split(const std::string& full, const char* delim, int flags);
|
||||
|
||||
std::vector<std::string> Split(const std::string& full, char delim, int flags);
|
||||
|
||||
// StringPiece version. Its advantages is that it avoids creating a lot of
|
||||
// string_view version. Its advantages is that it avoids creating a lot of
|
||||
// small strings. Note however that the full std::string must outlive the usage
|
||||
// of the result.
|
||||
//
|
||||
// Hack: the int64 allow the C++ compiler to distinguish the two functions. It
|
||||
// is possible to implement this more cleanly at the cost of more complexity.
|
||||
std::vector<StringPiece> Split(const std::string& full, const char* delim,
|
||||
int64 flags);
|
||||
std::vector<operations_research::string_view> Split(const std::string& full,
|
||||
const char* delim,
|
||||
int64 flags);
|
||||
|
||||
namespace delimiter {
|
||||
inline const char* AnyOf(const char* x) { return x; }
|
||||
@@ -53,7 +54,8 @@ inline int SkipEmpty() { return 0xDEADBEEF; }
|
||||
// all of them. This function will correctly handle parsing
|
||||
// strings that have embedded \0s.
|
||||
template <class T>
|
||||
bool SplitStringAndParse(StringPiece source, const std::string& delim,
|
||||
bool SplitStringAndParse(operations_research::string_view source,
|
||||
const std::string& delim,
|
||||
bool (*parse)(const std::string& str, T* value),
|
||||
std::vector<T>* result);
|
||||
|
||||
@@ -76,11 +78,11 @@ bool SplitStringAndParse(const std::string& source, const std::string& delim,
|
||||
CHECK(nullptr != parse);
|
||||
CHECK(nullptr != result);
|
||||
CHECK_GT(delim.size(), 0);
|
||||
const std::vector<StringPiece> pieces =
|
||||
const std::vector<operations_research::string_view> pieces =
|
||||
::strings::Split(source, strings::delimiter::AnyOf(delim.c_str()),
|
||||
static_cast<int64>(strings::SkipEmpty()));
|
||||
T t;
|
||||
for (StringPiece piece : pieces) {
|
||||
for (operations_research::string_view piece : pieces) {
|
||||
if (!parse(piece.as_string(), &t)) return false;
|
||||
result->push_back(t);
|
||||
}
|
||||
|
||||
@@ -11,16 +11,18 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "ortools/base/string_view.h"
|
||||
#include <iostream> // NOLINT
|
||||
#include <utility>
|
||||
#include "ortools/base/stringpiece.h"
|
||||
|
||||
std::ostream& operator<<(std::ostream& o, const StringPiece& piece) {
|
||||
namespace operations_research {
|
||||
|
||||
std::ostream& operator<<(std::ostream& o, const string_view& piece) {
|
||||
o.write(piece.data(), piece.size());
|
||||
return o;
|
||||
}
|
||||
|
||||
bool operator==(const StringPiece& x, const StringPiece& y) {
|
||||
bool operator==(const string_view& x, const string_view& y) {
|
||||
int len = x.size();
|
||||
if (len != y.size()) {
|
||||
return false;
|
||||
@@ -36,22 +38,22 @@ bool operator==(const StringPiece& x, const StringPiece& y) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator<(const StringPiece& x, const StringPiece& y) {
|
||||
bool operator<(const string_view& x, const string_view& y) {
|
||||
const int r = memcmp(x.data(), y.data(), std::min(x.size(), y.size()));
|
||||
return ((r < 0) || ((r == 0) && (x.size() < y.size())));
|
||||
}
|
||||
|
||||
void StringPiece::CopyToString(std::string* target) const {
|
||||
void string_view::CopyToString(std::string* target) const {
|
||||
target->assign(ptr_, length_);
|
||||
}
|
||||
|
||||
int StringPiece::copy(char* buf, size_type n, size_type pos) const {
|
||||
int string_view::copy(char* buf, size_type n, size_type pos) const {
|
||||
int ret = std::min(length_ - pos, n);
|
||||
memcpy(buf, ptr_ + pos, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int StringPiece::find(const StringPiece& s, size_type pos) const {
|
||||
int string_view::find(const string_view& s, size_type pos) const {
|
||||
if (length_ < 0 || pos > static_cast<size_type>(length_)) return npos;
|
||||
|
||||
const char* result =
|
||||
@@ -60,7 +62,7 @@ int StringPiece::find(const StringPiece& s, size_type pos) const {
|
||||
return xpos + s.length_ <= length_ ? xpos : npos;
|
||||
}
|
||||
|
||||
int StringPiece::compare(const StringPiece& x) const {
|
||||
int string_view::compare(const string_view& x) const {
|
||||
int r = memcmp(ptr_, x.ptr_, std::min(length_, x.length_));
|
||||
if (r == 0) {
|
||||
if (length_ < x.length_)
|
||||
@@ -71,7 +73,7 @@ int StringPiece::compare(const StringPiece& x) const {
|
||||
return r;
|
||||
}
|
||||
|
||||
int StringPiece::find(char c, size_type pos) const {
|
||||
int string_view::find(char c, size_type pos) const {
|
||||
if (length_ <= 0 || pos >= static_cast<size_type>(length_)) {
|
||||
return npos;
|
||||
}
|
||||
@@ -79,7 +81,7 @@ int StringPiece::find(char c, size_type pos) const {
|
||||
return result != ptr_ + length_ ? result - ptr_ : npos;
|
||||
}
|
||||
|
||||
int StringPiece::rfind(const StringPiece& s, size_type pos) const {
|
||||
int string_view::rfind(const string_view& s, size_type pos) const {
|
||||
if (length_ < s.length_) return npos;
|
||||
const size_t ulen = length_;
|
||||
if (s.length_ == 0) return std::min(ulen, pos);
|
||||
@@ -89,7 +91,7 @@ int StringPiece::rfind(const StringPiece& s, size_type pos) const {
|
||||
return result != last ? result - ptr_ : npos;
|
||||
}
|
||||
|
||||
int StringPiece::rfind(char c, size_type pos) const {
|
||||
int string_view::rfind(char c, size_type pos) const {
|
||||
if (length_ <= 0) return npos;
|
||||
for (int i = std::min(pos, static_cast<size_type>(length_ - 1)); i >= 0; --i) {
|
||||
if (ptr_[i] == c) {
|
||||
@@ -99,10 +101,12 @@ int StringPiece::rfind(char c, size_type pos) const {
|
||||
return npos;
|
||||
}
|
||||
|
||||
StringPiece StringPiece::substr(size_type pos, size_type n) const {
|
||||
string_view string_view::substr(size_type pos, size_type n) const {
|
||||
if (pos > length_) pos = length_;
|
||||
if (n > length_ - pos) n = length_ - pos;
|
||||
return StringPiece(ptr_ + pos, n);
|
||||
return string_view(ptr_ + pos, n);
|
||||
}
|
||||
|
||||
const StringPiece::size_type StringPiece::npos = size_type(-1);
|
||||
const string_view::size_type string_view::npos = size_type(-1);
|
||||
|
||||
} // namespace operations_research
|
||||
@@ -13,17 +13,17 @@
|
||||
|
||||
// A std::string-like object that points to a sized piece of memory.
|
||||
//
|
||||
// Functions or methods may use const StringPiece& parameters to accept either
|
||||
// Functions or methods may use const string_view& parameters to accept either
|
||||
// a "const char*" or a "std::string" value that will be implicitly converted to
|
||||
// a StringPiece. The implicit conversion means that it is often appropriate
|
||||
// a string_view. The implicit conversion means that it is often appropriate
|
||||
// to include this .h file in other files rather than forward-declaring
|
||||
// StringPiece as would be appropriate for most other Google classes.
|
||||
// string_view as would be appropriate for most other Google classes.
|
||||
//
|
||||
// Systematic usage of StringPiece is encouraged as it will reduce unnecessary
|
||||
// Systematic usage of string_view is encouraged as it will reduce unnecessary
|
||||
// conversions from "const char*" to "std::string" and back again.
|
||||
|
||||
#ifndef OR_TOOLS_BASE_STRINGPIECE_H_
|
||||
#define OR_TOOLS_BASE_STRINGPIECE_H_
|
||||
#ifndef OR_TOOLS_BASE_STRING_VIEW_H_
|
||||
#define OR_TOOLS_BASE_STRING_VIEW_H_
|
||||
|
||||
#include <string.h>
|
||||
|
||||
@@ -32,23 +32,23 @@
|
||||
#include <iosfwd>
|
||||
#include <string>
|
||||
|
||||
class StringPiece {
|
||||
namespace operations_research {
|
||||
|
||||
class string_view {
|
||||
private:
|
||||
const char* ptr_;
|
||||
int length_;
|
||||
|
||||
public:
|
||||
// We provide non-explicit singleton constructors so users can pass
|
||||
// in a "const char*" or a "std::string" wherever a "StringPiece" is
|
||||
// in a "const char*" or a "std::string" wherever a "string_view" is
|
||||
// expected.
|
||||
StringPiece() : ptr_(NULL), length_(0) {}
|
||||
StringPiece(const char* str) // NOLINT
|
||||
: ptr_(str),
|
||||
length_((str == NULL) ? 0 : static_cast<int>(strlen(str))) {}
|
||||
StringPiece(const std::string& str) // NOLINT
|
||||
: ptr_(str.data()),
|
||||
length_(static_cast<int>(str.size())) {}
|
||||
StringPiece(const char* offset, int len) : ptr_(offset), length_(len) {}
|
||||
string_view() : ptr_(NULL), length_(0) {}
|
||||
string_view(const char* str) // NOLINT
|
||||
: ptr_(str), length_((str == NULL) ? 0 : static_cast<int>(strlen(str))) {}
|
||||
string_view(const std::string& str) // NOLINT
|
||||
: ptr_(str.data()), length_(static_cast<int>(str.size())) {}
|
||||
string_view(const char* offset, int len) : ptr_(offset), length_(len) {}
|
||||
|
||||
// data() may return a pointer to a buffer with embedded NULs, and the
|
||||
// returned buffer may or may not be null terminated. Therefore it is
|
||||
@@ -88,13 +88,13 @@ class StringPiece {
|
||||
|
||||
void remove_suffix(int n) { length_ -= n; }
|
||||
|
||||
int compare(const StringPiece& x) const;
|
||||
int compare(const string_view& x) const;
|
||||
|
||||
std::string as_string() const { return std::string(data(), size()); }
|
||||
// We also define ToString() here, since many other std::string-like
|
||||
// interfaces name the routine that converts to a C++ std::string
|
||||
// "ToString", and it's confusing to have the method that does that
|
||||
// for a StringPiece be called "as_string()". We also leave the
|
||||
// for a string_view be called "as_string()". We also leave the
|
||||
// "as_string()" method defined here for existing code.
|
||||
std::string ToString() const { return std::string(data(), size()); }
|
||||
|
||||
@@ -102,12 +102,12 @@ class StringPiece {
|
||||
void AppendToString(std::string* target) const;
|
||||
|
||||
// Does "this" start with "x"?
|
||||
bool starts_with(const StringPiece& x) const {
|
||||
bool starts_with(const string_view& x) const {
|
||||
return ((length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0));
|
||||
}
|
||||
|
||||
// Does "this" end with "x"?
|
||||
bool ends_with(const StringPiece& x) const {
|
||||
bool ends_with(const string_view& x) const {
|
||||
return ((length_ >= x.length_) &&
|
||||
(memcmp(ptr_ + (length_ - x.length_), x.ptr_, x.length_) == 0));
|
||||
}
|
||||
@@ -136,35 +136,37 @@ class StringPiece {
|
||||
|
||||
int copy(char* buf, size_type n, size_type pos = 0) const;
|
||||
|
||||
int find(const StringPiece& s, size_type pos = 0) const;
|
||||
int find(const string_view& s, size_type pos = 0) const;
|
||||
int find(char c, size_type pos = 0) const;
|
||||
int rfind(const StringPiece& s, size_type pos = npos) const;
|
||||
int rfind(const string_view& s, size_type pos = npos) const;
|
||||
int rfind(char c, size_type pos = npos) const;
|
||||
|
||||
StringPiece substr(size_type pos, size_type n = npos) const;
|
||||
string_view substr(size_type pos, size_type n = npos) const;
|
||||
};
|
||||
|
||||
bool operator==(const StringPiece& x, const StringPiece& y);
|
||||
bool operator==(const string_view& x, const string_view& y);
|
||||
|
||||
inline bool operator!=(const StringPiece& x, const StringPiece& y) {
|
||||
inline bool operator!=(const string_view& x, const string_view& y) {
|
||||
return !(x == y);
|
||||
}
|
||||
|
||||
bool operator<(const StringPiece& x, const StringPiece& y);
|
||||
bool operator<(const string_view& x, const string_view& y);
|
||||
|
||||
inline bool operator>(const StringPiece& x, const StringPiece& y) {
|
||||
inline bool operator>(const string_view& x, const string_view& y) {
|
||||
return y < x;
|
||||
}
|
||||
|
||||
inline bool operator<=(const StringPiece& x, const StringPiece& y) {
|
||||
inline bool operator<=(const string_view& x, const string_view& y) {
|
||||
return !(x > y);
|
||||
}
|
||||
|
||||
inline bool operator>=(const StringPiece& x, const StringPiece& y) {
|
||||
inline bool operator>=(const string_view& x, const string_view& y) {
|
||||
return !(x < y);
|
||||
}
|
||||
|
||||
// Allow StringPiece to be logged.
|
||||
extern std::ostream& operator<<(std::ostream& o, const StringPiece& piece);
|
||||
// Allow string_view to be logged.
|
||||
extern std::ostream& operator<<(std::ostream& o, const string_view& piece);
|
||||
|
||||
#endif // OR_TOOLS_BASE_STRINGPIECE_H_
|
||||
} // namespace operations_research
|
||||
|
||||
#endif // OR_TOOLS_BASE_STRING_VIEW_H_
|
||||
@@ -16,16 +16,18 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "ortools/base/stringpiece.h"
|
||||
#include "ortools/base/string_view.h"
|
||||
|
||||
namespace strings {
|
||||
// Returns whether s begins with x.
|
||||
inline bool StartsWith(StringPiece s, StringPiece x) {
|
||||
inline bool StartsWith(operations_research::string_view s,
|
||||
operations_research::string_view x) {
|
||||
return s.size() >= x.size() && memcmp(s.data(), x.data(), x.size()) == 0;
|
||||
}
|
||||
|
||||
// Returns whether s ends with x.
|
||||
inline bool EndsWith(StringPiece s, StringPiece x) {
|
||||
inline bool EndsWith(operations_research::string_view s,
|
||||
operations_research::string_view x) {
|
||||
return s.size() >= x.size() &&
|
||||
memcmp(s.data() + (s.size() - x.size()), x.data(), x.size()) == 0;
|
||||
}
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "ortools/base/stringpiece.h"
|
||||
|
||||
namespace operations_research {
|
||||
std::string StringPrintf(const char* const format, ...);
|
||||
void SStringPrintf(std::string* const dst, const char* const format, ...);
|
||||
|
||||
@@ -15,17 +15,17 @@
|
||||
#define OR_TOOLS_BASE_STRUTIL_H_
|
||||
|
||||
#include <string>
|
||||
#include "ortools/base/stringpiece.h"
|
||||
#include "ortools/base/string_view.h"
|
||||
|
||||
using std::string;
|
||||
|
||||
namespace operations_research {
|
||||
|
||||
inline bool HasSuffixString(const StringPiece& str, const StringPiece& suffix) {
|
||||
inline bool HasSuffixString(const string_view& str, const string_view& suffix) {
|
||||
return str.ends_with(suffix);
|
||||
}
|
||||
|
||||
inline bool HasPrefixString(const StringPiece& str, const StringPiece& prefix) {
|
||||
inline bool HasPrefixString(const string_view& str, const string_view& prefix) {
|
||||
return str.starts_with(prefix);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
#include <cstdio>
|
||||
|
||||
#include "ortools/base/sysinfo.h"
|
||||
#include "ortools/base/stringpiece.h"
|
||||
|
||||
namespace operations_research {
|
||||
// GetProcessMemoryUsage
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "ortools/base/logging.h"
|
||||
#include "ortools/base/macros.h"
|
||||
#include "ortools/base/stringprintf.h"
|
||||
#include "ortools/base/string_view.h"
|
||||
#include "ortools/base/file.h"
|
||||
#include "ortools/base/recordio.h"
|
||||
#include "zlib.h"
|
||||
@@ -535,7 +536,7 @@ class NoCompressionTrailPacker : public TrailPacker<T> {
|
||||
void Pack(const addrval<T>* block, std::string* packed_block) override {
|
||||
DCHECK(block != nullptr);
|
||||
DCHECK(packed_block != nullptr);
|
||||
StringPiece block_str;
|
||||
string_view block_str;
|
||||
block_str.set(block, this->input_size());
|
||||
block_str.CopyToString(packed_block);
|
||||
}
|
||||
@@ -566,7 +567,7 @@ class ZlibTrailPacker : public TrailPacker<T> {
|
||||
compress(reinterpret_cast<Bytef*>(tmp_block_.get()), &size,
|
||||
reinterpret_cast<const Bytef*>(block), this->input_size());
|
||||
CHECK_EQ(Z_OK, result);
|
||||
StringPiece block_str;
|
||||
string_view block_str;
|
||||
block_str.set(tmp_block_.get(), size);
|
||||
block_str.CopyToString(packed_block);
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ using ::google::protobuf::FieldDescriptor;
|
||||
using ::google::protobuf::Reflection;
|
||||
using ::google::protobuf::TextFormat;
|
||||
|
||||
bool ReadFileToProto(const std::string& filename, google::protobuf::Message* proto) {
|
||||
bool ReadFileToProto(string_view filename, google::protobuf::Message* proto) {
|
||||
std::string data;
|
||||
CHECK_OK(file::GetContents(filename, &data, file::Defaults()));
|
||||
// Note that gzipped files are currently not supported.
|
||||
@@ -38,7 +38,7 @@ bool ReadFileToProto(const std::string& filename, google::protobuf::Message* pro
|
||||
return false;
|
||||
}
|
||||
|
||||
bool WriteProtoToFile(const std::string& filename, const google::protobuf::Message& proto,
|
||||
bool WriteProtoToFile(string_view filename, const google::protobuf::Message& proto,
|
||||
ProtoWriteFormat proto_write_format, bool gzipped) {
|
||||
// Note that gzipped files are currently not supported.
|
||||
gzipped = false;
|
||||
|
||||
@@ -18,18 +18,18 @@
|
||||
#include <vector>
|
||||
|
||||
#include "ortools/base/file.h"
|
||||
#include "ortools/base/stringpiece.h"
|
||||
#include "ortools/base/recordio.h"
|
||||
#include "google/protobuf/message.h"
|
||||
#include "ortools/base/string_view.h"
|
||||
|
||||
namespace operations_research {
|
||||
|
||||
// Reads a proto from a file. Supports the following formats: binary, text,
|
||||
// JSON, all of those optionally gzipped. Returns false on failure.
|
||||
bool ReadFileToProto(const std::string& filename, google::protobuf::Message* proto);
|
||||
bool ReadFileToProto(string_view filename, google::protobuf::Message* proto);
|
||||
|
||||
template <typename Proto>
|
||||
Proto ReadFileToProtoOrDie(const std::string& filename) {
|
||||
Proto ReadFileToProtoOrDie(string_view filename) {
|
||||
Proto proto;
|
||||
CHECK(ReadFileToProto(filename, &proto));
|
||||
return proto;
|
||||
@@ -42,7 +42,7 @@ enum class ProtoWriteFormat { kProtoText, kProtoBinary, kJson };
|
||||
// If 'proto_write_format' is kProtoBinary, ".bin" is appended to file_name. If
|
||||
// 'proto_write_format' is kJson, ".json" is appended to file_name. If 'gzipped'
|
||||
// is true, ".gz" is appended to file_name.
|
||||
bool WriteProtoToFile(const std::string& filename, const google::protobuf::Message& proto,
|
||||
bool WriteProtoToFile(string_view filename, const google::protobuf::Message& proto,
|
||||
ProtoWriteFormat proto_write_format, bool gzipped);
|
||||
|
||||
namespace internal {
|
||||
@@ -77,7 +77,7 @@ std::vector<Proto> ReadNumRecords(File* file, int expected_num_records) {
|
||||
|
||||
// Ditto, taking a filename as argument.
|
||||
template <typename Proto>
|
||||
std::vector<Proto> ReadNumRecords(const std::string& filename,
|
||||
std::vector<Proto> ReadNumRecords(string_view filename,
|
||||
int expected_num_records) {
|
||||
// return ReadNumRecords<Proto>(file::OpenOrDie(filename, "r", file::Defaults()),
|
||||
// expected_num_records);
|
||||
@@ -89,7 +89,7 @@ std::vector<Proto> ReadNumRecords(const std::string& filename,
|
||||
// file is empty. Dies if the file doesn't exist or contains something else than
|
||||
// protos encoded in RecordIO format.
|
||||
template <typename Proto>
|
||||
std::vector<Proto> ReadAllRecordsOrDie(StringPiece filename) {
|
||||
std::vector<Proto> ReadAllRecordsOrDie(string_view filename) {
|
||||
return internal::ReadNumRecords<Proto>(filename, -1);
|
||||
}
|
||||
template <typename Proto>
|
||||
@@ -101,7 +101,7 @@ std::vector<Proto> ReadAllRecordsOrDie(File* file) {
|
||||
// doesn't contain exactly one record, or contains something else than protos
|
||||
// encoded in RecordIO format.
|
||||
template <typename Proto>
|
||||
Proto ReadOneRecordOrDie(StringPiece filename) {
|
||||
Proto ReadOneRecordOrDie(string_view filename) {
|
||||
Proto p;
|
||||
p.Swap(&internal::ReadNumRecords<Proto>(filename, 1)[0]);
|
||||
return p;
|
||||
@@ -110,7 +110,7 @@ Proto ReadOneRecordOrDie(StringPiece filename) {
|
||||
// Writes all records in Proto format to 'file'. Dies if it is unable to open
|
||||
// the file or write to it.
|
||||
template <typename Proto>
|
||||
void WriteRecordsOrDie(const std::string filename,
|
||||
void WriteRecordsOrDie(string_view filename,
|
||||
const std::vector<Proto>& protos) {
|
||||
recordio::RecordWriter writer(File::OpenOrDie(filename, "w"));
|
||||
for (const Proto& proto : protos) {
|
||||
|
||||
Reference in New Issue
Block a user