Skip to content

Commit aa61ea5

Browse files
committed
wip
1 parent 322517e commit aa61ea5

9 files changed

Lines changed: 173 additions & 180 deletions

File tree

samples/quickstart.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,14 @@ struct ErrorHandlerExample
220220
ryml::Callbacks callbacks();
221221
public:
222222
// these are the functions that we want to execute on error
223-
[[noreturn]] void on_error_basic(const char* msg, size_t len, ryml::ErrorDataBasic const& errdata);
224-
[[noreturn]] void on_error_parse(const char* msg, size_t len, ryml::ErrorDataParse const& errdata);
225-
[[noreturn]] void on_error_visit(const char* msg, size_t len, ryml::ErrorDataVisit const& errdata);
223+
[[noreturn]] void on_error_basic(ryml::csubstr msg, ryml::ErrorDataBasic const& errdata);
224+
[[noreturn]] void on_error_parse(ryml::csubstr msg, ryml::ErrorDataParse const& errdata);
225+
[[noreturn]] void on_error_visit(ryml::csubstr msg, ryml::ErrorDataVisit const& errdata);
226226
public:
227227
// these are the functions that we set ryml to call
228-
[[noreturn]] static void s_error_basic(const char* msg, size_t len, ryml::ErrorDataBasic const& errdata, void *this_);
229-
[[noreturn]] static void s_error_parse(const char* msg, size_t len, ryml::ErrorDataParse const& errdata, void *this_);
230-
[[noreturn]] static void s_error_visit(const char* msg, size_t len, ryml::ErrorDataVisit const& errdata, void *this_);
228+
[[noreturn]] static void s_error_basic(ryml::csubstr msg, ryml::ErrorDataBasic const& errdata, void *this_);
229+
[[noreturn]] static void s_error_parse(ryml::csubstr msg, ryml::ErrorDataParse const& errdata, void *this_);
230+
[[noreturn]] static void s_error_visit(ryml::csubstr msg, ryml::ErrorDataVisit const& errdata, void *this_);
231231
public:
232232
// the handlers save these fields to be able to check on them
233233
// after the error is triggered. They are only valid after an
@@ -4902,7 +4902,7 @@ err: (here)
49024902
// containing location and source context indication:
49034903
std::string full;
49044904
auto dumpfn = [&full](ryml::csubstr s) { full.append(s.str, s.len); };
4905-
ryml::err_parse_format(dumpfn, msg.str, msg.len, exc.errdata_parse);
4905+
ryml::err_parse_format(dumpfn, msg, exc.errdata_parse);
49064906
full += '\n';
49074907
ryml::location_format_with_context(dumpfn, exc.errdata_parse.ymlloc, ymlsrc, "err", 3);
49084908
CHECK(ryml::to_csubstr(full).begins_with("file.yml:3: col=4 (12B): ERROR: [parse] invalid character: '['"));
@@ -5681,13 +5681,13 @@ bool ErrorHandlerExample::check_error_occurs(Fn &&fn)
56815681
/** this is where the callback implementation goes. Remember that it must not return.
56825682
* @ingroup doc_sample_helpers
56835683
* */
5684-
[[noreturn]] void ErrorHandlerExample::on_error_basic(const char* msg, size_t len, ryml::ErrorDataBasic const& errdata)
5684+
[[noreturn]] void ErrorHandlerExample::on_error_basic(ryml::csubstr msg, ryml::ErrorDataBasic const& errdata)
56855685
{
5686-
saved_msg_short.assign(msg, len);
5686+
saved_msg_short.assign(msg.str, msg.len);
56875687
// build a full error message with location
56885688
ryml::err_basic_format([this](ryml::csubstr s){
56895689
saved_msg_full.append(s.str, s.len);
5690-
}, msg, len, errdata);
5690+
}, msg, errdata);
56915691
saved_msg_full_with_context = saved_msg_full;
56925692
saved_basic_loc = errdata.location;
56935693
stopexec(saved_msg_short);
@@ -5696,13 +5696,13 @@ bool ErrorHandlerExample::check_error_occurs(Fn &&fn)
56965696
* @ingroup doc_sample_helpers
56975697
* @see ryml::format_location_context
56985698
* */
5699-
[[noreturn]] void ErrorHandlerExample::on_error_parse(const char* msg, size_t len, ryml::ErrorDataParse const& errdata)
5699+
[[noreturn]] void ErrorHandlerExample::on_error_parse(ryml::csubstr msg, ryml::ErrorDataParse const& errdata)
57005700
{
5701-
saved_msg_short.assign(msg, len);
5701+
saved_msg_short.assign(msg.str, msg.len);
57025702
// build a full error message with location
57035703
ryml::err_parse_format([this](ryml::csubstr s){
57045704
saved_msg_full.append(s.str, s.len);
5705-
}, msg, len, errdata);
5705+
}, msg, errdata);
57065706
// To add the source context, the source buffer is required. If
57075707
// the caller is interested in enriching the full message with the
57085708
// source buffer context, he can ensure that the source buffer is
@@ -5716,13 +5716,13 @@ bool ErrorHandlerExample::check_error_occurs(Fn &&fn)
57165716
/** this is where the callback implementation goes. Remember that it must not return.
57175717
* @ingroup doc_sample_helpers
57185718
* */
5719-
[[noreturn]] void ErrorHandlerExample::on_error_visit(const char* msg, size_t len, ryml::ErrorDataVisit const& errdata)
5719+
[[noreturn]] void ErrorHandlerExample::on_error_visit(ryml::csubstr msg, ryml::ErrorDataVisit const& errdata)
57205720
{
5721-
saved_msg_short.assign(msg, len);
5721+
saved_msg_short.assign(msg.str, msg.len);
57225722
// build a full error message with location
57235723
ryml::err_visit_format([this](ryml::csubstr s){
57245724
saved_msg_full.append(s.str, s.len);
5725-
}, msg, len, errdata);
5725+
}, msg, errdata);
57265726
// To add the source context, the source buffer is required. If
57275727
// the caller is interested in enriching the full message with the
57285728
// source buffer context, he can ensure that the source buffer is
@@ -5736,19 +5736,19 @@ bool ErrorHandlerExample::check_error_occurs(Fn &&fn)
57365736
}
57375737

