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
use std::char;
use std::cmp::Ordering;
use memory::*;
use dispatch::*;
use handle::Handle;
use std::fmt;
pub struct Character_ { }
pub fn prism_unit() -> Unit { mechanism::prism::<Character_>() }
pub fn is_prism(prism: AnchoredLine) -> bool { prism[0] == prism_unit() }
pub fn find_prism(h: Handle) -> Option<AnchoredLine> { h.find_prism(prism_unit()) }
pub fn is_character(h: Handle) -> bool { find_prism(h).is_some() }
pub fn new(c: char) -> Handle {
let s = Segment::new(1 + if cfg!(target_pointer_width = "32") { 2 } else { 1 });
s.set(0, prism_unit());
store(s.line_at(0), 0, c as u32);
s.unit().handle()
}
pub fn from_byte(b: u8) -> Handle {
new(char::from_u32(b as u32).unwrap())
}
pub fn from_four_hex(s: &[u8]) -> Handle { new(four_hex_to_char(s)) }
pub fn display(c: Unit, f: &mut fmt::Formatter) -> fmt::Result {
let ch = char::from_u32((c.u() >> 4) as u32).unwrap();
match ch {
'\n' => { write!(f, "\\newline") },
'\r' => { write!(f, "\\return") },
' ' => { write!(f, "\\space") },
'\t' => { write!(f, "\\tab") },
_ => { write!(f, "\\{}", ch)}
}
}
pub fn store(prism: AnchoredLine, hash: u32, c: u32) {
if cfg!(target_pointer_width = "32") {
prism.set(1, Unit::from(hash));
prism.set(2, Unit::from(c));
} else {
let x = ((hash as u64) << 32) | c as u64;
prism.set(1, Unit::from(x));
}
}
pub fn store_hash(prism: AnchoredLine, hash: u32, c: u32) {
if cfg!(target_pointer_width = "32") {
prism.store_hash(1, Unit::from(hash));
prism.store_hash(2, Unit::from(c));
} else {
let x = ((hash as u64) << 32) | c as u64;
prism.store_hash(1, Unit::from(x));
}
}
pub fn hydrate(prism: AnchoredLine) -> (u32, u32) {
assert!(is_prism(prism));
if cfg!(target_pointer_width = "32") {
(prism[1].u32(), prism[2].u32())
} else {
let x = prism[1].u64();
((x >> 32) as u32, x as u32)
}
}
pub fn four_hex_to_char(s: &[u8]) -> char {
let code = (hex_digit(s[0]) << 12) | (hex_digit(s[1]) << 8) |
(hex_digit(s[2]) << 4) | hex_digit(s[3]);
char::from_u32(code).unwrap()
}
fn lowercase_hex(b: u8) -> u8 { if b <= b'Z' { b + 32 } else { b } }
fn hex_digit(b: u8) -> u32 {
if b <= b'9' {
(b - b'0') as u32
} else {
(lowercase_hex(b) - b'a') as u32 + 10
}
}
pub fn as_char(prism: AnchoredLine) -> char {
assert!(is_prism(prism));
let (_h, c) = hydrate(prism);
char::from_u32(c).unwrap()
}
impl Dispatch for Character_ { }
impl Identification for Character_ {
fn type_name(&self) -> &'static str { "Character" }
}
impl Notation for Character_ {
fn edn(&self, prism: AnchoredLine, f: &mut fmt::Formatter) -> fmt::Result {
let (_, c) = hydrate(prism);
let ch = char::from_u32(c).unwrap();
match ch {
'\n' => { write!(f, "\\newline") },
'\r' => { write!(f, "\\return") },
' ' => { write!(f, "\\space") },
'\t' => { write!(f, "\\tab") },
_ => { write!(f, "\\{}", ch) }
}
}
}
impl Distinguish for Character_ {
fn hash(&self, prism: AnchoredLine) -> u32 {
let (h, c) = hydrate(prism);
if (h >> 31) & 0x1 == 0x1 {
return h
}
use hash::hash_64;
let h = hash_64(c as u64, 4) | (1 << 31);
store_hash(prism, h, c);
h
}
fn eq(&self, prism: AnchoredLine, other: Unit) -> bool {
let o = other.handle();
if let Some(o_char) = find_prism(o) {
let (_, c) = hydrate(prism);
let (_, d) = hydrate(o_char);
return c == d
}
false
}
fn cmp(&self, prism: AnchoredLine, other: Unit) -> Option<Ordering> {
let o = other.handle();
if let Some(o_char) = find_prism(o) {
log!("Character eq");
let (_, c) = hydrate(prism);
let (_, d) = hydrate(o_char);
let ch = char::from_u32(c).unwrap();
let dh = char::from_u32(d).unwrap();
return Some(ch.cmp(&dh))
}
if o.is_ref() {
let o_prism_unit = o.logical_value()[0];
Some(prism_unit().cmp(&o_prism_unit))
} else {
Some(Ordering::Greater)
}
}
}
impl Aggregate for Character_ { }
impl Sequential for Character_ { }
impl Associative for Character_ { }
impl Reversible for Character_ { }
impl Sorted for Character_ { }
impl Numeral for Character_ { }
impl Callable for Character_ { }
#[cfg(test)]
mod tests {
use super::*;
}