Skip to content

Commit

Permalink
Merge pull request #173 from librespot-org/base62
Browse files Browse the repository at this point in the history
Add to_base62 method
  • Loading branch information
sashahilton00 authored Feb 28, 2018
2 parents 593dfa0 + f830322 commit 8f8eb8c
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 115 deletions.
78 changes: 78 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ base64 = "0.5.0"
byteorder = "1.0"
bytes = "0.4"
error-chain = { version = "0.9.0", default_features = false }
extprim = "1.5.1"
futures = "0.1.8"
hyper = "0.11.2"
lazy_static = "0.2.0"
Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern crate base64;
extern crate byteorder;
extern crate bytes;
extern crate crypto;
extern crate extprim;
extern crate hyper;
extern crate num_bigint;
extern crate num_integer;
Expand Down
38 changes: 23 additions & 15 deletions core/src/spotify_id.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use byteorder::{BigEndian, ByteOrder};
use extprim::u128::u128;
use std;
use std::fmt;
use util::u128;
// Unneeded since 1.21
#[allow(unused_imports)]
use std::ascii::AsciiExt;
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -61,27 +61,35 @@ 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()
}

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
}
Expand Down
95 changes: 0 additions & 95 deletions core/src/util/int128.rs

This file was deleted.

4 changes: 0 additions & 4 deletions core/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ use rand::{Rand, Rng};
use std::mem;
use std::ops::{Mul, Rem, Shr};

mod int128;

pub(crate) use util::int128::u128;

pub fn rand_vec<G: Rng, R: Rand>(rng: &mut G, size: usize) -> Vec<R> {
rng.gen_iter().take(size).collect()
}
Expand Down
6 changes: 5 additions & 1 deletion playback/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,11 @@ impl PlayerInternal {
fn load_track(&self, track_id: SpotifyId, position: i64) -> Option<(Decoder, f32)> {
let track = Track::get(&self.session, track_id).wait().unwrap();

info!("Loading track \"{}\"", track.name);
info!(
"Loading track \"{}\" with Spotify URI \"spotify:track:{}\"",
track.name,
track_id.to_base62()
);

let track = match self.find_available_alternative(&track) {
Some(track) => track,
Expand Down

0 comments on commit 8f8eb8c

Please sign in to comment.