<commit_before>use "collections"

primitive EnvVars
  fun apply(
    envs: (Array[String] box | None),
    prefix: String = "",
    squash: Bool = false):
    Map[String, String] val
  =>
    """
    Turns an array of strings that look like environment variables, ie.
    key=value, into a map of string to string. Can optionally filter for
    keys matching a 'prefix', and will squash resulting keys to lowercase
    iff 'squash' is true.

    So:
      <PREFIX><KEY>=<VALUE>
    becomes:
      {KEY, VALUE} or {key, VALUE}
    """
    let envsmap = recover Map[String, String]() end
    match envs
    | let envsarr: Array[String] box =>
      let prelen = prefix.size().isize()
      for e in envsarr.values() do
        let eqpos = try e.find("=")? else ISize.max_value() end
        let ek: String val = e.substring(0, eqpos)
        let ev: String val = e.substring(eqpos + 1)
        if (prelen == 0) or ek.at(prefix, 0) then
          if squash then
            envsmap.update(ek.substring(prelen).lower(), ev)
          else
            envsmap.update(ek.substring(prelen), ev)
          end
        end
      end
    end
    envsmap<commit_msg>Prevent html escaping in documentation<commit_after>use "collections"

primitive EnvVars
  fun apply(
    envs: (Array[String] box | None),
    prefix: String = "",
    squash: Bool = false):
    Map[String, String] val
  =>
    """
    Turns an array of strings that look like environment variables, ie.
    key=value, into a map of string to string. Can optionally filter for
    keys matching a 'prefix', and will squash resulting keys to lowercase
    iff 'squash' is true.

    So:
      `<PREFIX><KEY>=<VALUE>`
    becomes:
      `{KEY, VALUE}` or `{key, VALUE}`
    """
    let envsmap = recover Map[String, String]() end
    match envs
    | let envsarr: Array[String] box =>
      let prelen = prefix.size().isize()
      for e in envsarr.values() do
        let eqpos = try e.find("=")? else ISize.max_value() end
        let ek: String val = e.substring(0, eqpos)
        let ev: String val = e.substring(eqpos + 1)
        if (prelen == 0) or ek.at(prefix, 0) then
          if squash then
            envsmap.update(ek.substring(prelen).lower(), ev)
          else
            envsmap.update(ek.substring(prelen), ev)
          end
        end
      end
    end
    envsmap
{'fraction_non_alphanumeric': 0.11277231952157198, 'fraction_numerical': 0.0034173430158052115, 'mean_word_length': 2.303244005641749, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '784436', 'n_tokens_mistral': 753, 'n_tokens_neox': 742, 'n_words': 249}