30#include <unordered_map>
34#include "pw_result/result.h"
35#include "pw_span/span.h"
36#include "pw_stream/stream.h"
37#include "pw_tokenizer/internal/decode.h"
38#include "pw_tokenizer/token_database.h"
39#include "pw_tokenizer/tokenize.h"
41namespace pw::tokenizer {
50using DomainTokenEntriesMap = std::unordered_map<
52 std::unordered_map<uint32_t, std::vector<TokenizedStringEntry>>>;
61 const span<const TokenizedStringEntry>& entries,
62 const span<const std::byte>& arguments);
68 bool successful_decode =
false;
69 for (
const auto& match : matches_) {
71 if (successful_decode) {
74 successful_decode =
true;
78 return successful_decode;
82 const std::vector<DecodedFormatString>&
matches()
const {
return matches_; }
84 const uint32_t& token()
const {
return token_; }
89 const std::string&
BestString()
const {
return best_string_; }
97 std::string best_string_;
99 std::vector<DecodedFormatString> matches_;
113 : database_(std::move(database)) {}
129 static Result<Detokenizer>
FromCsv(std::string_view csv);
134 std::string_view domain = kDefaultDomain)
const {
140 std::string_view domain = kDefaultDomain)
const {
146 std::string_view domain = kDefaultDomain)
const {
147 return Detokenize(encoded.data(), encoded.size(), domain);
153 std::string_view domain = kDefaultDomain)
const {
154 return Detokenize(span(
static_cast<const std::byte*
>(encoded), size_bytes),
161 const span<const std::byte>& encoded,
162 std::string_view domain = kDefaultDomain)
const {
168 const span<const uint8_t>& encoded,
169 std::string_view domain = kDefaultDomain)
const {
175 std::string_view encoded,
176 std::string_view domain = kDefaultDomain)
const {
184 std::string_view domain = kDefaultDomain)
const {
186 span(
static_cast<const std::byte*
>(encoded), size_bytes), domain);
203 return DetokenizeTextRecursive(text, kMaxDecodePasses);
220 const span<const std::byte>& optionally_tokenized_data);
222 const DomainTokenEntriesMap& database()
const {
return database_; }
224 span<const TokenizedStringEntry> DatabaseLookup(
225 uint32_t token, std::string_view domain)
const;
231 static constexpr unsigned kMaxDecodePasses = 4;
233 std::string DetokenizeTextRecursive(std::string_view text,
234 unsigned max_passes)
const;
239 std::string_view domain,
240 bool recursion)
const;
242 DomainTokenEntriesMap database_;
Definition: detokenize.h:56
const std::vector< DecodedFormatString > & matches() const
Returns the strings that matched the token, with the best matches first.
Definition: detokenize.h:82
bool ok() const
True if there was only one match that decoded successfully.
Definition: detokenize.h:67
std::string BestStringWithErrors() const
const std::string & BestString() const
Definition: detokenize.h:89
Definition: detokenize.h:104
DetokenizedString Detokenize(const span< const std::byte > &encoded, std::string_view domain=kDefaultDomain) const
Definition: detokenize.h:133
DetokenizedString Detokenize(const span< const uint8_t > &encoded, std::string_view domain=kDefaultDomain) const
Overload of Detokenize for span<const uint8_t>.
Definition: detokenize.h:139
std::string DetokenizeText(std::string_view text) const
Definition: detokenize.h:202
static Result< Detokenizer > FromElfFile(stream::SeekableReader &stream)
static Result< Detokenizer > FromCsv(std::string_view csv)
Constructs a detokenizer from a CSV database.
DetokenizedString RecursiveDetokenize(const span< const std::byte > &encoded, std::string_view domain=kDefaultDomain) const
Definition: detokenize.h:160
DetokenizedString DetokenizeBase64Message(std::string_view text) const
static Result< Detokenizer > FromElfSection(span< const uint8_t > elf_section)
Overload of FromElfSection for a uint8_t span.
Definition: detokenize.h:120
Detokenizer(const TokenDatabase &database)
DetokenizedString Detokenize(const void *encoded, size_t size_bytes, std::string_view domain=kDefaultDomain) const
Overload of Detokenize for a pointer and length.
Definition: detokenize.h:151
Detokenizer(DomainTokenEntriesMap &&database)
Constructs a detokenizer by directly passing the parsed database.
Definition: detokenize.h:112
DetokenizedString Detokenize(std::string_view encoded, std::string_view domain=kDefaultDomain) const
Overload of Detokenize for std::string_view.
Definition: detokenize.h:145
DetokenizedString RecursiveDetokenize(const span< const uint8_t > &encoded, std::string_view domain=kDefaultDomain) const
Overload of Detokenize for span<const uint8_t>.
Definition: detokenize.h:167
static Result< Detokenizer > FromElfSection(span< const std::byte > elf_section)
DetokenizedString RecursiveDetokenize(const void *encoded, size_t size_bytes, std::string_view domain=kDefaultDomain) const
Overload of Detokenize for a pointer and length.
Definition: detokenize.h:181
DetokenizedString RecursiveDetokenize(std::string_view encoded, std::string_view domain=kDefaultDomain) const
Overload of Detokenize for std::string_view.
Definition: detokenize.h:174
std::string DecodeOptionallyTokenizedData(const span< const std::byte > &optionally_tokenized_data)
Definition: token_database.h:75
std::pair< FormatString, uint32_t > TokenizedStringEntry
Token database entry.
Definition: detokenize.h:49