|
18 | 18 | #include <cstdlib> |
19 | 19 | #include <cmath> |
20 | 20 | #include <cctype> |
| 21 | +#include <climits> |
21 | 22 | #include <sstream> |
22 | 23 | #include <string> |
23 | 24 | #include <iomanip> |
|
30 | 31 | #include "wincrypt.h" |
31 | 32 | #endif |
32 | 33 |
|
| 34 | +#if defined __GNUC__ && ! defined __llvm__ |
| 35 | + #define GCC_VERSION (__GNUC__ * 10000 \ |
| 36 | + + __GNUC_MINOR__ * 100 \ |
| 37 | + + __GNUC_PATCHLEVEL__) |
| 38 | + #if GCC_VERSION < 40500 |
| 39 | + #include <tr1/random> |
| 40 | + #define IMPLEMENT_TR1 |
| 41 | + #define tr1ns std::tr1 |
| 42 | + #define uniform_real_distribution uniform_real |
| 43 | + #else |
| 44 | + #include <random> |
| 45 | + #define tr1ns std |
| 46 | + #endif |
| 47 | +#else |
| 48 | + #include <random> |
| 49 | + #define tr1ns std |
| 50 | +#endif |
| 51 | + |
33 | 52 | #define ARG(argname, argtype) get_arg<argtype>(argname, env, sig, pstate, backtrace) |
34 | 53 | #define ARGR(argname, argtype, lo, hi) get_arg_r(argname, env, sig, pstate, lo, hi, backtrace) |
35 | 54 | #define ARGM(argname, argtype, ctx) get_arg_m(argname, env, sig, pstate, backtrace, ctx) |
@@ -229,9 +248,42 @@ namespace Sass { |
229 | 248 | // random_device degrades sharply once the entropy pool |
230 | 249 | // is exhausted. For practical use, random_device is |
231 | 250 | // generally only used to seed a PRNG such as mt19937. |
232 | | - static std::mt19937 rand(static_cast<unsigned int>(GetSeed())); |
| 251 | + static tr1ns::mt19937 rand(static_cast<unsigned int>(GetSeed())); |
| 252 | + |
| 253 | + tr1ns::uniform_real_distribution<> std_dist(0, 1); |
| 254 | + #ifdef IMPLEMENT_TR1 |
| 255 | + tr1ns::variate_generator < |
| 256 | + tr1ns::mt19937, |
| 257 | + tr1ns::uniform_real_distribution <double> |
| 258 | + > gen_std_dist(rand, std_dist); |
| 259 | + #endif |
| 260 | + |
| 261 | + tr1ns::uniform_real_distribution<> full_dist(0, ULONG_MAX); |
| 262 | + #ifdef IMPLEMENT_TR1 |
| 263 | + tr1ns::variate_generator < |
| 264 | + tr1ns::mt19937, |
| 265 | + tr1ns::uniform_real_distribution <double> |
| 266 | + > gen_full_dist(rand, full_dist); |
| 267 | + #endif |
| 268 | + |
| 269 | + // helper function to retrieve a random number in interval |
| 270 | + // works around some compiler issues with older gcc versions |
| 271 | + static double random(double min, double max) |
| 272 | + { |
| 273 | + tr1ns::uniform_real_distribution<> distributor(min, max); |
| 274 | + #ifdef IMPLEMENT_TR1 |
| 275 | + tr1ns::variate_generator < |
| 276 | + tr1ns::mt19937, |
| 277 | + tr1ns::uniform_real_distribution <> |
| 278 | + > gen(rand, distributor); |
| 279 | + distributor(rand); |
| 280 | + return gen(); |
| 281 | + #else |
| 282 | + return distributor(rand); |
| 283 | + #endif |
| 284 | + } |
233 | 285 |
|
234 | | - // features |
| 286 | + // supported features lookup table |
235 | 287 | static std::set<std::string> features { |
236 | 288 | "global-variable-shadowing", |
237 | 289 | "extend-selector-pseudoclass", |
@@ -1188,13 +1240,19 @@ namespace Sass { |
1188 | 1240 | err << "Expected $limit to be an integer but got `" << v << "` for `random`"; |
1189 | 1241 | error(err.str(), pstate); |
1190 | 1242 | } |
1191 | | - std::uniform_real_distribution<> distributor(1, v + 1); |
1192 | | - uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand)); |
| 1243 | + // std::uniform_real_distribution<> distributor(1, v + 1); |
| 1244 | + // uint_fast32_t distributed = static_cast<uint_fast32_t>(distributor(rand)); |
| 1245 | + uint_fast32_t distributed = random(1, v + 1); |
1193 | 1246 | return SASS_MEMORY_NEW(ctx.mem, Number, pstate, (double)distributed); |
1194 | 1247 | } |
1195 | 1248 | else if (b) { |
1196 | | - std::uniform_real_distribution<> distributor(0, 1); |
1197 | | - double distributed = static_cast<double>(distributor(rand)); |
| 1249 | + // std::uniform_real_distribution<> distributor(0, 1); |
| 1250 | + // double distributed = static_cast<double>(distributor(rand)); |
| 1251 | + #ifdef IMPLEMENT_TR1 |
| 1252 | + double distributed = gen_std_dist(); |
| 1253 | + #else |
| 1254 | + double distributed = std_dist(rand); |
| 1255 | + #endif |
1198 | 1256 | return SASS_MEMORY_NEW(ctx.mem, Number, pstate, distributed); |
1199 | 1257 | } else if (v) { |
1200 | 1258 | throw Exception::InvalidArgumentType(pstate, "random", "$limit", "number", v); |
|
0 commit comments