<commit_before><commit_msg>lzb: implemented a LimitedWriteCloser<commit_after>package lzb

import (
	"errors"
	"fmt"
	"io"
)

type LimitedWriteCloser struct {
	W      io.WriteCloser
	N      int64
	Closed bool
}

const minInt64 = -1 << 63

var errEarlyClose = errors.New("close called before limit reached")

func (lw *LimitedWriteCloser) Write(p []byte) (n int, err error) {
	if lw.Closed {
		return 0, errClosed
	}
	if lw.N <= 0 {
		return 0, errLimit
	}
	if int64(len(p)) > lw.N {
		p = p[0:lw.N]
		err = errLimit
	}
	var werr error
	if n, werr = lw.W.Write(p); werr != nil {
		return n, werr
	}
	return n, err
}

func (lw *LimitedWriteCloser) Close(p []byte) error {
	if lw.Closed {
		return errClosed
	}
	if lw.N < 0 {
		panic(fmt.Errorf("lw.N has unexpected value %d", lw.N))
	}
	if lw.N > 0 {
		return errEarlyClose
	}
	if err := lw.W.Close(); err != nil {
		return err
	}
	lw.Closed = true
	return nil
}
{'fraction_non_alphanumeric': 0.13296703296703297, 'fraction_numerical': 0.016483516483516484, 'mean_word_length': 3.1036036036036037, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '6312113', 'n_tokens_mistral': 411, 'n_tokens_neox': 383, 'n_words': 106}