8000 [ADT][bugfix] Fixed extra leading zero in uhextostr by dybv-sc · Pull Request #141097 · llvm/llvm-project · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[ADT][bugfix] Fixed extra leading zero in uhextostr #141097

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 30, 2025

Conversation

dybv-sc
Copy link
Contributor
@dybv-sc dybv-sc commented May 22, 2025

fixed bug: if fixed-width mode uhextostr() is used with value zero, it prints extra '0' character.

fixed bug: if fixed-width mode uhextostr() is used with value zero, it prints
extra '0' character.
@llvmbot
Copy link
Member
llvmbot commented May 22, 2025

@llvm/pr-subscribers-llvm-adt

Author: Bushev Dmitry (dybv-sc)

Changes

fixed bug: if fixed-width mode uhextostr() is used with value zero, it prints extra '0' character.


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

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/StringExtras.h (+2-1)
  • (modified) llvm/unittests/ADT/StringExtrasTest.cpp (+1)
diff --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index a2cc36dd8caad..7d81c63485be2 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -179,7 +179,8 @@ inline std::string utohexstr(uint64_t X, bool LowerCase = false,
   char Buffer[17];
   char *BufPtr = std::end(Buffer);
 
-  if (X == 0) *--BufPtr = '0';
+  if (X == 0 && !Width)
+    *--BufPtr = '0';
 
   for (unsigned i = 0; Width ? (i < Width) : X; ++i) {
     unsigned char Mod = static_cast<unsigned char>(X) & 15;
diff --git a/llvm/unittests/ADT/StringExtrasTest.cpp b/llvm/unittests/ADT/StringExtrasTest.cpp
index 36908a98aa2ae..fbaed38da5943 100644
--- a/llvm/unittests/ADT/StringExtrasTest.cpp
+++ b/llvm/unittests/ADT/StringExtrasTest.cpp
@@ -144,6 +144,7 @@ TEST(StringExtrasTest, ToAndFromHex) {
 }
 
 TEST(StringExtrasTest, UINT64ToHex) {
+  EXPECT_EQ(utohexstr(0x0u, false, 2), "00");
   EXPECT_EQ(utohexstr(0xA0u), "A0");
   EXPECT_EQ(utohexstr(0xA0u, false, 4), "00A0");
   EXPECT_EQ(utohexstr(0xA0u, false, 8), "000000A0");

@dybv-sc dybv-sc requested review from dwblaikie and kuhar May 22, 2025 16:24
@dybv-sc
Copy link
Contributor Author
dybv-sc commented May 29, 2025

@dwblaikie, @kuhar - ping

Copy link
Contributor
@kazutakahirata kazutakahirata left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@dybv-sc dybv-sc merged commit 4f392f9 into llvm:main May 30, 2025
13 checks passed
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
fixed bug: if fixed-width mode uhextostr() is used with value zero, it
prints extra '0' character.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0