This provides no guarantees but is highly likely to work, and it's just for test purposes.
| 397 | |
| 398 | // This provides no guarantees but is highly likely to work, and it's just for test purposes. |
| 399 | Tempfile::Tempfile(bool ascii) |
| 400 | { |
| 401 | Utils::Random r; |
| 402 | |
| 403 | auto asciiChar = [&r]() -> std::string |
| 404 | { |
| 405 | // 10 numbers + 26 lowercase + 26 uppercase = 62 values. |
| 406 | uint8_t v = (uint8_t)std::uniform_int_distribution<>(0, 61)(r.generator()); |
| 407 | if (v < 10) |
| 408 | return std::string(1, '0' + v); |
| 409 | else if (v < 36) |
| 410 | return std::string(1, 'a' + v - 10); |
| 411 | return std::string(1, 'A' + v - 36); |
| 412 | }; |
| 413 | |
| 414 | auto latinChar = [&r]() -> std::string |
| 415 | { |
| 416 | // The first byte of UTF-8 latin is always C3. |
| 417 | std::string s(1, (char)0xC3); |
| 418 | |
| 419 | // The second byte of UTF-8 latin in in the range 0x80 - 0xBF; |
| 420 | const int range = 0xBF - 0x80; |
| 421 | uint8_t v = (uint8_t)std::uniform_int_distribution<>(0, range)(r.generator()); |
| 422 | s += (char)(0x80 + v); |
| 423 | return s; |
| 424 | }; |
| 425 | |
| 426 | m_name = temppath() + "tmp"; |
| 427 | if (ascii) |
| 428 | { |
| 429 | for (size_t i = 0; i < 20; ++i) |
| 430 | m_name += asciiChar(); |
| 431 | } |
| 432 | else // generate some latin and ascii chars |
| 433 | { |
| 434 | for (size_t i = 0; i < 20; ++i) |
| 435 | m_name += i % 2 ? asciiChar() : latinChar(); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | Tempfile::~Tempfile() |
| 440 | { |
nothing calls this directly
no test coverage detected