src/Pure/General/utf8.scala
author wenzelm
Tue, 26 Mar 2024 21:44:18 +0100
changeset 80018 ac4412562c7b
parent 76357 49463aef2ead
child 80350 96843eb96493
permissions -rw-r--r--
more robust XML body: allow empty text, as well as arbitrary pro-forma markup (e.g. see XML.blob in Isabelle/ML);
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
76357
49463aef2ead tuned signature, following isabelle.setup.Environment;
wenzelm
parents: 76356
diff changeset
    10
import java.nio.charset.{Charset, StandardCharsets}
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    11
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    12
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73561
diff changeset
    13
object UTF8 {
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    14
  /* charset */
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    15
76357
49463aef2ead tuned signature, following isabelle.setup.Environment;
wenzelm
parents: 76356
diff changeset
    16
  val charset: Charset = StandardCharsets.UTF_8
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    17
62527
aae9a2a855e0 tuned signature;
wenzelm
parents: 54444
diff changeset
    18
  def bytes(s: String): Array[Byte] = s.getBytes(charset)
aae9a2a855e0 tuned signature;
wenzelm
parents: 54444
diff changeset
    19
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    20
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    21
  /* permissive UTF-8 decoding */
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    22
68224
1f7308050349 prefer HTTPS;
wenzelm
parents: 65196
diff changeset
    23
  // see also https://en.wikipedia.org/wiki/UTF-8#Description
54444
a2290f36d1d6 prefer UTF8.decode_permissive;
wenzelm
parents: 54440
diff changeset
    24
  // overlong encodings enable byte-stuffing of low-ASCII
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    25
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73561
diff changeset
    26
  def decode_permissive(text: CharSequence): String = {
73561
c83152933579 clarified signature: Bytes extends CharSequence already (see d201996f72a8);
wenzelm
parents: 73340
diff changeset
    27
    val len = text.length
c83152933579 clarified signature: Bytes extends CharSequence already (see d201996f72a8);
wenzelm
parents: 73340
diff changeset
    28
    val buf = new java.lang.StringBuilder(len)
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    29
    var code = -1
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    30
    var rest = 0
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73561
diff changeset
    31
    def flush(): Unit = {
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    32
      if (code != -1) {
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    33
        if (rest == 0 && Character.isValidCodePoint(code))
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    34
          buf.appendCodePoint(code)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    35
        else buf.append('\uFFFD')
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    36
        code = -1
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    37
        rest = 0
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    38
      }
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    39
    }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73561
diff changeset
    40
    def init(x: Int, n: Int): Unit = {
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    41
      flush()
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    42
      code = x
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    43
      rest = n
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    44
    }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73561
diff changeset
    45
    def push(x: Int): Unit = {
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    46
      if (rest <= 0) init(x, -1)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    47
      else {
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    48
        code <<= 6
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    49
        code += x
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    50
        rest -= 1
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    51
      }
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    52
    }
73561
c83152933579 clarified signature: Bytes extends CharSequence already (see d201996f72a8);
wenzelm
parents: 73340
diff changeset
    53
    for (i <- 0 until len) {
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    54
      val c = text.charAt(i)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    55
      if (c < 128) { flush(); buf.append(c) }
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    56
      else if ((c & 0xC0) == 0x80) push(c & 0x3F)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    57
      else if ((c & 0xE0) == 0xC0) init(c & 0x1F, 1)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    58
      else if ((c & 0xF0) == 0xE0) init(c & 0x0F, 2)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    59
      else if ((c & 0xF8) == 0xF0) init(c & 0x07, 3)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    60
    }
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    61
    flush()
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    62
    buf.toString
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    63
  }
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    64
}