src/Pure/General/utf8.scala
author wenzelm
Sat, 24 Jul 2021 15:38:41 +0200
changeset 74056 fb8d5c0133c9
parent 73561 c83152933579
child 75393 87ebf5a50283
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64639
bad5de3f9554 clarified directories;
wenzelm
parents: 64617
diff changeset
     1
/*  Title:      Pure/General/utf8.scala
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
     3
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
     4
Variations on UTF-8.
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
     5
*/
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
     6
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
     7
package isabelle
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
     8
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
     9
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    10
import java.nio.charset.Charset
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    11
import scala.io.Codec
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    12
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    13
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    14
object UTF8
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    15
{
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    16
  /* charset */
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    17
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    18
  val charset_name: String = "UTF-8"
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    19
  val charset: Charset = Charset.forName(charset_name)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    20
  def codec(): Codec = Codec(charset)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    21
62527
aae9a2a855e0 tuned signature;
wenzelm
parents: 54444
diff changeset
    22
  def bytes(s: String): Array[Byte] = s.getBytes(charset)
aae9a2a855e0 tuned signature;
wenzelm
parents: 54444
diff changeset
    23
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    24
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    25
  /* permissive UTF-8 decoding */
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    26
68224
1f7308050349 prefer HTTPS;
wenzelm
parents: 65196
diff changeset
    27
  // see also https://en.wikipedia.org/wiki/UTF-8#Description
54444
a2290f36d1d6 prefer UTF8.decode_permissive;
wenzelm
parents: 54440
diff changeset
    28
  // overlong encodings enable byte-stuffing of low-ASCII
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    29
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    30
  def decode_permissive(text: CharSequence): String =
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    31
  {
73561
c83152933579 clarified signature: Bytes extends CharSequence already (see d201996f72a8);
wenzelm
parents: 73340
diff changeset
    32
    val len = text.length
c83152933579 clarified signature: Bytes extends CharSequence already (see d201996f72a8);
wenzelm
parents: 73340
diff changeset
    33
    val buf = new java.lang.StringBuilder(len)
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    34
    var code = -1
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    35
    var rest = 0
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73120
diff changeset
    36
    def flush(): Unit =
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    37
    {
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    38
      if (code != -1) {
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    39
        if (rest == 0 && Character.isValidCodePoint(code))
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    40
          buf.appendCodePoint(code)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    41
        else buf.append('\uFFFD')
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    42
        code = -1
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    43
        rest = 0
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    44
      }
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    45
    }
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73120
diff changeset
    46
    def init(x: Int, n: Int): Unit =
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    47
    {
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    48
      flush()
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    49
      code = x
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    50
      rest = n
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    51
    }
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73120
diff changeset
    52
    def push(x: Int): Unit =
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    53
    {
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    54
      if (rest <= 0) init(x, -1)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    55
      else {
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    56
        code <<= 6
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    57
        code += x
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    58
        rest -= 1
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    59
      }
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    60
    }
73561
c83152933579 clarified signature: Bytes extends CharSequence already (see d201996f72a8);
wenzelm
parents: 73340
diff changeset
    61
    for (i <- 0 until len) {
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    62
      val c = text.charAt(i)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    63
      if (c < 128) { flush(); buf.append(c) }
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    64
      else if ((c & 0xC0) == 0x80) push(c & 0x3F)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    65
      else if ((c & 0xE0) == 0xC0) init(c & 0x1F, 1)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    66
      else if ((c & 0xF0) == 0xE0) init(c & 0x0F, 2)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    67
      else if ((c & 0xF8) == 0xF0) init(c & 0x07, 3)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    68
    }
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    69
    flush()
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    70
    buf.toString
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    71
  }
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    72
}