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
//! Collection of common functions used by cubicle-jockey projects
//!
//! Current features relate to:
//! * Base64 encoding/decoding
//! * Hex encoding/decoding
//! * Bit manipulation
//! * In-set checking (values within a set of ranges)
//!
//! # Quick Start
//!
//! The simplest way to get started is by using prelude to import all the main functionality
//!
//! ```
//! use cj_common::prelude::*;
//!
//! // base64 examples
//! let s = "Many hands make light work.".to_b64_string();
//! assert_eq!(s.as_str(), "TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu");
//!
//! if let Some(v) = b64_to_bytes("TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu") {
//!     let r = String::from_utf8_lossy(v.as_slice()).to_string();
//!     assert_eq!(r.as_str(), "Many hands make light work.");
//! }
//!
//! // iter example
//! let mut s = String::new();
//! for c in "Many hands make light work.".iter_to_b64() {
//!     s.push(c);
//! }
//! assert_eq!(s.as_str(), "TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu");
//!
//! let mut v = Vec::new();
//! for b in "TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu".iter_b64_to_byte() {
//!     v.push(b);
//! }
//! let r = String::from_utf8_lossy(v.as_slice()).to_string();
//! assert_eq!(r.as_str(), "Many hands make light work.");
//!
//! // the above are str examples, but it works the same for u8
//! let s = vec![1u8,2,3,4,5,6].to_b64_string();
//! let iter = [1u8,2,3,4,5,6].as_slice().iter_to_b64();
//!
//!
//! // hex examples
//! let mut s = String::new();
//! for c in "Many hands make light work.".iter_to_hex() {
//!     s.push_str(c);
//! }
//! assert_eq!(s.as_str(), "4D616E792068616E6473206D616B65206C6967687420776F726B2E");
//!
//! let mut v = Vec::new();
//! for b in "4D616E792068616E6473206D616B65206C6967687420776F726B2E".iter_hex_to_byte() {
//!     v.push(b);
//! }
//! let s = String::from_utf8_lossy(v.as_slice()).to_string();
//! assert_eq!(s.as_str(), "Many hands make light work.");
//!
//!
//! // bitbuf examples
//! let x = 0b00000010u8;
//! assert_eq!(x.get_bit(1),true);
//!
//! let mut x = 0b00000000u8;
//! x.set_bit(1,true);
//! assert_eq!(x,0b00000010u8);
//!
//! // iter example
//! let x = 0xABu8;
//! let mut v = Vec::new();
//! for i in x.bit_iter() {
//!     v.push(i);
//! }
//! assert_eq!(
//!    v.as_slice(),
//!    &[true, true, false, true, false, true, false, true]
//! );
//!
//! // iter over vec example (iter_to_bit)
//! let x = vec![0xABu8, 0xAB, 0xAB];
//! let mut v = Vec::new();
//! for i in x.iter_to_bit() {
//!     v.push(i);
//! }
//!
//! assert_eq!(
//!     v.as_slice(),
//!     &[
//!         true, true, false, true, false, true, false, true,
//!         true, true, false, true, false, true, false, true,
//!         true, true, false, true, false, true, false, true,
//!     ]
//! );
//!
//! // iter over slice example (iter_to_bit)
//! let x = [2u128, 2, 2];
//! for i in x.as_slice().iter_to_bit().enumerate() {
//!     match i.0 {
//!         1 | 129 | 257 => assert_eq!(i.1, true),
//!         _ => assert_eq!(i.1, false),
//!     }
//! }
//!
//!
//! // in_set example
//! let list = "lmnop";
//! for c in list.chars() {
//!     assert_eq!(c.in_range('k'..'q'), true);
//!     assert_eq!(
//!         c.in_set(
//!             [
//!                 ('k'..='l').into(),                // RangeInclusive
//!                 ('m'..'n').into(),                 // Range
//!                 ('n'..='p').into(),                // RangeInclusive
//!                 ['a', 'b', 'c'].as_slice().into(), // Slice
//!                 "test123".into(),                  // str
//!             ]
//!             .as_slice()
//!         ),
//!         true
//!     );
//!     assert_eq!(c.in_range('w'..'z'), false);
//! }
//!
//! let list = [1_000, 10_000, 100_000_000];
//! for n in list {
//!     assert_eq!(n.in_range(1..200_000_000), true);
//!     assert_eq!(
//!         n.in_set(
//!             [
//!                 (1..=10).into(),                 // RangeInclusive
//!                 (500..2_000).into(),             // Range
//!                 (9_999..=100_000_000).into(),    // RangeInclusive
//!                 [30, 90, 700].as_slice().into()  // Slice
//!             ]
//!             .as_slice()
//!         ),
//!         true
//!     );
//!     assert_eq!(n.in_range(1_000_000_000..1_000_000_001), false);
//! }
//! ```

/// b64, hex and bitbuf
pub mod cj_binary;

/// in_set
pub mod cj_helpers;

/// easiest way to import all functionality
pub mod prelude {
    pub use crate::cj_binary::b64::*;
    pub use crate::cj_binary::bitbuf::*;
    pub use crate::cj_binary::hex::*;
    pub use crate::cj_helpers::in_set::*;
}

#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;