From: Philippe Proulx Date: Fri, 8 Dec 2023 18:28:53 +0000 (+0000) Subject: src/cpp-common: add bt2c::textLocStr() function X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=850d66dc51d3bf0afefbafb60dfd8beb655a5cf8;p=babeltrace.git src/cpp-common: add bt2c::textLocStr() function This new function formats a text location (`bt2c::TextLoc`) according to some format amongst: `bt2c::TextLocStrFmt::Offset`: Offset only. `bt2c::TextLocStrFmt::LineColNosAndOffset`: Line/column numbers and offset. `bt2c::TextLocStrFmt::LineColNos`: Line/column numbers only. Signed-off-by: Philippe Proulx Change-Id: I8afeb43a71c7103135f903f7d4eeb73ccf9e24c3 Reviewed-on: https://review.lttng.org/c/babeltrace/+/12679 --- diff --git a/src/Makefile.am b/src/Makefile.am index d5808506..4442a1f8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -177,6 +177,8 @@ cpp_common_libcpp_common_la_SOURCES = \ cpp-common/bt2c/std-int.hpp \ cpp-common/bt2c/text-loc.cpp \ cpp-common/bt2c/text-loc.hpp \ + cpp-common/bt2c/text-loc-str.cpp \ + cpp-common/bt2c/text-loc-str.hpp \ cpp-common/bt2c/type-traits.hpp \ cpp-common/bt2c/uuid.hpp \ cpp-common/bt2c/vector.hpp \ diff --git a/src/cpp-common/bt2c/text-loc-str.cpp b/src/cpp-common/bt2c/text-loc-str.cpp new file mode 100644 index 00000000..9c622673 --- /dev/null +++ b/src/cpp-common/bt2c/text-loc-str.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2016-2022 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#include + +#include "text-loc-str.hpp" + +namespace bt2c { + +std::string textLocStr(const TextLoc& loc, const TextLocStrFmt fmt) +{ + std::ostringstream ss; + + if (fmt == TextLocStrFmt::LineColNosAndOffset || fmt == TextLocStrFmt::LineColNos) { + ss << loc.naturalLineNo() << ':' << loc.naturalColNo(); + + if (fmt == TextLocStrFmt::LineColNosAndOffset) { + ss << ' '; + } + } + + if (fmt == TextLocStrFmt::Offset || fmt == TextLocStrFmt::LineColNosAndOffset) { + ss << "@ " << loc.offset() << " bytes"; + } + + return ss.str(); +} + +} /* namespace bt2c */ diff --git a/src/cpp-common/bt2c/text-loc-str.hpp b/src/cpp-common/bt2c/text-loc-str.hpp new file mode 100644 index 00000000..0dda233b --- /dev/null +++ b/src/cpp-common/bt2c/text-loc-str.hpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2016-2022 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2C_TEXT_LOC_STR_HPP +#define BABELTRACE_CPP_COMMON_BT2C_TEXT_LOC_STR_HPP + +#include + +#include "text-loc.hpp" + +namespace bt2c { + +/* + * Text location string format. + */ +enum class TextLocStrFmt +{ + Offset, + LineColNosAndOffset, + LineColNos, +}; + +/* + * Formats the text location `loc` as a string following the format + * `fmt`. + */ +std::string textLocStr(const TextLoc& loc, TextLocStrFmt fmt); + +} /* namespace bt2c */ + +#endif /* BABELTRACE_CPP_COMMON_BT2C_TEXT_LOC_STR_HPP */