Skip to content

[NFC] Prefer str.resize(len) over str.substr(0, len) #91067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2024

Conversation

SimplyDanny
Copy link
Member

Fixes #90323.

@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label May 4, 2024
@llvmbot
Copy link
Member

llvmbot commented May 4, 2024

@llvm/pr-subscribers-llvm-analysis

Author: Danny Mösch (SimplyDanny)

Changes

Fixes #90323.


Full diff: https://github.com/llvm/llvm-project/pull/91067.diff

1 Files Affected:

  • (modified) llvm/include/llvm/Analysis/DOTGraphTraitsPass.h (+4-5)
diff --git a/llvm/include/llvm/Analysis/DOTGraphTraitsPass.h b/llvm/include/llvm/Analysis/DOTGraphTraitsPass.h
index da72fb511f82d8..7aea7a3b0f6d69 100644
--- a/llvm/include/llvm/Analysis/DOTGraphTraitsPass.h
+++ b/llvm/include/llvm/Analysis/DOTGraphTraitsPass.h
@@ -87,13 +87,12 @@ struct DOTGraphTraitsViewer
 };
 
 static inline void shortenFileName(std::string &FN, unsigned char len = 250) {
-
-  FN = FN.substr(0, len);
-
+  if (FN.length() > len)
+    FN.resize(len);
   auto strLen = FN.length();
   while (strLen > 0) {
-    if (auto it = nameObj.find(FN); it != nameObj.end()) {
-      FN = FN.substr(0, --len);
+    if (nameObj.find(FN) != nameObj.end()) {
+      FN.resize(--len);
     } else {
       nameObj.insert(FN);
       break;

@SimplyDanny SimplyDanny force-pushed the use-resize branch 2 times, most recently from d0d4ba0 to 4ff3e0e Compare May 8, 2024 19:17
Copy link
Contributor

@shahidiqbal13 shahidiqbal13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me

@SimplyDanny SimplyDanny merged commit 06ad863 into llvm:main May 10, 2024
@SimplyDanny SimplyDanny deleted the use-resize branch May 10, 2024 05:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

llvm/include/llvm/Analysis/DOTGraphTraitsPass.h: 2 * clumsy string resize ?
3 participants