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
use std::iter::FromIterator;


/// Returns the minimum number of bits required to represent the integer. For positive input, gives
/// the number of unsigned bits. For negative input, gives the number of two's complement bits.
pub fn min_bits(value: i64) -> u32 {
    if value == 0 {
        1
    } else if value > 0 {
        (value as f64).log2().floor() as u32 + 1
    } else {
        (value.abs() as f64).log2().ceil() as u32 + 1
    }
}


/// Returns the output from min_bits() rounded up to a standard integer size - either 8, 16, 32,
/// or 64 bits.
pub fn std_bits(value: i64) -> u32 {
    let min_bits = min_bits(value);
    for std_size in vec![8, 16, 32, 64] {
        if min_bits <= std_size {
            return std_size;
        }
    }
    min_bits
}


/// Adds spacer characters to a string.
pub fn add_spacers(string: &str, spacer: char, block_len: u32) -> String {
    let mut chars: Vec<char> = Vec::new();
    for (i, c) in string.chars().rev().enumerate() {
        chars.push(c);
        if i as u32 % block_len == block_len - 1 {
            chars.push(spacer);
        }
    }
    if chars.last().unwrap() == &spacer {
        chars.pop();
    }
    chars.reverse();
    String::from_iter(chars)
}


/// Converts an integer into a binary string, showing the specified number of low-order bits.
pub fn bin_string(mut value: u64, num_bits: u32) -> String {
    let mut chars: Vec<char> = Vec::new();

    for i in 0..num_bits {
        if value & 1 == 1 {
            chars.push('1');
        } else {
            chars.push('0');
        }
        value >>= 1;
        if i < num_bits - 1 {
            if i % 8 == 3 {
                chars.push('_');
            }
            if i % 8 == 7 {
                chars.push(' ');
            }
        }
    }

    chars.reverse();
    String::from_iter(chars)
}


/// Returns the n-bit two's complement of `value`. Will panic if `n > 64` or `value >= 2^n`.
pub fn twos_complement(value: u64, num_bits: u32) -> u64 {
    assert!(num_bits <= 64);
    if value == 0 {
        return 0;
    }
    if num_bits < 64 {
        let cap = (2 as u64).pow(num_bits);
        assert!(value < cap);
        return cap - value;
    }
    return (u64::MAX - value) + 1
}


/// Attempts to parse the string as a binary, octal, decimal, or hex integer.
pub fn parse_int(arg: &str) -> Option<i64> {
    if arg.is_empty() {
        return None;
    }

    let mut trimmed = arg.trim_start_matches('0');
    if trimmed.is_empty() {
        return Some(0);
    }

    let mut radix: u32 = 10;
    if trimmed.starts_with('b') {
        radix = 2;
        trimmed = trimmed.trim_start_matches('b');
    } else if trimmed.starts_with('o') {
        radix = 8;
        trimmed = trimmed.trim_start_matches('o');
    } else if trimmed.starts_with('d') {
        radix = 10;
        trimmed = trimmed.trim_start_matches('d');
    } else if trimmed.starts_with('x') {
        radix = 16;
        trimmed = trimmed.trim_start_matches('x');
    }

    match i64::from_str_radix(trimmed, radix) {
        Ok(value) => Some(value),
        Err(_) => None,
    }
}


/// If `value` is a valid ASCII code, returns a string representation - either the character itself
/// or a description if the character is in the unprintable range.
pub fn ascii(value: i64) -> Option<String> {
    if value < 0 || value > 127 {
        return None;
    }
    if value > 32 && value < 127 {
        return Some(format!("{}", value as u8 as char));
    }
    let name = match value {
        0 => "[null]",
        1 => "[start of heading]",
        2 => "[start of text]",
        3 => "[end of text]",
        4 => "[end of transmission]",
        5 => "[enquiry]",
        6 => "[acknowledge]",
        7 => "[bell]",
        8 => "[backspace]",
        9 => "[horizontal tab]",
        10 => "[line feed]",
        11 => "[vertical tab]",
        12 => "[form feed]",
        13 => "[carriage return]",
        14 => "[shift out]",
        15 => "[shift in]",
        16 => "[data link escape]",
        17 => "[device control 1]",
        18 => "[device control 2]",
        19 => "[device control 3]",
        20 => "[device control 4]",
        21 => "[negative acknowledge]",
        22 => "[synchronous idle]",
        23 => "[end of transmission block]",
        24 => "[cancel]",
        25 => "[end of medium]",
        26 => "[substitute]",
        27 => "[escape]",
        28 => "[file separator]",
        29 => "[group separator]",
        30 => "[record separator]",
        31 => "[unit separator]",
        32 => "[space]",
        127 => "[del]",
        _ => panic!("unexpected value"),
    };
    Some(name.to_string())
}