nx_price_extract.nx source
↩ module page · 61 lines · 2323 B
1// nx_price_extract.nx -- the Nishi BROWSER's price detector: find the price on a rendered shopping page so
2// the "before you buy" check (nx_purchase) can show what you have vs what it costs. Sovereign, deterministic
3// (no JS, no scraping libraries): scan the page text for $-amounts and take the largest (the product price,
4// usually -- vs $9.99 shipping). Reuses the same exact money-parsing as nx_doc_extract. The browser feeds
5// this its rendered DOM text (nx_html_to_text / nx_dom_query). Honest scope: "largest $-amount" heuristic;
6// currency/locale + labelled-price selection are later rungs. No floats, no hardware writes. license_tier: ORIGINAL
7import "nx_syscalls.nx"
8
9func px_dig(c: i64) -> i64 {
10 if c < 0x30 { return 0 - 1 }
11 if c > 0x39 { return 0 - 1 }
12 return c - 0x30
13}
14
15// parse a "$D,DDD.CC" amount starting at text[i] (must be '$'); returns cents, or -1 if not a valid amount.
16func px_parse_at(text: *u8, n: i64, i: i64) -> i64 {
17 if i >= n { return 0 - 1 }
18 if text[i] != (0x24 as u8) { return 0 - 1 }
19 var j: i64 = i + 1
20 var dollars: i64 = 0
21 var cc: i64 = 0
22 var sawdig: i64 = 0
23 var dot: i64 = 0
24 var ccd: i64 = 0
25 var go: i64 = 1
26 while go == 1 {
27 if j >= n { go = 0 }
28 else {
29 let c: i64 = text[j]
30 if c == 0x2e { if dot == 1 { go = 0 } else { dot = 1; j = j + 1 } }
31 else {
32 if c == 0x2c { j = j + 1 }
33 else {
34 let d: i64 = px_dig(c)
35 if d < 0 { go = 0 }
36 else {
37 if dot == 0 { dollars = dollars * 10 + d; sawdig = 1; j = j + 1 }
38 else { if ccd < 2 { cc = cc * 10 + d; ccd = ccd + 1; j = j + 1 } else { go = 0 } }
39 }
40 }
41 }
42 }
43 }
44 if sawdig == 0 { return 0 - 1 }
45 if ccd == 1 { cc = cc * 10 }
46 return dollars * 100 + cc
47}
48
49// find the LARGEST $-amount in text[0..n) (the product price); returns cents, or -1 if none found.
50func px_find_price(text: *u8, n: i64) -> i64 {
51 var best: i64 = 0 - 1
52 var i: i64 = 0
53 while i < n {
54 if text[i] == (0x24 as u8) {
55 let c: i64 = px_parse_at(text, n, i)
56 if c > best { best = c }
57 }
58 i = i + 1
59 }
60 return best
61}