Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
BadFormatException.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
8
9#include "BadFormatException.h"
10
11/*
12 * Design Note:
13 *
14 * o Chose to include lineNumber etc stuff in message by default because in apps where this is not desired -
15 * (fancy gui apps) they are more likely to be the ones to override the exception mapping to message anyhow,
16 * and tune it themselves. Its the simple apps that do little but dump the string representations of a
17 * message that are more likely to want to know the line number (a bit of a guess).
18 */
19
20using namespace Stroika::Foundation;
22
23/*
24 ********************************************************************************
25 ************************ DataExchange::BadFormatException **********************
26 ********************************************************************************
27 */
28namespace {
29 String mkMessage_OffsetInfo_ (const optional<unsigned int>& lineNumber, const optional<unsigned int>& columnNumber, const optional<uint64_t>& fileOffset)
30 {
31 StringBuilder result;
32 if (lineNumber) {
33 result += Format ("Line {}"_f, *lineNumber);
34 if (columnNumber) {
35 result += Format ("; Column {}"_f, *columnNumber);
36 }
37 }
38 if (fileOffset) {
39 if (not result.empty ()) {
40 result += "; "sv;
41 }
42 result += Format ("; FileOffset {}"_f, *fileOffset);
43 }
44 return result;
45 }
46 inline String mkMessage_ ()
47 {
48 return "Badly formatted input"sv;
49 }
50 inline String mkMessage_ (const String& details)
51 {
52 return details.empty () ? mkMessage_ () : details;
53 }
54 String mkMessage_ (const optional<unsigned int>& lineNumber, const optional<unsigned int>& columnNumber, optional<uint64_t> fileOffset)
55 {
56 StringBuilder msg = mkMessage_ ();
57 String lineInfoExtra = mkMessage_OffsetInfo_ (lineNumber, columnNumber, fileOffset);
58 if (not lineInfoExtra.empty ()) {
59 msg << " ("sv << lineInfoExtra << ")."sv;
60 }
61 return msg;
62 }
63 String mkMessage_ (const String& details, const optional<unsigned int>& lineNumber, const optional<unsigned int>& columnNumber,
64 const optional<uint64_t>& fileOffset)
65 {
66 StringBuilder msg = mkMessage_ (details);
67 String lineInfoExtra = mkMessage_OffsetInfo_ (lineNumber, columnNumber, fileOffset);
68 if (not lineInfoExtra.empty ()) {
69 msg << " ("sv << lineInfoExtra << ")."sv;
70 }
71 return msg;
72 }
73}
74
75DataExchange::BadFormatException::BadFormatException ()
76 : inherited{mkMessage_ ()}
77{
78}
79
80DataExchange::BadFormatException::BadFormatException (const String& details)
81 : inherited{mkMessage_ (details)}
82 , fDetails_{details}
83{
84}
85
86DataExchange::BadFormatException::BadFormatException (const String& details, const optional<unsigned int>& lineNumber,
87 const optional<unsigned int>& columnNumber, const optional<uint64_t>& fileOffset)
88 : inherited{mkMessage_ (details, lineNumber, columnNumber, fileOffset)}
89 , fLineNumber_{lineNumber}
90 , fColumnNumber_{columnNumber}
91 , fFileOffset_{fileOffset}
92 , fDetails_{details}
93{
94}
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201