From: Philippe Proulx Date: Wed, 17 Apr 2024 17:49:55 +0000 (-0400) Subject: src/cpp-common: add `bt2c::TextLoc` class X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=2a8eb1258a6c3081d1cad4f7a3df0e0ca7880bce;p=babeltrace.git src/cpp-common: add `bt2c::TextLoc` class 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 Change-Id: Ice9cab35041f1637d220deb6af8f52dbfc8cd290 Reviewed-on: https://review.lttng.org/c/babeltrace/+/7408 Reviewed-on: https://review.lttng.org/c/babeltrace/+/12678 --- diff --git a/src/Makefile.am b/src/Makefile.am index 771c9de5..d5808506 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 00000000..58bb697d --- /dev/null +++ b/src/cpp-common/bt2c/text-loc.cpp @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2016-2022 Philippe Proulx + * + * 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 index 00000000..2a803688 --- /dev/null +++ b/src/cpp-common/bt2c/text-loc.hpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2016-2022 Philippe Proulx + * + * 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 */