src/Pure/General/length.scala
author wenzelm
Tue, 20 Dec 2016 16:08:02 +0100
changeset 64617 01e50039edc9
child 64618 c81bd30839a6
permissions -rw-r--r--
more systematic text length wrt. encoding;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64617
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/length.scala
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
     3
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
     4
Text length wrt. encoding.
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
     5
*/
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
     6
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
     7
package isabelle
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
     8
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
     9
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    10
trait Length
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    11
{
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    12
  def apply(text: String): Int
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    13
  def offset(text: String, i: Int): Option[Text.Offset]
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    14
}
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    15
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    16
object Length extends Length
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    17
{
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    18
  def apply(text: String): Int = text.length
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    19
  def offset(text: String, i: Int): Option[Text.Offset] =
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    20
    if (0 <= i && i <= text.length) Some(i) else None
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    21
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    22
  def encoding(name: String): Length =
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    23
    name match {
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    24
      case "UTF-8" => UTF8.Length
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    25
      case "UTF-16" => Length
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    26
      case "codepoints" => Codepoint.Length
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    27
      case "symbols" => Symbol.Length
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    28
      case _ =>
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    29
        error("Bad text encoding: " + name + " (expected UTF-8, UTF-16, codepoints, symbols)")
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    30
    }
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents:
diff changeset
    31
}