<commit_before>var bufferutils = require('./bufferutils')
var typeforce = require('typeforce')
var types = require('./types')

var ECSignature = require('./ecsignature')
var ecurve = require('ecurve')
var curve = ecurve.getCurveByName('secp256k1')

var OPS = require('./opcodes')
var REVERSE_OPS = []
for (var op in OPS) {
  var code = OPS[op]
  REVERSE_OPS[code] = op
}

function toASM (chunks) {
  if (types.Buffer(chunks)) {
    chunks = decompile(chunks)
  }

  return chunks.map(function (chunk) {
    // data chunk
    if (Buffer.isBuffer(chunk)) {
      return chunk.toString('hex')

    // opcode
    } else {
      return REVERSE_OPS[chunk]
    }
  }).join(' ')
}

function fromASM (asm) {
  typeforce(types.String, asm)

  var strChunks = asm.split(' ')
  var chunks = strChunks.map(function (strChunk) {
    // opcode
    if (strChunk in OPS) {
      return OPS[strChunk]

    // data chunk
    } else {
      return new Buffer(strChunk, 'hex')
    }
  })

  return compile(chunks)
}

function compile (chunks) {
  // TODO: remove me
  if (types.Buffer(chunks)) return chunks

  typeforce(types.Array, chunks)

  var bufferSize = chunks.reduce(function (accum, chunk) {
    // data chunk
    if (Buffer.isBuffer(chunk)) {
      return accum + bufferutils.pushDataSize(chunk.length) + chunk.length
    }

    // opcode
    return accum + 1
  }, 0.0)

  var buffer = new Buffer(bufferSize)
  var offset = 0

  chunks.forEach(function (chunk) {
    // data chunk
    if (Buffer.isBuffer(chunk)) {
      offset += bufferutils.writePushDataInt(buffer, chunk.length, offset)

      chunk.copy(buffer, offset)
      offset += chunk.length

    // opcode
    } else {<commit_msg>scripts: refactor to/from ASM<commit_after>var bufferutils = require('./bufferutils')
var typeforce = require('typeforce')
var types = require('./types')

var ECSignature = require('./ecsignature')
var ecurve = require('ecurve')
var curve = ecurve.getCurveByName('secp256k1')

var OPS = require('./opcodes')
var REVERSE_OPS = []
for (var op in OPS) {
  var code = OPS[op]
  REVERSE_OPS[code] = op
}

function toASM (chunks) {
  if (types.Buffer(chunks)) {
    chunks = decompile(chunks)
  }

  return chunks.map(function (chunk) {
    // data?
    if (Buffer.isBuffer(chunk)) return chunk.toString('hex')

    // opcode!
    return REVERSE_OPS[chunk]
  }).join(' ')
}

function fromASM (asm) {
  typeforce(types.String, asm)

  return compile(asm.split(' ').map(function (chunkStr) {
    // opcode?
    if (OPS[chunkStr] !== undefined) return OPS[chunkStr]

    // data!
    return new Buffer(chunkStr, 'hex')
  }))
}

function compile (chunks) {
  // TODO: remove me
  if (types.Buffer(chunks)) return chunks

  typeforce(types.Array, chunks)

  var bufferSize = chunks.reduce(function (accum, chunk) {
    // data chunk
    if (Buffer.isBuffer(chunk)) {
      return accum + bufferutils.pushDataSize(chunk.length) + chunk.length
    }

    // opcode
    return accum + 1
  }, 0.0)

  var buffer = new Buffer(bufferSize)
  var offset = 0

  chunks.forEach(function (chunk) {
    // data chunk
    if (Buffer.isBuffer(chunk)) {
      offset += bufferutils.writePushDataInt(buffer, chunk.length, offset)

      chunk.copy(buffer, offset)
      offset += chunk.length

    // opcode
    } else {
{'fraction_non_alphanumeric': 0.13954198473282442, 'fraction_numerical': 0.004885496183206107, 'mean_word_length': 3.084788029925187, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7269937', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1091, 'n_words': 263}