Skip to content

Commit 8f8eb8c

Browse files
Merge pull request #173 from librespot-org/base62
Add to_base62 method
2 parents 593dfa0 + f830322 commit 8f8eb8c

File tree

7 files changed

+108
-115
lines changed

7 files changed

+108
-115
lines changed

Cargo.lock

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ base64 = "0.5.0"
1212
byteorder = "1.0"
1313
bytes = "0.4"
1414
error-chain = { version = "0.9.0", default_features = false }
15+
extprim = "1.5.1"
1516
futures = "0.1.8"
1617
hyper = "0.11.2"
1718
lazy_static = "0.2.0"

core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern crate base64;
1515
extern crate byteorder;
1616
extern crate bytes;
1717
extern crate crypto;
18+
extern crate extprim;
1819
extern crate hyper;
1920
extern crate num_bigint;
2021
extern crate num_integer;

core/src/spotify_id.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use byteorder::{BigEndian, ByteOrder};
2+
use extprim::u128::u128;
23
use std;
34
use std::fmt;
4-
use util::u128;
55
// Unneeded since 1.21
66
#[allow(unused_imports)]
77
use std::ascii::AsciiExt;
@@ -23,10 +23,10 @@ impl SpotifyId {
2323
for c in data {
2424
let d = match BASE16_DIGITS.iter().position(|e| e == c) {
2525
None => return Err(SpotifyIdError),
26-
Some(x) => x as u8,
26+
Some(x) => x as u64,
2727
};
28-
n = n * u128::from(16);
29-
n = n + u128::from(d);
28+
n = n * u128::new(16);
29+
n = n + u128::new(d);
3030
}
3131

3232
Ok(SpotifyId(n))
@@ -39,10 +39,10 @@ impl SpotifyId {
3939
for c in data {
4040
let d = match BASE62_DIGITS.iter().position(|e| e == c) {
4141
None => return Err(SpotifyIdError),
42-
Some(x) => x as u8,
42+
Some(x) => x as u64,
4343
};
44-
n = n * u128::from(62);
45-
n = n + u128::from(d);
44+
n = n * u128::new(62);
45+
n = n + u128::new(d);
4646
}
4747

4848
Ok(SpotifyId(n))
@@ -61,27 +61,35 @@ impl SpotifyId {
6161

6262
pub fn to_base16(&self) -> String {
6363
let &SpotifyId(ref n) = self;
64-
let (high, low) = n.parts();
6564

6665
let mut data = [0u8; 32];
67-
for i in 0..16 {
68-
data[31 - i] = BASE16_DIGITS[(low.wrapping_shr(4 * i as u32) & 0xF) as usize];
66+
for i in 0..32 {
67+
data[31 - i] = BASE16_DIGITS[(n.wrapping_shr(4 * i as u32).low64() & 0xF) as usize];
6968
}
70-
for i in 0..16 {
71-
data[15 - i] = BASE16_DIGITS[(high.wrapping_shr(4 * i as u32) & 0xF) as usize];
69+
70+
std::str::from_utf8(&data).unwrap().to_owned()
71+
}
72+
73+
pub fn to_base62(&self) -> String {
74+
let &SpotifyId(mut n) = self;
75+
76+
let mut data = [0u8; 22];
77+
let sixty_two = u128::new(62);
78+
for i in 0..22 {
79+
data[21 - i] = BASE62_DIGITS[(n % sixty_two).low64() as usize];
80+
n /= sixty_two;
7281
}
7382

7483
std::str::from_utf8(&data).unwrap().to_owned()
7584
}
7685

7786
pub fn to_raw(&self) -> [u8; 16] {
7887
let &SpotifyId(ref n) = self;
79-
let (high, low) = n.parts();
8088

8189
let mut data = [0u8; 16];
8290

83-
BigEndian::write_u64(&mut data[0..8], high);
84-
BigEndian::write_u64(&mut data[8..16], low);
91+
BigEndian::write_u64(&mut data[0..8], n.high64());
92+
BigEndian::write_u64(&mut data[8..16], n.low64());
8593

8694
data
8795
}

core/src/util/int128.rs

Lines changed: 0 additions & 95 deletions
This file was deleted.

core/src/util/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ use rand::{Rand, Rng};
55
use std::mem;
66
use std::ops::{Mul, Rem, Shr};
77

8-
mod int128;
9-
10-
pub(crate) use util::int128::u128;
11-
128
pub fn rand_vec<G: Rng, R: Rand>(rng: &mut G, size: usize) -> Vec<R> {
139
rng.gen_iter().take(size).collect()
1410
}

playback/src/player.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,11 @@ impl PlayerInternal {
533533
fn load_track(&self, track_id: SpotifyId, position: i64) -> Option<(Decoder, f32)> {
534534
let track = Track::get(&self.session, track_id).wait().unwrap();
535535

536-
info!("Loading track \"{}\"", track.name);
536+
info!(
537+
"Loading track \"{}\" with Spotify URI \"spotify:track:{}\"",
538+
track.name,
539+
track_id.to_base62()
540+
);
537541

538542
let track = match self.find_available_alternative(&track) {
539543
Some(track) => track,

0 commit comments

Comments
 (0)