1#![no_std]
2
3include!(concat!(env!("OUT_DIR"), "/index.rs"));
4
5pub static BLOB: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/docs.xz"));
6
7const fn cmp_bytes(left: &[u8], right: &[u8]) -> i8 {
8 let n = if left.len() < right.len() {
9 left.len()
10 } else {
11 right.len()
12 };
13 let mut i = 0;
14 while i < n {
15 if left[i] != right[i] {
16 return if left[i] < right[i] { -1 } else { 1 };
17 }
18 i += 1;
19 }
20 if left.len() == right.len() {
21 0
22 } else if left.len() < right.len() {
23 -1
24 } else {
25 1
26 }
27}
28
29const fn composed_len(
30 prefix: &[u8],
31 module: &[u8],
32 class: &[u8],
33 attr: &[u8],
34 has_attr: bool,
35) -> usize {
36 let mut n = prefix.len() + module.len() + 1 + class.len();
37 if has_attr {
38 n += 1 + attr.len();
39 }
40 n
41}
42
43const fn composed_byte(
44 index: usize,
45 prefix: &[u8],
46 module: &[u8],
47 class: &[u8],
48 attr: &[u8],
49) -> u8 {
50 let mut index = index;
51 if index < prefix.len() {
52 return prefix[index];
53 }
54 index -= prefix.len();
55 if index < module.len() {
56 return module[index];
57 }
58 index -= module.len();
59 if index == 0 {
60 return b'.';
61 }
62 index -= 1;
63 if index < class.len() {
64 return class[index];
65 }
66 index -= class.len();
67 if index == 0 {
68 return b'.';
69 }
70 attr[index - 1]
71}
72
73const fn cmp_composed(
74 key: &[u8],
75 prefix: &[u8],
76 module: &[u8],
77 class: &[u8],
78 attr: &[u8],
79 has_attr: bool,
80) -> i8 {
81 let query_len = composed_len(prefix, module, class, attr, has_attr);
82 let n = if key.len() < query_len {
83 key.len()
84 } else {
85 query_len
86 };
87 let mut i = 0;
88 while i < n {
89 let query = composed_byte(i, prefix, module, class, attr);
90 if key[i] != query {
91 return if key[i] < query { -1 } else { 1 };
92 }
93 i += 1;
94 }
95 if key.len() == query_len {
96 0
97 } else if key.len() < query_len {
98 -1
99 } else {
100 1
101 }
102}
103
104const fn find_composed(
105 prefix: &[u8],
106 module: &[u8],
107 class: &[u8],
108 attr: &[u8],
109 has_attr: bool,
110) -> Option<DocRef> {
111 let mut lo = 0;
112 let mut hi = DB.len();
113 while lo < hi {
114 let mid = (lo + hi) / 2;
115 let ord = cmp_composed(DB[mid].0.as_bytes(), prefix, module, class, attr, has_attr);
116 if ord == 0 {
117 return Some(DB[mid].1);
118 } else if ord < 0 {
119 lo = mid + 1;
120 } else {
121 hi = mid;
122 }
123 }
124 None
125}
126
127const fn eq(left: &str, right: &str) -> bool {
128 cmp_bytes(left.as_bytes(), right.as_bytes()) == 0
129}
130
131const fn starts_with_underscore(module: &str) -> bool {
132 let bytes = module.as_bytes();
133 !bytes.is_empty() && bytes[0] == b'_'
134}
135
136const fn strip_underscore(module: &str) -> Option<&str> {
137 let bytes = module.as_bytes();
138 if bytes.len() <= 1 || bytes[0] != b'_' {
139 return None;
140 }
141 match core::str::from_utf8(bytes.split_at(1).1) {
142 Ok(rest) => Some(rest),
143 Err(_) => None,
144 }
145}
146
147const fn nonempty(doc: Option<DocRef>) -> Option<DocRef> {
148 match doc {
149 Some(doc) if doc.len != 0 => Some(doc),
150 _ => None,
151 }
152}
153
154const fn find_named(
155 prefix: &[u8],
156 module: &str,
157 class: &str,
158 attr: Option<&str>,
159) -> Option<DocRef> {
160 match attr {
161 Some(attr) => find_composed(
162 prefix,
163 module.as_bytes(),
164 class.as_bytes(),
165 attr.as_bytes(),
166 true,
167 ),
168 None => find_composed(prefix, module.as_bytes(), class.as_bytes(), b"", false),
169 }
170}
171
172#[must_use]
175pub const fn get(key: &str) -> Option<DocRef> {
176 let key = key.as_bytes();
177 let mut lo = 0;
178 let mut hi = DB.len();
179 while lo < hi {
180 let mid = (lo + hi) / 2;
181 let ord = cmp_bytes(DB[mid].0.as_bytes(), key);
182 if ord == 0 {
183 return Some(DB[mid].1);
184 } else if ord < 0 {
185 lo = mid + 1;
186 } else {
187 hi = mid;
188 }
189 }
190 None
191}
192
193#[must_use]
195pub const fn get_attr(module: &str, class: &str, attr: &str) -> Option<DocRef> {
196 find_composed(
197 b"",
198 module.as_bytes(),
199 class.as_bytes(),
200 attr.as_bytes(),
201 true,
202 )
203}
204
205#[must_use]
208pub const fn get_qualified(
209 module: &str,
210 class: &str,
211 attr: Option<&str>,
212 allow_builtins: bool,
213) -> Option<DocRef> {
214 if let Some(doc) = nonempty(find_named(b"", module, class, attr)) {
215 return Some(doc);
216 }
217 if eq(module, "os")
220 && let Some(doc) = nonempty(find_named(b"", "posix", class, attr))
221 {
222 return Some(doc);
223 }
224 if (eq(module, "os") || eq(module, "posix"))
225 && let Some(doc) = nonempty(find_named(b"", "nt", class, attr))
226 {
227 return Some(doc);
228 }
229 if eq(module, "nt")
230 && let Some(doc) = nonempty(find_named(b"", "posix", class, attr))
231 {
232 return Some(doc);
233 }
234 if let Some(stripped) = strip_underscore(module)
235 && let Some(doc) = nonempty(find_named(b"", stripped, class, attr))
236 {
237 return Some(doc);
238 }
239 if !module.is_empty()
240 && !starts_with_underscore(module)
241 && let Some(doc) = nonempty(find_named(b"_", module, class, attr))
242 {
243 return Some(doc);
244 }
245 if allow_builtins && !eq(module, "builtins") && !starts_with_underscore(module) {
246 return nonempty(find_named(b"", "builtins", class, attr));
247 }
248 None
249}
250
251#[must_use]
253pub const fn class_attr_doc(module: Option<&str>, class: &str, attr: &str) -> Option<DocRef> {
254 let module = match module {
255 Some(module) => module,
256 None => "builtins",
257 };
258 get_qualified(module, class, Some(attr), true)
259}
260
261#[cfg(test)]
262extern crate alloc;
263
264#[cfg(test)]
265pub mod plain {
266 include!("data.inc.rs");
267}
268
269#[cfg(test)]
270mod test {
271 use super::{BLOB, DB, class_attr_doc, get, get_attr};
272 use crate::plain;
273
274 fn text() -> alloc::string::String {
275 let bytes = xz::decode_all(BLOB).unwrap();
276 alloc::string::String::from_utf8(bytes).unwrap()
277 }
278
279 #[test]
280 fn db_sorted_unique_and_roundtrip() {
281 assert!(!DB.is_empty());
282 assert_eq!(DB.len(), plain::DB.len());
283 let text = text();
284 let mut i = 0;
285 while i < DB.len() {
286 if i > 0 {
287 assert!(DB[i - 1].0 < DB[i].0, "{}", DB[i].0);
288 }
289 assert_eq!(DB[i].0, plain::DB[i].0);
290 let doc = &DB[i].1;
291 let start = doc.offset as usize;
292 let end = start + doc.len as usize;
293 assert_eq!(&text[start..end], plain::DB[i].1);
294 i += 1;
295 }
296 }
297
298 #[test]
299 fn get_hits_ends_and_middle() {
300 assert_eq!(get(DB[0].0).unwrap().offset, DB[0].1.offset);
301 assert_eq!(get(DB[DB.len() / 2].0).unwrap().len, DB[DB.len() / 2].1.len);
302 assert_eq!(
303 get(DB[DB.len() - 1].0).unwrap().offset,
304 DB[DB.len() - 1].1.offset
305 );
306 assert!(get("no.such.key").is_none());
307 }
308
309 #[test]
310 fn get_attr_and_alias() {
311 let doc = get_attr("builtins", "int", "__add__").unwrap();
312 assert_eq!(doc.offset, get("builtins.int.__add__").unwrap().offset);
313 assert_eq!(
314 super::get_qualified("os", "stat_result", None, true)
315 .unwrap()
316 .offset,
317 get("posix.stat_result").unwrap().offset
318 );
319 assert_eq!(
320 class_attr_doc(None, "int", "__add__").unwrap().offset,
321 get_attr("builtins", "int", "__add__").unwrap().offset
322 );
323 }
324
325 fn nonempty_offset(key: &str) -> Option<u32> {
326 get(key).filter(|doc| doc.len != 0).map(|doc| doc.offset)
327 }
328
329 #[test]
330 fn posix_and_nt_alias_each_other() {
331 let posix = nonempty_offset("posix.fork");
332 let nt = nonempty_offset("nt._getfinalpathname");
333 assert!(posix.is_some());
334 assert!(nt.is_some());
335 assert!(nonempty_offset("nt.fork").is_none());
336 assert!(nonempty_offset("posix._getfinalpathname").is_none());
337 assert_eq!(
338 super::get_qualified("posix", "fork", None, false).map(|doc| doc.offset),
339 posix
340 );
341 assert_eq!(
342 super::get_qualified("nt", "fork", None, false).map(|doc| doc.offset),
343 posix
344 );
345 assert_eq!(
346 super::get_qualified("nt", "_getfinalpathname", None, false).map(|doc| doc.offset),
347 nt
348 );
349 assert_eq!(
350 super::get_qualified("posix", "_getfinalpathname", None, false).map(|doc| doc.offset),
351 nt
352 );
353 assert_eq!(
354 super::get_qualified("os", "fork", None, false).map(|doc| doc.offset),
355 nonempty_offset("os.fork").or(posix)
356 );
357 assert_eq!(
360 super::get_qualified("_os", "fork", None, false).map(|doc| doc.offset),
361 nonempty_offset("os.fork")
362 );
363 }
364}