56599
|
1 |
/* Title: Pure/General/word.scala
|
|
2 |
Module: PIDE
|
|
3 |
Author: Makarius
|
|
4 |
|
|
5 |
Support for plain text words.
|
|
6 |
*/
|
|
7 |
|
|
8 |
package isabelle
|
|
9 |
|
|
10 |
|
|
11 |
import java.util.Locale
|
|
12 |
|
|
13 |
|
|
14 |
object Word
|
|
15 |
{
|
56601
|
16 |
/* codepoints */
|
|
17 |
|
|
18 |
def codepoint_iterator(str: String): Iterator[Int] =
|
|
19 |
new Iterator[Int] {
|
|
20 |
var offset = 0
|
|
21 |
def hasNext: Boolean = offset < str.length
|
|
22 |
def next: Int =
|
|
23 |
{
|
|
24 |
val c = str.codePointAt(offset)
|
|
25 |
offset += Character.charCount(c)
|
|
26 |
c
|
|
27 |
}
|
|
28 |
}
|
|
29 |
|
|
30 |
|
56600
|
31 |
/* case */
|
|
32 |
|
56599
|
33 |
def lowercase(str: String): String = str.toLowerCase(Locale.ROOT)
|
|
34 |
def uppercase(str: String): String = str.toUpperCase(Locale.ROOT)
|
|
35 |
|
|
36 |
def capitalize(str: String): String =
|
|
37 |
if (str.length == 0) str
|
56601
|
38 |
else {
|
|
39 |
val n = Character.charCount(str.codePointAt(0))
|
|
40 |
uppercase(str.substring(0, n)) + str.substring(n)
|
|
41 |
}
|
|
42 |
|
|
43 |
sealed abstract class Case
|
|
44 |
case object Lowercase extends Case
|
|
45 |
case object Uppercase extends Case
|
|
46 |
case object Capitalized extends Case
|
|
47 |
|
|
48 |
object Case
|
|
49 |
{
|
|
50 |
def apply(c: Case, str: String): String =
|
|
51 |
c match {
|
|
52 |
case Lowercase => lowercase(str)
|
|
53 |
case Uppercase => uppercase(str)
|
|
54 |
case Capitalized => capitalize(str)
|
|
55 |
}
|
|
56 |
def unapply(str: String): Option[Case] =
|
|
57 |
if (!str.isEmpty) {
|
|
58 |
if (codepoint_iterator(str).forall(Character.isLowerCase(_))) Some(Lowercase)
|
|
59 |
else if (codepoint_iterator(str).forall(Character.isUpperCase(_))) Some(Uppercase)
|
|
60 |
else {
|
|
61 |
val it = codepoint_iterator(str)
|
|
62 |
if (Character.isUpperCase(it.next) && it.forall(Character.isLowerCase(_)))
|
|
63 |
Some(Capitalized)
|
|
64 |
else None
|
|
65 |
}
|
|
66 |
}
|
|
67 |
else None
|
|
68 |
}
|
56599
|
69 |
|
|
70 |
def is_capitalized(str: String): Boolean =
|
|
71 |
str.length > 0 &&
|
|
72 |
Character.isUpperCase(str(0)) && str.substring(1).forall(Character.isLowerCase(_))
|
|
73 |
|
|
74 |
def is_all_caps(str: String): Boolean =
|
|
75 |
str.length > 0 && str.forall(Character.isUpperCase(_))
|
|
76 |
|
56600
|
77 |
|
|
78 |
/* sequence of words */
|
|
79 |
|
|
80 |
def implode(words: Iterable[String]): String = words.iterator.mkString(" ")
|
|
81 |
|
|
82 |
def explode(sep: Char => Boolean, text: String): List[String] =
|
|
83 |
Library.separated_chunks(sep, text).map(_.toString).filter(_ != "").toList
|
|
84 |
|
|
85 |
def explode(sep: Char, text: String): List[String] =
|
|
86 |
explode(_ == sep, text)
|
|
87 |
|
|
88 |
def explode(text: String): List[String] =
|
|
89 |
explode(Symbol.is_ascii_blank(_), text)
|
56599
|
90 |
}
|
|
91 |
|