Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit edccee6

Browse filesBrowse files
committed
upgrade from u32 to u64
1 parent 489b0d8 commit edccee6
Copy full SHA for edccee6

3 files changed

+27-25Lines changed: 27 additions & 25 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Cargo.toml‎

Copy file name to clipboardExpand all lines: Cargo.toml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "base_custom"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
authors = ["Daniel P. Clark <6ftdan@gmail.com>"]
55
description = "Use any characters as your own numeric base and convert to and from decimal."
66
documentation = "http://danielpclark.github.io/base_custom/index.html"
Collapse file

‎src/lib.rs‎

Copy file name to clipboardExpand all lines: src/lib.rs
+18-16Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ use std::ops::Range;
6464
/// by the delimiter provided.
6565
pub struct BaseCustom<T> {
6666
primitives: Vec<T>,
67-
primitives_hash: HashMap<T, u32>,
68-
base: u32,
67+
primitives_hash: HashMap<T, u8>,
68+
base: u64,
6969
delim: Option<char>,
7070
}
7171

@@ -96,20 +96,21 @@ impl BaseCustom<char> {
9696
/// for measuring the custom numeric base will only be one character long each.
9797
pub fn new(chars: Vec<char>) -> BaseCustom<char> {
9898
if chars.iter().count() < 2 { panic!("Too few numeric units! Provide two or more.") }
99+
if chars.iter().count() > 255 { panic!("Too many numeric units!") }
99100
let mut mapped = HashMap::with_capacity(chars.iter().count());
100101
for (i,c) in chars.iter().enumerate() {
101-
mapped.insert(c.clone(), i as u32);
102+
mapped.insert(c.clone(), i as u8);
102103
}
103104
BaseCustom::<char> {
104105
primitives: chars.clone(),
105106
primitives_hash: mapped,
106-
base: chars.iter().count() as u32,
107+
base: chars.iter().count() as u64,
107108
delim: None,
108109
}
109110
}
110111

111112
/// `gen` returns a String computed from the character mapping and
112-
/// positional values the given u32 parameter evalutes to for your
113+
/// positional values the given u64 parameter evalutes to for your
113114
/// custom base
114115
///
115116
/// # Example
@@ -124,7 +125,7 @@ impl BaseCustom<char> {
124125
/// ```text
125126
/// "11"
126127
/// ```
127-
pub fn gen(&self, input_val: u32) -> String {
128+
pub fn gen(&self, input_val: u64) -> String {
128129
if input_val == 0 {
129130
return format!("{}", self.primitives[0]);
130131
}
@@ -139,7 +140,7 @@ impl BaseCustom<char> {
139140
}
140141

141142

142-
/// `decimal` returns a u32 value on computed from the units that form
143+
/// `decimal` returns a u64 value on computed from the units that form
143144
/// the custom base.
144145
///
145146
/// # Example
@@ -154,12 +155,12 @@ impl BaseCustom<char> {
154155
/// ```text
155156
/// 3
156157
/// ```
157-
pub fn decimal<S>(&self, input_val: S) -> u32
158+
pub fn decimal<S>(&self, input_val: S) -> u64
158159
where S: Into<String> {
159160
let input_val = input_val.into();
160161

161162
input_val.chars().rev().enumerate().fold(0, |sum, (i, chr)|
162-
sum + self.primitives_hash[&chr] * self.base.pow(i as u32)
163+
sum + (self.primitives_hash[&chr] as u64) * self.base.pow(i as u32)
163164
)
164165
}
165166

@@ -216,23 +217,24 @@ impl BaseCustom<String> {
216217
None => chars.chars().map(|c| format!("{}", c)).collect(),
217218
};
218219
if strings.iter().count() < 2 { panic!("Too few numeric units! Provide two or more.") }
220+
if strings.iter().count() > 255 { panic!("Too many numeric units!") }
219221
let mut enumerator = strings.iter().enumerate();
220222
loop {
221223
match enumerator.next() {
222-
Some((i,c)) => mapped.insert(format!("{}", c), i as u32),
224+
Some((i,c)) => mapped.insert(format!("{}", c), i as u8),
223225
None => break,
224226
};
225227
}
226228
BaseCustom::<String> {
227229
primitives: strings.iter().map(|s| format!("{}", s)).collect(),
228230
primitives_hash: mapped,
229-
base: strings.len() as u32,
231+
base: strings.len() as u64,
230232
delim: delim,
231233
}
232234
}
233235

234236
/// `gen` returns a String computed from the character mapping and
235-
/// positional values the given u32 parameter evalutes to for your
237+
/// positional values the given u64 parameter evalutes to for your
236238
/// custom base
237239
///
238240
/// # Example
@@ -247,7 +249,7 @@ impl BaseCustom<String> {
247249
/// ```text
248250
/// "11"
249251
/// ```
250-
pub fn gen(&self, input_val: u32) -> String {
252+
pub fn gen(&self, input_val: u64) -> String {
251253
if input_val == 0 {
252254
return format!("{}", self.primitives[0]);
253255
}
@@ -262,7 +264,7 @@ impl BaseCustom<String> {
262264
format!("{}", result)
263265
}
264266

265-
/// `decimal` returns a u32 value on computed from the units that form
267+
/// `decimal` returns a u64 value on computed from the units that form
266268
/// the custom base.
267269
///
268270
/// # Example
@@ -277,7 +279,7 @@ impl BaseCustom<String> {
277279
/// ```text
278280
/// 3
279281
/// ```
280-
pub fn decimal<S>(&self, input_val: S) -> u32
282+
pub fn decimal<S>(&self, input_val: S) -> u64
281283
where S: Into<String> {
282284
let input_val = input_val.into();
283285
let strings: Vec<String> = match self.delim {
@@ -286,7 +288,7 @@ impl BaseCustom<String> {
286288
};
287289

288290
strings.iter().rev().enumerate().fold(0, |sum, (i, chr)|
289-
sum + self.primitives_hash[&chr[..]] * self.base.pow(i as u32)
291+
sum + (self.primitives_hash[&chr[..]] as u64) * self.base.pow(i as u32)
290292
)
291293
}
292294

Collapse file

‎tests/lib.rs‎

Copy file name to clipboardExpand all lines: tests/lib.rs
+8-8Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ fn it_shows_zero_one_and_nth_for_string() {
4242
#[test]
4343
fn it_works_with_binary_for_char() {
4444
let base2 = BaseCustom::<char>::new(vec!['0','1']);
45-
assert_eq!(base2.decimal("00001"), 1_u32);
46-
assert_eq!(base2.decimal("100110101"), 309_u32);
45+
assert_eq!(base2.decimal("00001"), 1_u64);
46+
assert_eq!(base2.decimal("100110101"), 309_u64);
4747
assert_eq!(base2.gen(340), "101010100");
4848
assert_eq!(base2.gen(0xF45), "111101000101");
4949
assert_eq!(base2.gen(0b111), "111");
@@ -52,8 +52,8 @@ fn it_works_with_binary_for_char() {
5252
#[test]
5353
fn it_works_with_binary_for_char_from_ordinal_range() {
5454
let base2 = BaseCustom::<char>::from_ordinal_range(48..50);
55-
assert_eq!(base2.decimal("00001"), 1_u32);
56-
assert_eq!(base2.decimal("100110101"), 309_u32);
55+
assert_eq!(base2.decimal("00001"), 1_u64);
56+
assert_eq!(base2.decimal("100110101"), 309_u64);
5757
assert_eq!(base2.gen(340), "101010100");
5858
assert_eq!(base2.gen(0xF45), "111101000101");
5959
assert_eq!(base2.gen(0b111), "111");
@@ -63,23 +63,23 @@ fn it_works_with_binary_for_char_from_ordinal_range() {
6363
fn it_works_with_binary_for_char_from_min_of_ordinal_range() {
6464
let base2 = BaseCustom::<char>::from_ordinal_range(0..34);
6565
println!("{}", base2.gen(3));
66-
assert_eq!(base2.decimal("!"), 1_u32);
66+
assert_eq!(base2.decimal("!"), 1_u64);
6767
assert_eq!(base2.gen(340), "! ! ! ! ");
6868
}
6969

7070
#[test]
7171
fn it_works_with_binary_for_char_from_max_of_ordinal_range() {
7272
let base2 = BaseCustom::<char>::from_ordinal_range(125..500);
7373
println!("{}", base2.gen(3));
74-
assert_eq!(base2.decimal("~"), 1_u32);
74+
assert_eq!(base2.decimal("~"), 1_u64);
7575
assert_eq!(base2.gen(340), "~}~}~}~}}");
7676
}
7777

7878
#[test]
7979
fn it_works_with_binary_for_string() {
8080
let base2 = BaseCustom::<String>::new("01", None);
81-
assert_eq!(base2.decimal("00001"), 1_u32);
82-
assert_eq!(base2.decimal("100110101"), 309_u32);
81+
assert_eq!(base2.decimal("00001"), 1_u64);
82+
assert_eq!(base2.decimal("100110101"), 309_u64);
8383
assert_eq!(base2.gen(340), "101010100");
8484
assert_eq!(base2.gen(0xF45), "111101000101");
8585
assert_eq!(base2.gen(0b111), "111");

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.