57385738
/** trampoline function to call the object's method */
5739-
[[noreturn]] void ErrorHandlerExample::s_error_basic(const char* msg, size_t len, ryml::ErrorDataBasic const& errdata, void *this_)
5739+
[[noreturn]] void ErrorHandlerExample::s_error_basic(ryml::csubstr msg, ryml::ErrorDataBasic const& errdata, void *this_)
57405740
{
5741-
static_cast<ErrorHandlerExample*>(this_)->on_error_basic(msg, len, errdata);
5741+
static_cast<ErrorHandlerExample*>(this_)->on_error_basic(msg, errdata);
57425742
}
57435743
/** trampoline function to call the object's method */
5744-
[[noreturn]] void ErrorHandlerExample::s_error_parse(const char* msg, size_t len, ryml::ErrorDataParse const& errdata, void *this_)
5744+
[[noreturn]] void ErrorHandlerExample::s_error_parse(ryml::csubstr msg, ryml::ErrorDataParse const& errdata, void *this_)
57455745
{
5746-
static_cast<ErrorHandlerExample*>(this_)->on_error_parse(msg, len, errdata);
5746+
static_cast<ErrorHandlerExample*>(this_)->on_error_parse(msg, errdata);
57475747
}
57485748
/** trampoline function to call the object's method */
5749-
[[noreturn]] void ErrorHandlerExample::s_error_visit(const char* msg, size_t len, ryml::ErrorDataVisit const& errdata, void *this_)
5749+
[[noreturn]] void ErrorHandlerExample::s_error_visit(ryml::csubstr msg, ryml::ErrorDataVisit const& errdata, void *this_)
57505750
{
5751-
static_cast<ErrorHandlerExample*>(this_)->on_error_visit(msg, len, errdata);
5751+
static_cast<ErrorHandlerExample*>(this_)->on_error_visit(msg, errdata);
57525752
}
57535753

57545754

src/c4/yml/common.cpp

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,34 @@ C4_NO_INLINE void endmsg()
3333
fflush(stderr); // NOLINT
3434
}
3535

