Skip to content

Commit

Permalink
Add support for std::u16string strings
Browse files Browse the repository at this point in the history
  • Loading branch information
romainthomas committed Dec 11, 2024
1 parent 2eab480 commit 81b8b6f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/LIEF/iostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ class vector_iostream {
}

vector_iostream& write(std::vector<uint8_t> s) {
if (s.empty()) {
return *this;
}
return write(s.data(), s.size());
}

vector_iostream& write(const std::string& s) {
return write(reinterpret_cast<const uint8_t*>(s.c_str()), s.size() + 1);
}

vector_iostream& write(const std::u16string& s, bool with_null_char);

vector_iostream& write(size_t count, uint8_t value) {
raw_.insert(std::end(raw_), count, value);
current_pos_ += count;
Expand Down
14 changes: 14 additions & 0 deletions src/iostream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ vector_iostream& vector_iostream::seekp(vector_iostream::off_type p, std::ios_ba
return *this;
}

vector_iostream& vector_iostream::write(const std::u16string& s, bool with_null_char) {
const size_t nullchar = with_null_char ? 1 : 0;
const auto pos = static_cast<size_t>(tellp());
if (raw_.size() < (pos + s.size() + nullchar)) {
raw_.resize(pos + (s.size() + nullchar) * sizeof(char16_t));
}

std::copy(reinterpret_cast<const char16_t*>(s.data()),
reinterpret_cast<const char16_t*>(s.data()) + s.size(),
reinterpret_cast<char16_t*>(raw_.data() + current_pos_));
current_pos_ += (s.size() + nullchar) * sizeof(char16_t);
return *this;
}

vector_iostream& vector_iostream::align(size_t alignment, uint8_t fill) {
if (raw_.size() % alignment == 0) {
return *this;
Expand Down

0 comments on commit 81b8b6f

Please sign in to comment.