///|
/// Common man-page glyphs, plus bounded roff Unicode names (uXXXX..uXXXXXX).
fn named_glyph(name : String) -> String? {
  match name {
    "em" => Some("—")
    "en" => Some("–")
    "hy" => Some("-")
    "mi" => Some("−")
    "bu" => Some("•")
    "sq" => Some("□")
    "co" => Some("©")
    "rg" => Some("®")
    "tm" => Some("™")
    "aq" | "cq" | "oq" => Some("'")
    "dq" => Some("\"")
    "lq" => Some("“")
    "rq" => Some("”")
    "ga" => Some("\u{0060}")
    "ti" => Some("~")
    "rs" => Some("\\")
    "lt" => Some("<")
    "gt" => Some(">")
    "le" => Some("≤")
    "ge" => Some("≥")
    "ne" => Some("≠")
    "eq" => Some("=")
    "pl" => Some("+")
    "mu" => Some("×")
    "di" => Some("÷")
    "de" => Some("°")
    "->" => Some("→")
    "<-" => Some("←")
    "ua" => Some("↑")
    "da" => Some("↓")
    "Fo" => Some("«")
    "Fc" => Some("»")
    "Po" => Some("£")
    "Eu" => Some("€")
    "Do" => Some("$")
    "at" => Some("@")
    "sh" => Some("#")
    "lB" => Some("[")
    "rB" => Some("]")
    "lC" => Some("{")
    "rC" => Some("}")
    _ => unicode_glyph(name)
  }
}

///|
fn unicode_glyph(name : String) -> String? {
  let chars = name.to_array()
  if chars.length() < 5 || chars.length() > 7 || chars[0] != 'u' {
    return None
  }
  let mut scalar = 0
  for i = 1; i < chars.length(); i = i + 1 {
    let c = chars[i]
    let digit = if c >= '0' && c <= '9' {
      c.to_int() - 48
    } else if c >= 'A' && c <= 'F' {
      c.to_int() - 55
    } else if c >= 'a' && c <= 'f' {
      c.to_int() - 87
    } else {
      return None
    }
    scalar = scalar * 16 + digit
  }
  if scalar < 32 ||
    scalar == 127 ||
    (scalar >= 0xd800 && scalar <= 0xdfff) ||
    scalar > 0x10ffff {
    return None
  }
  scalar.to_char().map(c => c.to_string())
}