Thread overview
BetterC, int to string?
Jun 18, 2021
Mike Brown
Jun 18, 2021
jfondren
Jun 18, 2021
Dennis
June 18, 2021
Hi all,

I would like to convert a D string to an int - im doing this in a compile time function as well. conv throws an error due to it using TypeInfo?

How would I do this?

Kind regards,
Mike
June 18, 2021

On Friday, 18 June 2021 at 09:05:38 UTC, Mike Brown wrote:

>

Hi all,

I would like to convert a D string to an int - im doing this in a compile time function as well. conv throws an error due to it using TypeInfo?

How would I do this?

Kind regards,
Mike

BetterC has some CTFE-related bugs atm that might be the problem here.

You could if you need betterC use libc at runtime,

extern (C) void main() {
    import core.stdc.stdlib : atoi;
    import core.stdc.stdio : printf;

    const n = atoi("123"); // not enum
    printf("%d\n", n);
}

or if you're more concerned about limiting the GC, you can use @nogc annotations for that while still having more of the language at CTFE and elsewhere:

void main() @nogc nothrow {
    import core.stdc.stdio : printf;
    import std.conv : to;

    enum n = to!int("123"); // not const
    printf("%d\n", n);
}

If n were const in that last example:

exmaple.d(5): Error: `@nogc` function `D main` cannot call non-@nogc function `std.conv.to!int.to!string.to`
example.d(5): Error: function `std.conv.to!int.to!string.to` is not `nothrow`
example.d(1): Error: `nothrow` function `D main` may throw
June 18, 2021
On Friday, 18 June 2021 at 09:05:38 UTC, Mike Brown wrote:
>  im doing this in a compile time function as well.

If it's a compile time string you can use mixin()
June 18, 2021
On 6/18/21 5:05 AM, Mike Brown wrote:
> Hi all,
> 
> I would like to convert a D string to an int - im doing this in a compile time function as well. conv throws an error due to it using TypeInfo?
> 
> How would I do this?

std.conv.to really should support it, that seems like a bug.

But just FYI, doing string-to-int conversions is pretty easy (this doesn't handle overflow, but you can use core.checkedint to deal with it if necessary):

```d
int parseInt(string s) nothrow @safe @nogc
{
   bool isNeg = false;
   assert(s.length > 0);
   if(s[0] == '-') {isNeg = true; s = s[1 .. $];}
   int result = 0;
   foreach(c; s) {
      assert(c >= '0' && c <= '9');
      result = result * 10 + (c - '0');
   }
   return result;
}
```

-Steve
June 18, 2021
On 6/18/21 9:46 AM, Steven Schveighoffer wrote:
> On 6/18/21 5:05 AM, Mike Brown wrote:
>> Hi all,
>>
>> I would like to convert a D string to an int - im doing this in a compile time function as well. conv throws an error due to it using TypeInfo?
>>
>> How would I do this?
> 
> std.conv.to really should support it, that seems like a bug.
> 
> But just FYI, doing string-to-int conversions is pretty easy (this doesn't handle overflow, but you can use core.checkedint to deal with it if necessary):
> 
> ```d
> int parseInt(string s) nothrow @safe @nogc
> {
>     bool isNeg = false;
>     assert(s.length > 0);
>     if(s[0] == '-') {isNeg = true; s = s[1 .. $];}
>     int result = 0;
>     foreach(c; s) {
>        assert(c >= '0' && c <= '9');
>        result = result * 10 + (c - '0');
>     }

return isNeg ? -result : result;

> }
> ```