src/Pure/library.scala
author wenzelm
Mon, 28 Dec 2009 16:45:01 +0100
changeset 34196 c352f679dcca
parent 34191 b6960fc09ef3
child 34198 ff5486262cd6
permissions -rw-r--r--
higher-order treatment of temporary files;
     1 /*  Title:      Pure/library.scala
     2     Author:     Makarius
     3 
     4 Basic library.
     5 */
     6 
     7 package isabelle
     8 
     9 import java.lang.System
    10 import java.io.File
    11 
    12 
    13 object Library
    14 {
    15   /* reverse CharSequence */
    16 
    17   class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence
    18   {
    19     require(0 <= start && start <= end && end <= text.length)
    20 
    21     def this(text: CharSequence) = this(text, 0, text.length)
    22 
    23     def length: Int = end - start
    24     def charAt(i: Int): Char = text.charAt(end - i - 1)
    25 
    26     def subSequence(i: Int, j: Int): CharSequence =
    27       if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i)
    28       else throw new IndexOutOfBoundsException
    29 
    30     override def toString: String =
    31     {
    32       val buf = new StringBuilder(length)
    33       for (i <- 0 until length)
    34         buf.append(charAt(i))
    35       buf.toString
    36     }
    37   }
    38 
    39 
    40   /* permissive UTF-8 decoding */
    41 
    42   // see also http://en.wikipedia.org/wiki/UTF-8#Description
    43   def decode_permissive_utf8(text: CharSequence): String =
    44   {
    45     val buf = new java.lang.StringBuilder(text.length)
    46     var code = -1
    47     var rest = 0
    48     def flush()
    49     {
    50       if (code != -1) {
    51         if (rest == 0 && Character.isValidCodePoint(code))
    52           buf.appendCodePoint(code)
    53         else buf.append('\uFFFD')
    54         code = -1
    55         rest = 0
    56       }
    57     }
    58     def init(x: Int, n: Int)
    59     {
    60       flush()
    61       code = x
    62       rest = n
    63     }
    64     def push(x: Int)
    65     {
    66       if (rest <= 0) init(x, -1)
    67       else {
    68         code <<= 6
    69         code += x
    70         rest -= 1
    71       }
    72     }
    73     for (i <- 0 until text.length) {
    74       val c = text.charAt(i)
    75       if (c < 128) { flush(); buf.append(c) }
    76       else if ((c & 0xC0) == 0x80) push(c & 0x3F)
    77       else if ((c & 0xE0) == 0xC0) init(c & 0x1F, 1)
    78       else if ((c & 0xF0) == 0xE0) init(c & 0x0F, 2)
    79       else if ((c & 0xF8) == 0xF0) init(c & 0x07, 3)
    80     }
    81     flush()
    82     buf.toString
    83   }
    84 
    85 
    86   /* temporary file */
    87 
    88   def with_tmp_file[A](prefix: String)(body: File => A): A =
    89   {
    90     val file = File.createTempFile(prefix, null)
    91     val result =
    92       try { body(file) }
    93       finally { file.delete }
    94     result
    95   }
    96 
    97 
    98   /* timing */
    99 
   100   def timeit[A](e: => A) =
   101   {
   102     val start = System.currentTimeMillis()
   103     val result = Exn.capture(e)
   104     val stop = System.currentTimeMillis()
   105     System.err.println((stop - start) + "ms elapsed time")
   106     Exn.release(result)
   107   }
   108 }