🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB

  1. #include "config.h"
  2. #include "exception.h"
  3. #include <cassert>
  4. #include <string>
  5. EaxException::EaxException(
  6. const char* context,
  7. const char* message)
  8. :
  9. std::runtime_error{make_message(context, message)}
  10. {
  11. }
  12. std::string EaxException::make_message(
  13. const char* context,
  14. const char* message)
  15. {
  16. const auto context_size = (context ? std::string::traits_type::length(context) : 0);
  17. const auto has_contex = (context_size > 0);
  18. const auto message_size = (message ? std::string::traits_type::length(message) : 0);
  19. const auto has_message = (message_size > 0);
  20. if (!has_contex && !has_message)
  21. {
  22. return std::string{};
  23. }
  24. static constexpr char left_prefix[] = "[";
  25. const auto left_prefix_size = std::string::traits_type::length(left_prefix);
  26. static constexpr char right_prefix[] = "] ";
  27. const auto right_prefix_size = std::string::traits_type::length(right_prefix);
  28. const auto what_size =
  29. (
  30. has_contex ?
  31. left_prefix_size + context_size + right_prefix_size :
  32. 0) +
  33. message_size +
  34. 1;
  35. auto what = std::string{};
  36. what.reserve(what_size);
  37. if (has_contex)
  38. {
  39. what.append(left_prefix, left_prefix_size);
  40. what.append(context, context_size);
  41. what.append(right_prefix, right_prefix_size);
  42. }
  43. if (has_message)
  44. {
  45. what.append(message, message_size);
  46. }
  47. return what;
  48. }