src/Pure/General/word.scala
author wenzelm
Wed, 03 May 2017 23:04:25 +0200
changeset 65703 cead65c19f2e
parent 64610 1b89608974e9
child 71601 97ccf48c2f0c
permissions -rw-r--r--
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
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
{
62812
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    15
  /* directionality */
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    16
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    17
  def bidi_detect(str: String): Boolean =
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    18
    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
    19
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    20
  def bidi_override(str: String): String =
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    21
    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
    22
ce22e5c3d4ce more robust display of bidirectional Unicode text: enforce left-to-right;
wenzelm
parents: 59319
diff changeset
    23
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    24
  /* case */
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    25
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    26
  def lowercase(str: String): String = str.toLowerCase(Locale.ROOT)
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    27
  def uppercase(str: String): String = str.toUpperCase(Locale.ROOT)
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    28
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    29
  def capitalize(str: String): String =
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    30
    if (str.length == 0) str
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    31
    else {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    32
      val n = Character.charCount(str.codePointAt(0))
56602
e7e20d72756a capitalize fully (like in Emacs);
wenzelm
parents: 56601
diff changeset
    33
      uppercase(str.substring(0, n)) + lowercase(str.substring(n))
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    34
    }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    35
56609
5ac67041ccf8 capitalize more carefully, e.g. relevant for option "ML_exception_trace";
wenzelm
parents: 56602
diff changeset
    36
  def perhaps_capitalize(str: String): String =
64610
1b89608974e9 clarified modules;
wenzelm
parents: 64370
diff changeset
    37
    if (Codepoint.iterator(str).forall(c => Character.isLowerCase(c) || Character.isDigit(c)))
57087
16536c15d749 capitalize even more carefully (see 5ac67041ccf8), e.g. relevant for option "z3_non_commercial" and prospective "MaSh";
wenzelm
parents: 56792
diff changeset
    38
      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
    39
    else str
56609
5ac67041ccf8 capitalize more carefully, e.g. relevant for option "ML_exception_trace";
wenzelm
parents: 56602
diff changeset
    40
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    41
  sealed abstract class Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    42
  case object Lowercase extends Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    43
  case object Uppercase extends Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    44
  case object Capitalized extends Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    45
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    46
  object Case
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    47
  {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    48
    def apply(c: Case, str: String): String =
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    49
      c match {
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    50
        case Lowercase => lowercase(str)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    51
        case Uppercase => uppercase(str)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    52
        case Capitalized => capitalize(str)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    53
      }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    54
    def unapply(str: String): Option[Case] =
59319
wenzelm
parents: 57087
diff changeset
    55
      if (str.nonEmpty) {
64610
1b89608974e9 clarified modules;
wenzelm
parents: 64370
diff changeset
    56
        if (Codepoint.iterator(str).forall(Character.isLowerCase(_))) Some(Lowercase)
1b89608974e9 clarified modules;
wenzelm
parents: 64370
diff changeset
    57
        else if (Codepoint.iterator(str).forall(Character.isUpperCase(_))) Some(Uppercase)
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    58
        else {
64610
1b89608974e9 clarified modules;
wenzelm
parents: 64370
diff changeset
    59
          val it = Codepoint.iterator(str)
56601
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    60
          if (Character.isUpperCase(it.next) && it.forall(Character.isLowerCase(_)))
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    61
            Some(Capitalized)
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    62
          else None
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    63
        }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    64
      }
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    65
      else None
8f80a243857d clarified word case;
wenzelm
parents: 56600
diff changeset
    66
  }
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    67
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    68
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    69
  /* sequence of words */
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    70
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    71
  def implode(words: Iterable[String]): String = words.iterator.mkString(" ")
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    72
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    73
  def explode(sep: Char => Boolean, text: String): List[String] =
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    74
    Library.separated_chunks(sep, text).map(_.toString).filter(_ != "").toList
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    75
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    76
  def explode(sep: Char, text: String): List[String] =
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    77
    explode(_ == sep, text)
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    78
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    79
  def explode(text: String): List[String] =
56747
f87e3be0de9a clarified;
wenzelm
parents: 56744
diff changeset
    80
    explode(Character.isWhitespace(_), text)
63450
afd657fffdf9 indentation of brackets;
wenzelm
parents: 62812
diff changeset
    81
afd657fffdf9 indentation of brackets;
wenzelm
parents: 62812
diff changeset
    82
afd657fffdf9 indentation of brackets;
wenzelm
parents: 62812
diff changeset
    83
  /* brackets */
afd657fffdf9 indentation of brackets;
wenzelm
parents: 62812
diff changeset
    84
afd657fffdf9 indentation of brackets;
wenzelm
parents: 62812
diff changeset
    85
  val open_brackets = "([{«‹⟨⌈⌊⦇⟦⦃"
afd657fffdf9 indentation of brackets;
wenzelm
parents: 62812
diff changeset
    86
  val close_brackets = ")]}»›⟩⌉⌋⦈⟧⦄"
56599
c4424d8c890f tuned signature -- separate module Word;
wenzelm
parents:
diff changeset
    87
}