From f8a8f8a245c18a157c246246090f1e948b25fbab Mon Sep 17 00:00:00 2001 From: stackinspector Date: Tue, 16 May 2023 02:56:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Rust=E7=89=88=E6=9C=ACbvid?= =?UTF-8?q?=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/other/bvid_desc.md | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/docs/other/bvid_desc.md b/docs/other/bvid_desc.md index d3da777..264f897 100644 --- a/docs/other/bvid_desc.md +++ b/docs/other/bvid_desc.md @@ -74,9 +74,9 @@ ## 编程实现 -使用 Python、C、TypeScript、Java、Kotlin 以及 Golang 等语言作为示例,欢迎社区提交更多例程 +使用 [Python](#Python) [C](#C) [TypeScript](#TypeScript) [Java](#Java) [Kotlin](#Kotlin) [Golang](#Golang) [Rust](#Rust) 等语言作为示例,欢迎社区提交更多例程 -注: 新算法只提供了Python版本 +注: 新算法只提供了 [Python](#Python) 和 [Rust](#Rust) 版本 ### Python ```python @@ -385,3 +385,40 @@ func main() { BV17x411w7KC 170001 ``` +### Rust +crate: https://github.com/stackinspector/bvid +```rust +// Copyright (c) 2023 stackinspector. MIT license. + +const XORN: u64 = 177451812; +const ADDN: u64 = 100618342136696320; +const TABLE: [u8; 58] = *b"fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF"; +const MAP: [usize; 10] = [9, 8, 1, 6, 2, 4, 0, 7, 3, 5]; +const REV_TABLE: [u8; 74] = [ + 13, 12, 46, 31, 43, 18, 40, 28, 5, 0, 0, 0, 0, 0, 0, 0, 54, 20, 15, 8, + 39, 57, 45, 36, 0, 38, 51, 42, 49, 52, 0, 53, 7, 4, 9, 50, 10, 44, 34, 6, + 25, 1, 0, 0, 0, 0, 0, 0, 26, 29, 56, 3, 24, 0, 47, 27, 22, 41, 16, 0, + 11, 37, 2, 35, 21, 17, 33, 30, 48, 23, 55, 32, 14, 19, +]; +const POW58: [u64; 10] = [ + 1, 58, 3364, 195112, 11316496, 656356768, 38068692544, + 2207984167552, 128063081718016, 7427658739644928, +]; + +pub fn av2bv(avid: u64) -> [u8; 10] { + let a = (avid ^ XORN) + ADDN; + let mut bvid = [0; 10]; + for i in 0..10 { + bvid[MAP[i]] = TABLE[(a / POW58[i]) as usize % 58]; + } + bvid +} + +pub fn bv2av(bvid: [u8; 10]) -> u64 { + let mut a = 0; + for i in 0..10 { + a += REV_TABLE[bvid[MAP[i]] as usize - 49] as u64 * POW58[i]; + } + (a - ADDN) ^ XORN +} +```