Merge pull request #4735 from google/backport_string_view_migration

Backport string_view_migration.h
This commit is contained in:
Guillaume Chatelet
2025-07-21 10:01:20 +02:00
committed by GitHub
15 changed files with 74 additions and 15 deletions

View File

@@ -567,6 +567,12 @@ cc_library(
deps = [":base"],
)
cc_library(
name = "string_view_migration",
hdrs = ["string_view_migration.h"],
deps = ["@abseil-cpp//absl/strings"],
)
cc_library(
name = "strong_int",
hdrs = ["strong_int.h"],

View File

@@ -0,0 +1,34 @@
// Copyright 2010-2025 Google LLC
// 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 THIRD_PARTY_ORTOOLS_ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_
#define THIRD_PARTY_ORTOOLS_ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_
#include <string>
#include "absl/strings/string_view.h"
// This file contains helpers for various string_view migration efforts. These
// are not intended to be stable long-term.
namespace google::protobuf {
inline std::string StringCopy(absl::string_view str) {
return std::string(str);
}
inline std::string StringCopy(const std::string& str) { return str; }
} // namespace google::protobuf
#endif // THIRD_PARTY_ORTOOLS_ORTOOLS_BASE_STRING_VIEW_MIGRATION_H_

View File

@@ -37,6 +37,7 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
"//ortools/base:file",
"//ortools/base:string_view_migration",
"//ortools/linear_solver:linear_solver_cc_proto",
"//ortools/linear_solver:model_exporter",
"//ortools/linear_solver:solve_mp_model",

View File

@@ -30,6 +30,7 @@
#include "absl/strings/str_join.h"
#include "ortools/base/helpers.h"
#include "ortools/base/options.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/linear_solver/gurobi_util.h"
#include "ortools/linear_solver/linear_solver.h"
#include "ortools/linear_solver/linear_solver.pb.h"
@@ -183,7 +184,7 @@ double ModelBuilderHelper::VarObjectiveCoefficient(int var_index) const {
}
std::string ModelBuilderHelper::VarName(int var_index) const {
return std::string(model_.variable(var_index).name());
return google::protobuf::StringCopy(model_.variable(var_index).name());
}
int ModelBuilderHelper::AddLinearConstraint() {
@@ -259,7 +260,7 @@ double ModelBuilderHelper::ConstraintUpperBound(int ct_index) const {
}
std::string ModelBuilderHelper::ConstraintName(int ct_index) const {
return std::string(model_.constraint(ct_index).name());
return google::protobuf::StringCopy(model_.constraint(ct_index).name());
}
std::vector<int> ModelBuilderHelper::ConstraintVarIndices(int ct_index) const {
@@ -399,7 +400,8 @@ double ModelBuilderHelper::EnforcedConstraintUpperBound(int ct_index) const {
std::string ModelBuilderHelper::EnforcedConstraintName(int ct_index) const {
DCHECK(IsEnforcedConstraint(ct_index));
return std::string(model_.general_constraint(ct_index).name());
return google::protobuf::StringCopy(
model_.general_constraint(ct_index).name());
}
std::vector<int> ModelBuilderHelper::EnforcedConstraintVarIndices(
@@ -437,7 +439,7 @@ int ModelBuilderHelper::num_constraints() const {
}
std::string ModelBuilderHelper::name() const {
return std::string(model_.name());
return google::protobuf::StringCopy(model_.name());
}
void ModelBuilderHelper::SetName(const std::string& name) {
@@ -743,7 +745,7 @@ double ModelSolverHelper::activity(int ct_index) {
std::string ModelSolverHelper::status_string() const {
if (!has_response()) return "";
return std::string(response_.value().status_str());
return google::protobuf::StringCopy(response_.value().status_str());
}
double ModelSolverHelper::wall_time() const {

View File

@@ -64,6 +64,7 @@ cc_library(
"//ortools/base:status_macros",
"//ortools/math_opt:model_cc_proto",
"//ortools/math_opt:model_update_cc_proto",
"//ortools/base:string_view_migration",
"@abseil-cpp//absl/algorithm:container",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/log:check",

View File

@@ -32,6 +32,7 @@
#include "absl/types/span.h"
#include "ortools/base/linked_hash_map.h"
#include "ortools/base/status_macros.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/math_opt/model.pb.h"
#include "ortools/math_opt/model_update.pb.h"
@@ -256,7 +257,8 @@ absl::Status UpdateBiMapFromMappedData(
}
absl::c_sort(new_ids);
for (const int64_t id : new_ids) {
RETURN_IF_ERROR(bimap.Insert(id, std::string(proto_map.at(id).name())));
RETURN_IF_ERROR(bimap.Insert(
id, google::protobuf::StringCopy(proto_map.at(id).name())));
}
return absl::OkStatus();
}

View File

@@ -196,6 +196,7 @@ cc_library(
":variable_and_expressions",
"//ortools/base:protoutil",
"//ortools/base:status_macros",
"//ortools/base:string_view_migration",
"//ortools/gscip:gscip_cc_proto",
"//ortools/math_opt:result_cc_proto",
"//ortools/math_opt:solution_cc_proto",
@@ -386,6 +387,7 @@ cc_library(
srcs = ["streamable_solver_init_arguments.cc"],
hdrs = ["streamable_solver_init_arguments.h"],
deps = [
"//ortools/base:string_view_migration",
"//ortools/math_opt:parameters_cc_proto",
"//ortools/math_opt/solvers:gurobi_cc_proto",
"@abseil-cpp//absl/status:statusor",

View File

@@ -33,6 +33,7 @@
#include "absl/types/span.h"
#include "ortools/base/protoutil.h"
#include "ortools/base/status_macros.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/math_opt/core/math_opt_proto_utils.h"
#include "ortools/math_opt/cpp/linear_constraint.h"
#include "ortools/math_opt/cpp/variable_and_expressions.h"
@@ -373,7 +374,7 @@ absl::StatusOr<Termination> Termination::FromProto(
return absl::InvalidArgumentError("reason must be specified");
}
Termination result(/*is_maximize=*/false, *reason,
std::string(termination_proto.detail()));
google::protobuf::StringCopy(termination_proto.detail()));
result.limit = EnumFromProto(termination_proto.limit());
OR_ASSIGN_OR_RETURN3(
result.problem_status,

View File

@@ -16,6 +16,7 @@
#include <optional>
#include "absl/status/statusor.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/math_opt/parameters.pb.h"
#include "ortools/math_opt/solvers/gurobi.pb.h"
@@ -34,10 +35,11 @@ GurobiInitializerProto::ISVKey GurobiISVKey::Proto() const {
GurobiISVKey GurobiISVKey::FromProto(
const GurobiInitializerProto::ISVKey& key_proto) {
return GurobiISVKey{
.name = std::string(key_proto.name()),
.application_name = std::string(key_proto.application_name()),
.name = google::protobuf::StringCopy(key_proto.name()),
.application_name =
google::protobuf::StringCopy(key_proto.application_name()),
.expiration = key_proto.expiration(),
.key = std::string(key_proto.key()),
.key = google::protobuf::StringCopy(key_proto.key()),
};
}

View File

@@ -62,6 +62,7 @@ cc_library(
":symmetry",
":thread_safe_id_map",
"//ortools/base:status_macros",
"//ortools/base:string_view_migration",
"//ortools/math_opt:model_cc_proto",
"//ortools/math_opt:model_update_cc_proto",
"//ortools/math_opt:sparse_containers_cc_proto",

View File

@@ -26,6 +26,7 @@
#include "google/protobuf/repeated_ptr_field.h"
#include "ortools/base/status_builder.h"
#include "ortools/base/status_macros.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/math_opt/core/model_summary.h"
#include "ortools/math_opt/elemental/attr_key.h"
#include "ortools/math_opt/elemental/attributes.h"
@@ -253,8 +254,8 @@ absl::StatusOr<Elemental> ElementalFromModelProtoImpl(const ModelProto& proto) {
return absl::UnimplementedError(
"Elemental does not support sos2 constraints yet");
}
Elemental elemental(std::string(proto.name()),
std::string(proto.objective().name()));
Elemental elemental(google::protobuf::StringCopy(proto.name()),
google::protobuf::StringCopy(proto.objective().name()));
AddVariables(proto.variables(), elemental);
{
const ObjectiveProto& objective = proto.objective();

View File

@@ -33,6 +33,7 @@ cc_library(
":model",
":sat_parameters_cc_proto",
"//ortools/util:sorted_interval_list",
"//ortools/base:string_view_migration",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/log:check",
"@abseil-cpp//absl/strings",

View File

@@ -26,6 +26,7 @@
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/cp_model_utils.h"
#include "ortools/util/sorted_interval_list.h"
@@ -125,7 +126,8 @@ IntVar IntVar::WithName(absl::string_view name) {
std::string IntVar::Name() const {
if (builder_ == nullptr) return "null";
return std::string(builder_->Proto().variables(index_).name());
return google::protobuf::StringCopy(
builder_->Proto().variables(index_).name());
}
::operations_research::Domain IntVar::Domain() const {
@@ -619,7 +621,8 @@ BoolVar IntervalVar::PresenceBoolVar() const {
std::string IntervalVar::Name() const {
if (builder_ == nullptr) return "null";
return std::string(builder_->Proto().constraints(index_).name());
return google::protobuf::StringCopy(
builder_->Proto().constraints(index_).name());
}
std::string IntervalVar::DebugString() const {

View File

@@ -89,6 +89,7 @@ pybind_extension(
":linear_expr",
":linear_expr_doc",
":proto_builder_pybind11",
"//ortools/base:string_view_migration",
"//ortools/sat:cp_model_cc_proto",
"//ortools/sat:cp_model_utils",
"//ortools/sat:sat_parameters_cc_proto",

View File

@@ -25,6 +25,7 @@
#include "absl/functional/any_invocable.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "ortools/base/string_view_migration.h"
#include "ortools/port/proto_utils.h" // IWYU: keep
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/cp_model_utils.h"
@@ -131,7 +132,7 @@ class ResponseWrapper {
}
std::string SolutionInfo() const {
return std::string(response_.solution_info());
return google::protobuf::StringCopy(response_.solution_info());
}
std::vector<int> SufficientAssumptionsForInfeasibility() const {