mirror of
https://github.com/librespot-org/librespot.git
synced 2025-10-03 01:39:28 +02:00
Add to_base62 method
This commit is contained in:
parent
593dfa04b6
commit
a7334b6c23
7 changed files with 104 additions and 115 deletions
|
@ -1,7 +1,7 @@
|
|||
use byteorder::{BigEndian, ByteOrder};
|
||||
use std;
|
||||
use std::fmt;
|
||||
use util::u128;
|
||||
use extprim::u128::u128;
|
||||
// Unneeded since 1.21
|
||||
#[allow(unused_imports)]
|
||||
use std::ascii::AsciiExt;
|
||||
|
@ -23,10 +23,10 @@ impl SpotifyId {
|
|||
for c in data {
|
||||
let d = match BASE16_DIGITS.iter().position(|e| e == c) {
|
||||
None => return Err(SpotifyIdError),
|
||||
Some(x) => x as u8,
|
||||
Some(x) => x as u64,
|
||||
};
|
||||
n = n * u128::from(16);
|
||||
n = n + u128::from(d);
|
||||
n = n * u128::new(16);
|
||||
n = n + u128::new(d);
|
||||
}
|
||||
|
||||
Ok(SpotifyId(n))
|
||||
|
@ -39,10 +39,10 @@ impl SpotifyId {
|
|||
for c in data {
|
||||
let d = match BASE62_DIGITS.iter().position(|e| e == c) {
|
||||
None => return Err(SpotifyIdError),
|
||||
Some(x) => x as u8,
|
||||
Some(x) => x as u64,
|
||||
};
|
||||
n = n * u128::from(62);
|
||||
n = n + u128::from(d);
|
||||
n = n * u128::new(62);
|
||||
n = n + u128::new(d);
|
||||
}
|
||||
|
||||
Ok(SpotifyId(n))
|
||||
|
@ -61,14 +61,23 @@ impl SpotifyId {
|
|||
|
||||
pub fn to_base16(&self) -> String {
|
||||
let &SpotifyId(ref n) = self;
|
||||
let (high, low) = n.parts();
|
||||
|
||||
let mut data = [0u8; 32];
|
||||
for i in 0..16 {
|
||||
data[31 - i] = BASE16_DIGITS[(low.wrapping_shr(4 * i as u32) & 0xF) as usize];
|
||||
for i in 0..32 {
|
||||
data[31 - i] = BASE16_DIGITS[(n.wrapping_shr(4 * i as u32).low64() & 0xF) as usize];
|
||||
}
|
||||
for i in 0..16 {
|
||||
data[15 - i] = BASE16_DIGITS[(high.wrapping_shr(4 * i as u32) & 0xF) as usize];
|
||||
|
||||
std::str::from_utf8(&data).unwrap().to_owned()
|
||||
}
|
||||
|
||||
pub fn to_base62(&self) -> String {
|
||||
let &SpotifyId(mut n) = self;
|
||||
|
||||
let mut data = [0u8; 22];
|
||||
let sixty_two = u128::new(62);
|
||||
for i in 0..22 {
|
||||
data[21-i] = BASE62_DIGITS[(n % sixty_two).low64() as usize];
|
||||
n /= sixty_two;
|
||||
}
|
||||
|
||||
std::str::from_utf8(&data).unwrap().to_owned()
|
||||
|
@ -76,12 +85,11 @@ impl SpotifyId {
|
|||
|
||||
pub fn to_raw(&self) -> [u8; 16] {
|
||||
let &SpotifyId(ref n) = self;
|
||||
let (high, low) = n.parts();
|
||||
|
||||
let mut data = [0u8; 16];
|
||||
|
||||
BigEndian::write_u64(&mut data[0..8], high);
|
||||
BigEndian::write_u64(&mut data[8..16], low);
|
||||
BigEndian::write_u64(&mut data[0..8], n.high64());
|
||||
BigEndian::write_u64(&mut data[8..16], n.low64());
|
||||
|
||||
data
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue