<commit_before>package com.wcaokaze.japanecraft

import kotlin.coroutines.experimental.buildSequence

class VariableExpander(strWithVars: String) {
  private val tokenExpanderList = parse(strWithVars)

  fun expand(variableMap: Map<String, String>): String {
    return tokenExpanderList
        .map { it.expand(variableMap) }
        .fold(StringBuffer()) { buffer, token -> buffer.append(token) }
        .toString()
  }

  private fun parse(str: String): List<TokenExpander> {
    return buildSequence {
      val buffer = StringBuffer(str)

      yieldToken@ while (buffer.isNotEmpty()) {
        searchDollar@ for (i in 0 until buffer.lastIndex) {
          if (buffer[i] != '$') continue@searchDollar

          if (buffer[i + 1] == '{') {
            val closeBraceIdx = buffer.indexOf("}")
            if (closeBraceIdx == -1) continue@searchDollar

            val constantStr  = buffer.substring(0, i)
            val variableName = buffer.substring(i + 2, closeBraceIdx)

            yield(TokenExpander.ConstantString(constantStr))
            yield(TokenExpander.VariableExpander(variableName))

            buffer.delete(0, closeBraceIdx + 1)<commit_msg>ignore spaces after { and before }<commit_after>package com.wcaokaze.japanecraft

import kotlin.coroutines.experimental.buildSequence

class VariableExpander(strWithVars: String) {
  private val tokenExpanderList = parse(strWithVars)

  fun expand(variableMap: Map<String, String>): String {
    return tokenExpanderList
        .map { it.expand(variableMap) }
        .fold(StringBuffer()) { buffer, token -> buffer.append(token) }
        .toString()
  }

  private fun parse(str: String): List<TokenExpander> {
    return buildSequence {
      val buffer = StringBuffer(str)

      yieldToken@ while (buffer.isNotEmpty()) {
        searchDollar@ for (i in 0 until buffer.lastIndex) {
          if (buffer[i] != '$') continue@searchDollar

          if (buffer[i + 1] == '{') {
            val closeBraceIdx = buffer.indexOf("}")
            if (closeBraceIdx == -1) continue@searchDollar

            val constantStr  = buffer.substring(0, i)
            val variableName = buffer.substring(i + 2, closeBraceIdx)
                                     .dropWhile     { it == ' ' }
                                     .dropLastWhile { it == ' ' }

            yield(TokenExpander.ConstantString(constantStr))
            yield(TokenExpander.VariableExpander(variableName))

            buffer.delete(0, closeBraceIdx + 1)
{'fraction_non_alphanumeric': 0.1099959855479727, 'fraction_numerical': 0.005620232838217583, 'mean_word_length': 2.6486090775988287, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6718757', 'n_tokens_mistral': 725, 'n_tokens_neox': 701, 'n_words': 152}