36-
[[noreturn]] C4_NO_INLINE void error_basic_impl(const char* msg, size_t length, ErrorDataBasic const& errdata, void * /*user_data*/)
36+
[[noreturn]] C4_NO_INLINE void error_basic_impl(csubstr msg, ErrorDataBasic const& errdata, void * /*user_data*/)
3737
{
38-
err_basic_format(dump2stderr, msg, length, errdata);
38+
err_basic_format(dump2stderr, msg, errdata);
3939
endmsg();
4040
#ifdef _RYML_WITH_EXCEPTIONS
41-
throw BasicException(msg, length, errdata);
41+
throw BasicException(msg, errdata);
4242
#else
4343
abort(); // LCOV_EXCL_LINE
4444
#endif
4545
}
4646

47-
[[noreturn]] C4_NO_INLINE void error_parse_impl(const char* msg, size_t length, ErrorDataParse const& errdata, void * /*user_data*/)
47+
[[noreturn]] C4_NO_INLINE void error_parse_impl(csubstr msg, ErrorDataParse const& errdata, void * /*user_data*/)
4848
{
49-
err_parse_format(dump2stderr, msg, length, errdata);
49+
err_parse_format(dump2stderr, msg, errdata);
5050
endmsg();
5151
#ifdef _RYML_WITH_EXCEPTIONS
52-
throw ParseException(msg, length, errdata);
52+
throw ParseException(msg, errdata);
5353
#else
5454
abort(); // LCOV_EXCL_LINE
5555
#endif
5656
}
5757

