2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 module thrift.internal.ctfe;
26 * Simple eager join() for strings, std.algorithm.join isn't CTFEable yet.
28 string ctfeJoin(string[] strings, string separator = ", ") {
30 if (strings.length > 0) {
32 foreach (s; strings[1..$]) {
33 result ~= separator ~ s;
40 * A very primitive to!string() implementation for floating point numbers that
41 * is evaluatable at compile time.
43 * There is a wealth of problems associated with the algorithm used (e.g. 5.0
44 * prints as 4.999…, incorrect rounding, etc.), but a better alternative should
45 * be included with the D standard library instead of implementing it here.
47 string ctfeToString(T)(T val) if (isFloatingPoint!T) {
48 if (val is T.nan) return "nan";
49 if (val is T.infinity) return "inf";
50 if (val is -T.infinity) return "-inf";
51 if (val is 0.0) return "0";
52 if (val is -0.0) return "-0";
72 foreach (i; 0 .. T.dig) {
73 if (i == 1) result ~= '.';
75 auto first = cast(ubyte)b;
76 result ~= to!string(first);
80 if (b < pow(10.0, i - T.dig)) break;
84 if (magnitude != 0) result ~= "e" ~ to!string(magnitude);
90 static assert(ctfeToString(double.infinity) == "inf");
91 static assert(ctfeToString(-double.infinity) == "-inf");
92 static assert(ctfeToString(double.nan) == "nan");
93 static assert(ctfeToString(0.0) == "0");
94 static assert(ctfeToString(-0.0) == "-0");
95 static assert(ctfeToString(2.5) == "2.5");
96 static assert(ctfeToString(3.1415).startsWith("3.141"));
97 static assert(ctfeToString(2e-200) == "2e-200");