Skip to content

Commit

Permalink
src/cpp-common: add bt2c::textLocStr() function
Browse files Browse the repository at this point in the history
This new function formats a text location (`bt2c::TextLoc`) according
to some format amongst:

`bt2c::TextLocStrFmt::OFFSET`:
    Offset only.

`bt2c::TextLocStrFmt::LINE_COL_NOS_AND_OFFSET`:
    Line/column numbers and offset.

`bt2c::TextLocStrFmt::LINE_COL_NOS`:
    Line/column numbers only.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I8afeb43a71c7103135f903f7d4eeb73ccf9e24c3
  • Loading branch information
simark committed Dec 11, 2023
1 parent 14cd9fe commit f778da9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,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/uuid.hpp \
cpp-common/bt2c/vector.hpp \
cpp-common/bt2s/make-unique.hpp \
Expand Down
32 changes: 32 additions & 0 deletions src/cpp-common/bt2c/text-loc-str.cpp
Original file line number Diff line number Diff line change
@@ -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::LINE_COL_NOS_AND_OFFSET || fmt == TextLocStrFmt::LINE_COL_NOS) {
ss << loc.naturalLineNo() << ':' << loc.naturalColNo();

if (fmt == TextLocStrFmt::LINE_COL_NOS_AND_OFFSET) {
ss << ' ';
}
}

if (fmt == TextLocStrFmt::OFFSET || fmt == TextLocStrFmt::LINE_COL_NOS_AND_OFFSET) {
ss << "@ " << loc.offset() << " bytes";
}

return ss.str();
}

} /* namespace bt2c */
34 changes: 34 additions & 0 deletions src/cpp-common/bt2c/text-loc-str.hpp
Original file line number Diff line number Diff line change
@@ -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,
LINE_COL_NOS_AND_OFFSET,
LINE_COL_NOS,
};

/*
* 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 */

0 comments on commit f778da9

Please sign in to comment.