[Issue 21926] Allow leading zeros in std.conv.octal

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed May 19 23:48:20 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=21926

Dlang Bot <dlang-bot at dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #3 from Dlang Bot <dlang-bot at dlang.rocks> ---
dlang/phobos pull request #8077 "Fix issue 21926 - Allow leadings zeros in
std.conv.octal(string)" was merged into master:

- feeca84e51303a6eea71ae65c021285439adc53c by Witold Baryluk:
  Fix issue 21926 - Allow leadings zeros in std.conv.octal

  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`.

https://github.com/dlang/phobos/pull/8077

--


More information about the Digitalmars-d-bugs mailing list