58-
[[noreturn]] C4_NO_INLINE void error_visit_impl(const char* msg, size_t length, ErrorDataVisit const& errdata, void * /*user_data*/)
58+
[[noreturn]] C4_NO_INLINE void error_visit_impl(csubstr msg, ErrorDataVisit const& errdata, void * /*user_data*/)
5959
{
60-
err_visit_format(dump2stderr, msg, length, errdata);
60+
err_visit_format(dump2stderr, msg, errdata);
6161
endmsg();
6262
#ifdef _RYML_WITH_EXCEPTIONS
63-
throw VisitException(msg, length, errdata);
63+
throw VisitException(msg, errdata);
6464
#else
6565
abort(); // LCOV_EXCL_LINE
6666
#endif
@@ -70,10 +70,7 @@ void* allocate_impl(size_t length, void * /*hint*/, void * /*user_data*/)
7070
{
7171
void *mem = ::malloc(length);
7272
if(mem == nullptr)
73-
{
74-
const char msg[] = "could not allocate memory"; // LCOV_EXCL_LINE
75-
error_basic_impl(msg, sizeof(msg)-1, ErrorDataBasic{RYML_LOC_HERE()}, nullptr); // LCOV_EXCL_LINE
76-
}
73+
error_basic_impl("could not allocate memory", ErrorDataBasic{RYML_LOC_HERE()}, nullptr); // LCOV_EXCL_LINE
7774
return mem;
7875
}
7976

@@ -202,7 +199,7 @@ C4_NORETURN C4_NO_INLINE void err_basic(ErrorDataBasic const& errdata, const cha
202199
C4_NORETURN C4_NO_INLINE void err_basic(Callbacks const& callbacks, ErrorDataBasic const& errdata, const char* msg_)
203200
{
204201
csubstr msg = to_csubstr(msg_);
205-
callbacks.m_error_basic(msg.str, msg.len, errdata, callbacks.m_user_data);
202+
callbacks.m_error_basic(msg, errdata, callbacks.m_user_data);
206203
abort(); // the call above should not return, so force it here in case it does // LCOV_EXCL_LINE
207204
C4_UNREACHABLE_AFTER_ERR();
208205
}
@@ -217,10 +214,10 @@ C4_NORETURN C4_NO_INLINE void err_parse(Callbacks const& callbacks, ErrorDataPar
217214
{
218215
csubstr msg = to_csubstr(msg_);
219216
if(callbacks.m_error_parse)
220-
callbacks.m_error_parse(msg.str, msg.len, errdata, callbacks.m_user_data);
217+
callbacks.m_error_parse(msg, errdata, callbacks.m_user_data);
221218
// fall to basic error if there is no parse handler set
222219
else if(callbacks.m_error_basic)
223-
callbacks.m_error_basic(msg.str, msg.len, errdata.ymlloc, callbacks.m_user_data);
220+
callbacks.m_error_basic(msg, errdata.ymlloc, callbacks.m_user_data);
224221
abort(); // the call above should not return, so force it here in case it does // LCOV_EXCL_LINE
225222
C4_UNREACHABLE_AFTER_ERR();
226223
}
@@ -235,53 +232,53 @@ C4_NORETURN C4_NO_INLINE void err_visit(Callbacks const& callbacks, ErrorDataVis
235232
{
236233
csubstr msg = to_csubstr(msg_);
237234
if(callbacks.m_error_visit)
238-
callbacks.m_error_visit(msg.str, msg.len, errdata, callbacks.m_user_data);
235+
callbacks.m_error_visit(msg, errdata, callbacks.m_user_data);
239236
// fall to basic error if there is no visit handler set
240237
else if(callbacks.m_error_basic)
241-
callbacks.m_error_basic(msg.str, msg.len, errdata.cpploc, callbacks.m_user_data);
238+
callbacks.m_error_basic(msg, errdata.cpploc, callbacks.m_user_data);
242239
abort(); // the call above should not return, so force it here in case it does // LCOV_EXCL_LINE
243240
C4_UNREACHABLE_AFTER_ERR();
244241
}
245242

246243

247244

248245
#ifdef _RYML_WITH_EXCEPTIONS
249-
BasicException::BasicException(const char *msg_, size_t len_, ErrorDataBasic const& errdata_) noexcept
246+
BasicException::BasicException(csubstr msg_, ErrorDataBasic const& errdata_) noexcept
250247
: errdata_basic(errdata_)
251248
, msg()
252249
{
253250
msg[0] = '\0';
254-
if(len_)
251+
if(msg_.len)
255252
{
256-
if(len_ >= sizeof(msg))
253+
if(msg_.len >= sizeof(msg))
257254
{
258255
static_assert(sizeof(msg) > 6u, "message buffer too small");
259-
len_ = sizeof(msg) - 6u;
260-
msg[len_ ] = '[';
261-
msg[len_ + 1u] = '.';
262-
msg[len_ + 2u] = '.';
263-
msg[len_ + 3u] = '.';
264-
msg[len_ + 4u] = ']';
265-
msg[len_ + 5u] = '\0';
256+
msg_.len = sizeof(msg) - 6u;
257+
msg[msg_.len ] = '[';
258+
msg[msg_.len + 1u] = '.';
259+
msg[msg_.len + 2u] = '.';
260+
msg[msg_.len + 3u] = '.';
261+
msg[msg_.len + 4u] = ']';
262+
msg[msg_.len + 5u] = '\0';
266263
}
267-
memcpy(msg, msg_, len_);
264+
memcpy(msg, msg_.str, msg_.len);
268265
}
269266
}
270-
ParseException::ParseException(const char *msg_, size_t len_, ErrorDataParse const& errdata_) noexcept
271-
: BasicException(msg_, len_, {errdata_.ymlloc})
267+
ParseException::ParseException(csubstr msg_, ErrorDataParse const& errdata_) noexcept
268+
: BasicException(msg_, {errdata_.ymlloc})
272269
, errdata_parse(errdata_)
273270
{
274271
}
275-
VisitException::VisitException(const char *msg_, size_t len_, ErrorDataVisit const& errdata_) noexcept
276-
: BasicException(msg_, len_, {errdata_.cpploc})
272+
VisitException::VisitException(csubstr msg_, ErrorDataVisit const& errdata_) noexcept
273+
: BasicException(msg_, {errdata_.cpploc})
277274
, errdata_visit(errdata_)
278275
{
279276
}
280277
#endif // _RYML_WITH_EXCEPTIONS
281278

282279

283280
namespace detail {
284-
csubstr _get_text_region(csubstr text, size_t pos, size_t num_lines_before, size_t num_lines_after)
281+
RYML_EXPORT csubstr _get_text_region(csubstr text, size_t pos, size_t num_lines_before, size_t num_lines_after)
285282
{
286283
if(pos > text.len)
287284
return text.last(0);

0 commit comments

Comments
 (0)