toChars buffer access?

Salih Dincer salihdb at hotmail.com
Fri Jun 10 00:18:09 UTC 2022


On Thursday, 9 June 2022 at 21:16:48 UTC, Steven Schveighoffer 
wrote:
>
> My original example is to look up a key in an AA (not to store 
> one).

I have a simple code that works perfectly as well. But I'm having 
problems to convert except string.

```d
struct HexStack
{
   private
   {
     string[ubyte] convert;
     ubyte index;
     const ubyte first;
   }

   this(ubyte start)
   {
    this.index = start;
    this.first = start;
   }

   bool empty()
   {
     return index == first;
   }

   @property top()
   {
     return this.convert[first];
   }

   @property pop()
   {
     if(!empty) --this.index;
     return this.convert[this.index];
   }

   @property push(uint data)
   {
     import std.conv;
     this.convert[index] = toChars!(16, char,
                            LetterCase.upper)
                            (data).to!string;
     if(index < ubyte.max) this.index++;
     else this.index = ubyte.max;
   }
}

unittest
{
   HexStack foo = 'a';     // x'61'
            foo.push('A'); // x'41'
   assert(foo.top == "41");

   foo.push('B');
   foo.push('C');

   assert(foo.pop == "43");
   assert(foo.pop == "42");

   assert(foo.pop == "41");
   assert(foo.empty);

   assert(foo.pop == "41");
   assert(foo.pop == foo.top);

   HexStack bar = ubyte.max - 1;
            bar.push('A'); // x'41'
   assert(bar.top == "41");

   bar.push('B');
   bar.push('C');

   assert(bar.pop == "41");
   assert(bar.empty);

   assert(bar.pop == "41");
   assert(bar.pop == bar.top);
}
```
So hexstring is ok. When switching to other types, it converts 
each letter independently of each other and gives the following 
error:

> ChArray(Reserved, T = char)
neme1234567
den
eme
9,8,7,6,5,4,3,2,1,e,m,e,n,e,d,
d,e,n,e,m,e,1,2,3,4,5,6,7,8,9,
std.conv.ConvException@/usr/src/dmd/linux/bin64/../../src/phobos/std/conv.d(1877): Unexpected '0' when converting from type Result to type char
----------------
```d
??:? pure @safe char std.conv.toImpl!(char, 
std.conv.toChars!(16u, char, 0, 
uint).toChars(uint).Result).toImpl(std.conv.toChars!(16u, char, 
0, uint).toChars(uint).Result) [0x55a583a8c4de]
??:? pure @safe char 
std.conv.to!(char).to!(std.conv.toChars!(16u, char, 0, 
uint).toChars(uint).Result).to(std.conv.toChars!(16u, char, 0, 
uint).toChars(uint).Result) [0x55a583a8c201]
??:? pure char (immutable(char)[], 
char).ChArray.toChar!(10u).toChar() [0x55a583a8bf01]
??:? _Dmain [0x55a583a8018d]
Process finished with exit code 1.
```

I also get this error when I use char[]:

> ChArray.d(180): Error: only one index allowed to index `char`

**Test Code:**
```d
   uint A = 65;
   auto result = A.toChars!16; //hex '41'
   auto myCodePage = HexStack('a');
        myCodePage.push(A);

   assert(myCodePage.top == result.to!string);
   auto test = result.to!char[];
```

The only solution is to use .array:

```d
assert(result.array == ['4', '1']);
```

But I don't like this!

SDB at 79



More information about the Digitalmars-d mailing list