<commit_before>// Copyright 2014, <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.

package ioutil

import "io"
import "os"
import "errors"

type MemoryReader struct {
	rd    io.Reader
	buf   []byte
	rdPtr int64
	wrPtr int64
}

func NewMemoryReader(rd io.Reader, buf []byte) *MemoryReader {
	return &MemoryReader{rd, buf, 0, 0}
}

func (r *MemoryReader) Read(data []byte) (cnt int, err error) {
	// If there is data to read from ring buffer, use it
	for len(data) > 0 && r.wrPtr > r.rdPtr {
		bufLen := int64(len(r.buf))
		offLo, offHi := r.rdPtr%bufLen, r.wrPtr%bufLen
		if offHi <= offLo {
			offHi = bufLen
		}
		cpyCnt := copy(data, r.buf[offLo:offHi])
		data = data[cpyCnt:]
		cnt += cpyCnt
		r.rdPtr += int64(cpyCnt)
	}

	// Top off the input buffer with actual Read() call
	var rdCnt int
	rdCnt, err = r.rd.Read(data)
	cnt += rdCnt

	// Write data to ring buffer
	if len(data) > len(r.buf) {
		skipCnt := len(data) - len(r.buf)
		data = data[skipCnt:]
		r.rdPtr += int64(skipCnt)
		r.wrPtr += int64(skipCnt)
	}
	for len(data) > 0 {
		off := r.wrPtr % int64(len(r.buf))
		cpyCnt := copy(r.buf[off:], data)
		data = data[cpyCnt:]
		r.rdPtr += int64(cpyCnt)
		r.wrPtr += int64(cpyCnt)
	}

	return cnt, err
}

func (r *MemoryReader) Seek(offset int64, whence int) (pos int64, err error) {
	switch whence {
	case os.SEEK_SET:
		pos = offset
	case os.SEEK_CUR:
		pos = offset + r.rdPtr
	default:
		return 0, errors.New("ioutil.MemoryReader.Seek: invalid whence")
	}
	if posLo, _, posHi := r.SeekOffsets(); pos < posLo || pos > posHi {<commit_msg>Fixed MemoryReader bug with updating data slice<commit_after>// Copyright 2014, <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.

package ioutil

import "io"
import "os"
import "errors"

type MemoryReader struct {
	rd    io.Reader
	buf   []byte
	rdPtr int64
	wrPtr int64
}

func NewMemoryReader(rd io.Reader, buf []byte) *MemoryReader {
	return &MemoryReader{rd, buf, 0, 0}
}

func (r *MemoryReader) Read(data []byte) (cnt int, err error) {
	// If there is data to read from ring buffer, use it
	for len(data) > 0 && r.wrPtr > r.rdPtr {
		bufLen := int64(len(r.buf))
		offLo, offHi := r.rdPtr%bufLen, r.wrPtr%bufLen
		if offHi <= offLo {
			offHi = bufLen
		}
		cpyCnt := copy(data, r.buf[offLo:offHi])
		data = data[cpyCnt:]
		cnt += cpyCnt
		r.rdPtr += int64(cpyCnt)
	}

	// Top off the input buffer with actual Read() call
	var rdCnt int
	rdCnt, err = r.rd.Read(data)
	data = data[:rdCnt]
	cnt += rdCnt

	// Write data to ring buffer
	if len(data) > len(r.buf) {
		skipCnt := len(data) - len(r.buf)
		data = data[skipCnt:]
		r.rdPtr += int64(skipCnt)
		r.wrPtr += int64(skipCnt)
	}
	for len(data) > 0 {
		off := r.wrPtr % int64(len(r.buf))
		cpyCnt := copy(r.buf[off:], data)
		data = data[cpyCnt:]
		r.rdPtr += int64(cpyCnt)
		r.wrPtr += int64(cpyCnt)
	}

	return cnt, err
}

func (r *MemoryReader) Seek(offset int64, whence int) (pos int64, err error) {
	switch whence {
	case os.SEEK_SET:
		pos = offset
	case os.SEEK_CUR:
		pos = offset + r.rdPtr
	default:
		return 0, errors.New("ioutil.MemoryReader.Seek: invalid whence")
	}
	if posLo, _, posHi := r.SeekOffsets(); pos < posLo || pos > posHi {
{'fraction_non_alphanumeric': 0.14143845922359313, 'fraction_numerical': 0.01865783930183569, 'mean_word_length': 3.414342629482072, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '6114580', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1321, 'n_words': 386}