Scoop API

scoop:scoop.url:0.1.0

src/iri.scoop

Selected production source · src/iri.scoop

1package scoop.url
3import scoop.bytes.ByteArrayBuilder
5internal class IriInput(val text: String, val locations: Array<Long>)
7internal fun iriScalar(code: Int, query: Boolean): Boolean {
8 if (code >= 0xA0 && code <= 0xD7FF) return true
9 if (code >= 0xF900 && code <= 0xFDCF) return true
10 if (code >= 0xFDF0 && code <= 0xFFEF) return true
11 if (code >= 0x10000 && code <= 0xDFFFD && (code and 0xFFFF) <= 0xFFFD) return true
12 if (code >= 0xE1000 && code <= 0xEFFFD) return true
13 return query && ((code >= 0xE000 && code <= 0xF8FF) ||
14 (code >= 0xF0000 && code <= 0x10FFFD && (code and 0xFFFF) <= 0xFFFD))
17internal fun prepareIri(text: String): IriInput {
18 val bytes = text.toByteArray()
19 val output = ByteArrayBuilder()
20 val locations = ArrayList<Long>()
21 var offset = 0L
22 var skip = 0
23 var query = false
24 var fragment = false
25 for (character in text) {
26 if (skip > 0) { skip -= 1; continue }
27 val code = character.code
28 if (code <= 32 || code == 127 || code == 92 || (code >= 128 && !iriScalar(code, query))) {
29 urlError(UrlComponent.Reference, "character is not allowed in an IRI", Some(offset))
30 }
31 if (code == 37) {
32 if (bytes.size - offset < 3L) urlError(UrlComponent.Reference, "incomplete percent escape", Some(offset))
33 val high = hexDigit(bytes[offset + 1L].toInt())
34 val low = hexDigit(bytes[offset + 2L].toInt())
35 if (high < 0 || low < 0) urlError(UrlComponent.Reference, "invalid percent escape", Some(offset))
36 appendPercent(output, (high * 16 + low).toByte())
37 locations.add(offset); locations.add(offset + 1L); locations.add(offset + 2L)
38 offset += 3L
39 skip = 2
40 } else if (code < 128) {
41 output.append(code.toByte()); locations.add(offset)
42 offset += 1L
43 if (code == 63 && !fragment) query = true
44 if (code == 35) { fragment = true; query = false }
45 } else {
46 val count = if (code <= 0x7FF) 2L else if (code <= 0xFFFF) 3L else 4L
47 for (index in 0L..<count) {
48 appendPercent(output, bytes[offset + index])
49 locations.add(offset + index); locations.add(offset + index); locations.add(offset + index)
50 }
51 offset += count
52 }
53 }
54 locations.add(bytes.size)
55 return IriInput(String.fromUtf8(output.toByteArray()), locations.toArray())
58internal fun iriDisplay(text: String, query: Boolean = false): String {
59 val bytes = text.toByteArray()
60 val output = StringBuilder()
61 var index = 0L
62 while (index < bytes.size) {
63 if (bytes[index] != 37.toByte()) {
64 output.append((bytes[index].toInt() and 255).toChar())
65 index += 1L
66 continue
67 }
68 val start = index
69 val decoded = ByteArrayBuilder()
70 while (index + 2L < bytes.size && bytes[index] == 37.toByte()) {
71 decoded.append((hexDigit(bytes[index + 1L].toInt()) * 16 + hexDigit(bytes[index + 2L].toInt())).toByte())
72 index += 3L
73 }
74 val unicode = String.fromUtf8OrNone(decoded.toByteArray())
75 if (unicode == None) output.append(text.slice(start, index))
76 else {
77 for (character in unicode!!) {
78 if (character.code >= 128 && iriScalar(character.code, query)) output.append(character)
79 else {
80 val encoded = ByteArrayBuilder()
81 for (byte in character.toString().toByteArray()) appendPercent(encoded, byte)
82 output.append(String.fromUtf8(encoded.toByteArray()))
83 }
84 }
85 }
86 }
87 return output.toString()