C/C++ API Reference
Loading...
Searching...
No Matches
detokenize.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15// This file provides the Detokenizer class, which is used to decode tokenized
16// strings. To use a Detokenizer, load a binary format token database into
17// memory, construct a TokenDatabase, and pass it to a Detokenizer:
18//
19// std::vector data = ReadFile("my_tokenized_strings.db");
20// Detokenizer detok(TokenDatabase::Create(data));
21//
22// DetokenizedString result = detok.Detokenize(my_data);
23// std::cout << result.BestString() << '\n';
24//
25#pragma once
26
27#include <cstddef>
28#include <cstdint>
29#include <string>
30#include <unordered_map>
31#include <utility>
32#include <vector>
33
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"
40
41namespace pw::tokenizer {
42
44
45class Detokenizer;
46
48using TokenizedStringEntry = std::pair<FormatString, uint32_t /*date removed*/>;
49using DomainTokenEntriesMap = std::unordered_map<
50 std::string,
51 std::unordered_map<uint32_t, std::vector<TokenizedStringEntry>>>;
52
56 public:
57 DetokenizedString(const Detokenizer& detokenizer,
58 bool recursion,
59 uint32_t token,
61 const span<const std::byte>& arguments);
62
63 DetokenizedString() : has_token_(false), ok_(false) {}
64
66 bool ok() const { return ok_; }
67
69 const std::vector<DecodedFormatString>& matches() const { return matches_; }
70
71 const uint32_t& token() const { return token_; }
72
76 const std::string& BestString() const { return best_string_; }
77
80 std::string BestStringWithErrors() const;
81
82 private:
83 uint32_t token_;
84 std::string best_string_;
85 bool has_token_;
86 bool ok_;
87 std::vector<DecodedFormatString> matches_;
88};
89
93 public:
97 explicit Detokenizer(const TokenDatabase& database);
98
100 explicit Detokenizer(DomainTokenEntriesMap&& database)
101 : database_(std::move(database)) {}
102
106
109 return FromElfSection(as_bytes(elf_section));
110 }
111
115
117 static Result<Detokenizer> FromCsv(std::string_view csv);
118
122 std::string_view domain = kDefaultDomain) const {
123 return Detokenize(encoded, domain, false);
124 }
125
128 std::string_view domain = kDefaultDomain) const {
129 return Detokenize(as_bytes(encoded), domain);
130 }
131
133 DetokenizedString Detokenize(std::string_view encoded,
134 std::string_view domain = kDefaultDomain) const {
135 return Detokenize(encoded.data(), encoded.size(), domain);
136 }
137
139 DetokenizedString Detokenize(const void* encoded,
140 size_t size_bytes,
141 std::string_view domain = kDefaultDomain) const {
142 return Detokenize(span(static_cast<const std::byte*>(encoded), size_bytes),
143 domain);
144 }
145
149 const span<const std::byte>& encoded,
150 std::string_view domain = kDefaultDomain) const {
151 return Detokenize(encoded, domain, true);
152 }
153
156 const span<const uint8_t>& encoded,
157 std::string_view domain = kDefaultDomain) const {
158 return RecursiveDetokenize(as_bytes(encoded), domain);
159 }
160
163 std::string_view encoded,
164 std::string_view domain = kDefaultDomain) const {
165 return RecursiveDetokenize(encoded.data(), encoded.size(), domain);
166 }
167
170 const void* encoded,
171 size_t size_bytes,
172 std::string_view domain = kDefaultDomain) const {
173 return RecursiveDetokenize(
174 span(static_cast<const std::byte*>(encoded), size_bytes), domain);
175 }
176
179 DetokenizedString DetokenizeBase64Message(std::string_view text) const;
180
190 std::string DetokenizeText(std::string_view text) const {
191 return DetokenizeTextRecursive(text, kMaxDecodePasses);
192 }
193
208 span<const std::byte> optionally_tokenized_data) const;
209
210 const DomainTokenEntriesMap& database() const { return database_; }
211
213 Token token, std::string_view domain) const;
214
215 private:
216 // 4 passes supports detokenizing two layers of nested messages with tokenized
217 // domains (e.g. ${${bar}#ab12cd34}#00000012), without allowing a hypothetical
218 // detokenization cycle to continue for too long.
219 static constexpr unsigned kMaxDecodePasses = 4;
220
221 std::string DetokenizeTextRecursive(std::string_view text,
222 unsigned max_passes) const;
223
227 std::string_view domain,
228 bool recursion) const;
229
230 DomainTokenEntriesMap database_;
231};
232
234
235} // namespace pw::tokenizer
Definition: result.h:145
Definition: span_impl.h:235
Definition: stream.h:375
Definition: detokenize.h:55
const std::vector< DecodedFormatString > & matches() const
Returns the strings that matched the token, with the best matches first.
Definition: detokenize.h:69
bool ok() const
True the message decoded successfully and unambiguously.
Definition: detokenize.h:66
std::string BestStringWithErrors() const
const std::string & BestString() const
Definition: detokenize.h:76
Definition: detokenize.h:92
DetokenizedString Detokenize(const span< const std::byte > &encoded, std::string_view domain=kDefaultDomain) const
Definition: detokenize.h:121
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:127
std::string DetokenizeText(std::string_view text) const
Definition: detokenize.h:190
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:148
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:108
std::string DecodeOptionallyTokenizedData(span< const std::byte > optionally_tokenized_data) const
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:139
Detokenizer(DomainTokenEntriesMap &&database)
Constructs a detokenizer by directly passing the parsed database.
Definition: detokenize.h:100
DetokenizedString Detokenize(std::string_view encoded, std::string_view domain=kDefaultDomain) const
Overload of Detokenize for std::string_view.
Definition: detokenize.h:133
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:155
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:169
DetokenizedString RecursiveDetokenize(std::string_view encoded, std::string_view domain=kDefaultDomain) const
Overload of Detokenize for std::string_view.
Definition: detokenize.h:162
Definition: token_database.h:77
std::pair< FormatString, uint32_t > TokenizedStringEntry
Token database entry.
Definition: detokenize.h:48