| @@ -81,7 +81,7 @@ pub fn v7_from(unix_ms: Int, randomness: Bytes) -> Uuid { |
| 81 | 81 | [r0, r1, r2, ..rest] -> { |
| 82 | 82 | let time_bytes = list.map( |
| 83 | 83 | list.range(0, 5) |
| 84 | | − , fn(i) { unix_ms / power(256, 5 - i) % 256 } |
| 84 | + , fn(i) { bits.low_bits(bits.shift_right(unix_ms, 8 * (5 - i)), 8) } |
| 85 | 85 | ) |
| 86 | 86 | Uuid( |
| 87 | 87 | raw.from_list( |
| @@ -121,10 +121,6 @@ fn limit() -> Int { |
| 121 | 121 | bits.shift_left(1, 128) |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | | −fn power(base: Int, n: Int) -> Int { |
| 125 | | − if n == 0 { 1 } else { base * power(base, n - 1) } |
| 126 | | −} |
| 127 | | − |
| 128 | 124 | // ---- the RFC 9562 text form ---------------------------------------------------------- |
| 129 | 125 | |
| 130 | 126 | /// `id` as 32 lowercase hex digits in groups of 8-4-4-4-12: |
| @@ -166,10 +162,18 @@ fn ok_or_none(r: Result<a, e>) -> Option<a> { |
| 166 | 162 | /// The characters of a text form, from digit 0 up. Built by `alphabet`, |
| 167 | 163 | /// or one of the presets. |
| 168 | 164 | pub opaque type Alphabet { |
| 169 | | − /// `chars`, the number of them, the digits of an encoding, and other |
| 170 | | − /// characters `decode` also reads (lowercase for uppercase, say), with |
| 171 | | − /// their digit. |
| 172 | | − Alphabet(chars: Bytes, base: Int, width: Int, aliases: List<(Int, Int)>) |
| 165 | + /// `chars` (in ascending ASCII order), the number of them, the digits of |
| 166 | + /// an encoding, the bits per digit when the base is a power of 2 (0 |
| 167 | + /// otherwise), whether `decode` also reads a letter in the other case, |
| 168 | + /// and other characters `decode` reads, with their digit. |
| 169 | + Alphabet( |
| 170 | + chars: Bytes |
| 171 | + , base: Int |
| 172 | + , width: Int |
| 173 | + , bits: Int |
| 174 | + , fold_case: Bool |
| 175 | + , aliases: List<(Int, Int)> |
| 176 | + ) |
| 173 | 177 | } |
| 174 | 178 | |
| 175 | 179 | /// Why `alphabet` refused some characters. |
| @@ -188,7 +192,8 @@ pub type AlphabetError { |
| 188 | 192 | } |
| 189 | 193 | |
| 190 | 194 | /// An alphabet of the characters of `chars`, digit 0 first: at least 2, |
| 191 | | −/// each once, URL-safe, in ascending ASCII order. |
| 195 | +/// each once, URL-safe, in ascending ASCII order. Checking them takes a |
| 196 | +/// few microseconds: build an alphabet once and keep it. |
| 192 | 197 | pub fn alphabet(chars: String) -> Result<Alphabet, AlphabetError> { |
| 193 | 198 | let codes = raw.to_list(raw.from_string(chars)) |
| 194 | 199 | match ( |
| @@ -200,13 +205,34 @@ pub fn alphabet(chars: String) -> Result<Alphabet, AlphabetError> { |
| 200 | 205 | , ([c, ..], _, _) -> Error(NotUrlSafe(char(c))) |
| 201 | 206 | , (_, Some(c), _) -> Error(Repeated(char(c))) |
| 202 | 207 | , (_, _, Some(c)) -> Error(NotAscending(char(c))) |
| 203 | | − , _ -> Ok(make(codes, [])) |
| 208 | + , _ -> { |
| 209 | + let base = list.length(codes) |
| 210 | + Ok( |
| 211 | + Alphabet( |
| 212 | + raw.from_list(codes) |
| 213 | + , base |
| 214 | + , width_for(base, 0, 1) |
| 215 | + , bits_for(base) |
| 216 | + , False |
| 217 | + , [] |
| 218 | + ) |
| 219 | + ) |
| 220 | + } |
| 204 | 221 | } |
| 205 | 222 | } |
| 206 | 223 | |
| 207 | | −fn make(codes: List<Int>, aliases: List<(Int, Int)>) -> Alphabet { |
| 208 | | − let base = list.length(codes) |
| 209 | | − Alphabet(raw.from_list(codes), base, width_for(base, 0, 1), aliases) |
| 224 | +/// A preset: `chars` are known to be valid, and `width` and `k` right |
| 225 | +/// (the tests check them against `alphabet`), so nothing is computed per |
| 226 | +/// call. |
| 227 | +fn preset( |
| 228 | + chars: String |
| 229 | +, width: Int |
| 230 | +, k: Int |
| 231 | +, fold_case: Bool |
| 232 | +, aliases: List<(Int, Int)> |
| 233 | +) -> Alphabet { |
| 234 | + let b = raw.from_string(chars) |
| 235 | + Alphabet(b, raw.length(b), width, k, fold_case, aliases) |
| 210 | 236 | } |
| 211 | 237 | |
| 212 | 238 | /// The smallest `n` with `base^n >= 2^128`. |
| @@ -214,6 +240,11 @@ fn width_for(base: Int, n: Int, reach: Int) -> Int { |
| 214 | 240 | if reach >= limit() { n } else { width_for(base, n + 1, reach * base) } |
| 215 | 241 | } |
| 216 | 242 | |
| 243 | +/// `k` when `base` is `2^k`, else 0. |
| 244 | +fn bits_for(base: Int) -> Int { |
| 245 | + if bits.and(base, base - 1) == 0 { bits.bit_length(base) - 1 } else { 0 } |
| 246 | +} |
| 247 | + |
| 217 | 248 | /// RFC 3986's unreserved characters. |
| 218 | 249 | fn url_safe(c: Int) -> Bool { |
| 219 | 250 | c >= 48 && c <= 57 || c >= 65 && c <= 90 || c >= 97 && c <= 122 || c == 45 |
| @@ -248,14 +279,13 @@ fn char(c: Int) -> String { |
| 248 | 279 | /// The number of characters of every encoding with `a`. |
| 249 | 280 | pub fn width(a: Alphabet) -> Int { |
| 250 | 281 | match a { |
| 251 | | − Alphabet(_, _, w, _) -> w |
| 282 | + Alphabet(_, _, w, _, _, _) -> w |
| 252 | 283 | } |
| 253 | 284 | } |
| 254 | 285 | |
| 255 | 286 | /// Lowercase hex, 32 characters (`decode` also reads uppercase). |
| 256 | 287 | pub fn hex() -> Alphabet { |
| 257 | | − let digits = codes_of("0123456789abcdef") |
| 258 | | − make(digits, list.map(list.range(0, 5), fn(i) { (65 + i, 10 + i) })) |
| 288 | + preset("0123456789abcdef", 32, 4, True, []) |
| 259 | 289 | } |
| 260 | 290 | |
| 261 | 291 | /// Crockford's base 32, 26 characters: digits and uppercase letters |
| @@ -263,51 +293,105 @@ pub fn hex() -> Alphabet { |
| 263 | 293 | /// O as 0 (in either case), as Crockford specifies. The default: `show` |
| 264 | 294 | /// uses it. |
| 265 | 295 | pub fn crockford() -> Alphabet { |
| 266 | | − let digits = codes_of("0123456789ABCDEFGHJKMNPQRSTVWXYZ") |
| 267 | | − let lower = list.map(list.range(10, 31), fn(d) { (nth(digits, d) + 32, d) }) |
| 268 | | − make( |
| 269 | | − digits |
| 270 | | − , list.append( |
| 271 | | − lower |
| 272 | | − , [(73, 1), (105, 1), (76, 1), (108, 1), (79, 0), (111, 0)] |
| 273 | | − ) |
| 296 | + preset( |
| 297 | + "0123456789ABCDEFGHJKMNPQRSTVWXYZ" |
| 298 | + , 26 |
| 299 | + , 5 |
| 300 | + , True |
| 301 | + , [(73, 1), (76, 1), (79, 0)] |
| 274 | 302 | ) |
| 275 | 303 | } |
| 276 | 304 | |
| 277 | 305 | /// Bitcoin's base 58, 22 characters: no 0, O, I or l, which look alike. |
| 278 | 306 | pub fn base58() -> Alphabet { |
| 279 | | − make( |
| 280 | | − codes_of("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") |
| 307 | + preset( |
| 308 | + "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" |
| 309 | + , 22 |
| 310 | + , 0 |
| 311 | + , False |
| 281 | 312 | , [] |
| 282 | 313 | ) |
| 283 | 314 | } |
| 284 | 315 | |
| 285 | 316 | /// Digits, uppercase, then lowercase letters: 22 characters. |
| 286 | 317 | pub fn base62() -> Alphabet { |
| 287 | | − make( |
| 288 | | − codes_of("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") |
| 318 | + preset( |
| 319 | + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 320 | + , 22 |
| 321 | + , 0 |
| 322 | + , False |
| 289 | 323 | , [] |
| 290 | 324 | ) |
| 291 | 325 | } |
| 292 | 326 | |
| 293 | | −fn codes_of(s: String) -> List<Int> { |
| 294 | | − raw.to_list(raw.from_string(s)) |
| 327 | +/// The digit of the character `c`, or -1. `chars` is in ascending order, |
| 328 | +/// so it is searched by halves. |
| 329 | +fn digit_of( |
| 330 | + c: Int |
| 331 | +, chars: Bytes |
| 332 | +, fold_case: Bool |
| 333 | +, aliases: List<(Int, Int)> |
| 334 | +) -> Int { |
| 335 | + let d = search(chars, c, 0, raw.length(chars)) |
| 336 | + if d >= 0 { |
| 337 | + d |
| 338 | + } else { |
| 339 | + match list.find(aliases, fn(alias) { alias.0 == c }) { |
| 340 | + Some((_, alias)) -> alias |
| 341 | + , None if fold_case && is_letter(c) -> |
| 342 | + digit_of(other_case(c), chars, False, aliases) |
| 343 | + , None -> -1 |
| 344 | + } |
| 345 | + } |
| 295 | 346 | } |
| 296 | 347 | |
| 297 | | −fn nth(xs: List<Int>, i: Int) -> Int { |
| 298 | | − match xs { |
| 299 | | − [] -> 0 |
| 300 | | − , [x, ..rest] -> if i == 0 { x } else { nth(rest, i - 1) } |
| 348 | +/// The index of `c` in `chars[lo..hi)`, or -1. |
| 349 | +fn search(chars: Bytes, c: Int, lo: Int, hi: Int) -> Int { |
| 350 | + if lo >= hi { |
| 351 | + -1 |
| 352 | + } else { |
| 353 | + let mid = (lo + hi) / 2 |
| 354 | + let x = raw.read_u8(chars, mid) |
| 355 | + if x == c { |
| 356 | + mid |
| 357 | + } else if x < c { |
| 358 | + search(chars, c, mid + 1, hi) |
| 359 | + } else { |
| 360 | + search(chars, c, lo, mid) |
| 361 | + } |
| 301 | 362 | } |
| 302 | 363 | } |
| 303 | 364 | |
| 365 | +/// A letter in the other case (ASCII). |
| 366 | +fn other_case(c: Int) -> Int { |
| 367 | + if c >= 97 { c - 32 } else { c + 32 } |
| 368 | +} |
| 369 | + |
| 370 | +fn is_letter(c: Int) -> Bool { |
| 371 | + c >= 65 && c <= 90 || c >= 97 && c <= 122 |
| 372 | +} |
| 373 | + |
| 304 | 374 | // ---- encoding ------------------------------------------------------------------------ |
| 305 | 375 | |
| 376 | +// An alphabet of 2^k characters is read and written k bits at a time, in |
| 377 | +// small `Int`s: the bits not yet consumed are `acc`, with `p` = 2^(their |
| 378 | +// number), so plain arithmetic (inlined) does what shifts and masks |
| 379 | +// would (calls into the runtime). Other bases go through the id's value, |
| 380 | +// a 128-bit `Int`. |
| 381 | + |
| 306 | 382 | /// `id` with `a`'s characters: its 128 bits as a number in base |
| 307 | 383 | /// `a`'s size, padded to `width(a)` with the first character. |
| 308 | 384 | pub fn encode(id: Uuid, a: Alphabet) -> String { |
| 309 | 385 | match a { |
| 310 | | − Alphabet(chars, base, w, _) -> { |
| 386 | + Alphabet(chars, base, w, k, _, _) if k > 0 -> { |
| 387 | + // `w * k` bits: the id's 128 after `w * k - 128` zero bits, which |
| 388 | + // count as already read. |
| 389 | + let out = raw.builder_with_capacity(w) |
| 390 | + raw.build_string( |
| 391 | + encode_bits(to_bytes(id), 0, chars, base, 0, pow2(w * k - 128), out) |
| 392 | + ) |
| 393 | + } |
| 394 | + , Alphabet(chars, base, w, _, _, _) -> { |
| 311 | 395 | let value = list.fold( |
| 312 | 396 | raw.to_list(to_bytes(id)) |
| 313 | 397 | , 0 |
| @@ -318,6 +402,39 @@ pub fn encode(id: Uuid, a: Alphabet) -> String { |
| 318 | 402 | } |
| 319 | 403 | } |
| 320 | 404 | |
| 405 | +/// The digits of bytes `i..16` of `b`, after the bits `acc` (`p` = 2^n). |
| 406 | +fn encode_bits( |
| 407 | + b: Bytes |
| 408 | +, i: Int |
| 409 | +, chars: Bytes |
| 410 | +, base: Int |
| 411 | +, acc: Int |
| 412 | +, p: Int |
| 413 | +, out: raw.Builder |
| 414 | +) -> raw.Builder { |
| 415 | + if p >= base { |
| 416 | + let p = p / base |
| 417 | + let out = raw.append_byte(out, raw.read_u8(chars, acc / p)) |
| 418 | + encode_bits(b, i, chars, base, acc % p, p, out) |
| 419 | + } else if i == 16 { |
| 420 | + out |
| 421 | + } else { |
| 422 | + encode_bits( |
| 423 | + b |
| 424 | + , i + 1 |
| 425 | + , chars |
| 426 | + , base |
| 427 | + , acc * 256 + raw.read_u8(b, i) |
| 428 | + , p * 256 |
| 429 | + , out |
| 430 | + ) |
| 431 | + } |
| 432 | +} |
| 433 | + |
| 434 | +fn pow2(n: Int) -> Int { |
| 435 | + if n == 0 { 1 } else { 2 * pow2(n - 1) } |
| 436 | +} |
| 437 | + |
| 321 | 438 | /// The last `n` digits of `value` in base `base`, as `chars`' codes, |
| 322 | 439 | /// before `acc`. |
| 323 | 440 | fn digits(value: Int, base: Int, n: Int, chars: Bytes, acc: List<Int>) -> List< |
| @@ -326,10 +443,7 @@ fn digits(value: Int, base: Int, n: Int, chars: Bytes, acc: List<Int>) -> List< |
| 326 | 443 | if n == 0 { |
| 327 | 444 | acc |
| 328 | 445 | } else { |
| 329 | | − let code = match raw.get(chars, value % base) { |
| 330 | | − Some(c) -> c |
| 331 | | − , None -> 0 |
| 332 | | − } |
| 446 | + let code = raw.read_u8(chars, value % base) |
| 333 | 447 | digits(value / base, base, n - 1, chars, [code, ..acc]) |
| 334 | 448 | } |
| 335 | 449 | } |
| @@ -347,67 +461,128 @@ pub type DecodeError { |
| 347 | 461 | |
| 348 | 462 | /// The UUID `text` encodes with `a`. |
| 349 | 463 | pub fn decode(text: String, a: Alphabet) -> Result<Uuid, DecodeError> { |
| 464 | + let text = raw.from_string(text) |
| 350 | 465 | match a { |
| 351 | | − Alphabet(chars, base, w, aliases) -> { |
| 352 | | − let codes = raw.to_list(raw.from_string(text)) |
| 353 | | − if list.length(codes) != w { |
| 354 | | − Error(WrongLength(list.length(codes))) |
| 355 | | − } else { |
| 356 | | − match value_of(codes, 0, 0, chars, base, aliases) { |
| 357 | | − Error(e) -> Error(e) |
| 358 | | − , Ok(v) if v >= limit() -> Error(TooLarge) |
| 359 | | − , Ok(v) -> |
| 360 | | − Ok( |
| 361 | | − Uuid( |
| 362 | | − raw.from_list( |
| 363 | | − list.map( |
| 364 | | − list.range(0, 15) |
| 365 | | − , fn(i) { v / power(256, 15 - i) % 256 } |
| 366 | | − ) |
| 367 | | − ) |
| 368 | | − ) |
| 369 | | − ) |
| 370 | | − } |
| 371 | | − } |
| 466 | + Alphabet(_, _, w, _, _, _) if raw.length(text) != w -> |
| 467 | + Error(WrongLength(raw.length(text))) |
| 468 | + , Alphabet(chars, base, w, k, fold, aliases) if k > 0 -> { |
| 469 | + // The first digit only holds the id's top bits: the rest of its |
| 470 | + // bits are the padding, and must be 0. |
| 471 | + let top = base / pow2(w * k - 128) |
| 472 | + let out = raw.builder_with_capacity(16) |
| 473 | + decode_bits(text, 0, chars, fold, aliases, base, top, False, 0, 1, out) |
| 474 | + } |
| 475 | + , Alphabet(chars, base, _, _, fold, aliases) -> match value_of( |
| 476 | + text |
| 477 | + , 0 |
| 478 | + , 0 |
| 479 | + , chars |
| 480 | + , base |
| 481 | + , fold |
| 482 | + , aliases |
| 483 | + ) { |
| 484 | + Error(e) -> Error(e) |
| 485 | + , Ok(v) if v >= limit() -> Error(TooLarge) |
| 486 | + , Ok(v) -> Ok(Uuid(raw.from_list(bytes_of(v, 16, [])))) |
| 487 | + } |
| 488 | + } |
| 489 | +} |
| 490 | + |
| 491 | +/// The bytes of characters `i..` of `text`, after the bits `acc` (`p` = |
| 492 | +/// 2^n). A first digit of `top` or more makes the id too large, which is |
| 493 | +/// only reported when no character is invalid. |
| 494 | +fn decode_bits( |
| 495 | + text: Bytes |
| 496 | +, i: Int |
| 497 | +, chars: Bytes |
| 498 | +, fold: Bool |
| 499 | +, aliases: List<(Int, Int)> |
| 500 | +, base: Int |
| 501 | +, top: Int |
| 502 | +, too_large: Bool |
| 503 | +, acc: Int |
| 504 | +, p: Int |
| 505 | +, out: raw.Builder |
| 506 | +) -> Result<Uuid, DecodeError> { |
| 507 | + if p >= 256 { |
| 508 | + let p = p / 256 |
| 509 | + let out = raw.append_byte(out, acc / p) |
| 510 | + decode_bits( |
| 511 | + text |
| 512 | + , i |
| 513 | + , chars |
| 514 | + , fold |
| 515 | + , aliases |
| 516 | + , base |
| 517 | + , top |
| 518 | + , too_large |
| 519 | + , acc % p |
| 520 | + , p |
| 521 | + , out |
| 522 | + ) |
| 523 | + } else if i == raw.length(text) { |
| 524 | + if too_large { Error(TooLarge) } else { Ok(Uuid(raw.build(out))) } |
| 525 | + } else { |
| 526 | + let d = digit_of(raw.read_u8(text, i), chars, fold, aliases) |
| 527 | + if d < 0 { |
| 528 | + Error(InvalidChar(i)) |
| 529 | + } else if i == 0 { |
| 530 | + decode_bits( |
| 531 | + text |
| 532 | + , 1 |
| 533 | + , chars |
| 534 | + , fold |
| 535 | + , aliases |
| 536 | + , base |
| 537 | + , top |
| 538 | + , d >= top |
| 539 | + , d % top |
| 540 | + , top |
| 541 | + , out |
| 542 | + ) |
| 543 | + } else { |
| 544 | + decode_bits( |
| 545 | + text |
| 546 | + , i + 1 |
| 547 | + , chars |
| 548 | + , fold |
| 549 | + , aliases |
| 550 | + , base |
| 551 | + , top |
| 552 | + , too_large |
| 553 | + , acc * base + d |
| 554 | + , p * base |
| 555 | + , out |
| 556 | + ) |
| 372 | 557 | } |
| 373 | 558 | } |
| 374 | 559 | } |
| 375 | 560 | |
| 561 | +/// The value of `text` from character `at`, after `acc`. |
| 376 | 562 | fn value_of( |
| 377 | | − codes: List<Int> |
| 563 | + text: Bytes |
| 378 | 564 | , at: Int |
| 379 | 565 | , acc: Int |
| 380 | 566 | , chars: Bytes |
| 381 | 567 | , base: Int |
| 568 | +, fold: Bool |
| 382 | 569 | , aliases: List<(Int, Int)> |
| 383 | 570 | ) -> Result<Int, DecodeError> { |
| 384 | | − match codes { |
| 385 | | − [] -> Ok(acc) |
| 386 | | − , [c, ..rest] -> match digit_of(c, chars, 0, aliases) { |
| 387 | | − Some(d) -> value_of(rest, at + 1, acc * base + d, chars, base, aliases) |
| 388 | | − , None -> Error(InvalidChar(at)) |
| 571 | + if at == raw.length(text) { |
| 572 | + Ok(acc) |
| 573 | + } else { |
| 574 | + let d = digit_of(raw.read_u8(text, at), chars, fold, aliases) |
| 575 | + if d < 0 { |
| 576 | + Error(InvalidChar(at)) |
| 577 | + } else { |
| 578 | + value_of(text, at + 1, acc * base + d, chars, base, fold, aliases) |
| 389 | 579 | } |
| 390 | 580 | } |
| 391 | 581 | } |
| 392 | 582 | |
| 393 | | −fn digit_of(c: Int, chars: Bytes, i: Int, aliases: List<(Int, Int)>) -> Option< |
| 394 | | − Int |
| 395 | | −> { |
| 396 | | − match raw.get(chars, i) { |
| 397 | | − Some(x) if x == c -> Some(i) |
| 398 | | − , Some(_) -> digit_of(c, chars, i + 1, aliases) |
| 399 | | − , None -> |
| 400 | | − list.fold( |
| 401 | | − aliases |
| 402 | | − , None |
| 403 | | − , fn(found, alias) { |
| 404 | | − match alias { |
| 405 | | − (code, d) if code == c -> Some(d) |
| 406 | | − , _ -> found |
| 407 | | − } |
| 408 | | − } |
| 409 | | − ) |
| 410 | | − } |
| 583 | +/// The last `n` bytes of `v`, big-endian, before `acc`. |
| 584 | +fn bytes_of(v: Int, n: Int, acc: List<Int>) -> List<Int> { |
| 585 | + if n == 0 { acc } else { bytes_of(v / 256, n - 1, [v % 256, ..acc]) } |
| 411 | 586 | } |
| 412 | 587 | |
| 413 | 588 | impl Show<Uuid> { |