scoop:scoop.url:0.1.0
src/iri.scoop
Selected production source · src/iri.scoop
1
package scoop.url3
import scoop.bytes.ByteArrayBuilder5
internal class IriInput(val text: String, val locations: Array<Long>)7
internal fun iriScalar(code: Int, query: Boolean): Boolean {8
if (code >= 0xA0 && code <= 0xD7FF) return true9
if (code >= 0xF900 && code <= 0xFDCF) return true10
if (code >= 0xFDF0 && code <= 0xFFEF) return true11
if (code >= 0x10000 && code <= 0xDFFFD && (code and 0xFFFF) <= 0xFFFD) return true12
if (code >= 0xE1000 && code <= 0xEFFFD) return true13
return query && ((code >= 0xE000 && code <= 0xF8FF) ||14
(code >= 0xF0000 && code <= 0x10FFFD && (code and 0xFFFF) <= 0xFFFD))15
}17
internal fun prepareIri(text: String): IriInput {18
val bytes = text.toByteArray()19
val output = ByteArrayBuilder()20
val locations = ArrayList<Long>()21
var offset = 0L22
var skip = 023
var query = false24
var fragment = false25
for (character in text) {26
if (skip > 0) { skip -= 1; continue }27
val code = character.code28
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 += 3L39
skip = 240
} else if (code < 128) {41
output.append(code.toByte()); locations.add(offset)42
offset += 1L43
if (code == 63 && !fragment) query = true44
if (code == 35) { fragment = true; query = false }45
} else {46
val count = if (code <= 0x7FF) 2L else if (code <= 0xFFFF) 3L else 4L47
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 += count52
}53
}54
locations.add(bytes.size)55
return IriInput(String.fromUtf8(output.toByteArray()), locations.toArray())56
}58
internal fun iriDisplay(text: String, query: Boolean = false): String {59
val bytes = text.toByteArray()60
val output = StringBuilder()61
var index = 0L62
while (index < bytes.size) {63
if (bytes[index] != 37.toByte()) {64
output.append((bytes[index].toInt() and 255).toChar())65
index += 1L66
continue67
}68
val start = index69
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 += 3L73
}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()88
}