src/Pure/General/word.scala
changeset 64610 1b89608974e9
parent 64370 865b39487b5d
child 71601 97ccf48c2f0c
equal deleted inserted replaced
64609:7cc4b49be1ea 64610:1b89608974e9
    10 import java.util.Locale
    10 import java.util.Locale
    11 
    11 
    12 
    12 
    13 object Word
    13 object Word
    14 {
    14 {
    15   /* codepoints */
       
    16 
       
    17   def codepoint_iterator(str: String): Iterator[Int] =
       
    18     new Iterator[Int] {
       
    19       var offset = 0
       
    20       def hasNext: Boolean = offset < str.length
       
    21       def next: Int =
       
    22       {
       
    23         val c = str.codePointAt(offset)
       
    24         offset += Character.charCount(c)
       
    25         c
       
    26       }
       
    27     }
       
    28 
       
    29   def codepoint(c: Int): String = new String(Array(c), 0, 1)
       
    30 
       
    31 
       
    32   /* directionality */
    15   /* directionality */
    33 
    16 
    34   def bidi_detect(str: String): Boolean =
    17   def bidi_detect(str: String): Boolean =
    35     str.exists(c => c >= 0x590) && Bidi.requiresBidi(str.toArray, 0, str.length)
    18     str.exists(c => c >= 0x590) && Bidi.requiresBidi(str.toArray, 0, str.length)
    36 
    19 
    49       val n = Character.charCount(str.codePointAt(0))
    32       val n = Character.charCount(str.codePointAt(0))
    50       uppercase(str.substring(0, n)) + lowercase(str.substring(n))
    33       uppercase(str.substring(0, n)) + lowercase(str.substring(n))
    51     }
    34     }
    52 
    35 
    53   def perhaps_capitalize(str: String): String =
    36   def perhaps_capitalize(str: String): String =
    54     if (codepoint_iterator(str).forall(c => Character.isLowerCase(c) || Character.isDigit(c)))
    37     if (Codepoint.iterator(str).forall(c => Character.isLowerCase(c) || Character.isDigit(c)))
    55       capitalize(str)
    38       capitalize(str)
    56     else str
    39     else str
    57 
    40 
    58   sealed abstract class Case
    41   sealed abstract class Case
    59   case object Lowercase extends Case
    42   case object Lowercase extends Case
    68         case Uppercase => uppercase(str)
    51         case Uppercase => uppercase(str)
    69         case Capitalized => capitalize(str)
    52         case Capitalized => capitalize(str)
    70       }
    53       }
    71     def unapply(str: String): Option[Case] =
    54     def unapply(str: String): Option[Case] =
    72       if (str.nonEmpty) {
    55       if (str.nonEmpty) {
    73         if (codepoint_iterator(str).forall(Character.isLowerCase(_))) Some(Lowercase)
    56         if (Codepoint.iterator(str).forall(Character.isLowerCase(_))) Some(Lowercase)
    74         else if (codepoint_iterator(str).forall(Character.isUpperCase(_))) Some(Uppercase)
    57         else if (Codepoint.iterator(str).forall(Character.isUpperCase(_))) Some(Uppercase)
    75         else {
    58         else {
    76           val it = codepoint_iterator(str)
    59           val it = Codepoint.iterator(str)
    77           if (Character.isUpperCase(it.next) && it.forall(Character.isLowerCase(_)))
    60           if (Character.isUpperCase(it.next) && it.forall(Character.isLowerCase(_)))
    78             Some(Capitalized)
    61             Some(Capitalized)
    79           else None
    62           else None
    80         }
    63         }
    81       }
    64       }