src/Pure/General/utf8.scala
author wenzelm
Wed, 03 Jul 2024 15:24:34 +0200
changeset 80490 dd2f5fb363a5
parent 80444 2bbcfcfca0cd
child 80492 43323d886ea3
permissions -rw-r--r--
tuned;
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
80351
dbbe26afc319 clarified signature: more accurate types;
wenzelm
parents: 80350
diff changeset
    26
  def decode_permissive_bytes(bytes: Bytes.Vec): String = {
dbbe26afc319 clarified signature: more accurate types;
wenzelm
parents: 80350
diff changeset
    27
    val size = bytes.size
dbbe26afc319 clarified signature: more accurate types;
wenzelm
parents: 80350
diff changeset
    28
    val buf = new java.lang.StringBuilder((size min Space.GiB(1).bytes).toInt)
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) {
80444
wenzelm
parents: 80442
diff changeset
    33
        if (rest == 0 && Character.isValidCodePoint(code)) buf.appendCodePoint(code)
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    34
        else buf.append('\uFFFD')
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    35
        code = -1
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    36
        rest = 0
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    37
      }
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    38
    }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73561
diff changeset
    39
    def init(x: Int, n: Int): Unit = {
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    40
      flush()
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    41
      code = x
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    42
      rest = n
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    43
    }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73561
diff changeset
    44
    def push(x: Int): Unit = {
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    45
      if (rest <= 0) init(x, -1)
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    46
      else {
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    47
        code <<= 6
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    48
        code += x
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    49
        rest -= 1
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    50
      }
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    51
    }
80351
dbbe26afc319 clarified signature: more accurate types;
wenzelm
parents: 80350
diff changeset
    52
    for (i <- 0L until size) {
80355
5a555acad203 clarified signature (again);
wenzelm
parents: 80352
diff changeset
    53
      val c: Char = bytes(i)
5a555acad203 clarified signature (again);
wenzelm
parents: 80352
diff changeset
    54
      if (c < 128) { flush(); buf.append(c) }
80352
7a6cba7c77c9 minor performance tuning;
wenzelm
parents: 80351
diff changeset
    55
      else if ((c & 0xC0) == 0x80) push(c & 0x3F)
7a6cba7c77c9 minor performance tuning;
wenzelm
parents: 80351
diff changeset
    56
      else if ((c & 0xE0) == 0xC0) init(c & 0x1F, 1)
7a6cba7c77c9 minor performance tuning;
wenzelm
parents: 80351
diff changeset
    57
      else if ((c & 0xF0) == 0xE0) init(c & 0x0F, 2)
7a6cba7c77c9 minor performance tuning;
wenzelm
parents: 80351
diff changeset
    58
      else if ((c & 0xF8) == 0xF0) init(c & 0x07, 3)
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    59
    }
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    60
    flush()
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    61
    buf.toString
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    62
  }
80350
96843eb96493 clarified signature;
wenzelm
parents: 76357
diff changeset
    63
80490
wenzelm
parents: 80444
diff changeset
    64
  def decode_permissive(text: String): String = {
wenzelm
parents: 80444
diff changeset
    65
    val relevant = {
wenzelm
parents: 80444
diff changeset
    66
      var i = 0
wenzelm
parents: 80444
diff changeset
    67
      val n = text.length
wenzelm
parents: 80444
diff changeset
    68
      var found = false
wenzelm
parents: 80444
diff changeset
    69
      while (i < n && !found) {
wenzelm
parents: 80444
diff changeset
    70
        if (text(i) >= 128) { found = true }
wenzelm
parents: 80444
diff changeset
    71
        i += 1
wenzelm
parents: 80444
diff changeset
    72
      }
wenzelm
parents: 80444
diff changeset
    73
      found
wenzelm
parents: 80444
diff changeset
    74
    }
wenzelm
parents: 80444
diff changeset
    75
    if (relevant) decode_permissive_bytes(new Bytes.Vec_String(text))
80371
e43944fae5e5 clarified signature;
wenzelm
parents: 80355
diff changeset
    76
    else text
80490
wenzelm
parents: 80444
diff changeset
    77
  }
50203
00d8ad713e32 explicit module UTF8;
wenzelm
parents:
diff changeset
    78
}