Skip to content
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

Fix issue 21926 - Allow leadings zeros in std.conv.octal(string) #8077

Merged
merged 1 commit into from
May 19, 2021

Conversation

baryluk
Copy link
Contributor

@baryluk baryluk commented May 14, 2021

First of all, the DDoc currently says that:

"Leading zero is allowed, but not required."

The current implementation doesn't respect that.

D lexer allows leading zero(s) also.

And when considering octal from string, there is no possibility
of confusion, as the comments in the code claim. Disallowing
leading zeros for octal conversion from string, does not help
with anything actually.

So lets allow leading zeros.

Allowing leading zeros, does help when implementing various
APIs (i.e. glibc, Linux kernel), where a lot of octal constant,
are defined with multiple leading zeros, for readability and alignment,
in C headers. Having to manually or automatically special case
these when porting such API definitions, is counter-productive.

Example from a Linux kernel (include/uapi/linux/stat.h):

#define S_IFMT  00170000
#define S_IFSOCK 0140000
#define S_IFLNK	 0120000
#define S_IFREG  0100000
#define S_IFBLK  0060000
#define S_IFDIR  0040000
#define S_IFCHR  0020000
#define S_IFIFO  0010000
#define S_ISUID  0004000
#define S_ISGID  0002000
#define S_ISVTX  0001000

With this patch, now it is trivial and easier to convert these to D:

...
enum S_ISVTX = octal!"0001000";

while being close to original. That helps with readability,
and long term maintenance.

In fact the run-time version provided by parse!(int)(string, 8)
also supports leading zeros already. So this makes octal
more consistent.

@baryluk baryluk requested a review from JackStouffer as a code owner May 14, 2021 22:05
@dlang-bot
Copy link
Contributor

dlang-bot commented May 14, 2021

Thanks for your pull request and interest in making D better, @baryluk! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
21926 enhancement Allow leading zeros in std.conv.octal

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#8077"

@baryluk
Copy link
Contributor Author

baryluk commented May 15, 2021

Fixed style errors

Copy link
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

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

Looks good. I'm not quite sure why the tests are segfaulting on azure.
Also is there a bug report for this? If so, please retitle the commit message "Fix issue #####: Allow leading zeros in std.conv.octal", if not please file.

@baryluk baryluk changed the title Allow leadings zeros in std.conv.octal(string) Fix 21926 - Allow leadings zeros in std.conv.octal(string) May 17, 2021
@baryluk baryluk changed the title Fix 21926 - Allow leadings zeros in std.conv.octal(string) Fix issue 21926 - Allow leadings zeros in std.conv.octal(string) May 17, 2021
@baryluk
Copy link
Contributor Author

baryluk commented May 17, 2021

Looks good. I'm not quite sure why the tests are segfaulting on azure.
Also is there a bug report for this? If so, please retitle the commit message "Fix issue #####: Allow leading zeros in std.conv.octal", if not please file.

Done. Added the issue and renamed the commit.

The azur failures are gone, it looks to have been some other unrelated issue. The buildkite issue now also looks unrelated.

@RazvanN7
Copy link
Collaborator

I'm quite sure a rebase will fix the failing builkite test.

First of all, the DDoc currently says that:

"Leading zero is allowed, but not required."

The current implementation doesn't respect that.

D lexer allows leading zero(s) also.

And when considering octal from string, there is no possibility
of confusion, as the comments in the code claim. Disallowing
leading zeros for octal conversion from string, does not help
with anything actually.

So lets allow leading zeros.

Allowing leading zeros, does help when implementing various
APIs (i.e. glibc, Linux kernel), where a lot of octal constant,
are defined with multiple leading zeros, for readability and
alignment, in C headers. Having to manually or automatically
special case these when porting such API definitions, is
counter-productive.

Example from a Linux kernel (`include/uapi/linux/stat.h`):

```c
#define S_IFMT  00170000
#define S_IFSOCK 0140000
#define S_IFLNK	 0120000
#define S_IFREG  0100000
#define S_IFBLK  0060000
#define S_IFDIR  0040000
#define S_IFCHR  0020000
#define S_IFIFO  0010000
#define S_ISUID  0004000
#define S_ISGID  0002000
#define S_ISVTX  0001000
```

With this patch, now it is trivial and easier to convert these to D:

```d
...
enum S_ISVTX = octal!"0001000";
```

while being close to original. That helps with readability,
and long term maintenance.

In fact the run-time version provided by `parse!(int)(string, 8)`
also supports leading zeros already. So this makes `octal`
more consistent `parse`.
@baryluk
Copy link
Contributor Author

baryluk commented May 19, 2021

"Rebased". All tests are passing now.

@dlang-bot dlang-bot merged commit a8d4b00 into dlang:master May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants