Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
wasm-watermarker
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nagayama15
wasm-watermarker
Commits
dfdcaaa2
Verified
Commit
dfdcaaa2
authored
Oct 03, 2020
by
nagayama15
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(stream): add CircularBitStreamReader
parent
2363ea59
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
98 additions
and
0 deletions
+98
-0
BitStreamWriter.hpp
lib/kyut/BitStreamWriter.hpp
+1
-0
CircularBitStreamReader.hpp
lib/kyut/CircularBitStreamReader.hpp
+71
-0
CMakeLists.txt
test/CMakeLists.txt
+1
-0
test_CircularBitStreamReader.cpp
test/test_CircularBitStreamReader.cpp
+25
-0
No files found.
lib/kyut/BitStreamWriter.hpp
View file @
dfdcaaa2
...
...
@@ -2,6 +2,7 @@
#define INCLUDE_kyut_BitStreamWriter_hpp
#include <cassert>
#include <cstdint>
#include <string_view>
#include <vector>
...
...
lib/kyut/CircularBitStreamReader.hpp
0 → 100644
View file @
dfdcaaa2
#ifndef INCLUDE_kyut_CircularBitStreamReader_hpp
#define INCLUDE_kyut_CircularBitStreamReader_hpp
#include <cassert>
#include <cstdint>
#include <string_view>
#include <vector>
namespace
kyut
{
class
CircularBitStreamReader
{
public
:
explicit
CircularBitStreamReader
(
std
::
vector
<
std
::
uint8_t
>&&
data
)
:
data_
(
std
::
move
(
data
))
,
pos_bits_
(
0
)
{
}
explicit
CircularBitStreamReader
(
std
::
string_view
data
)
:
data_
(
reinterpret_cast
<
const
std
::
uint8_t
*>
(
data
.
data
()),
reinterpret_cast
<
const
std
::
uint8_t
*>
(
data
.
data
()
+
data
.
size
()))
,
pos_bits_
(
0
)
{
}
// Uncopyable and unmovable
CircularBitStreamReader
(
const
CircularBitStreamReader
&
)
=
delete
;
CircularBitStreamReader
(
CircularBitStreamReader
&&
)
=
delete
;
CircularBitStreamReader
&
operator
=
(
const
CircularBitStreamReader
&
)
=
delete
;
CircularBitStreamReader
&
operator
=
(
CircularBitStreamReader
&&
)
=
delete
;
~
CircularBitStreamReader
()
noexcept
=
default
;
std
::
uint64_t
read
(
std
::
size_t
size_bits
)
{
std
::
uint64_t
x
=
0
;
for
(
std
::
size_t
i
=
0
;
i
<
size_bits
;
i
++
)
{
x
<<=
1
;
x
|=
read_bit
()
?
1
:
0
;
}
return
x
;
}
bool
read_bit
()
{
const
auto
n
=
pos_bits_
>>
3
;
const
auto
k
=
pos_bits_
&
7
;
const
auto
bit
=
(
data_
[
n
]
>>
(
7
-
k
))
&
1
;
pos_bits_
++
;
pos_bits_
%=
size_bits
();
return
bit
!=
0
;
}
std
::
size_t
size_bytes
()
const
noexcept
{
return
data_
.
size
();
}
std
::
size_t
size_bits
()
const
noexcept
{
return
size_bytes
()
*
8
;
}
std
::
size_t
position_bits
()
const
noexcept
{
return
pos_bits_
;
}
private
:
std
::
vector
<
std
::
uint8_t
>
data_
;
std
::
size_t
pos_bits_
;
};
}
// namespace kyut
#endif // INCLUDE_kyut_CircularBitStreamReader_hpp
test/CMakeLists.txt
View file @
dfdcaaa2
...
...
@@ -2,6 +2,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/test")
add_executable
(
test_kyut
test_BitStreamWriter.cpp
test_CircularBitStreamReader.cpp
)
target_link_libraries
(
test_kyut
...
...
test/test_CircularBitStreamReader.cpp
0 → 100644
View file @
dfdcaaa2
#include "kyut/CircularBitStreamReader.hpp"
#include <gtest/gtest.h>
TEST
(
kyut
,
CircularBitStreamReader
)
{
kyut
::
CircularBitStreamReader
r
{
"
\x89\xAB\xCD\xEF
"
};
std
::
uint64_t
a
;
EXPECT_EQ
(
r
.
size_bytes
(),
4
);
EXPECT_EQ
(
r
.
size_bits
(),
32
);
EXPECT_EQ
(
r
.
position_bits
(),
0
);
a
=
r
.
read
(
4
);
EXPECT_EQ
(
a
,
0x8
);
EXPECT_EQ
(
r
.
position_bits
(),
4
);
a
=
r
.
read
(
12
);
EXPECT_EQ
(
a
,
0x9AB
);
EXPECT_EQ
(
r
.
position_bits
(),
16
);
a
=
r
.
read
(
20
);
EXPECT_EQ
(
a
,
0xCDEF8
);
EXPECT_EQ
(
r
.
position_bits
(),
4
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment