<commit_before>/*
** buffer.cpp
** Login : <<EMAIL>>
**
** Author(s):
**  - hcuche <<EMAIL>>
*/

#include <qimessaging/buffer.hpp>

#include <cstdio>
#include <cstring>
#include <ctype.h>

#include "src/buffer_p.hpp"

#include <iostream>

namespace qi
{

  BufferPrivate::BufferPrivate()
    : size(0)
    , cursor(0)
  {
  }

  Buffer::Buffer()
    : _p(new BufferPrivate) // TODO: add allocation-on-write.
  {
  }

  size_t Buffer::write(const void *data, size_t size)
  {
    if (sizeof(_p->data) - _p->cursor < size)
    {
      return -1;
    }

    memcpy(_p->data + _p->size, data, size);
    _p->size += size;
    _p->cursor += _p->size;

    return size;
  }

  size_t Buffer::read(void *data, size_t size)
  {
    if (_p->size - _p->cursor < size)
    {
      size = _p->size - _p->cursor;
    }

    memcpy(data, _p->data + _p->cursor, size);
    _p->cursor += size;

    return size;
  }

  void *Buffer::read(size_t size)
  {
    void *p = 0;
    if ((p = peek(size)))
    {
      seek(size);<commit_msg>qi::Buffer: fix write size handling<commit_after>/*
** buffer.cpp
** Login : <<EMAIL>>
**
** Author(s):
**  - hcuche <<EMAIL>>
*/

#include <qimessaging/buffer.hpp>
#include <qi/log.hpp>

#include <cstdio>
#include <cstring>
#include <ctype.h>

#include "src/buffer_p.hpp"

#include <iostream>

namespace qi
{

  BufferPrivate::BufferPrivate()
    : size(0)
    , cursor(0)
  {
  }

  Buffer::Buffer()
    : _p(new BufferPrivate) // TODO: add allocation-on-write.
  {
  }

  size_t Buffer::write(const void *data, size_t size)
  {
    if (sizeof(_p->data) - _p->size < size)
    {
      qiLogVerbose("qi.Buffer") << "write(" << size << ") failed, buffer size is " << _p->size;
      return -1;
    }

    memcpy(_p->data + _p->size, data, size);
    _p->size += size;

    return size;
  }

  size_t Buffer::read(void *data, size_t size)
  {
    if (_p->size - _p->cursor < size)
    {
      size = _p->size - _p->cursor;
    }

    memcpy(data, _p->data + _p->cursor, size);
    _p->cursor += size;

    return size;
  }

  void *Buffer::read(size_t size)
  {
    void *p = 0;
    if ((p = peek(size)))
    {
      seek(size);
{'fraction_non_alphanumeric': 0.1865671641791045, 'fraction_numerical': 0.0037313432835820895, 'mean_word_length': 2.5631229235880397, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2242163', 'n_tokens_mistral': 868, 'n_tokens_neox': 839, 'n_words': 175}