If I write
enum chars = digits ~ uppercase;
will the string be concatenated at compile time? I'm assuming it will. If I replace it with a string literal or a CTFE function I can't measure any significant performance differences (even calling it a hundred million times). I do get a difference if I replace enum with const. I've been told it's inefficient to write it like this. I thought it was kind of convenient and I don't see the inefficiency. (BTW, the line is in a function that's called recursively).
The full code (converting to a numeral system with a different base)
import std.string;
string toBase(long n, int b)
in {
assert(2 <= b && b <= 35);
} body {
static string sign;
if (n < 0) {
n *= -1;
sign = "-";
}
enum chars = digits ~ uppercase;
size_t r = cast(size_t)(n % b);
if (n == r) {
return sign ~ chars[r];
}
return toBase((n - r) / b, b) ~ chars[r];
}
Edit: updated code, in response to comments, not relevant to the question
string toBase(long n, int b)
in {
assert(2 <= b && b <= 35);
} body {
enum chars = digits ~ uppercase;
long r = n % b;
char c = chars[cast(size_t) abs(r)];
if (n == r) {
return (n < 0 ? "-" : "") ~ c;
}
return toBase((n - r) / b, b) ~ c;
}