aboutsummaryrefslogtreecommitdiffstats
path: root/al/eax_exception.cpp
blob: e36357932f3bbf7512efb93fdfae50af8cd7feb5 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "eax_exception.h"

#include <cassert>

#include <string>


EaxException::EaxException(
    const char* context,
    const char* message)
    :
    std::runtime_error{make_message(context, message)}
{
}

std::string EaxException::make_message(
    const char* context,
    const char* message)
{
    const auto context_size = (context ? std::string::traits_type::length(context) : 0);
    const auto has_contex = (context_size > 0);

    const auto message_size = (message ? std::string::traits_type::length(message) : 0);
    const auto has_message = (message_size > 0);

    if (!has_contex && !has_message)
    {
        return std::string{};
    }

    constexpr auto left_prefix = "[";
    const auto left_prefix_size = std::string::traits_type::length(left_prefix);

    constexpr auto right_prefix = "] ";
    const auto right_prefix_size = std::string::traits_type::length(right_prefix);

    const auto what_size =
        (
            has_contex ?
            left_prefix_size + context_size + right_prefix_size :
            0) +
        message_size +
        1;

    auto what = std::string{};
    what.reserve(what_size);

    if (has_contex)
    {
        what.append(left_prefix, left_prefix_size);
        what.append(context, context_size);
        what.append(right_prefix, right_prefix_size);
    }

    if (has_message)
    {
        what.append(message, message_size);
    }

    return what;
}