local assert = assert
local type = type
local string = require "string"
local byte = string.byte
local find = string.find
local match = string.match
local sub = string.sub
local gsub = string.gsub
local format = string.format
local lower = string.lower
local upper = string.upper
local table = require "table"
local concat = table.concat
local _ENV = {}
function strjoin(delimiter, list)
  assert(type(delimiter) == "string" or type(delimiter) == nil, "delimiter is of the wrong type! (did you get the parameters backward?)")
  return concat(list, delimiter);
end
function strsplit(pattern, text)
  local list, pos = {}, 1;
  assert(pattern ~= "", "delimiter matches empty string!");
  while true do
    local first, last = find(text, pattern, pos);
    if first then -- found?
      list[#list+1] = sub(text, pos, first-1);
      pos = last+1;
    else
      list[#list+1] = sub(text, pos);
      break;
    end
  end
  return list;
end
local FILESYSTEM_UNSAFE = "[^a-zA-Z0-9._-]"
function filename_escape(s)
  if s == "." then
    return "%2e"
  elseif s == ".." then
    return "%2e%2e"
  else
    return (gsub(s, FILESYSTEM_UNSAFE, function (c)
      return format("%%%02x", byte(c))
    end))
  end
end
function ipattern(pattern)
  local in_brackets = false
  local p = gsub(pattern, "(%%?)(.)", function(percent, letter)
    if percent ~= "" then
      return nil
    elseif not match(letter, "%a") then
      if letter == "[" then
        in_brackets = true
      elseif letter == "]" then
        in_brackets = false
      end
      return nil
    elseif not in_brackets then
      return format("[%s%s]", lower(letter), upper(letter))
    end
  end)
  return p
end
return _ENV
