diff --git a/ortools/base/BUILD.bazel b/ortools/base/BUILD.bazel index 568647b0e26..b4424c60dc4 100644 --- a/ortools/base/BUILD.bazel +++ b/ortools/base/BUILD.bazel @@ -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"], diff --git a/ortools/base/string_view_migration.h b/ortools/base/string_view_migration.h new file mode 100644 index 00000000000..df76521299a --- /dev/null +++ b/ortools/base/string_view_migration.h @@ -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 + +#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_ diff --git a/ortools/linear_solver/wrappers/BUILD.bazel b/ortools/linear_solver/wrappers/BUILD.bazel index 3d7a471396b..2695ff282ef 100644 --- a/ortools/linear_solver/wrappers/BUILD.bazel +++ b/ortools/linear_solver/wrappers/BUILD.bazel @@ -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", diff --git a/ortools/linear_solver/wrappers/model_builder_helper.cc b/ortools/linear_solver/wrappers/model_builder_helper.cc index d4ad60d073a..610d158a426 100644 --- a/ortools/linear_solver/wrappers/model_builder_helper.cc +++ b/ortools/linear_solver/wrappers/model_builder_helper.cc @@ -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 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 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 { diff --git a/ortools/math_opt/core/BUILD.bazel b/ortools/math_opt/core/BUILD.bazel index bdd9db9758f..dd45ee02077 100644 --- a/ortools/math_opt/core/BUILD.bazel +++ b/ortools/math_opt/core/BUILD.bazel @@ -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", diff --git a/ortools/math_opt/core/model_summary.h b/ortools/math_opt/core/model_summary.h index 4ba922e2118..9c481a39980 100644 --- a/ortools/math_opt/core/model_summary.h +++ b/ortools/math_opt/core/model_summary.h @@ -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(); } diff --git a/ortools/math_opt/cpp/BUILD.bazel b/ortools/math_opt/cpp/BUILD.bazel index 8319d01ce64..4c609e3a3a2 100644 --- a/ortools/math_opt/cpp/BUILD.bazel +++ b/ortools/math_opt/cpp/BUILD.bazel @@ -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", diff --git a/ortools/math_opt/cpp/solve_result.cc b/ortools/math_opt/cpp/solve_result.cc index 249b986dcba..caf28a4e758 100644 --- a/ortools/math_opt/cpp/solve_result.cc +++ b/ortools/math_opt/cpp/solve_result.cc @@ -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::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, diff --git a/ortools/math_opt/cpp/streamable_solver_init_arguments.cc b/ortools/math_opt/cpp/streamable_solver_init_arguments.cc index 41ab393e87d..e2248fa3060 100644 --- a/ortools/math_opt/cpp/streamable_solver_init_arguments.cc +++ b/ortools/math_opt/cpp/streamable_solver_init_arguments.cc @@ -16,6 +16,7 @@ #include #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()), }; } diff --git a/ortools/math_opt/elemental/BUILD.bazel b/ortools/math_opt/elemental/BUILD.bazel index 2ebdec571a2..4559db384f1 100644 --- a/ortools/math_opt/elemental/BUILD.bazel +++ b/ortools/math_opt/elemental/BUILD.bazel @@ -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", diff --git a/ortools/math_opt/elemental/elemental_from_proto.cc b/ortools/math_opt/elemental/elemental_from_proto.cc index 5da78b171a1..313b24f432f 100644 --- a/ortools/math_opt/elemental/elemental_from_proto.cc +++ b/ortools/math_opt/elemental/elemental_from_proto.cc @@ -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 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(); diff --git a/ortools/sat/BUILD.bazel b/ortools/sat/BUILD.bazel index 7576aa4d40d..7acad72904e 100644 --- a/ortools/sat/BUILD.bazel +++ b/ortools/sat/BUILD.bazel @@ -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", diff --git a/ortools/sat/cp_model.cc b/ortools/sat/cp_model.cc index f9db519ed95..ff19e87aae2 100644 --- a/ortools/sat/cp_model.cc +++ b/ortools/sat/cp_model.cc @@ -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 { diff --git a/ortools/sat/python/BUILD.bazel b/ortools/sat/python/BUILD.bazel index ff0364fcba2..0def869eee3 100644 --- a/ortools/sat/python/BUILD.bazel +++ b/ortools/sat/python/BUILD.bazel @@ -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", diff --git a/ortools/sat/python/cp_model_helper.cc b/ortools/sat/python/cp_model_helper.cc index ef21a6411cc..50f5bc2752f 100644 --- a/ortools/sat/python/cp_model_helper.cc +++ b/ortools/sat/python/cp_model_helper.cc @@ -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 SufficientAssumptionsForInfeasibility() const {