| 34 | } |
| 35 | |
| 36 | std::string LuaBaseStream::read(size_t len) { |
| 37 | if (len == 0) { |
| 38 | len = stream_->size(); |
| 39 | } |
| 40 | |
| 41 | if (len <= 0) { |
| 42 | return nullptr; |
| 43 | } |
| 44 | |
| 45 | std::string buffer; |
| 46 | buffer.resize(len); |
| 47 | |
| 48 | // Here, we write directly to the string data, which is safe starting in C++ 11: |
| 49 | // |
| 50 | // The char-like objects in a basic_string object shall be stored contiguously. That is, for any basic_string |
| 51 | // object s, the identity &*(s.begin() + n) == &*s.begin() + n shall hold for all values of n such that |
| 52 | // 0 <= n < s.size()." |
| 53 | // |
| 54 | // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf |
| 55 | auto read = stream_->read(reinterpret_cast<uint8_t *>(&buffer[0]), static_cast<int>(len)); |
| 56 | |
| 57 | if (read != len) { |
| 58 | buffer.resize(static_cast<size_t >(read)); |
| 59 | } |
| 60 | |
| 61 | return std::move(buffer); |
| 62 | } |
| 63 | |
| 64 | size_t LuaBaseStream::write(std::string buf) { |
| 65 | return static_cast<size_t>(stream_->write(reinterpret_cast<uint8_t *>(const_cast<char *>(buf.data())), |