src/Pure/General/word.scala
author wenzelm
Mon, 24 Oct 2016 12:16:12 +0200
changeset 64370 865b39487b5d
parent 63450 afd657fffdf9
child 64610 1b89608974e9
permissions -rw-r--r--
discontinued unused / untested distinction of separate PIDE modules;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/word.scala
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
     3
56747
f87e3be0de9a clarified;
wenzelm
parents: 56744
diff changeset
     4
Support for words within Unicode text.
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
     5
*/
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
     6
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
     7
package isabelle
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
     8
62812
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
     9
import java.text.Bidi
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    10
import java.util.Locale
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    11
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    12
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    13
object Word
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    14
{
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    15
  /* codepoints */
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    16
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    17
  def codepoint_iterator(str: String): Iterator[Int] =
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    18
    new Iterator[Int] {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    19
      var offset = 0
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    20
      def hasNext: Boolean = offset < str.length
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    21
      def next: Int =
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    22
      {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    23
        val c = str.codePointAt(offset)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    24
        offset += Character.charCount(c)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    25
        c
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    26
      }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    27
    }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    28
56792
wenzelm
parents: 56748
diff changeset
    29
  def codepoint(c: Int): String = new String(Array(c), 0, 1)
wenzelm
parents: 56748
diff changeset
    30
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    31
62812
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    32
  /* directionality */
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    33
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    34
  def bidi_detect(str: String): Boolean =
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    35
    str.exists(c => c >= 0x590) && Bidi.requiresBidi(str.toArray, 0, str.length)
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    36
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    37
  def bidi_override(str: String): String =
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    38
    if (bidi_detect(str)) "\u200E\u202D" + str + "\u202C" else str
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    39
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    40
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    41
  /* case */
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    42
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    43
  def lowercase(str: String): String = str.toLowerCase(Locale.ROOT)
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    44
  def uppercase(str: String): String = str.toUpperCase(Locale.ROOT)
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    45
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    46
  def capitalize(str: String): String =
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    47
    if (str.length == 0) str
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    48
    else {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    49
      val n = Character.charCount(str.codePointAt(0))
56602
e7e20d72756a capitalize fully (like in Emacs);
wenzelm
parents: 56601
diff changeset
    50
      uppercase(str.substring(0, n)) + lowercase(str.substring(n))
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    51
    }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    52
56609
5ac67041ccf8 capitalize more carefully, e.g. relevant for option "ML_exception_trace";
wenzelm
parents: 56602
diff changeset
    53
  def perhaps_capitalize(str: String): String =
57087
16536c15d749 capitalize even more carefully (see 5ac67041ccf8), e.g. relevant for option "z3_non_commercial" and prospective "MaSh";
wenzelm
parents: 56792
diff changeset
    54
    if (codepoint_iterator(str).forall(c => Character.isLowerCase(c) || Character.isDigit(c)))
16536c15d749 capitalize even more carefully (see 5ac67041ccf8), e.g. relevant for option "z3_non_commercial" and prospective "MaSh";
wenzelm
parents: 56792
diff changeset
    55
      capitalize(str)
16536c15d749 capitalize even more carefully (see 5ac67041ccf8), e.g. relevant for option "z3_non_commercial" and prospective "MaSh";
wenzelm
parents: 56792
diff changeset
    56
    else str
56609
5ac67041ccf8 capitalize more carefully, e.g. relevant for option "ML_exception_trace";
wenzelm
parents: 56602
diff changeset
    57
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    58
  sealed abstract class Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    59
  case object Lowercase extends Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    60
  case object Uppercase extends Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    61
  case object Capitalized extends Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    62
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    63
  object Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    64
  {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    65
    def apply(c: Case, str: String): String =
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    66
      c match {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    67
        case Lowercase => lowercase(str)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    68
        case Uppercase => uppercase(str)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    69
        case Capitalized => capitalize(str)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    70
      }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    71
    def unapply(str: String): Option[Case] =
59319
wenzelm
parents: 57087
diff changeset
    72
      if (str.nonEmpty) {
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    73
        if (codepoint_iterator(str).forall(Character.isLowerCase(_))) Some(Lowercase)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    74
        else if (codepoint_iterator(str).forall(Character.isUpperCase(_))) Some(Uppercase)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    75
        else {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    76
          val it = codepoint_iterator(str)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    77
          if (Character.isUpperCase(it.next) && it.forall(Character.isLowerCase(_)))
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    78
            Some(Capitalized)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    79
          else None
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    80
        }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    81
      }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    82
      else None
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    83
  }
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    84
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    85
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    86
  /* sequence of words */
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    87
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    88
  def implode(words: Iterable[String]): String = words.iterator.mkString(" ")
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    89
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    90
  def explode(sep: Char => Boolean, text: String): List[String] =
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    91
    Library.separated_chunks(sep, text).map(_.toString).filter(_ != "").toList
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    92
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    93
  def explode(sep: Char, text: String): List[String] =
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    94
    explode(_ == sep, text)
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    95
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    96
  def explode(text: String): List[String] =
56747
f87e3be0de9a clarified;
wenzelm
parents: 56744
diff changeset
    97
    explode(Character.isWhitespace(_), text)
63450
afd657fffdf9 indentation of brackets;
wenzelm
parents: 62812
diff changeset
    98
afd657fffdf9 indentation of brackets;
wenzelm
parents: 62812
diff changeset
    99
afd657fffdf9 indentation of brackets;
wenzelm
parents: 62812
diff changeset
   100
  /* brackets */
afd657fffdf9 indentation of brackets;
wenzelm
parents: 62812
diff changeset
   101
afd657fffdf9 indentation of brackets;
wenzelm
parents: 62812
diff changeset
   102
  val open_brackets = "([{«‹⟨⌈⌊⦇⟦⦃"
afd657fffdf9 indentation of brackets;
wenzelm
parents: 62812
diff changeset
   103
  val close_brackets = ")]}»›⟩⌉⌋⦈⟧⦄"
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
   104
}