Pigweed
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_tokenizer/internal/decode.h"
37#include "pw_tokenizer/token_database.h"
38
39namespace pw::tokenizer {
40
43
45using TokenizedStringEntry = std::pair<FormatString, uint32_t /*date removed*/>;
46
50 public:
51 DetokenizedString(uint32_t token,
52 const span<const TokenizedStringEntry>& entries,
53 const span<const std::byte>& arguments);
54
55 DetokenizedString() : has_token_(false) {}
56
58 bool ok() const { return matches_.size() == 1 && matches_[0].ok(); }
59
61 const std::vector<DecodedFormatString>& matches() const { return matches_; }
62
63 const uint32_t& token() const { return token_; }
64
68 std::string BestString() const;
69
72 std::string BestStringWithErrors() const;
73
74 private:
75 uint32_t token_;
76 bool has_token_;
77 std::vector<DecodedFormatString> matches_;
78};
79
83 public:
87 explicit Detokenizer(const TokenDatabase& database);
88
90 explicit Detokenizer(
91 std::unordered_map<uint32_t, std::vector<TokenizedStringEntry>>&&
92 database)
93 : database_(std::move(database)) {}
94
97 static Result<Detokenizer> FromElfSection(span<const std::byte> elf_section);
98
100 static Result<Detokenizer> FromElfSection(span<const uint8_t> elf_section) {
101 return FromElfSection(as_bytes(elf_section));
102 }
103
106 DetokenizedString Detokenize(const span<const std::byte>& encoded) const;
107
109 DetokenizedString Detokenize(const span<const uint8_t>& encoded) const {
110 return Detokenize(as_bytes(encoded));
111 }
112
114 DetokenizedString Detokenize(std::string_view encoded) const {
115 return Detokenize(encoded.data(), encoded.size());
116 }
117
119 DetokenizedString Detokenize(const void* encoded, size_t size_bytes) const {
120 return Detokenize(span(static_cast<const std::byte*>(encoded), size_bytes));
121 }
122
125 DetokenizedString DetokenizeBase64Message(std::string_view text) const;
126
140 std::string DetokenizeText(std::string_view text,
141 unsigned max_passes = 3) const;
142
145 [[deprecated("Use DetokenizeText() instead")]] std::string DetokenizeBase64(
146 std::string_view text) const {
147 return DetokenizeText(text, 1);
148 }
149
164 const span<const std::byte>& optionally_tokenized_data);
165
166 private:
167 std::unordered_map<uint32_t, std::vector<TokenizedStringEntry>> database_;
168};
169
171
172} // namespace pw::tokenizer
Definition: detokenize.h:49
const std::vector< DecodedFormatString > & matches() const
Returns the strings that matched the token, with the best matches first.
Definition: detokenize.h:61
std::string BestString() const
bool ok() const
True if there was only one valid match and it decoded successfully.
Definition: detokenize.h:58
std::string BestStringWithErrors() const
Definition: detokenize.h:82
DetokenizedString Detokenize(const span< const uint8_t > &encoded) const
Overload of Detokenize for span<const uint8_t>.
Definition: detokenize.h:109
DetokenizedString Detokenize(std::string_view encoded) const
Overload of Detokenize for std::string_view.
Definition: detokenize.h:114
std::string DetokenizeBase64(std::string_view text) const
Definition: detokenize.h:145
Detokenizer(std::unordered_map< uint32_t, std::vector< TokenizedStringEntry > > &&database)
Constructs a detokenizer by directly passing the parsed database.
Definition: detokenize.h:90
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:100
Detokenizer(const TokenDatabase &database)
std::string DetokenizeText(std::string_view text, unsigned max_passes=3) const
DetokenizedString Detokenize(const void *encoded, size_t size_bytes) const
Overload of Detokenize for a pointer and length.
Definition: detokenize.h:119
static Result< Detokenizer > FromElfSection(span< const std::byte > elf_section)
DetokenizedString Detokenize(const span< const std::byte > &encoded) const
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:45