BLI: support formatting StringRef with the fmt library

People were manually converting it to `std::string_view` in many places already.
It's easy enough to allow using `StringRef` directly by providing a `format_as` function.

Also see https://fmt.dev/latest/api.html#udt.

Pull Request: https://projects.blender.org/blender/blender/pulls/117788
This commit is contained in:
Jacques Lucke 2024-02-03 19:14:51 +01:00
parent f6276ec163
commit 442429a97c
13 changed files with 49 additions and 39 deletions

View File

@ -134,7 +134,7 @@ BlobSlice DiskBlobWriter::write_as_stream(const StringRef file_extension,
BLI_assert(file_extension.startswith("."));
independent_file_count_++;
const std::string file_name = fmt::format(
"{}_file_{}{}", base_name_, independent_file_count_, std::string_view(file_extension));
"{}_file_{}{}", base_name_, independent_file_count_, file_extension);
char path[FILE_MAX];
BLI_path_join(path, sizeof(path), blob_dir_.c_str(), file_name.c_str());

View File

@ -621,4 +621,18 @@ constexpr bool operator>=(StringRef a, StringRef b)
/** \} */
/* -------------------------------------------------------------------- */
/** \name Formatting
* \{ */
/**
* Support using the fmt library with #StringRef and implicitly also #StringRefNull.
*/
inline std::string_view format_as(StringRef str)
{
return str;
}
/** \} */
} // namespace blender

View File

@ -2,12 +2,12 @@
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "BLI_string_ref.hh"
#include "BLI_timeit.hh"
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string_view>
#include <fmt/format.h>
@ -43,7 +43,7 @@ void print_duration(Nanoseconds duration)
{
fmt::memory_buffer buf;
format_duration(duration, buf);
std::cout << std::string_view(buf.data(), buf.size());
std::cout << StringRef(buf.data(), buf.size());
}
ScopedTimer::~ScopedTimer()
@ -54,8 +54,8 @@ ScopedTimer::~ScopedTimer()
fmt::memory_buffer buf;
fmt::format_to(fmt::appender(buf), FMT_STRING("Timer '{}' took "), name_);
format_duration(duration, buf);
buf.append(std::string_view("\n"));
std::cout << std::string_view(buf.data(), buf.size());
buf.append(StringRef("\n"));
std::cout << StringRef(buf.data(), buf.size());
}
ScopedTimerAveraged::~ScopedTimerAveraged()
@ -70,12 +70,12 @@ ScopedTimerAveraged::~ScopedTimerAveraged()
fmt::memory_buffer buf;
fmt::format_to(fmt::appender(buf), FMT_STRING("Timer '{}': (Average: "), name_);
format_duration(total_time_ / total_count_, buf);
buf.append(std::string_view(", Min: "));
buf.append(StringRef(", Min: "));
format_duration(min_time_, buf);
buf.append(std::string_view(", Last: "));
buf.append(StringRef(", Last: "));
format_duration(duration, buf);
buf.append(std::string_view(")\n"));
std::cout << std::string_view(buf.data(), buf.size());
buf.append(StringRef(")\n"));
std::cout << StringRef(buf.data(), buf.size());
}
} // namespace blender::timeit

View File

