src/Pure/General/word.scala
author wenzelm
Sat, 09 Apr 2016 16:16:05 +0200
changeset 62930 51ac6bc389e8
parent 62812 ce22e5c3d4ce
child 63450 afd657fffdf9
permissions -rw-r--r--
shared output primitives of physical/virtual Pure;
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
56748
10b52ca3b4a2 clarified PIDE modules;
wenzelm
parents: 56747
diff changeset
     2
    Module:     PIDE
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
     4
56747
f87e3be0de9a clarified;
wenzelm
parents: 56744
diff changeset
     5
Support for words within Unicode text.
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
     6
*/
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
     7
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
     8
package isabelle
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
     9
62812
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    10
import java.text.Bidi
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    11
import java.util.Locale
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    12
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    13
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    14
object Word
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    15
{
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    16
  /* codepoints */
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    17
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    18
  def codepoint_iterator(str: String): Iterator[Int] =
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    19
    new Iterator[Int] {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    20
      var offset = 0
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    21
      def hasNext: Boolean = offset < str.length
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    22
      def next: Int =
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    23
      {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    24
        val c = str.codePointAt(offset)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    25
        offset += Character.charCount(c)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    26
        c
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    27
      }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    28
    }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    29
56792
wenzelm
parents: 56748
diff changeset
    30
  def codepoint(c: Int): String = new String(Array(c), 0, 1)
wenzelm
parents: 56748
diff changeset
    31
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    32
62812
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    33
  /* directionality */
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    34
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    35
  def bidi_detect(str: String): Boolean =
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    36
    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
    37
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    38
  def bidi_override(str: String): String =
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    39
    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
    40
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    41
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    42
  /* case */
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    43
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    44
  def lowercase(str: String): String = str.toLowerCase(Locale.ROOT)
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    45
  def uppercase(str: String): String = str.toUpperCase(Locale.ROOT)
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    46
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    47
  def capitalize(str: String): String =
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    48
    if (str.length == 0) str
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    49
    else {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    50
      val n = Character.charCount(str.codePointAt(0))
56602
e7e20d72756a capitalize fully (like in Emacs);
wenzelm
parents: 56601
diff changeset
    51
      uppercase(str.substring(0, n)) + lowercase(str.substring(n))
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    52
    }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    53
56609
5ac67041ccf8 capitalize more carefully, e.g. relevant for option "ML_exception_trace";
wenzelm
parents: 56602
diff changeset
    54
  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
    55
    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
    56
      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
    57
    else str
56609
5ac67041ccf8 capitalize more carefully, e.g. relevant for option "ML_exception_trace";
wenzelm
parents: 56602
diff changeset
    58
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    59
  sealed abstract class Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    60
  case object Lowercase extends Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    61
  case object Uppercase extends Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    62
  case object Capitalized extends Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    63
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    64
  object Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    65
  {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    66
    def apply(c: Case, str: String): String =
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    67
      c match {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    68
        case Lowercase => lowercase(str)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    69
        case Uppercase => uppercase(str)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    70
        case Capitalized => capitalize(str)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    71
      }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    72
    def unapply(str: String): Option[Case] =
59319
wenzelm
parents: 57087
diff changeset
    73
      if (str.nonEmpty) {
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    74
        if (codepoint_iterator(str).forall(Character.isLowerCase(_))) Some(Lowercase)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    75
        else if (codepoint_iterator(str).forall(Character.isUpperCase(_))) Some(Uppercase)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    76
        else {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    77
          val it = codepoint_iterator(str)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    78
          if (Character.isUpperCase(it.next) && it.forall(Character.isLowerCase(_)))
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    79
            Some(Capitalized)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    80
          else None
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    81
        }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    82
      }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    83
      else None
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    84
  }
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    85
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    86
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    87
  /* sequence of words */
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    88
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    89
  def implode(words: Iterable[String]): String = words.iterator.mkString(" ")
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    90
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    91
  def explode(sep: Char => Boolean, text: String): List[String] =
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    92
    Library.separated_chunks(sep, text).map(_.toString).filter(_ != "").toList
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    93
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    94
  def explode(sep: Char, text: String): List[String] =
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    95
    explode(_ == sep, text)
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    96
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    97
  def explode(text: String): List[String] =
56747
f87e3be0de9a clarified;
wenzelm
parents: 56744
diff changeset
    98
    explode(Character.isWhitespace(_), text)
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    99
}