struct Location size

Dennis dkorpel at gmail.com
Tue May 9 09:35:41 UTC 2023


On Tuesday, 9 May 2023 at 00:24:44 UTC, Walter Bright wrote:
> So, for great glory, can anyone come up with a clever scheme 
> that uses only 32 bits?

I put mine in the PR already: 
https://github.com/dlang/dmd/pull/15199#issuecomment-1538120181

It's the same idea as Adam's. You really only need a file offset, 
which currently already exists, but only for DMD as a lib:

```D
version (LocOffset)
     uint fileOffset; /// utf8 code unit index relative to start 
of file, starting from 0
```

Line number and column number can be computed when needed, and 
can be accelerated with a binary search in a list of line numbers 
sorted by file offset (because of #line directives lines won't be 
monotonically increasing and need to be stored in the list).

To handle multiple files, add the sum of all previous file sizes 
to the file offset and have a mapping from the resulting 'global 
offset' to local file offset.

The cool thing is that DMDLIB can still access `fileOffset`, 
while keeping the small 4 byte `Loc`!



More information about the Digitalmars-d mailing list