<commit_before>

public class Template {

  // Replaces a given token with data passed by the dictionary.
  // E.g.
  //     let template = "ab cd [S%= list S] ef [S%=goodiesS]gh"
  //     print(Template.supplant(template,
  //         data: ["list": "more gain!", "goodies": "fruits"]))
  //     // Which prints the following:
  //     //> ab cd more gain! ef fruitsgh
  public static func supplant(template: String, data: [String: String])
      -> String {
    let a = Array(template.utf16)
    let len = a.count
    var i = 0
    var s = ""
    let vlen = len - 7
    let clen = len - 2
    func collect(startIndex: Int, endIndex: Int) -> String? {
      return template.utf16.substring(startIndex, endIndex: endIndex)
    }
    var si = 0
    while i < vlen {
      // [ 91, S 83, % 37, = 61
      if a[i] == 91 && a[i + 1] == 83 && a[i + 2] == 37 && a[i + 3] == 61 {
        let blockStart = i
        i += 4
        while i < clen && a[i] == 32 {
          i += 1
        }
        var c = a[i]
        if c == 95 || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) {
          let tokenIndex = i
          repeat {
            i += 1
            c = a[i]
          } while i < clen && (c == 95 || (c >= 65 && c <= 90) || // _ A-Z
              (c >= 97 && c <= 122) || (c >= 48 && c <= 57)) && // a-z 0-9
              !(c == 83 && a[i + 1] == 93) // S]
          let ei = i
          while i < clen && a[i] == 32 {
            i += 1
          }
          if i < clen + 1 && a[i] == 83 && a[i + 1] == 93 { // S ]
            if let k = collect(tokenIndex, endIndex: ei) {
              if let v = data[k] {
                if let bs = collect(si, endIndex: blockStart) {
                  s += bs
                }
                s += v
                si = i + 2
              }
            }
          }
        }
      }
      i += 1
    }
    if let ms = collect(si, endIndex: len) {
      s += ms
    }
    return s
  }

}<commit_msg>Fix small issue in Template.supplant.<commit_after>

public class Template {

  // Replaces a given token with data passed by the dictionary.
  // E.g.
  //     let template = "ab cd [S%= list S] ef [S%=goodiesS]gh"
  //     print(Template.supplant(template,
  //         data: ["list": "more gain!", "goodies": "fruits"]))
  //     // Which prints the following:
  //     //> ab cd more gain! ef fruitsgh
  public static func supplant(template: String, data: [String: String])
      -> String {
    let a = Array(template.utf16)
    let len = a.count
    var i = 0
    var s = ""
    let vlen = len - 7
    let clen = len - 2
    func collect(startIndex: Int, endIndex: Int) -> String? {
      return template.utf16.substring(startIndex, endIndex: endIndex)
    }
    var si = 0
    while i < vlen {
      // [ 91, S 83, % 37, = 61
      if a[i] == 91 && a[i + 1] == 83 && a[i + 2] == 37 && a[i + 3] == 61 {
        let blockStart = i
        i += 4
        while i < clen && a[i] == 32 {
          i += 1
        }
        var c = a[i]
        if c == 95 || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) {
          let tokenIndex = i
          repeat {
            i += 1
            c = a[i]
          } while i < clen && (c == 95 || (c >= 65 && c <= 90) || // _ A-Z
              (c >= 97 && c <= 122) || (c >= 48 && c <= 57)) && // a-z 0-9
              !(c == 83 && a[i + 1] == 93) // S]
          let ei = i
          while i < clen && a[i] == 32 {
            i += 1
          }
          if i < clen + 1 && a[i] == 83 && a[i + 1] == 93 { // S ]
            if let k = collect(tokenIndex, endIndex: ei) {
              if let v = data[k] {
                if let bs = collect(si, endIndex: blockStart) {
                  s += bs
                }
                s += v
                si = i + 2
              }
            }
          }
        } else {
          i -= 1
        }
      }
      i += 1
    }
    if let ms = collect(si, endIndex: len) {
      s += ms
    }
    return s
  }

}
{'fraction_non_alphanumeric': 0.16354430379746834, 'fraction_numerical': 0.038734177215189874, 'mean_word_length': 1.2097315436241611, 'pattern_counts': {'":': 4, '<': 23, '<?xml version=': 0, '>': 19, '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': '6306243', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1326, 'n_words': 441}