www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - BetterC, int to string?

reply Mike Brown <mikey.be gmail.com> writes:
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
Jun 18
next sibling parent jfondren <julian.fondren gmail.com> writes:
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](https://issues.dlang.org/buglist.cgi?f1=keywords&list_id=236535&o1=allwordssubstr&query_format=advanced&resolution=-- &v1=betterC%20CTFE) atm that might be the problem here. You could if you need betterC use libc at runtime, ```d 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: ```d 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 ```
Jun 18
prev sibling next sibling parent Dennis <dkorpel gmail.com> writes:
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()
Jun 18
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Jun 18
parent Steven Schveighoffer <schveiguy gmail.com> writes:
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;
 }
 ```
Jun 18