@ -131,8 +131,8 @@ class BoneCollectionDropTarget : public TreeViewItemDropTarget {
const BoneCollection &drag_bcoll = drag_bone_collection->bcoll();
const BoneCollection &drop_bcoll = drop_bonecoll_.bcoll();
std::string_view drag_name = drag_bcoll.name;
std::string_view drop_name = drop_bcoll.name;
const StringRef drag_name = drag_bcoll.name;
const StringRef drop_name = drop_bcoll.name;
switch (drag_info.drop_location) {
case DropLocation::Into:

View File

@ -60,8 +60,8 @@ class LayerNodeDropTarget : public TreeViewItemDropTarget {
static_cast<const wmDragGreasePencilLayer *>(drag_info.drag_data.poin);
Layer &drag_layer = drag_grease_pencil->layer->wrap();
std::string_view drag_name = drag_layer.name();
std::string_view drop_name = drop_tree_node_.name();
const StringRef drag_name = drag_layer.name();
const StringRef drop_name = drop_tree_node_.name();
switch (drag_info.drop_location) {
case DropLocation::Into:

View File

@ -133,7 +133,7 @@ class ReorderCollectionDropTarget : public TreeViewItemDropTarget {
std::string drop_tooltip(const DragInfo &drag) const override
{
const std::string_view drop_name = std::string_view(drop_id_.name + 2);
const StringRef drop_name = drop_id_.name + 2;
switch (drag.drop_location) {
case DropLocation::Into:

View File

@ -406,9 +406,8 @@ std::string AssetCatalogDropTarget::drop_tooltip_asset_catalog(const wmDrag &dra
BLI_assert(drag.type == WM_DRAG_ASSET_CATALOG);
const AssetCatalog *src_catalog = get_drag_catalog(drag, get_asset_library());
return fmt::format(TIP_("Move catalog {} into {}"),
std::string_view(src_catalog->path.name()),
std::string_view(catalog_item_.get_name()));
return fmt::format(
TIP_("Move catalog {} into {}"), src_catalog->path.name(), catalog_item_.get_name());
}
std::string AssetCatalogDropTarget::drop_tooltip_asset_list(const wmDrag &drag) const
@ -623,7 +622,7 @@ std::string AssetCatalogTreeViewAllItem::DropTarget::drop_tooltip(
drag_info.drag_data, *get_view<AssetCatalogTreeView>().asset_library_);
return fmt::format(TIP_("Move catalog {} to the top level of the tree"),
std::string_view(drag_catalog->path.name()));
drag_catalog->path.name());
}
bool AssetCatalogTreeViewAllItem::DropTarget::on_drop(bContext * /*C*/,

View File

@ -2524,7 +2524,7 @@ static std::string named_attribute_tooltip(bContext * /*C*/, void *argN, const c
for (const NameWithUsage &attribute : sorted_used_attribute) {
const StringRefNull name = attribute.name;
const geo_log::NamedAttributeUsage usage = attribute.usage;
ss << fmt::format(TIP_(" \u2022 \"{}\": "), std::string_view(name));
ss << fmt::format(TIP_(" \u2022 \"{}\": "), name);
Vector<std::string> usages;
if ((usage & geo_log::NamedAttributeUsage::Read) != geo_log::NamedAttributeUsage::None) {
usages.append(TIP_("read"));

View File

@ -44,26 +44,23 @@ void FileBuffer::close_file()
void FileBuffer::write_header_element(StringRef name, int count)
{
write_fstring("element {} {}\n", std::string_view(name), count);
write_fstring("element {} {}\n", name, count);
}
void FileBuffer::write_header_scalar_property(StringRef dataType, StringRef name)
{
write_fstring("property {} {}\n", std::string_view(dataType), std::string_view(name));
write_fstring("property {} {}\n", dataType, name);
}
void FileBuffer::write_header_list_property(StringRef countType,
StringRef dataType,
StringRef name)
{
write_fstring("property list {} {} {}\n",
std::string_view(countType),
std::string_view(dataType),
std::string_view(name));
write_fstring("property list {} {} {}\n", countType, dataType, name);
}
void FileBuffer::write_string(StringRef s)
{
write_fstring("{}\n", std::string_view(s));
write_fstring("{}\n", s);
}
void FileBuffer::write_newline()

View File

@ -111,11 +111,11 @@ class FormatHandler : NonCopyable, NonMovable {
}
void write_obj_usemtl(StringRef s)
{
write_impl("usemtl {}\n", std::string_view(s));
write_impl("usemtl {}\n", s);
}
void write_obj_mtllib(StringRef s)
{
write_impl("mtllib {}\n", std::string_view(s));
write_impl("mtllib {}\n", s);
}
void write_obj_smooth(int s)
{
@ -123,11 +123,11 @@ class FormatHandler : NonCopyable, NonMovable {
}
void write_obj_group(StringRef s)
{
write_impl("g {}\n", std::string_view(s));
write_impl("g {}\n", s);
}
void write_obj_object(StringRef s)
{
write_impl("o {}\n", std::string_view(s));
write_impl("o {}\n", s);
}
void write_obj_edge(int a, int b)
{
@ -172,7 +172,7 @@ class FormatHandler : NonCopyable, NonMovable {
void write_mtl_newmtl(StringRef s)
{
write_impl("newmtl {}\n", std::string_view(s));
write_impl("newmtl {}\n", s);
}
void write_mtl_float(const char *type, float v)
{
@ -189,12 +189,12 @@ class FormatHandler : NonCopyable, NonMovable {
/* NOTE: options, if present, will have its own leading space. */
void write_mtl_map(const char *type, StringRef options, StringRef value)
{
write_impl("{}{} {}\n", type, std::string_view(options), std::string_view(value));
write_impl("{}{} {}\n", type, options, value);
}
void write_string(StringRef s)
{
write_impl("{}\n", std::string_view(s));
write_impl("{}\n", s);
}
private:

View File

@ -978,7 +978,7 @@ static std::optional<std::string> rna_prepend_real_ID_path(Main * /*bmain*/,
if (!path.is_empty()) {
if (real_id) {
if (prefix[0]) {
return fmt::format("{}{}{}", prefix, path[0] == '[' ? "" : ".", std::string_view(path));
return fmt::format("{}{}{}", prefix, path[0] == '[' ? "" : ".", path);
}
return path;
}
@ -1105,11 +1105,11 @@ static std::string rna_path_from_ptr_to_property_index_ex(const PointerRNA *ptr,
if (!path_prefix.is_empty()) {
if (is_rna) {
return fmt::format("{}.{}{}", std::string_view(path_prefix), propname, index_str);
return fmt::format("{}.{}{}", path_prefix, propname, index_str);
}
char propname_esc[MAX_IDPROP_NAME * 2];
BLI_str_escape(propname_esc, propname, sizeof(propname_esc));
return fmt::format("{}[\"{}\"]{}", std::string_view(path_prefix), propname_esc, index_str);
return fmt::format("{}[\"{}\"]{}", path_prefix, propname_esc, index_str);
}
if (is_rna) {

View File

@ -274,14 +274,14 @@ static void node_geo_exec(GeoNodeExecParams params)
if (!mesh_attributes_eval.contains(uv_map_name)) {
pass_through_input();
const std::string message = fmt::format(TIP_("Evaluated surface missing UV map: \"{}\""),
std::string_view(uv_map_name));
uv_map_name);
params.error_message_add(NodeWarningType::Error, message);
return;
}
if (!mesh_attributes_orig.contains(uv_map_name)) {
pass_through_input();
const std::string message = fmt::format(TIP_("Original surface missing UV map: \"{}\""),
std::string_view(uv_map_name));
uv_map_name);
params.error_message_add(NodeWarningType::Error, message);
return;
}

View File

@ -345,7 +345,7 @@ class LazyFunctionForGeometryNode : public LazyFunction {
GField output_field{std::make_shared<AnonymousAttributeFieldInput>(
std::move(attribute_id),
*bsocket.typeinfo->base_cpp_type,
fmt::format(TIP_("{} node"), std::string_view(node_.label_or_name())))};
fmt::format(TIP_("{} node"), node_.label_or_name()))};
void *r_value = params.get_output_data_ptr(lf_index);
new (r_value) SocketValueVariant(std::move(output_field));
params.output_set(lf_index);