<commit_before>package com.cognitect.transit.impl;

import com.cognitect.transit.Handler;
import com.cognitect.transit.Writer;
import org.msgpack.MessagePack;
import org.msgpack.packer.Packer;
import org.apache.commons.codec.binary.Base64;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public class MsgpackEmitter extends AbstractEmitter {

    private final Packer gen;

    public MsgpackEmitter(Packer gen, Map<Class, Handler> handlers) {
        super(handlers);
        this.gen = gen;
    }

    @Override
    public void emit(Object o, boolean asMapKey, WriteCache cache) throws Exception {
        marshal(o, asMapKey, cache);
    }

    @Override
    public void emitNil(boolean asMapKey, WriteCache cache) throws Exception {
        this.gen.writeNil();
    }

    @Override
    public void emitString(String prefix, String tag, String s, boolean asMapKey, WriteCache cache) throws Exception {

        StringBuilder sb = new StringBuilder();
        if(prefix != null)
            sb.append(prefix);
        if(tag != null)
            sb.append(tag);
        if(s != null)
            sb.append(s);

        String outString = cache.cacheWrite(sb.toString(), asMapKey);

        if(asMapKey)
            this.gen.write(outString);
        else
            this.gen.write(outString);
    }

    @Override
    public void emitBoolean(Boolean b, boolean asMapKey, WriteCache cache) throws Exception {
        this.gen.write(b);
    }

    @Override
    public void emitInteger(Object o, boolean asMapKey, WriteCache cache) throws Exception {
        // TODO: BigInteger?

        if (o instanceof String) this.emitString(Writer.ESC, "i", o.toString(), asMapKey, cache);

        long i = Util.numberToPrimitiveLong(o);

        if ((i > Long.MAX_VALUE) || (i < Long.MIN_VALUE))
            this.emitString(Writer.ESC, "i", o.toString(), asMapKey, cache);

        this.gen.write(i);
    }

    @Override
    public void emitDouble(Object d, boolean asMapKey, WriteCache cache) throws Exception {
        if (d instanceof Double)
            this.gen.write((Double) d);
        else if (d instanceof Float)
            this.gen.write((Float) d);
        else
            throw new Exception("Unknown floating point type: " + d.getClass());
    }

    @Override
    public void emitBinary(Object b, boolean asMapKey, WriteCache cache) throws Exception {
        byte[] encodedBytes = Base64.encodeBase64((byte[])b);
        emitString(Writer.ESC, "b", new String(encodedBytes), asMapKey, cache);
    }

    @Override
    public long arraySize(Object a) {
        if(a instanceof List)
            return ((List)a).size();
        else return 0;
    }

    @Override
    public void emitArrayStart(Long size) throws Exception {
        this.gen.writeArrayBegin(size.intValue());<commit_msg>Updating to latest Writer.ESC_STR literal.<commit_after>package com.cognitect.transit.impl;

import com.cognitect.transit.Handler;
import com.cognitect.transit.Writer;
import org.msgpack.MessagePack;
import org.msgpack.packer.Packer;
import org.apache.commons.codec.binary.Base64;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public class MsgpackEmitter extends AbstractEmitter {

    private final Packer gen;

    public MsgpackEmitter(Packer gen, Map<Class, Handler> handlers) {
        super(handlers);
        this.gen = gen;
    }

    @Override
    public void emit(Object o, boolean asMapKey, WriteCache cache) throws Exception {
        marshalTop(o, cache);
    }

    @Override
    public void emitNil(boolean asMapKey, WriteCache cache) throws Exception {
        this.gen.writeNil();
    }

    @Override
    public void emitString(String prefix, String tag, String s, boolean asMapKey, WriteCache cache) throws Exception {

        StringBuilder sb = new StringBuilder();
        if(prefix != null)
            sb.append(prefix);
        if(tag != null)
            sb.append(tag);
        if(s != null)
            sb.append(s);

        String outString = cache.cacheWrite(sb.toString(), asMapKey);

        if(asMapKey)
            this.gen.write(outString);
        else
            this.gen.write(outString);
    }

    @Override
    public void emitBoolean(Boolean b, boolean asMapKey, WriteCache cache) throws Exception {
        this.gen.write(b);
    }

    @Override
    public void emitInteger(Object o, boolean asMapKey, WriteCache cache) throws Exception {
        // TODO: BigInteger?

        if (o instanceof String) this.emitString(Writer.ESC_STR, "i", o.toString(), asMapKey, cache);

        long i = Util.numberToPrimitiveLong(o);

        if ((i > Long.MAX_VALUE) || (i < Long.MIN_VALUE))
            this.emitString(Writer.ESC_STR, "i", o.toString(), asMapKey, cache);

        this.gen.write(i);
    }

    @Override
    public void emitDouble(Object d, boolean asMapKey, WriteCache cache) throws Exception {
        if (d instanceof Double)
            this.gen.write((Double) d);
        else if (d instanceof Float)
            this.gen.write((Float) d);
        else
            throw new Exception("Unknown floating point type: " + d.getClass());
    }

    @Override
    public void emitBinary(Object b, boolean asMapKey, WriteCache cache) throws Exception {
        byte[] encodedBytes = Base64.encodeBase64((byte[])b);
        emitString(Writer.ESC_STR, "b", new String(encodedBytes), asMapKey, cache);
    }

    @Override
    public void emitQuoted(Object o, WriteCache cache) throws Exception {

        emitMapStart(1L);
        emitString(Writer.ESC_TAG, "'", null, true, cache);
        marshal(o, false, cache);
        emitMapEnd();
    }

    @Override
    public long arraySize(Object a) {
        if(a instanceof List)
            return ((List)a).size();
        else return 0;
    }

    @Override
    public void emitArrayStart(Long size) throws Exception {
        this.gen.writeArrayBegin(size.intValue());
{'fraction_non_alphanumeric': 0.1068561872909699, 'fraction_numerical': 0.002508361204013378, 'mean_word_length': 2.687422934648582, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5710844', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1717, 'n_words': 458}