forked from efficios/babeltrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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::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
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |