src/cpp-common: add `bt2c::TextLoc` class
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 17 Apr 2024 17:49:55 +0000 (13:49 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 4 Sep 2024 19:05:14 +0000 (15:05 -0400)
This patch adds the `bt2c::TextLoc` class, defined in `text-loc.hpp` and
implemented in `text-loc.cpp`.

A text location is a location within some text: an offset (in bytes), a
line number, and a column number.

This is part of an effort to implement a JSON parser to
support CTF2‑SPEC‑2.0 [1].

[1]: https://diamon.org/ctf/CTF2-SPEC-2.0.html

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ice9cab35041f1637d220deb6af8f52dbfc8cd290
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7408
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12678

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

index 771c9de56fa13b2890ba891e1afddd4acfcef69c..d58085064846d51e2c5eb1a90c0ad0200144ef43 100644 (file)
@@ -175,6 +175,8 @@ cpp_common_libcpp_common_la_SOURCES = \
        cpp-common/bt2c/read-fixed-len-int.hpp \
        cpp-common/bt2c/safe-ops.hpp \
        cpp-common/bt2c/std-int.hpp \
+       cpp-common/bt2c/text-loc.cpp \
+       cpp-common/bt2c/text-loc.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.cpp b/src/cpp-common/bt2c/text-loc.cpp
new file mode 100644 (file)
index 0000000..58bb697
--- /dev/null
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2016-2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#include "text-loc.hpp"
+
+namespace bt2c {
+
+TextLoc::TextLoc(const unsigned long long offset, const unsigned long long lineNo,
+                 const unsigned long long colNo) noexcept :
+    _mOffset {offset},
+    _mLineNo {lineNo}, _mColNo {colNo}
+{
+}
+
+} /* namespace bt2c */
diff --git a/src/cpp-common/bt2c/text-loc.hpp b/src/cpp-common/bt2c/text-loc.hpp
new file mode 100644 (file)
index 0000000..2a80368
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2016-2022 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_TEXT_LOC_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_TEXT_LOC_HPP
+
+namespace bt2c {
+
+/*
+ * Location in a multiline text.
+ */
+class TextLoc final
+{
+public:
+    /*
+     * Builds a text location which targets offset `offset` bytes,
+     * zero-based line number `lineNo`, and zero-based column number
+     * `colNo`.
+     */
+    explicit TextLoc(unsigned long long offset = 0, unsigned long long lineNo = 0,
+                     unsigned long long colNo = 0) noexcept;
+
+    /*
+     * Offset (bytes).
+     */
+    unsigned long long offset() const noexcept
+    {
+        return _mOffset;
+    }
+
+    /*
+     * Line number (zero-based).
+     */
+    unsigned long long lineNo() const noexcept
+    {
+        return _mLineNo;
+    }
+
+    /*
+     * Column number (zero-based).
+     */
+    unsigned long long colNo() const noexcept
+    {
+        return _mColNo;
+    }
+
+    /*
+     * Line number (one-based).
+     */
+    unsigned long long naturalLineNo() const noexcept
+    {
+        return _mLineNo + 1;
+    }
+
+    /*
+     * Column number (one-based).
+     */
+    unsigned long long naturalColNo() const noexcept
+    {
+        return _mColNo + 1;
+    }
+
+private:
+    unsigned long long _mOffset = 0;
+    unsigned long long _mLineNo = 0;
+    unsigned long long _mColNo = 0;
+};
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_TEXT_LOC_HPP */
This page took 0.027172 seconds and 4 git commands to generate.