aboutsummaryrefslogtreecommitdiffstats
path: root/al/eax/exception.cpp
blob: cd32e11a2f2c7013512e39ca7ed6948c13ee5884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "config.h"

#include "exception.h"

#include <cassert>
#include <string>


EaxException::EaxException(std::string_view context, std::string_view message)
    : std::runtime_error{make_message(context, message)}
{
}
EaxException::~EaxException() = default;


std::string EaxException::make_message(std::string_view context, std::string_view message)
{
    auto what = std::string{};
    if(context.empty() && message.empty())
        return what;

    static constexpr char left_prefix[] = "[";
    static constexpr auto left_prefix_size = std::string::traits_type::length(left_prefix);

    static constexpr char right_prefix[] = "] ";
    static constexpr auto right_prefix_size = std::string::traits_type::length(right_prefix);

    what.reserve((!context.empty() ? left_prefix_size + context.size() + right_prefix_size : 0) +
        message.length() + 1);

    if(!context.empty())
    {
        what.append(left_prefix, left_prefix_size);
        what += context;
        what.append(right_prefix, right_prefix_size);
    }
    what += message;

    return what;
}