src/cpp-common: add bt2c::textLocStr() function
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 8 Dec 2023 18:28:53 +0000 (18:28 +0000)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 4 Sep 2024 19:05:14 +0000 (15:05 -0400)
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 <eeppeliteloop@gmail.com>
Change-Id: I8afeb43a71c7103135f903f7d4eeb73ccf9e24c3
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12679

src/Makefile.am
src/cpp-common/bt2c/text-loc-str.cpp [new file with mode: 0644]
src/cpp-common/bt2c/text-loc-str.hpp [new file with mode: 0644]

index d58085064846d51e2c5eb1a90c0ad0200144ef43..4442a1f806753d9bb6e25267817706e0b7382e28 100644 (file)
@@ -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 (file)
index 0000000..9c62267
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2016-2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include <sstream>
+
+#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 (file)
index 0000000..0dda233
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2016-2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_TEXT_LOC_STR_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_TEXT_LOC_STR_HPP
+
+#include <string>
+
+#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 */
This page took 0.025081 seconds and 4 git commands to generate.