1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*! 
# based

`based` provides support for custom numeral systems with single-character digits.

`based` does not support multi-character digits.

# Examples

```
use based::{Base, NumeralSystem};

let base16 = Base::new("0123456789abcdef");
let val: usize = base16.from_str("10").unwrap();
assert_eq!(val, 16);
assert_eq!(base16.digits(16 as usize).unwrap(), "10")
```
*/

use std::convert::TryFrom;
use std::num::TryFromIntError;

/**
`StrError` is the error type produced when 
[`Base::from_str`](Base::from_str) encounters an unknown character
or fails to convert between two integer types.
*/
#[derive(Debug)]
pub enum StrError {
  /// Contains the unknown character.
  UnknownChar(char),
  Try(TryFromIntError)
}

impl std::fmt::Display for StrError {
  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    match self {
      StrError::UnknownChar(c) => write!(f, "Encountered char {} not in base", c),
      StrError::Try(t) => t.fmt(f)
    }   
  }
}

impl std::error::Error for StrError {
  fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
    match self {
      StrError::UnknownChar(_) => None,
      StrError::Try(t) => Some(t)
    }
  }
}

impl From<TryFromIntError> for StrError {
  fn from(err: TryFromIntError) -> StrError {
    StrError::Try(err)
  }
}


/// `Base` represents a numeral system with single-character digits.
pub struct Base {
  base: Vec<char>,
  vals: std::collections::HashMap<char, usize>,
}

impl std::fmt::Display for Base {
  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
      write!(f, "{}", self.base.iter().collect::<String>())
  }
}

impl Base {
  /**
  Creates a new numeral system from the given string slice.
  
  The value of each character is its index in the slice,
  e.g. the first character has value `0`, the second value `1`, etc.
   
  The behavior of this function is undefined when
  a character is present more than once in the given string slice.
  
  # Examples
  
  ```
  use based::Base;
  
  let base16 = based::Base::new("0123456789abcdef");
  ```
  */
  pub fn new(base: &str) -> Base {
    let mut vals: std::collections::HashMap<char, usize> = base
    .chars()
    .enumerate()
    .map(|(i, c)| (c, i))
    .collect();
    vals.shrink_to_fit();

    let mut base: Vec<char> = base.chars().collect();
    base.shrink_to_fit();

    Base {
      base,
      vals, 
    }
  }

}

/// `NumeralSystem` provides conversions to and from representations in the given system.
pub trait NumeralSystem<T> {

  /**
  Given a `NumeralSystem` and a number's representation
  in that system, return the number.

  Returns `Err` if this function encounters a character not in the system,
  or if an int to int conversion fails.
  */
  fn from_str(&self, rep: &str) -> Result<T, StrError>;

  /** 
  Given a `NumeralSystem` and a number, return the 
  representation of that number in the system.

  Returns `Err` if an int to int conversion fails.

  This will interpret signed integers as if their bits represented their
  unsigned counterparts.
  */
  fn digits(&self, val: T) -> Result<String, TryFromIntError>;
}

impl NumeralSystem<usize> for Base {
  fn from_str(&self, rep: &str) -> Result<usize, StrError> {
    let mut val = 0;
    let radix = self.base.len();
    for c in rep.chars() {
      match self.vals.get(&c) {
        None => return Err(StrError::UnknownChar(c)),
        Some(v) => {
          val *= radix;
          val += *v;
        }
      }
    }
    Ok(val)
  }

  /// Never produces `Err`.
  fn digits(&self, val: usize) -> Result<String, TryFromIntError> {
    let mut stack = Vec::new();
    let radix = self.base.len();
    let mut rem = val % radix;
    stack.push(self.base[rem]);
    let mut div = val / radix;
    while div > 0 {
      rem = div % radix;
      div = div / radix;
      stack.push(self.base[rem]);
    } 
    stack.reverse();
    Ok(stack.into_iter().collect())
  }
}

macro_rules! from_str {
  ($type:ty) => {
    fn from_str(&self, rep: &str) -> Result<$type, StrError> {
      let mut val = 0;
      let radix = self.base.len();
      for c in rep.chars() {
        match self.vals.get(&c) {
          None => return Err(StrError::UnknownChar(c)),
          Some(v) => {
            val *= radix;
            val += *v;
          }
        }
      }
      Ok(<$type>::try_from(val)?)
    }
  };
}

macro_rules! small_uint {
  ($type:ty) => {
    impl NumeralSystem<$type> for Base {
      from_str!{$type}
    
      /// Never produces `Err`.
      fn digits(&self, val: $type) -> Result<String, std::num::TryFromIntError> {
        let val: usize = usize::from(val);
        let mut stack = Vec::new();
        let radix = self.base.len();
        let mut rem = val % radix;
        stack.push(self.base[rem]);
        let mut div = val / radix;
        while div > 0 {
          rem = div % radix;
          div = div / radix;
          stack.push(self.base[rem]);
        } 
        stack.reverse();
        Ok(stack.into_iter().collect())
      }
    }
  };
}

small_uint!{u8}
small_uint!{u16}

macro_rules! large_uint {
  ($type:ty) => {
    impl NumeralSystem<$type> for Base {
      from_str!{$type}
    
      fn digits(&self, val: $type) -> Result<String, std::num::TryFromIntError> {
        if std::mem::size_of::<$type>() <= std::mem::size_of::<usize>() {
          let val: usize = usize::try_from(val)?;
          let mut stack = Vec::new();
          let radix = self.base.len();
          let mut rem = val % radix;
          stack.push(self.base[rem]);
          let mut div = val / radix;
          while div > 0 {
            rem = div % radix;
            div = div / radix;
            stack.push(self.base[rem]);
          } 
          stack.reverse();
          Ok(stack.into_iter().collect())
        }
        else {
          let mut stack = Vec::new();
          let radix = <$type>::try_from(self.base.len())?;
          let mut rem = val % radix;
          stack.push(self.base[usize::try_from(rem)?]);
          let mut div = val / radix;
          while div > 0 {
            rem = div % radix;
            div = div / radix;
            stack.push(self.base[usize::try_from(rem)?]);
          } 
          stack.reverse();
          Ok(stack.into_iter().collect())
        }
      }
    }
  };
}

large_uint!{u32}
large_uint!{u64}
large_uint!{u128}

macro_rules! iint {
  ($itype:ty, $utype:ty) => {
    impl NumeralSystem<$itype> for Base {
      from_str!{$itype}
    
      fn digits(&self, val: $itype) -> Result<String, std::num::TryFromIntError> {
        self.digits(val as $utype)
      }
    }
  };
}

iint!{i8, u8}
iint!{i16, u16}
iint!{i32, u32}
iint!{isize, usize}
iint!{i64, u64}
iint!{i128, u128}