Skip to content

Commit

Permalink
Feature/add xsgetn for char array buffer (#1198)
Browse files Browse the repository at this point in the history
* Load multiple characters at once

* Apply code-format changes

* fix build

---------

Co-authored-by: curioyang <curioyang@users.noreply.github.com>
  • Loading branch information
curioyang and curioyang committed May 7, 2024
1 parent b17e5b2 commit a2869fa
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/Native/include/nncase/runtime/char_array_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,36 @@ class char_array_buffer : public std::streambuf {
: begin_(data.begin()), end_(data.end()), current_(data.data()) {}

private:
int_type underflow() {
int_type underflow() override {
if (current_ == end_)
return traits_type::eof();

return traits_type::to_int_type(*current_);
}

int_type uflow() {
int_type uflow() override {
if (current_ == end_)
return traits_type::eof();

return traits_type::to_int_type(*current_++);
}

int_type pbackfail(int_type ch) {
int_type pbackfail(int_type ch) override {
if (current_ == begin_ ||
(ch != traits_type::eof() && ch != current_[-1]))
return traits_type::eof();

return traits_type::to_int_type(*--current_);
}

std::streamsize showmanyc() {
std::streamsize showmanyc() override {
assert(std::less_equal<const char *>()(current_, end_));
return end_ - current_;
}

std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way,
[[maybe_unused]] std::ios_base::openmode which) {
std::streampos
seekoff(std::streamoff off, std::ios_base::seekdir way,
[[maybe_unused]] std::ios_base::openmode which) override {
if (way == std::ios_base::beg) {
current_ = begin_ + off;
} else if (way == std::ios_base::cur) {
Expand All @@ -67,8 +68,9 @@ class char_array_buffer : public std::streambuf {
return current_ - begin_;
}

std::streampos seekpos(std::streampos sp,
[[maybe_unused]] std::ios_base::openmode which) {
std::streampos
seekpos(std::streampos sp,
[[maybe_unused]] std::ios_base::openmode which) override {
current_ = begin_ + sp;

if (current_ < begin_ || current_ > end_)
Expand All @@ -77,6 +79,17 @@ class char_array_buffer : public std::streambuf {
return current_ - begin_;
}

std::streamsize xsgetn(char_type *s, std::streamsize count) override {
std::streamsize available =
static_cast<std::streamsize>(end_ - current_);
std::streamsize n = (count > available) ? available : count;
if (n > 0) {
traits_type::copy(s, current_, static_cast<size_t>(n));
current_ += n;
}
return n;
}

const char *const begin_;
const char *const end_;
const char *current_;
Expand Down

0 comments on commit a2869fa

Please sign in to comment.