BLI: Add std::string variant of BLI_uniquename_cb #114052

Merged
Sergey Sharybin merged 2 commits from Sergey/blender:unique_name_stdstring into main 2023-10-24 11:36:02 +02:00

Allows to ensure unique name for cases when name is a dynamically
sized string.

Allows to ensure unique name for cases when name is a dynamically sized string.
Sergey Sharybin added 1 commit 2023-10-23 10:03:18 +02:00
5f8f136640 BLI: Add std::string variant of BLI_uniquename_cb
Allows to ensure unique name for cases when name is a dynamically
sized string.
Sergey Sharybin requested review from Hans Goudey 2023-10-23 10:03:24 +02:00
Sergey Sharybin requested review from Falk David 2023-10-23 10:03:29 +02:00
Hans Goudey approved these changes 2023-10-23 10:10:35 +02:00
@ -426,1 +429,4 @@
std::string BLI_uniquename_cb(blender::FunctionRef<bool(blender::StringRef)> unique_check,
const char delim,
const blender::StringRef name)
Member

There may be an opportunity to avoid a copy by taking a std::string by value as an argument. That way you could call BLI_uniquename_cb(..., ..., std::move(string));

Since this has to store a local copy of the string anyway, that shouldn't be worse in any case.

But maybe that's not worth it right now.

There may be an opportunity to avoid a copy by taking a `std::string` by value as an argument. That way you could call `BLI_uniquename_cb(..., ..., std::move(string));` Since this has to store a local copy of the string anyway, that shouldn't be worse in any case. But maybe that's not worth it right now.
Falk David requested changes 2023-10-23 10:30:22 +02:00
@ -0,0 +23,4 @@
EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', ""), "");
EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', "Baz"), "Baz");
EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', "Foo"), "Foo.001");
EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', "Baz.001"), "Baz.002");
Member

We should also have a check like EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', "Bar.003"), "Bar.001"); if current_names has a "Bar.003".
And maybe another that tries adding .999 and returns .1000.

We should also have a check like `EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', "Bar.003"), "Bar.001");` if `current_names` has a `"Bar.003"`. And maybe another that tries adding `.999` and returns `.1000`.

EXPECT_EQ(BLI_uniquename_cb(random_prefix + to_string(i)), random_prefix + to_string(i + 1));
EXPECT_EQ(BLI_uniquename_cb(random_prefix + to_string(i)), random_prefix + to_string(first_true(all_holded_indices)));
... ?)

`EXPECT_EQ(BLI_uniquename_cb(random_prefix + to_string(i)), random_prefix + to_string(i + 1));` `EXPECT_EQ(BLI_uniquename_cb(random_prefix + to_string(i)), random_prefix + to_string(first_true(all_holded_indices)));` ... ?)
Author
Owner

@filedescriptor I've added tests for the Bar.003 and the .999. One thing to note is that EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', "Bar.003"), "Bar.001"); is not going to pass a test. This variant of unique naming only goes up. This behavior is consistent for current C function and the new C++ function.

@filedescriptor I've added tests for the `Bar.003` and the `.999`. One thing to note is that `EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', "Bar.003"), "Bar.001");` is not going to pass a test. This variant of unique naming only goes up. This behavior is consistent for current C function and the new C++ function.
Author
Owner

@mod_moder I am not sure i'll go this route. From the code coverage perspective I think we are all good. From the semantic/logic behavior on user level of this API such automated testing doesn't help much for understanding I think.

@mod_moder I am not sure i'll go this route. From the code coverage perspective I think we are all good. From the semantic/logic behavior on user level of this API such automated testing doesn't help much for understanding I think.
Member

This behavior is consistent for current C function and the new C++ function.

I'm pretty sure that's not true. When I tested it in Blender 3.6, having an object named "Bar.001" and "Bar.003", naming another object "Bar.003" made it "Bar.002".

> This behavior is consistent for current C function and the new C++ function. I'm pretty sure that's not true. When I tested it in Blender 3.6, having an object named "Bar.001" and "Bar.003", naming another object "Bar.003" made it "Bar.002".
Author
Owner

See the behavior of the C version of the funciton. It did not change with this PR, and the behavior is consistent when handling the Bar.003.

The ID naming does not use these functions to ensure naming. They use BLI_libblock_ensure_unique_name, which does more comprehensive things like name map caching (for faster addition of objects to a complex files, avoiding N^2 complexity when adding N objects). It also does smarter suffix lookup by trying to reuse minimal available suffix for values below ~1000.

See the behavior of the C version of the funciton. It did not change with this PR, and the behavior is consistent when handling the `Bar.003`. The ID naming does not use these functions to ensure naming. They use `BLI_libblock_ensure_unique_name`, which does more comprehensive things like name map caching (for faster addition of objects to a complex files, avoiding N^2 complexity when adding N objects). It also does smarter suffix lookup by trying to reuse minimal available suffix for values below ~1000.
Member

I see makes sense.

I see makes sense.
Sergey Sharybin added 1 commit 2023-10-24 10:55:53 +02:00
87152fd0ec Expand testing of the BLI_uniquename_cb
- Add tests for C version of the function, for behavior comparison.
- Add tests of more cases of naming collisions for C++ version.
Falk David approved these changes 2023-10-24 11:32:02 +02:00
Sergey Sharybin merged commit 7afa5aaa59 into main 2023-10-24 11:36:02 +02:00
Sergey Sharybin deleted branch unique_name_stdstring 2023-10-24 11:36:03 +02:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#114052
No description provided.