Flutter Engine
base32.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_FML_BASE32_H_
6 #define FLUTTER_FML_BASE32_H_
7 
8 #include <string_view>
9 #include <utility>
10 
11 #include "flutter/fml/logging.h"
12 
13 namespace fml {
14 
15 template <int from_length, int to_length, int buffer_length>
16 class BitConverter {
17  public:
18  void Append(int bits) {
19  FML_DCHECK(bits < (1 << from_length));
21  lower_free_bits_ -= from_length;
22  buffer_ |= (bits << lower_free_bits_);
23  }
24 
25  int Extract() {
27  int result = Peek();
28  buffer_ = (buffer_ << to_length) & mask_;
29  lower_free_bits_ += to_length;
30  return result;
31  }
32 
33  int Peek() const { return (buffer_ >> (buffer_length - to_length)); }
34  int BitsAvailable() const { return buffer_length - lower_free_bits_; }
35  bool CanAppend() const { return lower_free_bits_ >= from_length; }
36  bool CanExtract() const { return BitsAvailable() >= to_length; }
37 
38  private:
39  static_assert(buffer_length >= 2 * from_length);
40  static_assert(buffer_length >= 2 * to_length);
41  static_assert(buffer_length < sizeof(int) * 8);
42 
43  static constexpr int mask_ = (1 << buffer_length) - 1;
44 
45  int buffer_ = 0;
46  int lower_free_bits_ = buffer_length;
47 };
48 
51 
52 std::pair<bool, std::string> Base32Encode(std::string_view input);
53 std::pair<bool, std::string> Base32Decode(const std::string& input);
54 
55 } // namespace fml
56 
57 #endif // FLUTTER_FML_BASE32_H_
bool CanExtract() const
Definition: base32.h:36
int BitsAvailable() const
Definition: base32.h:34
#define FML_DCHECK(condition)
Definition: logging.h:86
int Extract()
Definition: base32.h:25
Definition: ascii_trie.cc:9
void Append(int bits)
Definition: base32.h:18
std::pair< bool, std::string > Base32Encode(std::string_view input)
Definition: base32.cc:14
std::pair< bool, std::string > Base32Decode(const std::string &input)
Definition: base32.cc:54
int Peek() const
Definition: base32.h:33
bool CanAppend() const
Definition: base32.h:35